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.
An index sensor turns on at one point in a mechanism's travel. When it does, the code knows exactly where the mechanism is, and can zero it. In short:
- An index sensor marks one position. A limit switch, a hall effect sensor, or a beam break turns on at one point. Zero the mechanism when it does.
- A hall effect sensor needs no contact. It turns on when a magnet on the mechanism comes close. Nothing wears out, and it can sit in the middle of the travel, where a limit switch can't.
- A sensor triggers over a window, not at a point. Where it turns on depends on the direction and on how fast the mechanism moves. Zero slowly, from the same side every time.
- The motor's rotor angle can sharpen it. A hall effect sensor finds a turret to within a couple of degrees. The motor's rotor angle then finds the exact turret angle inside that window.
4.1 Kinds of index sensors
| Sensor | Triggers when | Good | Watch out for |
|---|---|---|---|
| Limit switch | The mechanism presses its lever or button | Cheap and simple. Easy to see when it works. | The contact wears and bends. It needs a hard stop next to it, or the mechanism can crush it. Only works at an end of travel. |
| Hall effect sensor | A magnet on the mechanism comes within a few millimetres | No contact, so nothing wears. Works anywhere in the travel, including the middle of a turret's. | Needs a magnet, at a fixed gap. Triggers over a window, not at a point. |
| Beam break | Something blocks a light beam | No contact | Needs a clear line between the emitter and receiver. Better known for detecting game pieces. |
| Proximity sensor (CTRE CANrange) | Something comes within a set distance | No contact, on the CAN bus | Measures distance to whatever is in front of it, so aim it at a solid target. |
A digital hall effect sensor is a small switch with three wires: power, ground, and signal. Most pull the signal low when they see a magnet (an "open-collector" output), so they wire like a limit switch. Many respond to only one pole of the magnet, so if one doesn't trigger, flip the magnet over. A CANcoder can also act as a magnet sensor: a Talon FX can treat it as a limit switch that turns on when the magnet's strength goes from bad (red) to adequate (orange) or good (green).
4.2 Wiring a sensor to a Talon FX
CTRE's documentation notes that the Talon FX built into a Kraken X60 doesn't support wired limit switches. There are two ways to use a sensor anyway:
| Route | How | Notes |
|---|---|---|
| A remote limit on the Talon FX | Wire the sensor to a CTRE CANdi (input S1 or S2). In the motor's HardwareLimitSwitchConfigs, set ReverseLimitSource to RemoteCANdiS1 and ReverseLimitRemoteSensorID to the CANdi's ID. (Forward works the same way.) | The motor stops driving in that direction while the sensor is on (ReverseLimitEnable, on by default). With ReverseLimitAutosetPositionEnable and ReverseLimitAutosetPositionValue, it also sets its own position when the sensor turns on. It acts without waiting for the robot loop, though the sensor's state still crosses the CAN bus. |
| A roboRIO digital input | Wire the sensor to a DIO port and read it with WPILib's DigitalInput. Call setPosition() when it turns on. | Read once per robot loop, every 20 ms. To stop the motor too, pass the reading to the control request's LimitReverseMotion (or LimitForwardMotion). |
The CANcoder and CANrange can also be remote limit sources, with RemoteCANcoder and RemoteCANrange.
At an end of travel, the sensor can do both jobs: stop the motor, and zero it. A sensor in the middle of the travel, such as on a turret, should only zero. Read it in code rather than set it up as a limit that stops the motor.
4.3 Zeroing on the fly
A sensor in the travel lets the code re-zero every time the mechanism passes it, not just at startup. How precise that zero is depends on three things.
1. How often you look. Robot code reads a sensor once per loop. The mechanism keeps moving between reads, so the zero can be off by up to the distance it moves in one loop:
| Mechanism moving at | Read every 20 ms, zero off by up to |
|---|---|
| Elevator at 1 m/s | 20 mm |
| Elevator at 0.1 m/s (homing speed) | 2 mm |
| Turret at 180°/s | 3.6° |
| Turret at 20°/s | 0.4° |
Zero while moving slowly. A sensor set up as a remote limit at an end of travel avoids the wait: the Talon FX sets its own position with the limit autoset.
2. Which edge, from which side. A hall effect sensor turns on over a window a few degrees or millimetres wide. Coming from the left, it turns on at the window's left edge. From the right, at its right edge. Hall effect switches also turn off a little later than they turned on (this is called hysteresis), which adds to the difference. Either always zero on the same edge, approaching from the same side, or sweep across the sensor and use the middle of the window.
3. The magnet's gap. A magnet closer to the sensor makes a wider window. Mount both rigidly, so the gap doesn't change when the mechanism flexes or vibrates.
4.4 Sensor plus rotor angle
A turret often has no hard stop to home against. If the robot tracks it through the match with only the motor's built-in encoder, and no camera aims it directly at the target, nothing corrects a bad starting angle. So the starting angle has to be right. A stationary hall effect sensor and a magnet on the turret gear can find home, and the motor's rotor angle can make it exact.
The problem. The sensor sees the magnet over a window a couple of degrees wide. Setting the turret to 0° whenever the sensor sees the magnet could leave it more than a degree off. At 5 m, 1.2° is about 10 cm to the side.
The trick. The motor's encoder knows the rotor's angle within one rotor turn exactly (Relative Encoders). Through the gearbox, one rotor turn is a small slice of turret angle. On a 50:1 turret it is 360° ÷ 50 = 7.2°. Turret angles one slice apart give the same rotor angle, so the rotor angle alone repeats every 7.2°. But the sensor's window is only about 3° wide, narrower than one slice. Inside the window, only one turret angle matches the rotor's angle.
The turret angle from home, in turret rotations, is:
where is the rotor's angle (rotations), is the rotor's angle measured once with the turret exactly at home, is the gear ratio (rotor turns per turret turn), and wrap moves a value into the range −0.5 to 0.5 by adding or subtracting whole turns. This gives the one turret angle within half a slice of home that matches the rotor. It is right as long as the turret is within half a slice of home, which the sensor guarantees when its window is narrower than one slice.
An example. Team 2056 (OP Robotics) has homed its turret this way since about 2020. A stationary hall effect sensor, a Littelfuse 55100 digital hall effect switch, sees a magnet on the turret gear over "a couple degrees". The turret is then tracked through the match by its Kraken X44's built-in encoder, with "no camera on our turret aiming directly based off the target". So instead of setting the turret "straight to 0 Deg" when the sensor triggers, "we use the absolute rotor position of the motor". Until the turret is homed, the robot's LEDs flash purple and the turret stays out of its aiming modes (Chief Delphi, August 2026, and the sensor).
4.4.1 Try it
The default is a 50:1 turret, a 3° window, and the turret 1.2° from home when the sensor is read.
Only one turret angle in the window gives this rotor reading: 1.2° from home. Calling it home instead would be 1.2° off, about 10 cm to the side at 5 m.
The sawtooth is the rotor's absolute angle, which the motor reads exactly but which repeats every 7.2° of turret travel. The dashed line is the rotor reading, and each dot is a turret angle that gives it. The shaded band is where the sensor sees the magnet. Home is calibrated once by recording the rotor angle with the turret exactly at home. The model ignores backlash between the rotor and the turret, which adds to the window, and treats the window as the same from both directions.
Things to notice:
- Where in the window doesn't matter. Move the turret anywhere inside the window. The rotor angle still gives the exact turret angle, from either direction.
- The window has to be narrower than one slice. Widen the window past 7.2°, or raise the gear ratio past 120:1 so one slice is narrower than the 3° window. The widget warns that the setup is unsafe. Now move the turret across the window. Near its edges, two turret angles in the window match, and picking the one nearest home is a whole slice off.
- Outside the window there is no answer. The sensor doesn't see the magnet, so the turret isn't homed yet.
4.4.2 Doing it on a robot
- Calibrate once. Put the turret exactly at home, in the middle of the sensor's window. Read the rotor angle and store it as . If home can't be in the middle of the window, the window's farther edge must still be less than half a slice from home. Redo it whenever the gearbox is taken apart or the motor is replaced, because re-meshing the gears changes which rotor angle lines up with home.
- While disabled, watch the sensor. Until it sees the magnet, the turret isn't homed. Show that to the drive team, for example with the robot's LEDs, and keep the turret out of its aiming modes. Someone can turn the turret by hand until it is over the sensor, or the robot can boot with it there.
- When the sensor sees the magnet, compute the angle and set it. Read the rotor angle before the first
setPosition()call, becausesetPosition()also shifts the rotor reading. - Stop checking. From here, the motor's encoder tracks the turret for the rest of the match.
- Plan for homing again. After
setPosition(), the rotor reading no longer shows the rotor's true angle, so the sketch below homes once per boot. It homes again after the motor controller reboots, which resets the reading. A re-zero button needs code that records how far the rotor reading shifted and subtracts it.
A sketch, run in periodic():
private static final double GEAR_RATIO = 50.0; // Rotor turns per turret turn.
private static final double HOME_ROTOR_ANGLE = 0.37; // Rotor angle (rotations) with the turret at home, measured once.
@Override
public void periodic() {
// The sensor pulls the input low when it sees the magnet.
if (!homed && !homeSensor.get()) {
// The rotor's absolute angle. setPosition() shifts it, so use it before the first call.
double rotor = motor.getRotorPosition().getValueAsDouble();
double turret = MathUtil.inputModulus(rotor - HOME_ROTOR_ANGLE, -0.5, 0.5) / GEAR_RATIO;
motor.setPosition(turret); // Turret rotations from home, with SensorToMechanismRatio = GEAR_RATIO.
homed = true;
}
}homeSensor is a WPILib DigitalInput. The constants are examples. Measure your own.
What limits it. Backlash between the rotor and the turret moves the turret without moving the rotor. The rotor angle is exact, but the turret can sit anywhere within the backlash, so the homed angle is only as good as the gear train. Backlash also adds to the window, and the window plus backlash must stay under one slice. A higher gear ratio makes slices narrower: at 120:1, one slice is 3°, about the width of the window. Check the margin when you choose the ratio.
This is the same idea as the two-encoder method on the next page: a coarse reading picks which slice the mechanism is in, and a fine reading that repeats every slice says exactly where in it.
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.
Two Encoders and the Chinese Remainder Theorem
How two absolute encoders on pinions with different tooth counts read a turret's angle over more than one turn, how to choose the tooth counts, how much error the solve tolerates, and how to run it safely.