EasyCRT
Chinese Remainder Theorem! Made easy by 6911
EasyCRT finds the absolute angle of a mechanism that turns more than once, usually a turret, from two absolute encoders. Each encoder sits on a pinion with a different tooth count, and both pinions mesh with the same gear. Neither encoder knows the angle on its own, but the pair of readings is unique over a much longer travel than either one.
This page explains how it works, then how to set it up step by step, then how to fix it when it misbehaves.
How it works

Encoder A's reading fits several angles of the gear both pinions mesh with (the shared gear), one for each number of whole turns the pinion could have made.
EasyCRTlists every one inside the travel.For each candidate it predicts what encoder B should read.
The candidate whose prediction is closest to encoder B's real reading wins.
This is not a textbook Chinese Remainder Theorem solve. It is the same idea done as a search, which keeps working with backlash and sensor noise.
The readings only repeat after the shared gear has turned a long way. That distance is the unique range. Past it, two positions give the same pair of readings, so the travel of your mechanism has to fit inside it.
Team 4967 wrote a white paper describing how the Chinese Remainder Theorem works for gears:
What to know before you choose it
Solve for the absolute angle once at startup, then seed the motor controller with the result. Do not use
EasyCRTas closed loop feedback.CRT is sensitive to any delay between the two encoder reads, so it is unreliable while the mechanism moves. Read both encoders while the mechanism is still.
Backlash between the two encoders, and loose pinions or magnets, cause failures.
solvereturns an empty result instead of a guess when the two readings do not agree, so your code has to handle an empty result.EasyCRThas no offset or inversion settings. Both encoders must read zero with the mechanism at zero, and both must count up when the mechanism moves in its positive direction.
Alternatives
CRT is not the only way to find the absolute angle of your turret. Choose whatever is easiest for you:
A zero sensor (limit switch, magnetic limit switch) and rotating to hit it
A 10-turn pot on a reduction
An absolute encoder with a 1:1 reduction
Optionally add a slightly less than 1:1 pinion on a second absolute encoder and find how many turns there are algebraically.
Set it up
The whole API:
new EasyCRT(shared, a, b)
EasyCRT{shared, a, b}
Tooth counts of the shared gear and the two encoder pinions.
solve(a, b)
Solve(a, b)
Angle of the shared gear, for travel centered on zero.
solve(a, b, min, max)
Solve(a, b, min, max)
The same, for travel that is not centered on zero.
getUniqueRange()
GetUniqueRange()
How far the shared gear can turn before the readings repeat.
solve returns an empty Optional (std::nullopt in C++) when the two readings do not agree on one angle. Everything else (choosing gears, zeroing, inversion, gear trains, checking your margin) is something you do yourself, with the steps below.
Step 1: Choose tooth counts
The readings repeat together every lcm(a, b) teeth of the shared gear, so:
Pick pinions with no common factor (coprime), so the lcm is their product. Sharing a factor throws range away: 20 and 30 teeth only cover 60 teeth of the shared gear, not 600.
13
15
195
1.95 turns
0.975 turns
17
19
323
3.23 turns
1.615 turns
19
21
399
3.99 turns
1.995 turns
20
21
420
4.20 turns
2.10 turns
25
27
675
6.75 turns
3.375 turns
30
31
930
9.30 turns
4.65 turns
The range has to be at least your full travel, including how far the mechanism can coast past its soft limits. getUniqueRange() returns it, and solve(a, b, min, max) throws an IllegalArgumentException if the travel you pass is longer.
To check a pair before you build it:
Check: the range is longer than your travel, with room on both sides.
Step 2: Zero the encoders and check their direction


EasyCRT takes the two readings as they are, so the encoders have to be set up first:
Zero both encoders with the mechanism at zero. Use the vendor tool to set each encoder's offset. If an encoder cannot store an offset, subtract it yourself:
crt.solve(encoderA.get().minus(offsetA), encoderB.get().minus(offsetB)).Both encoders must count up when the mechanism moves in its positive direction. A pinion turns the opposite way to the gear it meshes with, so the direction depends on the layout (see Step 3). Set the direction on the encoder, or negate the reading before passing it in.
Read while the mechanism is still. The two readings are taken one after the other, so any motion between them looks like disagreement.
Check: turn the mechanism by hand in its positive direction. Both readings go up.
Step 3: Solve once at startup and seed the motor
There are four common places to put the two encoder pinions on a turret. In every one, the gear both pinions mesh with is the shared gear.

