Programming for an encoder connected via Talon

We procured a number of AMT103 encoders with our FIRST Choice selections. When we first oriented on using these encoders, we connected them directly to the RIO using the DIO ports, but our plan was to use them with our Talons. We’ve now successfully hooked them up to our Talons using the encoder breakout board from AndyMark. We’re able to read values from them using the WPI_TalonSRX class methods, notably velocity (getSelectedSensorVelocity()) and position (getSelectedSensorPosition()). But, the current CTRE classes seem lacking in a number of features, or we’re having extreme difficulty in finding them (cumulatively 4+ hours of research in the API reference, CD, and random examples on the web).

When connected directly to the RIO, we used the WPILib Encoder class. Using that class, we could effectively set the resolution using setDistancePerPulse() - the AMT103 has variable resolution set by DIP switches from 48 PPR to 2048 PPR. We could also access a number of methods like getDistance(), getRate(), getDirection(), and getStopped() that seemed useful. Mapping to WPI_TalonSRX, I believe getDistance() maps to getSelectedSensorPosition(), and getRate() maps to getSelectedSensorVelocity(), but we can’t find equivalents for setting resolution, getting direction, or detecting a stopped state.

Regarding resolution, we finally considered we can just set the DIP switches such that it returns a PPR that results in getSelectedSensorPostion() returning 4096 from one full turn, which seems to be the resolution assumed for quadrature encoders connected to the Talon.

During our research, we noticed references to a deprecated class CANTalon. This class seemed to have more clear direct access and control over the attached encoder, such as configEncoderCodesPerRev() to set resolution, and these methods do not seem to exist in the newer TalonSRX/WPI_TalonSRX classes.

Do these methods exist and we’re just missing them?

To my knowledge, CANTalon was made by WPI when they provided the class for the Talons. Now CTRE provides their own API. Best advice would be searching the TalonSRX Class file in your dependencies (assuming java) and the classes it extends.

The classes changed from last year to this year. Talon was renamed PWMTalon (or something like that) and CANTalon became TalonSRX.

I think what you’re looking for is here:
https://phoenix-documentation.readthedocs.io/en/latest/ch14_MCSensor.html

And here’s the java class documentation:
http://www.ctr-electronics.com/downloads/api/java/html/classcom_1_1ctre_1_1phoenix_1_1motorcontrol_1_1can_1_1_talon_s_r_x.html

Thanks y’all. We spent hours pouring through the documentation you referenced, to no avail, and hoped someone out there had suffered similar pain and found the magic methods. Someone offered some offline help, which gave me a lot of additional insight (thanks @Juniormunk).

The basic questions we were trying to answer are, do the CTRE classes support:

  1. Programatically setting the encoder resolution, so position and velocity are calculated correctly?
  2. Directly getting the direction of a quadrature encoder?
  3. Directly getting the stopped state of an encoder?

Right now, it looks like the answer is no to all three, whereas the WPILib Encoder class supports all of these, which is unfortunate and limits the usefulness of an encoder connected via a Talon.

From my research:

  1. Not this year. There’s a note in the documentation saying they might add this for 2020.
  2. Assuming this means the “most recent direction of movement”, the GetSensorVelocity functions I believe are signed, so you can tell the direction.
  3. Similar to 2, you can see if the velocity is zero (or smaller than some threshold) and treat that as “stopped”.

The velocity is measured on 100 ms timeframes, but this can be customized.

1 Like

:man_facepalming: I can’t believe we didn’t think of that. We were (I was) too focused on looking for direct equivalents that we didn’t think of how the methods given could be used to achieve the same objective. Thank you! :exploding_head:

I think adding a feature like that is something you can do yourself by extending the TalonSRX class.