2702 Recipes

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.

A swerve drive is four identical modules bolted to the frame, plus a gyro and the software that ties them together. This page takes a module apart and explains what each part is for. In short:

  1. Each module has two motors. The drive motor spins the wheel. The steer motor turns the module to point the wheel.
  2. An absolute encoder tells the steer motor where the wheel points. It remembers the angle through a power cycle. Its offset has to be set once per module.
  3. Gear ratios set the module's character. The drive ratio trades top speed for acceleration. The steer ratio sets how fast and how precisely the module turns.
  4. The gyro gives the robot's heading. Field-relative driving and odometry both depend on it.

2.1 Inside a module

The drawing below is the layout drawing of the MK3, a module from Swerve Drive Specialties (SDS). The MK3 is discontinued, but its parts are easy to see. The numbers in the drawing match the table.

1122345678
  1. 1Wheel and tread
  2. 2Bevel gear on the wheel
  3. 3Fork, which turns with the wheel
  4. 4Steering axis
  5. 5Module plate, bolted to the frame
  6. 6Gear stages for steering and drive
  7. 7Drive motor
  8. 8Steer (azimuth) motor
Layout drawing of the SDS MK3 module, © Swerve Drive Specialties. Left: from below. Right: from the side.
#PartWhat it doesNotes
1Wheel and treadGrips the carpetTread wears and compresses, which changes the wheel radius (§2.5)
2Bevel gearTurns the drive 90°, from the vertical shaft to the wheel axleThe last stage of the drive gearing
3ForkHolds the wheel and turns with itTurns on a large bearing in the module plate
4Steering axisThe vertical line the wheel turns aroundThe kinematics needs its position on the robot (§2.7)
5Module plateBolts the module to the frameOn the MK3, eight 1/4-20 threaded holes
6Gear stagesReduce each motor's speed and multiply its torqueThe steering stages end at a large gear that turns the fork. The drive stage feeds a shaft down the steering axis.
7Drive motorSpins the wheel to move the robotA Kraken X60 is the common choice. Runs a velocity loop.
8Steer motor (or azimuth motor)Turns the module to point the wheelOften a Kraken X44 or X60. Runs a position loop.
Absolute encoderMeasures the angle the wheel points, even right after power-upHidden in the drawing. On the MK3 it sits in the center of the module and reads the steering angle directly (§2.3).

The word azimuth means the direction the wheel points, measured as an angle. It is used for the steer motor, the steer encoder, and the steer gains.

SDS's full drawing gives the MK3's plate as 5.50 in square, with the steering axis 3.00 in from the plate's two outer edges. SDS sold it with a 12.8:1 steer ratio and a 6.86:1 or 8.16:1 drive ratio.

Newer modules have the same parts in a tighter package. SDS's current MK5n, for example, pairs a Kraken X60 drive motor with a Kraken X44 steer motor, uses a 287:11 (about 26.09:1) steer ratio, and encloses the absolute encoder on the steering axis. Its footprint isn't symmetric, so a robot needs two mirror-image versions (SDS calls them A and B). Ready to run, it weighs about 5.9 lb. Four of them are about 24 lb of a 115 lb robot.

2.2 Gear ratios

A gear ratio says how many times the motor turns for each turn of the output. A drive ratio of 6.75:1 means the drive motor turns 6.75 times for each turn of the wheel.

Drive ratio. A lower number (for example 5:1) gives a higher top speed but less acceleration and less force per amp of current. A higher number (for example 8:1) gives the reverse. Module makers sell each module with several drive ratios to choose from. Speed, Acceleration, and Traction shows how to choose.

Steer ratio. Turns of the steer motor per turn of the module. The MK3's is 12.8:1, and the MK5n's about 26.09:1. A higher ratio turns the module more slowly but holds its angle more precisely and against more scrub. Steering only has to turn the module a quarter turn at most (§4.2), so steering speed is rarely the limit.

In CTRE's swerve API, these are DriveMotorGearRatio and SteerMotorGearRatio in SwerveModuleConstants. The Tuner X Swerve Project Generator fills them in from the module you pick.

2.3 The absolute encoder

A Kraken has an encoder built into the motor. It is a relative encoder: it counts how far the rotor has turned since power-up, and starts from zero every time. That is fine for the drive motor, which only needs speed and distance. It is not enough for steering. After power-up, the steer motor would have no idea which way the wheel points.

An absolute encoder solves this. It reads a magnet fixed to the steering axis and reports the angle directly, from the moment it powers up. The steer motor uses it as its feedback sensor. In CTRE's swerve API, the steer motor's feedback source (SteerFeedbackType) can be:

OptionWhat it doesLicense
FusedCANcoderCombines the CANcoder's absolute angle with the motor's fast built-in encoderPhoenix Pro
SyncCANcoderSets the motor's built-in encoder from the CANcoder, then keeps the two in stepPhoenix Pro
RemoteCANcoderUses the CANcoder reading directly as feedbackNo Pro license needed

