Mecanum Kinematics
Easydifficulty
This is one of the easier lessons on the site. It's adding and subtracting, done every loop, and by the end you should be able to write a teleop drive from memory.
Three numbers in, four motors out
The driver gives you three numbers from two thumbsticks: how much to go forward, how much to strafe (slide sideways), and how much to turn. The robot has four motors. Every loop you turn those three numbers into four wheel powers, and if a sign is wrong the robot strafes when you meant to spin.
The obvious "add the sticks together" code has two more problems. It drives relative to the robot, so once the robot turns around the driver is fighting the controls. And pushing two sticks at once can ask a wheel for more than 100% power, which the motor clips, bending a straight line into a curve. Both are fixed below.
One idea: each wheel pushes along a diagonal
A mecanum wheel has small rollers around its edge set at 45°. Because of that angle, a spinning wheel doesn't push straight forward like a normal wheel. It pushes along a diagonal. The four wheels are mirrored so their diagonals make an X when you look down at the robot.
One wheel can only push along its diagonal, but add up all four pushes and interesting things happen. The sideways parts can cancel, leaving pure forward motion. The forward parts can cancel, leaving pure sideways motion. Or they can combine into a spin. Getting the motion you want is a matter of choosing the four wheel speeds.
Mixing: add the three things you want
Work in the robot's own frame first. Call the three parts of the command (forward), (strafe, positive to the left), and (turn, positive counter-clockwise). Each wheel gets a share of all three. For front-left, front-right, back-left, and back-right:
Check it one input at a time. Push only forward, : all four wheels get 0.5 and the robot drives straight. Push only left, : FR and BL get +0.5 and FL and BR get −0.5, and the X pattern pushes the robot sideways. Turn only, : the right wheels get +0.5 and the left wheels −0.5, and it spins in place.
The same four lines written as one matrix multiplication, which is how the ECAM and gm0 references at the end present it:
In the full physical version the column is , where and are the distances from the robot's center to the wheels, because wheels farther out have to travel further to turn the robot. For teleop with normalized powers you can fold that into and use . Push the sticks in the demo and watch each wheel's number come from those sums:
Field-centric: rotate the command first
Drivers think in field directions, like "toward the goal," not robot directions. So before mixing, rotate the driver's command from the field frame into the robot's frame using the robot's heading from the IMU.
Check with a case you can picture. The robot faces left, , and the driver pushes the stick away from them, toward the far wall. From the robot's point of view, the far wall is to its right, so the robot should strafe right: , negative. The rotation by does exactly that:
With , and , so a field command of becomes in the robot frame: no forward, full right. Now "push the stick away from you" means the same direction on the field whichever way the robot faces. Turn field-centric on in the demo and spin the heading to feel it.
Desaturation: keep the robot driving straight
The sums can come out bigger than 1. Full forward plus full turn asks the front-right wheel for , and the front-left for . Clip the front-right to 1 and leave the rest alone, and the ratio between the wheels changes, so the robot curves away from where you aimed.
Instead, divide all four by the biggest one, but only when it's over 1. Here that's 2, so the wheels become 1 and 0: the same turn-and-drive, just at half speed:
Commands that already fit pass through unchanged, because of the 1 in the max. Oversized ones shrink but keep their ratios, so the robot still goes the direction you asked. (gm0's tutorial divides by instead, which is a slightly more cautious version of the same idea.) In the demo, push forward and turn all the way and watch the "desaturate ÷" readout go above 1.
Scaling the joystick for fine control
A raw stick is linear: half stick gives half speed. But the moments that need precision, like creeping up to score, are small-stick moments, and a linear response makes them twitchy. Bending the response fixes it: small pushes give very small speeds, and full stick still gives full speed. Raise the stick's magnitude to a power . (Shape the magnitude rather than each axis separately, or the direction gets distorted.)
is linear; (squared) is the classic FTC feel — half stick now gives a quarter speed, so slow moves are gentle and the top end is untouched. Some drivers prefer cubic ().
Dead zone: ignore a stick that won't sit still
Thumbsticks don't return exactly to zero. Let go and the stick might report 0.03 forever, so the robot creeps on its own. A dead zone fixes that: any value smaller than a threshold counts as zero.
A plain dead zone causes a small lurch, though. The moment the stick passes , the output jumps from 0 to . The fix is to stretch what's left of the range so the output starts at 0 at the edge of the dead zone and reaches 1 at full stick. Combined with the response curve:
Here is the raw stick value, the dead zone (around 0.06 is typical), and the curve exponent. With and , a stick at 0.53 gives .
Drag the input below. The flat red band is the dead zone, and past it the blue curve climbs to full. A dead zone of 0.05 to 0.10 stops the drift without the driver noticing it.
A clean field-centric teleop drive
The whole thing is short as long as you keep the order: shape, rotate, mix, desaturate.
// One stick axis in [-1, 1] -> a shaped command in [-1, 1].
// DEADZONE ~ 0.06, DRIVE_CURVE ~ 2.0.
double shape(double s) {
double a = Math.abs(s);
if (a < DEADZONE) return 0; // ignore stick drift at rest
double scaled = (a - DEADZONE) / (1 - DEADZONE);
return Math.signum(s) * Math.pow(scaled, DRIVE_CURVE);
}
scaled runs from 0 at the edge of the dead zone to 1 at full stick, so the output doesn't jump.
// fwd +1 away from the driver, strafe +1 to the driver's left,
// turn +1 counter-clockwise, all [-1, 1]. heading is field-frame radians.
void driveFieldCentric(double fwd, double strafe, double turn, double heading) {
double mag = Math.min(1, Math.hypot(fwd, strafe)); // shape the MAGNITUDE
double dir = Math.atan2(strafe, fwd);
double m = shape(mag);
double fwdC = m * Math.cos(dir);
double strafeC = m * Math.sin(dir);
double w = shape(turn);
double c = Math.cos(heading), s = Math.sin(heading); // rotate by -heading
double vx = fwdC * c + strafeC * s;
double vy = -fwdC * s + strafeC * c;
double fl = vx - vy - w; // mix the wheels
double fr = vx + vy + w;
double bl = vx + vy - w;
double br = vx - vy + w;
double scale = Math.max(1, Math.max(
Math.max(Math.abs(fl), Math.abs(fr)),
Math.max(Math.abs(bl), Math.abs(br))));
setWheelPowers(fl / scale, fr / scale, bl / scale, br / scale);
}
Make a table of Squaring gives 0.0625, 0.25, 0.5625, and 1.0, so the bottom half of the stick now covers only a quarter
of the output range. Slow, precise driving gets most of the stick's travel. The cost is at the top,
where the same stick movement changes the speed more, so full-speed driving is a bit twitchier. Without stretching, the first output past the dead zone is 0.06 instead of 0, a 6% power jump. On a
drivetrain that's a visible twitch every time the driver starts moving.scaled^1 and scaled^2 at 0.25, 0.5, 0.75, and 1.0. Where does the driver gain fine
control, and what do they give up? Then, with DEADZONE = 0.06 and no stretching, how big is the jump
when the stick leaves the dead zone?Show solution
Push the stick to exactly 45° and square At 45° it still drives at 45°, because both parts are equal and get squared the same way. At 60° the
parts are 0.5 and 0.866. Squared, they're 0.25 and 0.75, and fwd and strafe separately. What angle does the robot drive
at? Now try 60°.Show solution
atan2(0.75, 0.25) is 71.6°, not 60°.
Shaping each axis separately bends every direction except straight along an axis or a diagonal.
Shaping the magnitude and rebuilding from the angle keeps the direction.
Each roller turns wheel spin into a push at 45°. Draw all four from above and add up the pushes for
"all wheels forward," then "left side forward, right side backward," then one diagonal pair (FL, BR)
driven against the other (FR, BL). All four forward: the sideways parts cancel in pairs and the forward parts add, so the robot drives
straight. Left side forward, right side backward: the forward and sideways parts both cancel, leaving
only a twist, so the robot spins in place. One diagonal against the other: the forward parts and the
twist cancel, leaving a sideways push, which is the strafe. Each sign in the mixer just says which of
those three a wheel contributes to, and in which direction.Show solution
Why divide every wheel by the same number instead of clipping each one at 1? The ratios between the four wheels are what set the direction and turn rate. Clip one wheel and the
robot goes somewhere you didn't ask, usually curving, and it happens exactly when you're pushing
hardest. Scaling all four keeps the ratios, so the robot goes the right way, just slower. When you've
asked for too much, slower is the right way to fall short.Show solution
In an FTC OpMode the sticks map in like this. The gamepad's Y axis is upside down, so negate it:
double fwd = -gamepad1.left_stick_y; // up on the stick = forward
double strafe = -gamepad1.left_stick_x; // left on the stick = +left (our convention)
double turn = -gamepad1.right_stick_x; // left = counter-clockwise
double heading = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.RADIANS);
driveFieldCentric(fwd, strafe, turn, heading);
In autonomous, the same (vx, vy, omega) command comes from a controller instead of the sticks. Before
building one, the next lesson, Angle Wrapping, fixes the one bug every heading
controller runs into.
The angled rollers lose a little grip sideways, so a mecanum robot strafes slower than it drives forward at the same power. The usual fix, from gm0, is to multiply the strafe command by about 1.1 before mixing. Tune the exact number on your robot.
You can use mecanum wheel encoders for position tracking, but the rollers slip sideways, so strafing distance and heading drift more than the ideal math suggests, especially when driving hard or pushing. Before an autonomous follower relies on that position, set up your localization library and check it with the Pose Estimation Contract, including how far off it is after returning to the start.
If one wheel fights the others, flip that motor's direction in the configuration, not the math. Pick one convention (+x forward, +y left, +ω counter-clockwise), write it in a comment, and make every wheel follow it.
This lesson follows three excellent public tutorials, adapted to our curriculum's conventions (+x forward, +y left, +ω CCW):
- Wheel kinematics and the mixing matrix: ECAM Eurobot, Mecanum wheel kinematics.
- The FTC wheel-power equations, field-centric rotation, normalization, and the strafe correction — gm0, Mecanum Drive.
- Joystick direction and magnitude, 45° roller-force intuition, sinusoidal wheel contributions, turn superposition, and common-factor normalization — SEA Monsters 2605, How a Mecanum Drive Works. Its interactive diagrams are a useful second explanation before deriving the four-sign mixer by hand.