12 | | |
13 | | // Uncomment next 2 lines for PWM motor controllers (Macademia) |
14 | | // private Talon leftMotor, rightMotor; |
15 | | // private Encoder leftEncoder, rightEncoder; |
16 | | |
17 | | // Uncomment next line for CAN motor controllers (Hazelnut) |
18 | | // private WPI_TalonSRX leftMotor, rightMotor; |
| 11 | private Talon leftMotor, rightMotor; |
| 12 | private Encoder leftEncoder, rightEncoder; |
22 | | * For PWM Motor Controller robots (Macadamia): |
23 | | {{{ |
24 | | // Quadrature encoder has A and B channels connected to DIO0,1, DIO2,3 |
25 | | leftEncoder = new Encoder(0, 1); |
26 | | rightEncoder = new Encoder(2, 3); |
27 | | // Peanut wheels are 7.5" in diameter |
28 | | // Encoders provide 1 count per degree of rotation |
29 | | leftEncoder.setDistancePerPulse((3.141592*7.5)/360); |
30 | | rightEncoder.setDistancePerPulse((3.141592*7.5)/360); |
31 | | // PWM motor controllers connected to PWM0, PWM1 |
32 | | leftMotor = new Talon(0); |
33 | | rightMotor = new Talon(1); |
34 | | }}} |
35 | | * For CAN Motor Controller robots (Hazelnut): |
36 | | {{{ |
37 | | // CAN motor controllers connected to CAN bus (addrs 3,4) |
38 | | leftMotor = new WPI_TalonSRX(3); |
39 | | rightMotor = new WPI_TalonSRX(4); |
40 | | // clear any previously configured settings |
41 | | leftMotor.configFactoryDefault(); |
42 | | rightMotor.configFactoryDefault(); |
43 | | // Encoders are connected directly to the motor controllers |
44 | | leftMotor.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder); |
45 | | rightMotor.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder); |
46 | | }}} |
| 16 | {{{ |
| 17 | // Quadrature encoder has A and B channels connected to DIO0,1, DIO2,3 |
| 18 | leftEncoder = new Encoder(0, 1); |
| 19 | rightEncoder = new Encoder(2, 3); |
| 20 | // Peanut wheels are 7.5" in diameter |
| 21 | // Encoders provide 1 count per degree of rotation |
| 22 | leftEncoder.setDistancePerPulse((3.141592*7.5)/360); |
| 23 | rightEncoder.setDistancePerPulse((3.141592*7.5)/360); |
| 24 | // PWM motor controllers connected to PWM0, PWM1 |
| 25 | leftMotor = new Talon(0); |
| 26 | rightMotor = new Talon(1); |
| 27 | }}} |
48 | | * For PWM controller robots (Macadamia),[[BR]] |
49 | | in robotPeriodic() display the encoder values: |
50 | | {{{ |
51 | | SmartDashboard.putNumber("Left", leftEncoder.getDistance()); |
52 | | SmartDashboard.putNumber("Right", rightEncoder.getDistance()); |
53 | | }}} |
54 | | * For CAN motor controllers (Hazelnut): |
55 | | * First add the following new method to the Robot class: |
56 | | {{{ |
57 | | double raw2inches(int raw_count, int counts_per_rotation, double wheel_diameter) { |
58 | | return (raw_count / counts_per_rotation) * (3.141492 * wheel_diameter); |
59 | | } |
60 | | }}} |
61 | | * Add the following to the robotPeriodic() method; note the use of the [https://www.ctr-electronics.com/downloads/api/java/html/classcom_1_1ctre_1_1phoenix_1_1motorcontrol_1_1can_1_1_base_motor_controller.html#afb934a11682e710df123eec35aba1ba9 getSelectedFeedbackSensor()] method of the motor controller object which returns raw encoder counts: |
62 | | {{{ |
63 | | // Encoders connected to CAN controller return raw counts |
64 | | int l_raw = leftMotor.getSelectedSensorPosition(); |
65 | | int r_raw = rightMotor.getSelectedSensorPosition(); |
66 | | // display distances on smart dashboard |
67 | | SmartDashboard.putNumber("Left", raw2inches(l_raw, 360*2, 7.5)); |
68 | | SmartDashboard.putNumber("Right", raw2inches(r_raw, 360*2, 7.5)); |
69 | | }}} |
| 29 | * In robotPeriodic() display the encoder values: |
| 30 | {{{ |
| 31 | SmartDashboard.putNumber("Left", leftEncoder.getDistance()); |
| 32 | SmartDashboard.putNumber("Right", rightEncoder.getDistance()); |
| 33 | }}} |
84 | | * For PWM motor controllers: |
85 | | {{{ |
86 | | if ((leftEncoder.getDistance() > 36.0) || |
87 | | (rightEncoder.getDistance() > 36.0)) { |
88 | | leftMotor.stopMotor(); |
89 | | rightMotor.stopMotor(); |
90 | | } |
91 | | }}} |
92 | | * For CAN motor controllers: |
93 | | {{{ |
94 | | int l_raw = leftMotor.getSelectedSensorPosition(); |
95 | | int r_raw = rightMotor.getSelectedSensorPosition(); |
96 | | if ((raw2inches(l_raw, 360*2, 7.5) > 36.0) || |
97 | | (raw2inches(r_raw, 360*2, 7.5) > 36.0)) { |
98 | | leftMotor.stopMotor(); |
99 | | rightMotor.stopMotor(); |
100 | | } |
101 | | }}} |
| 48 | {{{ |
| 49 | if ((leftEncoder.getDistance() > 36.0) || |
| 50 | (rightEncoder.getDistance() > 36.0)) { |
| 51 | leftMotor.stopMotor(); |
| 52 | rightMotor.stopMotor(); |
| 53 | } |
| 54 | }}} |