Using Encoders with TalonSRX

Hi,
I’m trying to learn how to use encoders in a command based robot, and I’m having trouble finding how to create them and use them. I’ve checked several other forums already, and found nothing. Also, we can read values from our encoders, if that helps at all. If you could point me to a source that explains how to do those things, that would be amazing and very much appreciated.

Thank you!

Sorry if I sound a little unprofessional, I’m a student from my team and this is my first post.

Take a look at the Talon SRX Software Reference Manual…
http://crosstheroadelectronics.com/control_system.html

Also the SRX User’s Guide is worth checking out for wiring.

EDIT : Are you connecting encoders to the Talon SRX Data Port. Or are you just asking how to read encoders using the roboRIO I/O and use them that way?

1 Like

We’ve looked at the software reference manual, and we couldn’t find out how to create the encoder correctly. In the middle of writing this, one of our programmers figured out was wrong and fixed it. Now we are wondering what getEncPositon() measures in. Our encoders are connected through the Talon SRX Data Port. Also, do you know where any examples of encoder code are? Because I think that would really help us. Thanks for the quick response!

The Units are explained in Section 17. Quad. Encoder units are in Section 17.1.
They are in 4X units.

Examples for reading the encoder? That would be a one line example…
int quadEncPos = motorController.getEncPosition(); /* section 5 */

You can also sanity check the values without code changes by using the Self-Test (section 2.4).

How do you plan on using the encoders exactly? Do you want to setup the Talon SRX to internally closed-loop position and/or velocity? Or do you just want to retrieve the encoder values so your logic in the RIO can make it’s own decisions (for example doing a closed-loop in the RIO).

I think once you make that clear, then the community can give you better answers.

Thank you so much for the response! We plan to setup the Talon SRX to use closed-loop.

If you’re doing closed loop, then you don’t actually talk to the encoders. You wire the encoders to the TalonSRX, and you tell it to use them. This is fairly well explained in the updated version of the Talon Software Guide.

However, you may run into several challenges:

  1. Assuming these are your drive motors, RobotDrive will have an issue providing the correct range of values to the Talon object using the set() method.

You’ll want to call the method “ControlMode” with the Speed constant (the default is PercentVbus). And tell it that you’re using an Encoder (via “setFeedbackDevice” with value QuadEncoder).

When I was playing with this, I derived a class from the CANTalon and overloaded the set() method to multiply the -1…0…+1 range to the range of encoder counts I expected. How well this would have worked, we won’t know, because the CANTalon in C++ doesn’t have the methods necessary to reverse (described below for Java), and the CanTalonSrx looked too foreign to rewrap myself, and my encoders needed to spin in the opposite direction from the motor, so…

  1. Since you’re in Java, you have the call to reverse the control input. You’ll know you need to do this if you’re telling the motor to spin at 0.50 speed and the encoder says it’s doing a negative turn rate (and vice versa). A good way to see that your encoder is working and the direction values it’s giving you is by using the RoboRio’s web page, selecting your CANTalonSRX and clicking SelfTest while it’s operating. Anyway, the CANTalon method to reverse the sign is called “reverseSensor()”

  2. If you normally create your RobotDrive giving it port numbers, you’ll need
    to create the objects yourself and pass them to RobotDrive.

Yes, you can get the Encoder information from the Talon if you want to look at it or put it on the Dashboard, but you don’t need to do that to do closed loop control.

How well this would have worked, we won’t know, because the CANTalon in C++ doesn’t have the methods necessary to reverse…

…section 7.2 of the Talon SRX software reference manual.

  1. Since you’re in Java, you have the call to reverse the control input. You’ll know you need to do this if you’re telling the motor to spin at 0.50 speed and the encoder says it’s doing a negative turn rate (and vice versa). A good way to see that your encoder is working and the direction values it’s giving you is by using the RoboRio’s web page, selecting your CANTalonSRX and clicking SelfTest while it’s operating. Anyway, the CANTalon method to reverse the sign is called “reverseSensor()”

…section 7.4 of the Talon SRX software reference manual.

Ah… I had not gone back for the 2/1 update of the manual. I’ll look at this yet again.

Well, yes. Do you disagree with something that I said?

Nope just doc’ing for other teams benefit.

Sorry to bother you again. How would I use the set() method when I change a CANTalon to the speed control mode? I have my CANTalon bound to a button that, when pressed, sets the CANTalon speed to 0.5. Thanks for the help!

If you set the control mode to Speed, then set() will take the target speed you want. The units are sensor-dependent and better explained in section 17, but it’s basically the change in sensor value per 100ms. You can also cheat by using the Self-Test to see what your “Sensor Velocity” reads while driving the motor.

Examples on how to change modes is in Section 3.6. (Section 3.6.3 for Java).

Also after you changed the mode, the Talon SRX will not drive motors until the next call to set(). See Section 16.12 for more info.

Ok, I just thought set() was something else that applied to CANTalon modes and PID control. I figured out that the set() values in speed mode go from 0 to 1000. Everything works now. Thank you so much for your help!

Well…wait…it sets the target speed in pos per 100ms, which can be negative (reverse) and can be larger than 1000.

So do you see how in section 17.1 (Talon SRX software reference manual) the example math for a CPR=360 quadrature encoder calculates a speed of 28800 for 20 rotations per second (or RPM=1200)? So set(28800) would servo a velocity of 1200RPM in the positive direction IF using a quad encoder (that’s CPR=360).

Make sense?

Ohhh. Our encoder is the E4P-250-250-D-D-D-B. I don’t know the CPR, is it 250? If we set the speed to 500, the encoder reads a velocity of 300.

Yes.

If we set the speed to 500, the encoder reads a velocity of 300.

What value is the Talon reporting for the setpoint-process_variable error?

Thanks! How many rotations would the encoder have then? I don’t know where to find the setpoint-process_variable error, but the ClosedLoopError variable in the self-test section is 200. The motor not going 500 is probably a result of me messing with the PID variables. I was changing the P variable around to see the ocsillation and other things. The motor goes a velocity of 300 when my P = 1.5, and my I and D equal 0.