How to Measure velocity on a TalonSRX

How would you substitute a NEO’s name.getEncoder().getVelocity() for a Mag SRX encoder mounted to the data port of a Talon? For context this is for the Kinematic and Odometer classes. We were thinking about getRate() or getSelectedSensorVelocity() but we are just confused on what to do about them.

This is what we are trying to get:


1 Like

Take a look at their sample code.

I would start with


I don’t want to use a PID loop, just the velocity.

Ok look at line 113


This line is getting the current Velocity and printing it to the console. You might be able to use this.

You don’t need to PID loop to call this method, AFAIK, it’s just that you can configure inside the Talon SRX more than one feedback loop.
However, if you use only one feedback sensor, you can use this version of the aforementioned method which uses the default slot ID.

Would this go in the constructor?

Call configSelectedFeedbackSensor(TalonSRXFeedbackDevice. CTRE_MagEncoder_Relative) in the constructor for using the mag encoder in relative mode

Call getSelectedSensorVelocity() whenever you need to measure the velocity.

Keep in mind that velocity is in native units per 100ms. The native unit is 4096 per rotation for the mag encoder. You might want to convert units before using it.

1 Like

If you’re doing this for odometry/kinematics/WPILib trajectories, your speeds will need to return in rotations per second. talon.getSelectedSensorVelocity() returns encoder units per 100 ms. Assuming you have a CTRE mag encoder attached to your talons (CTRE Mag encoders return 4096 units per rotation), your getSpeeds() function will look like this:

public DifferentialDriveWheelSpeeds getSpeeds() {
    return new DifferentialDriveWheelSpeeds(
        leftMaster.getSelectedSensorVelocity() * (10.0 / 4096) * wheelCurcumference,
        rightMaster.getSelectedSensorVelocity() * (10.0 / 4096) * wheelCurcumference

Edit: Changed 10 to 10.0 in the conversion

Note that in your code snippet 10 / 4096 won’t give you the result you expect, since it’s integer division, the result will be 0. At least one of the numbers needs to be floating point (e.g. 10.0 / 4096).


Why 10?

Convert 100 ms into 1 s.