Debugging Swerve Drive Lesson

Debugging Swerve Drive

A swerve drive is a complex system. When things go wrong, it can be intimidating to find the cause. This lesson provides a systematic checklist for debugging, allowing you to quickly isolate whether a problem is in hardware, low-level control, or high-level logic.

The Debugging Mindset: Isolate and Test

The core principle of debugging is isolation. Instead of looking at the entire complex system at once, we test the smallest possible components first and work our way up. Our most powerful tool for this process is the SmartDashboard, which gives us a real-time window into the robot's state.

Step 1: Check the SmartDashboard

The first step is always to visualize the state of each module. We use custom widgets to display the angle and speed of each of the four swerve modules.

Common Problems to Look For:

  • A module is "stuck" at one angle and won't turn.
  • A module's angle is jittery or jumping erratically.
  • A drive wheel isn't spinning when it should be.
  • A module is spinning the wrong way (a simple motor inversion issue in code).

Step 2: The "Wheel Test" - Isolating a Single Module

If the dashboard reveals an issue with a specific module, we need to isolate it. We put the robot on blocks and run a special test routine that commands the suspicious module to spin its drive motor slowly while its steering motor rotates through a full 360 degrees.

What to Watch For:

  • Does the drive motor spin smoothly?
  • Does the steering motor rotate smoothly?
  • Does the angle on SmartDashboard match the physical angle of the wheel? If not, the absolute encoder is the likely culprit.

Step 3: Verifying the Gyroscope

If all four modules pass the wheel test but the robot still "drifts" or doesn't drive straight in field-oriented mode, the problem is almost always the gyroscope.

To test it, we place the robot on the ground, reset the gyro to 0, physically rotate the robot exactly 90 degrees, and check the SmartDashboard. The reading should be very close to 90.0. If not, the gyro may be improperly calibrated, mounted loosely, or failing.

Step 4: Final Logic Check - Is the Math Right?

If all hardware checks out, the final place to look is in the high-level code.

  • Check Module Locations: In our constants file, we define the physical (X, Y) location of each swerve module. If these values are incorrect, the kinematics calculations will be wrong.
  • Verify Units: Ensure all our calculations are consistent (e.g., meters vs. inches, radians vs. degrees).

Test Your Knowledge

Question: Your swerve drive drifts to the left when you command it to drive straight forward in field-oriented mode. The individual wheel tests all pass. What is the most likely culprit?