To control an omnidirectional swerve drivetrain or differential tank robot, software must translate between two coordinate systems:
- Chassis Speeds: The robot’s overall forward speed (), strafe speed (), and angular rotation rate (ω).
- Module States: The specific drive speed () and steering angle () of each individual wheel module.
Forward and Inverse Kinematics
Section titled “Forward and Inverse Kinematics”- Inverse Kinematics: Takes desired robot velocity vectors and calculates the required wheel speeds and angles for each of the 4 swerve modules:
[vx, vy, ω]^T -> [v_FL, v_FR, v_BL, v_BR] - Forward Kinematics: Takes measured wheel velocities and angles from motor encoders and calculates actual chassis velocity.
// ChassisSpeeds to SwerveModuleState arrayChassisSpeeds speeds = ChassisSpeeds.fromFieldRelativeSpeeds( vx, vy, omega, m_gyro.getRotation2d());
SwerveModuleState[] states = m_kinematics.toSwerveModuleStates(speeds);SwerveDriveKinematics.desaturateWheelSpeeds(states, MAX_SPEED);If you command full forward throttle and full angular rotation simultaneously, the kinematics calculation may demand 6 m/s from the outer wheels when your motors can only achieve 4.5 m/s. Calling desaturateWheelSpeeds() scales all module speeds down proportionally, preserving your heading without motor saturation.
Odometry: Integrating Pose2d
Section titled “Odometry: Integrating Pose2d”Robot odometry estimates field position by continuously integrating wheel distance deltas and gyro angles:
Δp = Twist2d(Δx, Δy, Δθ)
Modern WPILib uses exponential pose integration (Lie groups) rather than naive Euler integration, accurately accounting for circular arc paths traveled during the 20ms robot loop cycle.
Verified Sources & Standards
- WPILib Kinematics, Odometry, and Coordinate Systemsdocs.wpilib.orgWPILib Standard