2 | | In the example above, we controlled the amount of power we were delivering the motor (0=none, 1.0=full power), but that doesn't tell us how fast the robot is moving or the wheel is turning. Just like a car or a bicycle, the speed a robot will go under a given amount of power depends on how heavy the robot is, whether it's going up a hill or down a hill, etc. To tell how fast a wheel is turning or to determine exactly where it has turned, we use encoders. An encoder is a device attached to the motor or wheel shaft that generates a pulse with each partial rotation (e.g. every 5-degrees of rotation, another pulse is generated). By counting these pulses, we can tell how fast the wheel is turning or what position it is in. You can read more about encoders [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599717-encoders-measuring-rotation-of-a-wheel-or-other-shaft here]. |
| 2 | In the example above, we controlled the amount of power we were delivering the motor (0=none, 1.0=full power), but that doesn't tell us how fast the robot is moving or the wheel is turning. Just like a car or a bicycle, the speed a robot will go under a given amount of power depends on how heavy the robot is, whether it's going up a hill or down a hill, how good the traction is, etc. To determine how fast a wheel is turning or exactly how far it has turned, we use encoders. |
| 36 | |
| 37 | === Encoder connected to a TalonSRX smart motor controller (Hazelnut) |
| 38 | Create another program using the !TimedRobot java template and name it !EncoderTest2. Modify the generated code as follows: |
| 39 | |
| 40 | * Add the following imports: |
| 41 | {{{ |
| 42 | import com.ctre.phoenix.motorcontrol.can*; |
| 43 | import com.ctre.phoenix.motorcontrol.FeedbackDevice; |
| 44 | }}} |
| 45 | * Declare a variable for the smart motor controller with connected encoder in the Robot class |
| 46 | {{{ |
| 47 | private WPI_TalonSRX leftMotor; |
| 48 | }}} |
| 49 | * • In robotInit() instantiate the motor controller object and reset it |
| 50 | {{{ |
| 51 | // TalonSRX is configured with CAN bus address 3 |
| 52 | leftMotor = new WPI_TalonSRX(3); |
| 53 | // Clear any non default configuration/settings |
| 54 | leftMotor.configFactoryDefaults(); |
| 55 | // A quadrature encoder is connected to the TalonSRX |
| 56 | leftMotor.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder); |
| 57 | }}} |
| 58 | * In robotPeriodic() read and display the encoder valuey |
| 59 | {{{ |
| 60 | SmartDashboard.putNumber("Encoder value", leftMotor.getSelectedSensorPosition()); |
| 61 | }}} |