Objects and Odometry
In this module we will cover the basics of Object Oriented Programming: classes, objects and static methods. We will then use WPILib classes for controlling a differential drive robot, including kinematics, a NavX gyro for the heading, and a pose estimator that tracks where the robot is. Finally, we will compare that estimate with the simulator's true position.
Watch wheel odometry drift as you drive, and see how vision corrects it. Open the Pose Estimator Lab in a new tab ↗
Presentation
The exercises below are the Objects and Odometry lesson in Scriptum. Open the lesson, start the simulated robot and follow its README. Each step is checked with the Verify button.
New Java in this lesson: classes and objects, new, import, static and abstract.
Exercises
Classes
- Finish an abstract OI class
- Add a static getter for forward (the left stick Y axis) with a deadband
- Add a static getter for turn (the left stick X axis) with a deadband
- Update your code to use the OI class methods and remove the XboxController from Robot.java
WPILib
- Use a DifferentialDriveKinematics object in the drive mechanism
- Write a getChassisSpeeds method
- Use it to get the ChassisSpeeds of the robot and compare it with your hand-written speeds
- Write a getWheelYaw method that works out the robot's heading from how far each side has driven
- The heading in radians is (right meters - left meters) / track width (0.61 m)
- Return it as a Rotation2d
- Write a getYaw method that reads the heading from the robot's NavX gyro
- The NavX is plugged into the roboRIO's top (MXP) port, and the drive mechanism already reads it into
inputs_.gyroYawRad - If the gyro is connected, return its heading as a Rotation2d; if not, fall back to
backupYaw(), which carries on from the gyro's last heading using the wheels
- The NavX is plugged into the roboRIO's top (MXP) port, and the drive mechanism already reads it into
- Use a DifferentialDrivePoseEstimator object
- Update it every loop and log the estimated pose
- Use the x value from the estimated pose to drive 3 meters in autonomous
The NavX IMU
- The NavX is an IMU (Inertial Measurement Unit): a gyroscope, an accelerometer and a magnetometer on one board
- It reports yaw (turning left and right, our heading), pitch (nose up or down) and roll (tipping side to side)
- Using it on a real robot
- Keep the robot still while it boots: the NavX calibrates at power-on
- Mount it flat and solid, near the middle of the robot
- The NavX's getYaw() wraps at -180 to 180 degrees but counts clockwise; flip it with a minus sign so counterclockwise is positive, like WPILib
Simulation
- Drive the robot in simulation and watch the pose on the AdvantageScope 2D field
- Compare your estimated pose with the simulator's true pose
- Why do they drift apart? What would a real robot add to fix it?
- Graph Drive/GyroYawRad and Drive/WheelYawRad together and drive in circles
- Which one wanders off, and why does getYaw use the other?
- Optional: try WPILib's DifferentialDrive
arcadeDriveIKmethod and compare it with your own arcade math
Related Documentation
Software
- Classes and Objects: Overview of classes and objects in Java.
- WPILib: The main library used for programming FRC robots.
- Kinematics and the ChassisSpeeds Class: Overview of kinematics and how to use the ChassisSpeeds class in WPILib.
Software Tools
-
VSCode: Our primary Integrated Development Environment (IDE) for programming.
-
Driver Station: Software used to control and monitor the robot during competitions.
-
Advantage Scope: Tool for analyzing robot performance data.
-
Elastic: Tool for logging and visualizing robot data.