Differential Drive Odometry¶
A user can use the differential drive kinematics classes in order to perform odometry. WPILib contains a
DifferentialDriveOdometry class that can be used to track the position of a differential drive robot on the field.
Because this method only uses encoders and a gyro, the estimate of the robot’s position on the field will drift over time, especially as your robot comes into contact with other robots during gameplay. However, odometry is usually very accurate during the autonomous period.
Creating the Odometry Object¶
DifferentialDriveOdometry class requires one mandatory argument and one optional argument. The mandatory argument is the angle reported by your gyroscope (as a Rotation2d). The optional argument is the starting pose of your robot on the field (as a
Pose2d). By default, the robot will start at
x = 0, y = 0, theta = 0.
0 degrees / radians represents the robot angle when the robot is facing directly toward your opponent’s alliance station. As your robot turns to the left, your gyroscope angle should increase. By default, WPILib gyros exhibit the opposite behavior, so you should negate the gyro angle.
The encoder positions must be reset to zero before constructing the
Updating the Robot Pose¶
update method can be used to update the robot’s position on the field. This method must be called periodically, preferably in the
periodic() method of a Subsystem. The
update method returns the new updated pose of the robot. This method takes in the gyro angle of the robot, along with the left encoder distance and right encoder distance.
The encoder distances in Java must be in meters. In C++, the units library can be used to represent the distance using any linear distance unit. If the robot is moving forward in a straight line, both distances (left and right) must be positive.
Resetting the Robot Pose¶
The robot pose can be reset via the
resetPose method. This method accepts two arguments – the new field-relative pose and the current gyro angle.
If at any time, you decide to reset your gyroscope, the
resetPose method MUST be called with the new gyro angle. Furthermore, the encoders must also be reset to zero when resetting the pose.
In addition, the
GetPose (C++) /
getPoseMeters (Java) methods can be used to retrieve the current robot pose without an update.