The offset. The magnet can be glued in at any angle, so each module's encoder reads some arbitrary value when its wheel points straight ahead. The encoder offset removes it. Line up all four wheels to point forward, read each encoder, and store the negative of that reading as the offset (EncoderOffset in SwerveModuleConstants, which the generator writes for you). On the CANcoder itself the matching config is MagnetOffset. An offset that is wrong by a few degrees makes the robot drift sideways or turn while it drives. An offset that is wrong by 180° makes that module drive backward.

Redo the offset

Redo the offset whenever a module is taken apart, or its encoder or magnet is moved.

2.4 Coupling

In most modules the drive shaft runs down the middle of the steering axis to the bevel gears (§2.1). Because of that, turning the module also turns the wheel a little, even when the drive motor holds still. The bevel gear rolls around the gear it meshes with.

This coupling fools both control and odometry. While the module turns, the drive motor's encoder sees a rotation that didn't move the robot. CTRE's swerve API corrects for it with CouplingGearRatio: the number of drive motor rotations caused by one rotation of the module. The generator fills it in for the modules it knows.

2.5 Wheels and tread

Swerve wheels are usually about 4 in (0.1 m) in diameter, with a replaceable tread on the rim. The tread decides how hard the robot can push and accelerate before the wheels slip (Speed, Acceleration, and Traction §5.2).

Tread compresses under the robot's weight and wears through a season, so the real radius is a little smaller than the nominal one. A wheel radius that is 2% off makes every odometry distance 2% off. Swerve Tuning §5.5 shows how to measure it on the robot. Check tread wear at every event.

2.6 The gyro

The gyro (or IMU, inertial measurement unit) measures the robot's heading. A swerve robot uses it for two things:

  1. Field-relative driving. The robot turns the driver's request from the field's frame into its own frame using the heading (Swerve Kinematics §3.4).
  2. Odometry. The heading from the gyro is much better than one worked out from the wheels (Localization Basics).

CTRE's Pigeon 2 is a common choice. Let it calibrate while the robot is still. §2.7 covers where to mount it.

2.7 The drivetrain as a whole

FrontFL (0.29, 0.29)FR (0.29, −0.29)BR (−0.29, −0.29)BL (−0.29, 0.29)R = 0.41 m0.58 m+x+yGyroFrame tubeBumpersSteering axis
A 29 in square drivetrain from above, with the front to the right and an SDS MK3 in each corner. With the modules flush with the frame's outer edges, each steering axis is 3.00 in in from both edges, or about 0.29 m from the robot's center in x and y. These are the module positions the kinematics uses. The MK3 fits every corner, turned. The gyro sits flat near the center, with its x axis pointing forward.
  • Module placement. Modules usually sit at the corners of the frame. The farther apart they are, the more stable the robot is and the more torque the wheels have to turn it. The distance from the robot's center to each module also sets how fast the robot can spin (Speed, Acceleration, and Traction §5.6).
  • Module positions in code. The kinematics needs each steering axis's position from the robot's center, in meters, with +x+x forward and +y+y left (Swerve Kinematics §3.1). Measure them from the CAD, not the frame edge. On the 29 in frame above, each axis is about 0.29 m from the center in both directions, the square used on the Kinematics page. List the modules in the same order everywhere in the code.
  • One module, or two versions. The MK3 fits every corner, turned. A module whose footprint isn't symmetric, such as the MK5n, comes in two mirror-image versions. Diagonal corners take the same one, and neighboring corners take the other.
  • The gyro. Mount it flat, firmly, and near the center of the robot. Line its xx axis up with the robot's front, or tell the software how it is turned (the Pigeon 2's MountPose configs). A gyro that is tilted or loose reads the robot's turning wrong, and a heading error moves every field-relative command and every odometry step.
  • Eight motors on CAN. The four drive motors, four steer motors, four CANcoders, and the gyro all talk over CAN. A CTRE CANivore puts them on a faster CAN FD bus, which lets the swerve code read them more often. CTRE's SwerveDrivetrain runs odometry at 250 Hz on CAN FD by default (Localization Basics §1.5).
  • One battery. All eight motors draw from the same battery. Current limits keep the wheels from slipping and the battery from sagging (Speed, Acceleration, and Traction §5.5).

2.8 The software pieces

PieceWhat it doesWhere to read more
KinematicsTurns a robot velocity into a speed and angle for each module, and backSwerve Kinematics
Module controlPoints each module and spins its wheel, the short way roundCommanding the Modules
OdometryTracks the robot's pose from wheel distances, module angles, and the gyroLocalization Basics
Drive commandsTurn joystick input or a planned path into a robot velocityCommanding the Modules §4.6

WPILib has the math as plain classes (SwerveDriveKinematics, SwerveModuleState, SwerveDriveOdometry), and teams write the rest. CTRE's swerve API (SwerveDrivetrain, set up by the Tuner X Swerve Project Generator) does all of it, including a fast odometry thread. Team 2702 uses CTRE's API.

On this page