191 | | Note: you'll need to manually turn the left wheel forward and backward to see the Encoder values change. Try turning the wheel 360 degrees to see what value is equivalent to a full revolution of the wheel. |
192 | | |
| 191 | Note: you'll need to manually turn the left wheel forward and backward to see the Encoder values change. Try turning the wheel 360 degrees to see what value is equivalent to a full revolution of the wheel. Notice that the raw count reports the sum of both A and B channels of the quadrature encoder. |
| 192 | |
| 193 | == Sixth program: Autonomous Control! |
| 194 | In the examples above, we learned to apply power to the wheel motors and to see how far the wheel has turned using encoders. Now we're going to put them together to drive the robot a controlled distance. We're also going to learn about autonomous mode. Every FRC match starts with a 15 second autonmous period where the robot drives itself and tries to accomplish some task with no human driver. For this example, our task is just to drive 3 feet and then stop (harder than it sounds). |
| 195 | |
| 196 | Create another program using the !TimedRobot java template and name it !AutonomousTest1. Modify the generated code as follows: |
| 197 | |
| 198 | * Add the following imports: |
| 199 | {{{ |
| 200 | import edu.wpi.first.wpilibj.Encoder; |
| 201 | import com.ctre.phoenix.motorcontrol.can.*; // for Hazelnut |
| 202 | import edu.wpi.first.wpilibj.Talon; // for Macadamia |
| 203 | }}} |
| 204 | * Declare variables for the Encoders and motor controllers in the Robot class |
| 205 | {{{ |
| 206 | private Encoder leftEncoder, rightEncoder; |
| 207 | // Uncomment next line for Macademia |
| 208 | // private Talon leftMotor, rightMotor; |
| 209 | // Uncomment next line for Hazelnut |
| 210 | // private WPI_TalonSRX leftMotor, rightMotor; |
| 211 | }}} |
| 212 | * • In robotInit() instantiate the Encoder and Motor objects |
| 213 | {{{ |
| 214 | // Quadrature encoder has A and B channels connected to DIO0, DIO1 |
| 215 | leftEncoder = new Encoder(0, 1); |
| 216 | rightEncoder = new Encoder(2, 3); |
| 217 | // Peanut wheels are 7.5" in diameter |
| 218 | // Encoders provide 1 count per degree of rotation |
| 219 | leftEncoder.setDistancePerPulse((3.141592*7.5)/360); |
| 220 | rightEncoder.setDistancePerPulse((3.141592*7.5)/360); |
| 221 | }}} |
| 222 | * In robotPeriodic() read and display the encoder valuey |
| 223 | {{{ |
| 224 | int encoderValue = leftEncoder.getRaw(); |
| 225 | double distance = leftEncoder.getDistance(); |
| 226 | SmartDashboard.putNumber("Encoder Value", encoderValue); |
| 227 | SmartDashboard.putNumber("Encoder Distance", distance); |
| 228 | }}} |
| 229 | |
| 230 | * In autonomousInit() reset the encoder counts and start the motors: |
| 231 | {{{ |
| 232 | // reset encoder counts to 0 |
| 233 | leftEncoder.reset(); |
| 234 | rightEncoder.reset(); |
| 235 | // start motors turning at 20% power |
| 236 | leftMotor.set(0.20); |
| 237 | rightMotor.set(0.20); |
| 238 | }}} |
| 239 | |
| 240 | * In autonomousPeriodic() check to see if we've gone the desired distance |
| 241 | then stop the motors. |
| 242 | {{{ |
| 243 | if ((leftEncoder.getDistance() > 36.0) || |
| 244 | (rightEncoder.getDistance() > 36.0)) { |
| 245 | leftMotor.stopMotor(); |
| 246 | rightMotor.stopMotor(); |
| 247 | } |
| 248 | }}} |