A swerve drivebase achieves omnidirectional motion by independently commanding the drive velocity and steering azimuth of each of its four wheel modules.
To control this system, software must continuously convert between overall chassis motion and individual wheel states using Swerve Kinematics.
1. Coordinate Frames & ChassisSpeeds
Section titled “1. Coordinate Frames & ChassisSpeeds”WPILib robotics standardizes on the NWU (North-West-Up) coordinate system:
- +X: Forward downfield
- +Y: Left
- +: Counter-clockwise rotation (in radians or degrees)
A robot’s instantaneous motion is represented by a ChassisSpeeds object:
// Robot commanded: 3.0 m/s forward, 1.0 m/s strafe left, 1.5 rad/s counter-clockwise rotationChassisSpeeds speeds = new ChassisSpeeds(3.0, 1.0, 1.5);2. Field-Oriented vs. Robot-Oriented Drive
Section titled “2. Field-Oriented vs. Robot-Oriented Drive”- Robot-Oriented: Pushing the joystick forward drives in the direction the robot’s front bumper currently points. If the robot spins 180°, pushing forward drives toward the driver station.
- Field-Oriented: Pushing the joystick forward drives away from the driver station regardless of robot heading.
WPILib converts field-oriented velocity vectors into robot-centric velocities using the robot’s gyro heading:
ChassisSpeeds robotSpeeds = ChassisSpeeds.fromFieldRelativeSpeeds( fieldVx, fieldVy, fieldOmega, m_gyro.getRotation2d());The transformation rotates the translation vector by :
v_robot_x = v_field_x · cos(θ) + v_field_y · sin(θ)v_robot_y = -v_field_x · sin(θ) + v_field_y · cos(θ)3. Inverse Kinematics: toSwerveModuleStates
Section titled “3. Inverse Kinematics: toSwerveModuleStates”Given the chassis velocity vector , Inverse Kinematics computes the target speed and steering angle for each module:
// Convert chassis speeds to 4 SwerveModuleState objects [FL, FR, BL, BR]SwerveModuleState[] moduleStates = m_kinematics.toSwerveModuleStates(robotSpeeds);Each module state contains:
speedMetersPerSecond: Target linear velocity of the drive wheel.angle: Target azimuth steering angle (Rotation2d).
4. Wheel Speed Desaturation
Section titled “4. Wheel Speed Desaturation”Consider a robot capable of a physical maximum wheel speed of 4.5 m/s. If the driver commands full forward throttle (4.5 m/s) while simultaneously commanding full spin rotation (3.0 rad/s), the outer wheels will mathematically be demanded to travel at 6.5 m/s.
Because the physical motors cannot exceed 4.5 m/s, the outer wheels will saturate at maximum output while the inner wheels run at lower commanded speeds. This destroys the velocity ratios and causes the robot to skew off course.
To prevent saturation, always call desaturateWheelSpeeds:
// Scales all module speeds down proportionally if any module exceeds physical limitSwerveDriveKinematics.desaturateWheelSpeeds(moduleStates, MAX_PHYSICAL_SPEED_MPS);Calling desaturateWheelSpeeds preserves the ratio between translation and rotation so the robot follows the commanded heading even at 100% throttle.
Common Failure Modes
Section titled “Common Failure Modes”1. Inverted Gyroscope Heading
Section titled “1. Inverted Gyroscope Heading”- Symptom: Field-oriented driving veers in bizarre circles or inverts controls when the robot rotates.
- Cause: Gyro yaw is reporting clockwise positive instead of counter-clockwise positive (NWU).
- Fix: Verify gyro direction. In Phoenix 6, Pigeon 2.0 natively outputs CCW-positive angles matching WPILib.
2. Drivetrain Skews During Fast Rotation
Section titled “2. Drivetrain Skews During Fast Rotation”- Symptom: Driving straight works, but spinning while translating causes the robot to veer sharply.
- Cause: Forgot to call
SwerveDriveKinematics.desaturateWheelSpeeds(states, MAX_SPEED). - Fix: Add desaturation immediately after
toSwerveModuleStates().
Check Your Work
Section titled “Check Your Work”- In simulation or on the cart, command forward translation with the robot at 0° heading. Verify all 4 wheels steer to 0° and spin forward.
- Manually rotate the robot 90° CCW. Command forward translation again.
- Verify that the wheels steer to -90° (robot-relative) to maintain true field-forward motion.
Verified Sources & Standards
- WPILib Swerve Drive Kinematics & Odometrydocs.wpilib.orgWPILib Standard