How do I use CTRE mag encoder as an absolute encoder

I am confused as to how the CTRE mag encoder can keep an absolute position through powercycles.

You can use the pulse width encoder commands (in WPILIB or the Talon API, depending on where the mag encoder is connected) to read absolute position. The encoder puts out a 1-4096us pulse every 4097us (ish). The pulse width is proportional to the absolute angle of its target magnet.
The encoder works by sensing the rotation of a “diametrically polarized” magnet, so it can always derive the absolute rotation. Here’s an article that explains it a bit: #05 Principle and advantages of magnetic encoder | Basic Knowledge of Encoder | TUTORIALS | Asahi Kasei Microdevices (AKM)

What is your setup? What exactly do you need help with?


With a CANCoder, you can just get the info from the api

As to how it actually works, there is a sensor that can determine the rotation of the magnet. The magnet is most likely magnetized through the diameter, and there is probably some sort of 2 or 3 axis magnetometer that senses the direction of the magnet and coverts it into an absolute position


Our set up is the MK4 swerve modules. I am wondering if we can use the CTRE mag encoders and get a absolute startup position.

Yup, that’s why they are there. Just read the position using the CANcoder API and you’re good to go. They will report absolute position. CTRE will have more info, try checking their example code.

Connect pins 2 (+5V), 9 (PWM), and 10 (GND) from the SRX Mag Encoder to a roboRIO digital input and use the DutyCycle or DutyCycleEncoder class in the code. For CANcoder, see above.

For the SRX Mag Encoder, you will need to be very careful in how you wire things. I’d reccomend something like this, connected to a regular PWM-style cable.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.