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.
A turret ring is a large gear with nothing in the middle to mount an encoder on, so its encoder sits on a small pinion that meshes with the ring. A small pinion turns many times per turret turn, and its reading repeats each time. Two pinions with different tooth counts repeat at different places, and together they pin the turret down. In short:
- One encoder repeats. A 19-tooth pinion on a 200-tooth turret gear turns 10.5 times per turret turn. Its reading alone says where the turret is within a 34.2° slice, not which slice.
- Two different pinions repeat together much less often. A 19-tooth and a 21-tooth pinion only line up the same way again after 399 teeth of the turret gear: almost two full turns. Inside that range, each pair of readings belongs to one turret angle.
- Solve by search. List every turret angle that fits encoder A, predict what encoder B should read for each, and keep the one that matches.
- Think in teeth. Every wrong answer misses encoder B by at least a whole tooth of the turret gear. Accept a match only within a quarter tooth, 0.45° on a 200-tooth gear. Solve once with the turret still, and keep the turret still if the solve fails.
5.1 The problem
Turrets often need more than 360° of travel, so they can track a target while the robot spins without stopping to unwind. That rules out a single absolute encoder geared to the turret 1:1, which would repeat inside the travel (The one-turn limit). There are four ways around it:
| Option | Trade-off |
|---|---|
| One encoder geared down, turning less than once over the whole travel | Simple, but needs a large gear on the encoder, and each encoder count covers more turret angle. |
| An index sensor with the motor's rotor angle (§4.4) | One cheap sensor, but someone has to turn the turret over it before the match. |
| A known starting angle, lined up by eye or with an alignment pin | No sensor, but easy to get wrong, with nothing to check it. |
| Two encoders on small pinions | Absolute at power-up over more than a turn, and fine resolution. Needs a second encoder and the math on this page. |
5.2 Counting in teeth
Count the turret's position in teeth: how many teeth of the turret gear have passed a pinion since zero. On a 200-tooth gear, each tooth is 1.8° of turret.
Pinion A has 19 teeth. Each time 19 turret teeth pass it, it makes one full turn and its reading starts over. So encoder A's reading, times 19, is the turret position's remainder after dividing by 19. Mathematicians write this as "position mod 19". Encoder B, with 21 teeth, gives the position mod 21.
Example. The turret is at 225°, which is 125 teeth.
- Encoder A: 125 = 6 × 19 + 11, so the remainder is 11, and A reads 11/19 = 0.579 rotation.
- Encoder B: 125 = 5 × 21 + 20, so the remainder is 20, and B reads 20/21 = 0.952 rotation.
Now work backward. Encoder A says the position is one of 11, 30, 49, 68, 87, 106, 125, and so on, every 19 teeth. Divide each by 21 and look at the remainder: 11, 9, 7, 5, 3, 1, 20. Only 125 leaves 20, which is what B reads. The turret is at 125 teeth, 225°.
This is the Chinese Remainder Theorem (CRT): if two numbers share no common factor, knowing a position's remainder for each pins the position down within their product. Here that is 19 × 21 = 399 teeth. In general, the two remainders repeat together every lcm(a, b) teeth, the least common multiple of the two tooth counts. So the turret angle is unique over:
where and are the pinions' teeth and is the turret gear's. For 19, 21, and 200, that is 399 / 200 = 1.995 turns, or 718°.
Real readings aren't whole teeth, because the turret can stop anywhere. The solve below works with fractions in the same way. It uses encoder A for the fine angle, and encoder B only to choose which slice.
5.3 Solving by search
- List the candidates. Encoder A's reading fits one turret angle per turn of pinion A, teeth apart. Keep every one inside the travel.
- Predict encoder B for each. Turn the candidate angle into turns of pinion B.
- Measure the miss. Subtract the prediction from B's actual reading , wrap it to half a turn either way, and multiply by to get teeth.
- Pick the smallest miss. Accept it only if it is under a quarter tooth. Otherwise, report a failure.
In symbols, with angles in turret turns and counting the candidates:
Why a quarter tooth. The right candidate misses only by however much the two encoders disagree. Every wrong candidate is a whole number of A's slices away, and none of those is a whole number of B's slices inside the unique range. So each wrong candidate misses by at least one whole tooth, give or take the disagreement. That leaves a one-tooth gap between right and wrong. Accepting only misses under a quarter tooth keeps a wide margin on both sides. A larger disagreement gets the solve refused, instead of letting it pick a wrong answer.
Solved: 225°. Encoder B misses it by 0 teeth, and the next best candidate by 1 tooth.
Top view, with the pinions drawn larger than scale. The pinions turn opposite to the turret, so each encoder is inverted in its config to count up as the turret turns positive (counterclockwise). Both read 0 with the turret at 0°. The chart has one stem for each turret angle in the travel that fits encoder A, 19 teeth (34.2°) apart. Its height is how far encoder B misses the reading that angle predicts, in teeth of the turret gear. The right candidate misses only by the disagreement. Every wrong one misses by at least a whole tooth, less the disagreement. A filled dot is accepted, a hollow one refused. The disagreement is added to encoder B only, and stands for any mix of backlash, offset error, and turret motion between the two reads.
The defaults are the example above: a 200-tooth turret gear, 19- and 21-tooth pinions, the turret at 225°, and travel of ±270°. Encoder A fits 16 candidates in the travel, and only the one at 225° matches B.
Things to try:
- Raise the disagreement. Up to a quarter tooth (0.45°), the solve is accepted and right. Between a quarter and three quarters of a tooth, it is refused: no candidate is a clean match. Past three quarters (1.35°), a wrong candidate fits within a quarter tooth and is accepted. It is 342° or 376° off.
- Make the pinions equal. With both at 19 teeth, every candidate matches, and the solve is ambiguous everywhere.
- Try 18 and 21 teeth. They share a factor of 3, so the remainders repeat together every 126 teeth, a range of only 227°.
- Widen the travel to ±360°. The travel is now longer than the 718° unique range. Turn the turret to 359° and two candidates fit.
5.4 Choosing the tooth counts
- Pick pinions with no common factor. Then lcm(a, b) is their product, and the range is as long as it can be. Close tooth counts are fine: 19 and 21, or 20 and 21.
- Cover the travel with margin. The unique range has to be longer than the travel, plus how far the turret can coast past its soft limits. Otherwise a turret that overshoots one end can read as being near the other.
- Larger pinions give more range, but they turn fewer times per turret turn. Each encoder's own error is divided by how many times its pinion turns per turret turn, so smaller pinions read the turret more finely.
On a 200-tooth turret gear:
| Pinions | lcm (teeth) | Unique range |
|---|---|---|
| 13 and 15 | 195 | 0.98 turn (351°) |
| 17 and 19 | 323 | 1.6 turns (581°) |
| 19 and 21 | 399 | 2.0 turns (718°) |
| 20 and 21 | 420 | 2.1 turns (756°) |
| 25 and 27 | 675 | 3.4 turns (1215°) |
| 18 and 21 | 126 | 0.63 turn (227°), since both share a factor of 3 |
AndyMark's 6.875 in turret kit uses a 200-tooth gear, and offers encoder mounts for 19- and 21-tooth pinions for this method (product page).
5.5 The error budget
The solve works only while the two encoders agree. Turn every error into turret degrees and add them up. On a 200-tooth gear, the total has to stay under a quarter tooth, 0.45°, for the solve to be accepted.
| Source | Size | Fix |
|---|---|---|
| Backlash at each pinion | The pinion can sit anywhere in its backlash. It is measured at the turret gear's teeth, so it counts in full. | Mesh the pinions tightly against the turret gear. |
| The turret moving between the two reads | Speed × time between reads. At 90°/s with reads 10 ms apart, 0.9°: half a tooth. | Solve only with the turret still. |
| Encoder offsets | An offset error at the encoder, divided by its pinion's turns per turret turn (about 9.5 for B). 4° at encoder B is 0.42° of turret. | Set both offsets carefully, at the same turret position. |
| Encoder noise | Divided by the same factor, so small. | None needed. |
A wrong answer is never close. On the 19 and 21 example, the nearest wrong candidates are 190 and 209 teeth away: 342° and 376° of turret. A turret that thinks it is 342° from where it is will wind its cables up as soon as it tries to aim. This is why the solve must refuse when the match isn't clean, and why the code must not move the turret after a refusal.
5.6 Running it on the robot
- Configure both encoders the same way. Each must count up as the turret turns in its positive direction. Pinions that mesh directly with the turret gear turn the opposite way, so invert them with
SensorDirection. Each must read 0 with the turret at 0, set withMagnetOffset. - Solve once, with the turret still. At boot, or while disabled, once both encoders report valid readings. Refresh both readings together, for example with
BaseStatusSignal.refreshAll(). - Seed the motor. Call
setPosition()with the solved angle, then control the turret with the motor's encoder. CRT finds the starting angle. It isn't a feedback sensor. - Handle a refused solve. Raise an alert, flash the LEDs, and keep the turret still until someone checks it.
- Check the margin. Turn the turret slowly through its whole travel and log the miss, in teeth, from a solve at each point. It should stay near zero everywhere. Misses that grow at some angles point to backlash or a gear that isn't centered.
A sketch of the solver:
private static final int TURRET_TEETH = 200;
private static final int A_TEETH = 19;
private static final int B_TEETH = 21;
private static final double MIN_TURRET = -0.8; // Rotations: the ±270° travel plus margin.
private static final double MAX_TURRET = 0.8;
/** Turret angle in rotations from the two encoder readings, or empty if they don't agree. */
static Optional<Double> solveTurret(double readA, double readB) {
double step = (double) A_TEETH / TURRET_TEETH; // Turret rotations per turn of pinion A.
double best = 0;
double bestMiss = Double.POSITIVE_INFINITY;
for (int k = (int) Math.ceil(MIN_TURRET / step - readA); (k + readA) * step <= MAX_TURRET; k++) {
double turret = (k + readA) * step;
double predictedB = turret * TURRET_TEETH / B_TEETH;
// How far encoder B misses this candidate, in teeth of the turret gear.
double miss = Math.abs(MathUtil.inputModulus(readB - predictedB, -0.5, 0.5)) * B_TEETH;
if (miss < bestMiss) {
bestMiss = miss;
best = turret;
}
}
return bestMiss < 0.25 ? Optional.of(best) : Optional.empty();
}readA and readB are the encoders' absolute positions in rotations, such as a CANcoder's getAbsolutePosition(). A CANcoder reads from −0.5 to 0.5 by default, and from 0 to 1, as in the example above, with AbsoluteSensorDiscontinuityPoint set to 1. The solver works with either. The travel of ±0.8 rotation (576°) is shorter than the 718° unique range, so two candidates can't both match.
Community code. YAMS (Yet Another Mechanism System) is a community library for FRC mechanisms, not part of WPILib or Phoenix 6. It includes a CRT helper, EasyCRT, contributed by Team 6911, with worked gear layouts and a check that the range covers the travel (EasyCRT docs, as of November 2026). It uses the same search as this page. It accepts a match within a quarter of the gap between candidates, which is a quarter tooth for pinions with no common factor.
Is it worth it? CRT adds an encoder, a pinion, and math that fails badly when the encoders disagree. Use CRT when only small pinions fit and the turret must know its angle over more than one turn the moment it powers up.
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.
Choosing a Strategy
Which homing strategy fits each kind of mechanism, the questions that decide it, a checklist for a reliable zero, and how to test it before competition.