Relative and Absolute Encoders
What a Kraken's built-in encoder knows at power-up, how absolute encoders work and where to mount them, the one-turn limit and the wrap point, offsets, and how Phoenix 6 combines an absolute encoder with the motor's encoder.
An encoder measures rotation. A relative encoder counts how far something has turned since it started. An absolute encoder reports where it is within one turn, right from power-up. Homing is only needed because the motor's encoder is relative for the mechanism. In short:
- The motor's encoder doesn't know the mechanism's position. It knows the rotor's angle within one turn, but a gearbox puts many rotor turns in the mechanism's travel.
- An absolute encoder reads the mechanism directly. Mount it on the mechanism's side of the gearbox, such as on the arm's axle. It reports the angle the moment it powers up.
- It only knows one turn. The reading repeats every turn of the encoder. Gear it so the whole travel fits in less than one turn, and put the wrap point where the mechanism never goes.
- Use both. Let the absolute encoder set the motor's position, then control with the motor's fast encoder. Phoenix 6 can do this for you.
3.1 Relative encoders
A Kraken's encoder is built into the motor and reads the rotor's angle with a magnet. It is absolute within one rotor turn, because the motor controller needs the rotor angle to drive the motor. Phoenix 6 starts the motor's position from that angle when the motor controller boots, then counts every turn after that.
The gearbox is the problem. On the 100:1 arm from the voltage-mode example, one rotor turn moves the arm 3.6°. At power-up the motor knows where the rotor is within its turn, but not which of the 100 turns per arm revolution it is on. For the arm, the motor's position only counts motion since boot.
Relative encoders have real strengths:
- High resolution and fast updates. The motor controller reads its rotor sensor directly, with no bus in between.
- Already there. Every Kraken has one.
- No limit on turns. They count as far as the mechanism goes.
Their one weakness is that they don't know where they started. Something else has to tell them: a hard stop, a sensor, or an absolute encoder.
3.2 Absolute encoders
An absolute encoder reads a magnet fixed to a shaft, and reports the magnet's angle within one turn from the moment it powers up. Two kinds are common in FRC:
| Kind | Reads | Connects to |
|---|---|---|
| CAN magnetic encoder (CTRE CANcoder) | A magnet fixed to the end of a shaft, or held in a housing that slides over a hex shaft | The CAN bus. A Talon FX can use it as its feedback sensor. |
| Duty-cycle (PWM) absolute encoder, such as a through-bore encoder | A magnet in a housing that slides over a hex shaft | A roboRIO digital input, read with WPILib's DutyCycleEncoder. Or a CTRE CANdi, which a Talon FX can also use as its feedback sensor. |
A potentiometer is another absolute sensor. It is a variable resistor that turns with the shaft and is read as an analog voltage. A multi-turn potentiometer covers several turns, and a string potentiometer measures straight-line distance, such as an elevator's height. They are cheap and simple, but noisier than magnetic encoders, and they wear.
3.3 Where to mount it
- Mount it on the mechanism's side of the gearbox. The best place is on the axle that the mechanism turns on, such as the arm's pivot shaft. Every stage between the encoder and the mechanism adds its backlash to the reading. A belt or a hub between them can slip and leave the encoder reading something else.
- Gear it so the travel fits in one turn. An arm with 200° of travel can read 1:1, since 200° is 0.56 of a turn. A turret with more than 360° of travel needs the encoder geared down, or two encoders. An elevator's drum turns many times, so an absolute encoder on the drum doesn't work unless it is geared down a lot. Elevators usually zero against a hard stop instead.
- Gearing the encoder up adds resolution, and adds wraps. An encoder that turns faster than the mechanism reads small angles more finely, but it repeats inside the travel. That is the problem two encoders solve.
- Lock the magnet. Glue or press-fit it so it can't turn on its shaft. A magnet that slips moves the zero.
- Keep the gap right. Center the magnet on the sensor at the distance its maker gives. A CANcoder reports magnet health (green, orange, or red) to check this.
3.4 The offset and the wrap point
Two settings make an absolute encoder read the mechanism's true angle.
The offset. The magnet's own zero is wherever it happened to be glued. To set the offset, put the mechanism at its zero (an arm level, checked with a level), read the encoder, and store the negative of that reading. On a CANcoder, the config is MagnetOffset in MagnetSensorConfigs, in rotations from −1 to 1. Also check SensorDirection: the reading has to go up when the mechanism moves in its positive direction.
The wrap point. An absolute reading covers one turn, so somewhere on the circle it jumps by a full turn, from just under the top of its range back to the bottom. On a CANcoder, AbsoluteSensorDiscontinuityPoint (0 to 1 rotation, default 0.5) sets where: the absolute position stays between that value minus one and that value. With the default, the reading runs from −0.5 to 0.5 rotation, and the jump is half a turn from zero.
The jump matters most at power-up. A CANcoder's position starts from its absolute reading at boot, and counts smoothly from there. If the mechanism is past the wrap point when the robot turns on, its position starts a full turn off, and every setpoint asks it to go the long way around.
Worked example. An arm swings from level in front (0°) over the top to 20° below level at the back (200°). With the default wrap point, the jump is at 180°, inside the travel. An arm that powers up at 190° reads −170°. Setting AbsoluteSensorDiscontinuityPoint to 0.75 moves the jump to 270°, straight down, where this arm never goes. The reading then runs from 0 to 0.56 rotation across the whole travel.
The rule: put the zero where it is useful (level, for an arm's gravity feedforward), then move the wrap point into the part of the circle the mechanism never reaches.
3.5 Using an absolute encoder with the motor
An absolute encoder is usually slower and coarser than the motor's encoder, and it sits on the CAN bus or the roboRIO. The motor's encoder is fast but doesn't know where it started. The usual answer is to let the absolute encoder set the starting point and the motor's encoder run the loop:
| How | What it does | Notes |
|---|---|---|
| Seed once at boot | Read the absolute encoder once and call setPosition() on the motor with that angle. From then on, use only the motor's encoder. | Works with any absolute encoder. Wait until the encoder reports a valid reading. It won't notice a slip after boot, or recover from a motor controller reboot unless the code seeds again. |
RemoteCANcoder (or RemoteCANdiPWM1/2) | The Talon FX uses the remote encoder as its feedback sensor. | No Phoenix Pro license needed. The feedback crosses the CAN bus, so it updates less often than the rotor sensor. |
FusedCANcoder (or FusedCANdiPWM1/2) | The Talon FX combines the encoder's absolute angle with its fast rotor sensor. | Needs Phoenix Pro. Absolute and fast. Built for swerve steering, and works for arms too. |
SyncCANcoder (or SyncCANdiPWM1/2) | The Talon FX sets its rotor position from the encoder, then controls with the rotor sensor. It reports when the two disagree by a lot. | Needs Phoenix Pro. Built for mechanisms where the encoder could fail or slip and report a position that doesn't match the mechanism. |
The last three rows are FeedbackSensorSource options in FeedbackConfigs, with the encoder's ID in FeedbackRemoteSensorID. For those, set two ratios so positions come out in mechanism rotations:
SensorToMechanismRatio: encoder turns per mechanism turn. For the 100:1 arm with a 1:1 encoder on its axle, 1.RotorToSensorRatio: rotor turns per encoder turn. For that arm, 100. The fused and synced sources need it to combine the two sensors.
When seeding once at boot, the motor still uses its rotor sensor. SensorToMechanismRatio is then the whole gear ratio, 100 for that arm, and RotorToSensorRatio stays at 1.
With any of the three feedback sources, a Talon FX that reboots during a match recovers its position from the encoder on its own, which seeding once at boot can't do (How a zero gets lost).
3.6 Checking an absolute encoder
- Check the direction. Move the mechanism in its positive direction by hand. The reading should go up.
- Check the zero. At level, an arm should read 0. Pointing straight up, it should read 0.25 rotation.
- Sweep the whole travel. Move slowly from one end to the other and watch for a jump. A jump means the wrap point is inside the travel.
- Power-cycle at several positions. Turn the robot off and on with the mechanism near each end and in the middle. The reading should be right every time.
- Log both positions. Log the absolute encoder and the motor's position side by side in matches. A difference that grows means something is slipping.
Hard Stops
Why every mechanism with limited travel needs solid hard stops, how to place soft limits inside them, and how to zero against them, either at a known starting position or with a stall-detection homing routine.
Limit Switches and Hall Effect Sensors
Sensors that mark one position. Limit switches, hall effect sensors and magnets, wiring them to a Talon FX, zeroing on the fly, and homing a turret exactly with a hall effect sensor and the motor's rotor angle.