Two gearings are involved, and they are not the same thing:
The motor's gearing (
motorToTurret) goes in theSmartMotorControllerConfigwithwithGearing(...). It is the whole reduction from the motor to the turret.The shared gear's gearing is only needed in layout 4, to turn the solved angle into the turret angle.
setEncoderPosition(...) always takes the turret angle, and applies the motor's gearing itself. The tooth counts below are examples.
Layouts 1, 2 and 3: use the turret's tooth count

When every gear between the pinions and the turret simply meshes with the next one, the gears in between cancel out: each pinion turns 200 / 19 or 200 / 21 times per turret turn, whichever gear it touches. So in these three layouts you give EasyCRT the turret gear's tooth count, and solve returns the turret angle with no conversion:
1
On the turret gear
Pinions turn opposite to the turret, so both encoders usually need inverting
2
On the motor's drive gear, which drives the turret gear
Two meshes away, so the pinions turn the same way as the turret
3
On a common gear that meshes with the turret gear
Two meshes away, the same way as the turret

Layout 4: CRT gears on the drive shaft


Both pinions mesh with a second gear fixed to the same shaft as the motor's drive gear. Two different gears on one shaft do not cancel, so the shortcut does not work. Give EasyCRT the 60 tooth gear, which makes solve return the shaft angle, and multiply by the shaft to turret MechanismGearing. The same applies whenever the shared gear sits behind a gearbox stage. The pinions turn the same way as the turret.
Layout 4 also has a shorter unique range. 399 teeth of the 60 tooth gear is 399 / 60 = 6.65 turns of the shaft, which is 6.65 / 5 = 1.33 turns of the turret, against 1.995 in layouts 1 to 3. A bigger gear on the shaft uses the range up faster. The unique range in turret turns is getUniqueRange() multiplied by shaftToTurret.getRotorToMechanismRatio().
Travel that is not centered on zero
solve(a, b) assumes the mechanism's zero is in the middle of its travel, and returns an angle within half the unique range either side of zero. For a turret that goes from 0 to 540 degrees, give the travel:
Give the real travel, not the whole unique range. A tighter range leaves fewer candidates, and it keeps a reading taken slightly past an end stop from wrapping to the other end.

The travel is in shared gear angles. In layouts 1 to 3 with the turret's tooth count, that is the turret angle. In layout 4, convert the turret's travel to shaft angles first:

The BroncBotz_2026 examples have a complete Turret class: Commands v2 and Commands v3.
Check: after a power cycle, the turret's mechanism/position matches its real angle at several positions across the travel.
Step 4: Check your margin
EasyCRT has no tolerance setting. It works the tolerance out from the gears:
Neighboring candidates predict encoder B readings
gcd(a, b) / bof a rotation apart. For 19 and 21 teeth that is 1/21 of a rotation, about 17 degrees of encoder B.A match is accepted when encoder B is within a quarter of that gap of its prediction, about 4.3 degrees here. Anything further off returns empty instead of a guess.
So the backlash and noise between the two encoders, measured at encoder B, has to stay under a quarter of the gap. To measure what you actually have, log the error yourself while moving the mechanism slowly by hand through its travel:
If the error gets near the limit:
Preload the mechanism to one side of its backlash before reading.
Use a larger pinion B, or pinions with a bigger tooth difference, which widens the gap.
Check that both encoder magnets and pinions are secure.
The accuracy of the result comes from encoder A alone: an error on encoder A shows up divided by shared / a.
Troubleshooting
Two failures are worth seeing in motion.


Always empty
An encoder is not zeroed at the mechanism's zero, or one encoder counts the wrong way.
Empty in some positions only
Backlash or a loose pinion is using up the margin. See Step 4: Check your margin.
Empty at the ends of travel
The mechanism is past the travel you gave, or the travel is the whole unique range. See the warning above.
Angle is off by a fixed amount
Encoder A's zero is off. The result follows encoder A.
Angle is off by a whole number of pinion turns
The readings were taken while moving, or encoder B's zero is off.
Angle is mirrored
Both encoders count the wrong way.
solve(a, b, min, max) throws an IllegalArgumentException
The travel is longer than the unique range. Choose different pinions.
Checklist
Last updated
Was this helpful?


