Confused on how to use Encoder

We’re trying to use the encoder to make the robot travel a specific distance and trying to figure out how to use a PID controller to keep the both sides of the robot travel at the same speed. Any help or guidance would be much appreciated. We use CANTalon SRX’s as our speed controllers for the motors and an SRX Magnetic Encoder.

The Talon SRX’s has onboard speed controllers which assist a lot with running PID loops. CTRE does a really good job of documenting their work and I would recommend reading through the software manual http://www.ctr-electronics.com/downloads/pdf/Talon%20SRX%20Software%20Reference%20Manual.pdf and checking out the sample code https://github.com/CrossTheRoadElec/FRC-Examples. The section on closed-loop position control can be found in section 10.1 (page 62) of the manual. Note that to use the onboard processing that the Talon provides, the encoders need to plugged in directly to the data ports on the Talons.

Specific to running any loop through TalonSRX’s the first steps would be ensuring the Talon drives the correct direction and that the encoder is wired correctly and returning values.


talon.setFeedbackDevice(CANTalon.FeedbackDevice.CtreMagEncoder_Relative)
talon.reverseSensor(is_reversed)
talon.reverseOutput(is_reversed)

.

To run a closed-loop position loop you must set the Talons to position mode, set PID constants, and then set a setpoint.


talon.changeControlMode(CANTalon.TalonControlMode.Position)
talon.setPID(P, I, D, 0, 0, 0, 0)
talon.setSetpoint(target_distance)