- Required Robot: Competition Robot (2025 Swerve Drive)
Field-oriented driving is one of the most important features of swerve drive systems. Unlike robot-oriented driving where the robot moves relative to its current orientation, field-oriented driving allows the robot to move relative to the field itself. This means pushing the joystick "forward" will always move the robot toward the opposite alliance wall, regardless of which direction the robot is facing. This creates an intuitive driving experience that allows drivers to focus on strategy rather than constantly adjusting for robot orientation.
Modify your existing swerve drivetrain to implement field-oriented driving using the robot's IMU (NavX gyroscope). The robot should move in the direction the driver pushes the joystick relative to the field coordinate system, not the robot's current heading. The driver should be able to toggle between robot-oriented and field-oriented modes using a controller button.
- Swerve Modules: 4 complete swerve modules (Drive Motors: CAN IDs 20, 30, 40, 50; Angle Motors: CAN IDs 21, 31, 41, 51)
- Encoders: Thrifty encoders (CAN IDs 0, 1, 2, 3) with configured offsets
- IMU: NavX (CAN ID: 0) for field-oriented calculations
- Controller: Xbox Controller (Port: 0)
- Ensure your swerve drivetrain subsystem from Challenge 4.7 is working with robot-oriented driving
- Add field-oriented capability to your drive method by using the IMU heading
- Modify the drive command to pass field-oriented control inputs
- Add a toggle button to switch between robot-oriented and field-oriented modes
- Implement IMU zeroing functionality for field calibration
- Add telemetry to display current drive mode and robot heading
- Test both drive modes thoroughly to ensure smooth transitions
- Robot moves relative to the field coordinate system in field-oriented mode
- Pushing joystick "forward" always moves robot toward opposite alliance wall regardless of robot orientation
- Robot moves relative to its own orientation in robot-oriented mode
- Driver can seamlessly toggle between modes using a controller button
- IMU heading resets properly when zeroing function is activated
- Telemetry clearly shows current drive mode and robot heading
- Smooth operation with no jerky movements during mode transitions
- For Step 1: Make sure your existing swerve drive from Challenge 4.7 is functioning properly. The
drive()method should accept translation X, translation Y, and rotation inputs. - For Step 2: In your drivetrain subsystem, modify the drive method to accept a field-oriented boolean parameter. Use
ChassisSpeeds.fromFieldRelativeSpeeds(xSpeed, ySpeed, rot, getRotation2d())instead of the robot-relative constructor when in field-oriented mode. - For Step 3: The NavX heading can be accessed through your IMU object. Make sure to return a
Rotation2dobject from yourgetRotation2d()method. Example:return Rotation2d.fromDegrees(-navx.getYaw());(note the negative sign for proper field orientation). - For Step 4: Add a boolean variable to track drive mode and use
driverController.getAButton()or similar to toggle. UseCommandScheduler.getInstance().schedule()to switch modes or implement as a toggle command. - For Step 5: Create a method in your drivetrain subsystem to zero the IMU:
public void zeroHeading() { navx.reset(); }. Bind this to a controller button like the start button. - For Step 6: In the subsystem's
periodic()method, useSmartDashboard.putString("Drive Mode", fieldOriented ? "Field" : "Robot");andSmartDashboard.putNumber("Robot Heading", getRotation2d().getDegrees()); - For Step 7: Test robot movement in all four cardinal directions in both modes. In field-oriented mode, the robot should always move in the same field direction regardless of its rotation.
- Remember that field-oriented driving requires the IMU to be properly calibrated and zeroed at the start of each match or test session.
- If the robot moves in unexpected directions, check the IMU mounting orientation and consider inverting the yaw value.
- The transition between modes should be instantaneous - avoid any ramping or smoothing that might cause delays.
- Heading Lock: Add a feature that locks the robot's heading when the rotation joystick is not being used, maintaining orientation even when translating.
- Precision Mode: Implement a button that reduces all movement speeds by 50% for precise positioning.
- Auto-Align: Add buttons that automatically rotate the robot to face 0°, 90°, 180°, or 270° field headings.
- Gyro Drift Compensation: Implement automatic gyro drift compensation by slowly adjusting the zero point during periods of no rotation input.
- Visual Indicators: Add LED indicators or dashboard graphics that show the current robot orientation and drive mode.
- WPILib Documentation: Swerve Drive Kinematics
- WPILib Documentation: Field-Oriented Drive
- WPILib Documentation: Using Gyroscopes
- NavX Documentation
- YAGSL (Yet Another Generic Swerve Library)
- Team 364 BaseFalconSwerve
- WPILib Example: Swerve Drive Bot
- Chief Delphi: Swerve Drive Programming
- FRC Team 95 Swerve Tutorial