Lab 5: Add Rotation (Inverse Kinematics)¶
Goal: drive and spin at the same time. Now each wheel needs its own speed and angle, and you'll write the math that computes them.
Why: each wheel moves differently when the robot spins¶
When the robot spins at ω (rad/s, CCW positive), a point at position (x, y) from the center moves sideways around the center. Add that to the robot's translation:
module vx = vx − ω × y
module vy = vy + ω × x
speed = √(vx² + vy²) angle = atan2(vy, vx)
Worked example. FL module at (0.3, 0.3) m. Robot drives forward 1 m/s and spins 1 rad/s CCW:
vx = 1 − 1 × 0.3 = 0.7
vy = 0 + 1 × 0.3 = 0.3
speed = √(0.49 + 0.09) = 0.76 m/s angle = atan2(0.3, 0.7) = 23.2°
Do the other three modules on paper with our real module positions before writing code. Then predict: which wheel goes fastest? (Check the swerve view in AdvantageScope to see if you're right.)
Module positions. Measured from the robot center to each wheel's center (L = wheelbase front-to-back, W = trackwidth left-to-right):
FL ( +L/2, +W/2 ) FR ( +L/2, −W/2 )
BL ( −L/2, +W/2 ) BR ( −L/2, −W/2 )
Desaturation. At full stick plus full spin, some wheels may be asked to go faster than they can. If you clamp just those wheels, the ratios between wheels change and the robot moves in the wrong direction. Instead, scale every wheel by the same factor:
if fastest wheel > maxSpeed:
every speed × (maxSpeed / fastest)
Same motion shape, just slower.
Students write: MyKinematics¶
package frc.robot.lib;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.SwerveModuleState;
public class MyKinematics {
private final Translation2d[] modulePositionsMeters; // FL, FR, BL, BR; robot frame
public MyKinematics(Translation2d... modulePositionsMeters) {
this.modulePositionsMeters = modulePositionsMeters;
}
/** Chassis speeds (robot frame: vx, vy m/s; omega rad/s CCW) → one state per module. */
public SwerveModuleState[] toModuleStates(ChassisSpeeds speeds) {
// TODO (students): the formulas above, once per module.
return new SwerveModuleState[4];
}
/** If any wheel exceeds maxSpeed, scale ALL wheels down by the same factor. */
public static void desaturate(SwerveModuleState[] states, double maxSpeedMetersPerSecond) {
// TODO (students)
}
}
Unit test against WPILib's SwerveDriveKinematics (same positions, same order). Test: pure forward, pure sideways, pure rotation, and "forward while spinning". Compare to SwerveDriveKinematics.desaturateWheelSpeeds too.
Code¶
Add to Constants.Swerve:
// TODO: measure ours, wheel center to wheel center.
public static final double WHEELBASE_METERS = 0.6; // front-to-back
public static final double TRACKWIDTH_METERS = 0.6; // left-to-right
In SwerveSubsystem:
private final MyKinematics kinematics = new MyKinematics(
new Translation2d(+WHEELBASE_METERS / 2, +TRACKWIDTH_METERS / 2), // FL
new Translation2d(+WHEELBASE_METERS / 2, -TRACKWIDTH_METERS / 2), // FR
new Translation2d(-WHEELBASE_METERS / 2, +TRACKWIDTH_METERS / 2), // BL
new Translation2d(-WHEELBASE_METERS / 2, -TRACKWIDTH_METERS / 2)); // BR
/** Robot-relative driving. vx forward, vy left (m/s), omega CCW (rad/s). */
public void drive(ChassisSpeeds speeds) {
SwerveModuleState[] states = kinematics.toModuleStates(speeds);
double maxWheelSpeed = Tunable.number("Drive/MaxWheelSpeedMps", 1.0);
if (Tunable.toggle("Swerve/UseDesaturation", true)) {
MyKinematics.desaturate(states, maxWheelSpeed);
} else {
// The "wrong" way, kept so we can compare: clamp each wheel on its own.
for (SwerveModuleState state : states) {
state.speedMetersPerSecond = MathUtil.clamp(state.speedMetersPerSecond, -maxWheelSpeed, maxWheelSpeed);
}
}
for (int i = 0; i < 4; i++) {
modules[i].setDesiredState(states[i]);
}
SmartDashboard.putNumberArray("Swerve/ChassisSpeeds",
new double[] {speeds.vxMetersPerSecond, speeds.vyMetersPerSecond, speeds.omegaRadiansPerSecond});
}
In RobotContainer, the right stick X becomes rotation:
double omega = MathUtil.applyDeadband(-controller.getRightX(), deadband)
* Tunable.number("Drive/MaxOmegaRadPerSec", 1.0); // stick left = CCW = positive
swerve.drive(new ChassisSpeeds(x * maxSpeed, y * maxSpeed, omega));
Try it¶
- Pure rotation (right stick only). All four wheels should form a diamond: each one tangent to a circle around the center.
- Forward + rotation. Compare AdvantageScope to your paper calculation.
- On the 2D field (
Sim/TruePose): drive a slow circle while keeping the front pointed at the center.
Experiments¶
| Change | Predict | What you'll see |
|---|---|---|
| Pure rotation: which wheel is fastest? | All the same (square robot). With a rectangle robot? Still all the same: why? | |
| Forward + CCW spin: which side's wheels go faster? | The right side. Check the swerve view. | |
Set MaxWheelSpeedMps low (0.6), full forward + full spin, desaturation on |
Direction of travel? | Correct direction, just slower |
| Same, desaturation off (each wheel clamped on its own) | The robot spins less and translates more than you asked: the motion's shape changed | |
| Swap the FR and BL positions in the constructor | Pure rotation turns into a mess. That's a module order bug. | |
| Make one Y positive that should be negative | Spinning makes the robot "wobble" around a wrong center |
Checkpoint¶
Summary: Each module's velocity is the robot's translation plus a rotation term that depends on where it sits. Desaturation scales all wheels together.
Quiz
- For pure rotation, what angle does the FL module point?
- Hand-compute all four states for our robot driving 1 m/s left while spinning 1 rad/s CW.
- Why does clamping one wheel change the robot's direction?
- Which stick axis gives ω, and why the minus sign?
- Why must the module position order match the module array order?
What could go wrong in the real world: module positions in inches instead of meters (rotation is ~40× too strong); +Y sign wrong on the right side; trackwidth measured to the outside of the wheels instead of the centers.