๐Ÿ’ป Programming ยท Engineer ยท Intermediate

IMU Setup & Calibration

Inconsistent turns are almost always an IMU problem. This guide covers mounting, PROS API, EZ Template integration, drift, and a complete troubleshooting table for the four failure modes you'll actually encounter.

Before this guide: You should have a working drivetrain that uses pid_drive_set(). If your turns are off by more than a few degrees consistently, this is where to look.
๐Ÿงญ What the IMU Does

The V5 Inertial Sensor (IMU) is a gyroscope + accelerometer. For drivetrains, you only care about the gyroscope โ€” it measures rotation rate and integrates it into a heading angle (degrees). EZ Template uses this heading to:

๐Ÿ“ Mounting Rules
Mounting matters more than calibration. A correctly mounted IMU needs no special handling. A poorly mounted one can't be compensated with software.
๐Ÿ’ป PROS API Basics
// Declare in globals.hpp
pros::Imu imu(7); // port number

// In initialize() โ€” ALWAYS call before using
imu.reset(); // starts calibration, takes ~2 seconds
while(imu.is_calibrating()) pros::delay(10); // wait for finish

// Read heading (0โ€“360, clockwise positive in EZ Template)
double heading = imu.get_rotation(); // accumulates past 360
double heading = imu.get_heading();  // wraps 0โ€“360

// Reset to zero mid-auton
imu.set_rotation(0.0);
โš™ EZ Template Integration

EZ Template handles the IMU automatically โ€” you just declare it in the constructor. The 4th argument is the IMU port:

ez::Drive chassis ({-1,-2,-3}, {4,5,6}, 7, 3.25, 36.0/48.0, 360);
//                              โ†‘ IMU port โ€” must match physical wiring

EZ Template's initialize() calls chassis.initialize() which resets the IMU automatically. You don't need manual imu.reset() calls in most setups.

Resetting heading mid-auton: Use chassis.drive_imu_reset(); to zero the heading at any point. Useful for long routes where drift may accumulate.
๐Ÿ” Calibration Protocol
๐Ÿ”ง Troubleshooting Table
SymptomCauseFix
IMU never finishes calibratingRobot was moving during calibration, or IMU port is wrongCheck port number. Ensure robot is completely still for 2+ seconds after upload. Try a different port.
Turns overshoot consistentlyPID kP too high, or exit conditions too looseReduce turn kP in default_constants(). Check pid_turn_exit_condition_set โ€” tighten the settle time.
Turns undershoot consistentlyPID kP too low, or too much friction in drivetrainIncrease turn kP. Check wheel bearings and drive screws โ€” mechanical resistance stops turns early.
Heading drifts during straight driveHeading PID constants off, or physical drivetrain imbalanceTune pid_heading_constants_set. Check that all drive motors are the same cartridge. Ensure left/right motor counts match.
โฑ Understanding Drift

All gyroscopes drift โ€” the V5 IMU is no exception. In a 15-second auton, expect 0.5โ€“2ยฐ of total drift if well-calibrated. This is acceptable. If drift exceeds 3ยฐ across the full auton:

⚙ STEM Highlight Physics: Gyroscope Physics & Sensor Bias Correction
The IMU applies MEMS gyroscope technology: a microscopic vibrating mass that experiences Coriolis force proportional to angular velocity. Integration amplifies small measurement errors — a 0.1 degree/s bias accumulates to 6 degrees error per minute without correction. Calibration measures and cancels this bias. Placement near motors introduces electromagnetic interference that corrupts the magnetometer, causing systematic heading error.
🎤 Interview line: “We place our IMU away from motor controllers and test calibration accuracy quantitatively. After every significant code change, we run a 48-inch straight-line test and measure heading error. Our current IMU placement achieves under 0.5 degrees of drift over 48 inches. We have that data in our notebook because we treat sensor calibration as an engineering experiment.”
Your IMU is mounted next to a motor controller and heading drift worsens after calibration. What is the most likely cause?
⬛ The IMU needs a longer delay after power-on before calibrating
⬛ Electromagnetic interference from the motor controller is corrupting the magnetometer, causing systematic heading bias
⬛ The IMU library has a calibration bug in this PROS version
📝
Notebook entry tip: Build & Program — Orange slide — Document your IMU placement as a build entry: which port, where on the robot, and the reasoning (distance from motors, mounting plane, vibration isolation). Include calibration accuracy data: heading reported vs actual after a 48-inch straight drive, 5 trials. Moving the IMU between robot versions with documented before/after accuracy improvement is strong iterative build evidence.
← ALL GUIDES