Field-Centric Driving
Rotate driver input using IMU heading.
Rotate driver input using IMU heading.
In this lesson, you will:
Field-centric drive changes the driver’s frame of reference. Instead of forward meaning the robot’s front, forward means the chosen field direction after heading reset.
The code rotates the joystick vector by negative robot heading before feeding it into mecanum math. The driver experiences a robot that moves relative to the field even when it is turned around.
Choose a field zero, reset yaw, read heading in radians, rotate x/y, then use the normal mecanum formula. Test at 0, 90, 180, and 270 degrees.
FieldCentric.javaJava
if (gamepad1.options) imu.resetYaw(); double heading = imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.RADIANS); double rotX = x * Math.cos(-heading) - y * Math.sin(-heading); double rotY = x * Math.sin(-heading) + y * Math.cos(-heading);
If controls feel rotated, check reset procedure, heading units, heading sign, and hub orientation. Do not tune strafe constants to fix a coordinate-frame bug.
Implement a heading reset button and run the same forward-stick test at four robot orientations.
Check your understanding before moving on.
0 of 2 answered