Swerve Kinematics
The math that turns a robot velocity into a speed and angle for each swerve module. Field-relative driving, the center of rotation, desaturation, and forward kinematics.
Kinematics is the math of motion without forces. For a swerve drive, it answers one question: to move the robot at a given velocity, how fast must each wheel spin, and which way must it point? This page works through it. In short:
- Describe the robot's motion with three numbers: forward speed , sideways speed , and turn rate .
- Each module's velocity is the robot's velocity plus a turning part. The turning part is times the module's distance from the center, at 90° to the line from the center to the module.
- For field-relative driving, rotate the request by the gyro heading first.
- If any module would go faster than it can, slow all of them by the same factor. That keeps the robot on its path.
WPILib's SwerveDriveKinematics does all of this. CTRE's swerve requests do it too. Knowing the math helps you debug a robot that drives the wrong way.
3.1 Chassis speeds
The robot's velocity at one moment is three numbers, called chassis speeds (WPILib's ChassisSpeeds class):
| Symbol | Meaning | Units |
|---|---|---|
| Speed forward, toward the robot's front | m/s | |
| Speed to the robot's left | m/s | |
| Turn rate, counterclockwise seen from above | rad/s |
These use WPILib's robot frame: forward, left, and counterclockwise turns positive. Each module has a position in the same frame, measured from the robot's center. For a square drivetrain with modules 0.58 m apart, the front-left module is at and the back-right at .
3.2 One module
The robot is rigid, so every point on it moves with the center, plus a part from turning. A point at distance from the center, turning at , moves at speed , at 90° to the line from the center. For a module at , that gives:
The module's wheel speed and angle come from that vector:
Where:
- is the wheel's speed along the carpet (m/s)
- is the direction the wheel must point, counterclockwise from the robot's front
- is the two-argument arctangent, which gives the angle in the right quadrant
WPILib packs this into a matrix with two rows per module, and multiplies it by the chassis speeds:
That is SwerveDriveKinematics.toSwerveModuleStates(speeds). It returns one SwerveModuleState (speed and angle) per module, in the order you listed the module positions.
Worked example. Drive forward at 2 m/s while turning counterclockwise at 180°/s ( rad/s), on the 0.58 m square. The front-left module at gets:
So it runs at 1.42 m/s, pointed 39.9° to the left. All four:
| Module | Position (m) | Speed (m/s) | Angle |
|---|---|---|---|
| Front left | 1.42 | 39.9° | |
| Front right | 3.05 | 17.4° | |
| Back left | 1.42 | −39.9° | |
| Back right | 3.05 | −17.4° |
The right-side modules go faster. They are on the outside of the turn.
Module angles are in the robot frame, counterclockwise from the robot's front. Modules sit on a 0.58 m square. The fastest a module can go is 4.57 m/s (Kraken X60, FOC, at free speed through 6.75:1 and 4 in wheels). No module is over its limit. The model has no slip, no acceleration limits, and modules that steer instantly.
The widget starts at this example. The orange dashed line is where the robot will be over the next 0.75 s.
Things to try:
- Set vx and vy to 0. The robot spins in place. Every module points at 45° to the frame, along a circle around the center, and all four run at the same speed: m/s.
- Set ω to 0. All four modules point the same way at the same speed. The robot slides without turning.
- Switch to Robot-relative. The modules don't change, but the path does. It curves, because "forward" turns with the robot (§3.4).
- Raise vx to 4 m/s. The front-right module would need 5.0 m/s, more than it can give. Turn desaturation off to see the requested arrows that can't be reached (§3.5).
3.3 The center of rotation
At any moment, a turning robot is rotating around some point, the instantaneous center of rotation. Every module's velocity is at 90° to the line from that point to the module. That is why the dashed blue lines in the widget meet the module arrows at right angles.
The center of rotation is at
in the robot frame. In the worked example it is 0.64 m to the robot's left (). Spinning in place puts it at the center. Driving fast while turning slowly puts it far away, and the modules point almost the same way.
You can also choose the center of rotation. toSwerveModuleStates(speeds, centerOfRotation) turns the robot around any point, for example around a corner of the frame to pivot around a game element.
3.4 Field-relative driving
Drivers think in field directions: push the stick up, and the robot goes away from the driver station, whichever way it faces. The kinematics needs robot-relative speeds. So the code rotates the driver's request by the robot's heading from the gyro:
is the same in both frames. In WPILib, this is ChassisSpeeds.fromFieldRelativeSpeeds(vx, vy, omega, robotAngle). CTRE's SwerveRequest.FieldCentric does it for you.
The code repeats this every loop with the new heading. So a field-relative robot that spins while it drives still goes in a straight line. A robot-relative request turns with the robot and drives an arc. Try it in the widget above: set a heading, then switch between the two.
Alliance direction. WPILib's field frame has pointing from the blue wall toward the red wall. A red-alliance driver faces the other way, so "stick up" should be . CTRE's FieldCentric request handles this through the operator perspective: by default (ForwardPerspective = OperatorPerspective) it rotates by a forward direction that your code sets on the drivetrain for each alliance. The Tuner X generated code sets it from the alliance color.
A wrong heading breaks it. If the gyro's zero is off by 10°, every field-relative request is off by 10° too. Reset the heading only when the robot really faces a known direction, or set it from a vision pose.
3.5 Desaturation
Each module has a top speed, set by its motor and gearing (Speed, Acceleration, and Traction §5.1). For a Kraken X60 (FOC) through 6.75:1 to a 4 in wheel, that is 4.57 m/s. The kinematics doesn't know about it. Ask for 4 m/s forward while turning at 180°/s, and the front-right module is told to go 4.995 m/s.
If each module just clipped its own speed to 4.57 m/s, the outside modules would slow down and the inside ones wouldn't. The modules would no longer agree, and the robot would curve off its path. Desaturation fixes this by scaling every module by the same factor, so the fastest one is exactly at the limit:
Every module, and so the whole robot, slows to 91.5% of the request. The robot goes the same direction and turns at the same rate relative to its speed, just a little slower.
In WPILib, call SwerveDriveKinematics.desaturateWheelSpeeds(states, maxSpeed) after toSwerveModuleStates. It changes the array in place. CTRE's swerve requests do it by default (DesaturateWheelSpeeds is true).
Translation and rotation share the same speed. A module's speed is the robot's speed plus its turning speed. At full speed forward, there is nothing left over for turning, so a hard turn slows the whole robot. Teams that want to keep turning authority at full speed limit the driver's translation speed below the module's top speed.
3.6 Forward kinematics
The reverse problem matters for odometry: from the measured module speeds and angles, what is the robot doing? Four modules give eight equations for three unknowns (, , ). They never agree exactly, because of noise and slip, so WPILib finds the chassis speeds that fit them best (a least-squares fit, using the matrix's pseudoinverse). That is toChassisSpeeds(states), and toTwist2d does the same for the distance each module moved. Localization Basics §1.2 uses it.
When one wheel slips, it disagrees with the other three. Some teams measure that disagreement to detect slip.
3.7 Skew and discretization
Discretize every request before the kinematics. CTRE's FieldCentric request does it for you. With WPILib, call ChassisSpeeds.discretize(speeds, dt), where dt is the loop period. Without it, a robot that spins while it drives drifts off its path.
Why it drifts. The robot code sends a new command every loop, often every 20 ms, and the modules hold it until the next one. A held robot-relative command turns with the robot, so during the loop the robot drives a small arc instead of a straight line (§3.4). On average, the robot moves in the direction it was asked to at the middle of the loop, not the start. So every loop's movement is turned by half the loop's turn, toward the spin. This is called skew. At 360°/s and 50 Hz, the robot turns 7.2° per loop, so it drives 3.6° off course, the same way every loop.
What discretizing does. discretize works out the robot-relative speeds that, held for one loop along an arc, end exactly where the request meant the robot to be. The same arc math is behind odometry's pose exponential (Localization Basics §1.3). The robot still bends a little inside each loop, but it is back on the line at the end of every loop.
The robot is told to drive along field +x while spinning. Each loop the code reads a perfect gyro, turns the request into robot-relative speeds, and holds them until the next loop. The modules follow every command exactly. They don't slip, and they steer and change speed instantly. Error is measured at the end of each loop. The robot is drawn to scale (0.75 m), with a bar on its front edge.
At the defaults (3 m/s along the field while spinning at 360°/s):
| Command rate | Turn per loop | Not discretized, after 4 m | Discretized, after 4 m |
|---|---|---|---|
| 50 Hz | 7.2° | 25 cm (3.6° off course) | 0 cm |
| 100 Hz | 3.6° | 13 cm (1.8° off course) | 0 cm |
| 250 Hz | 1.44° | 5.0 cm (0.72° off course) | 0 cm |
Things to try:
- Press Replay. Both robots spin the same way. The orange one slides steadily to the side of the line.
- Change the speed. The angle off course stays the same. It depends only on how far the robot turns in one loop, not on how fast it drives.
- Double the spin rate, or halve the command rate. Either one doubles the turn per loop, and the error doubles too.
- Set the spin rate to 0. The two paths match. Skew only happens while the robot turns.
Odometry and control need the arc fix separately. A robot can have perfect odometry and still drift from skew, and the odometry will faithfully report the drift.
CTRE's RobotCentric request doesn't discretize. WPILib's documentation also warns that scaling speeds after discretizing (as desaturation does) brings some of the skew back. That error is small next to the skew of not discretizing at all.
Anatomy of a Swerve Drive
The parts of a swerve module and drivetrain and what each one does. Drive and steer motors, gear ratios, the absolute encoder, coupling, wheels, and the gyro.
Commanding the Modules
How a swerve module follows its speed and angle. Turning the short way, continuous wrap, cosine compensation, holding still, and the full path from joystick to motor.