← Back to Course

Odometry

Adding a coordinate system to your robot.

Chapter 8 of 11

73%

What is Odometry?

Odometry is a method used in robotics to estimate the position and orientation of a robot over time using data from motion sensors.

Odometry works by integrating the robot's velocity over time to estimate its change in position. This is typically done using encoders attached to the robot's wheels. The main components of odometry are:

  • Motor Encoders / Tracking Wheel: Measure the rotation of the wheels to calculate distance traveled.
  • Inertial Sensor: Measures the robot's orientation or angle.

To implement odometry, you can follow these general steps:

  1. Initialize All Sensors: Set up the sensors and initialize their values.
  2. Calculate Distance and Angle: Use the sensor data to calculate the distance traveled and change in orientation.
  3. Update Position: Use the distance and angle to update the robot's position on the field.
cpp
double x = 0.0;
double y = 0.0;
void update_odometry() {
    double theta = inertial_sensor.get_heading();

    double distance = (left_distance + right_distance) / 2.0; // You will need to convert encoder values to distance
    
    // Make sure sensor values don't read NaN
    if (!isnan(distance) && !isnan(theta)) {
        x += distance * cos(theta);
        y += distance * sin(theta);
    }

    // Print position for verification
    std::cout << "X: " << x << " Y: " << y << " Theta: " << theta << std::endl; // To print to the brain you will need to use pros::lcd
}

We won't be implementing our own complete odometry system ourselves in this tutorial because it is quite difficult, but you can find many resources online to help with that. We will show you libraries that with Odometry that you can install in the next chapter.

Common Challenges with Odometry

Odometry can be affected by various factors, leading to inaccuracies. Common challenges include:

  • Wheel Slippage: Causes incorrect distance measurements.
  • Sensor Drift: Inertial sensor values can drift over time, affecting orientation accuracy.
  • Uneven Terrain: Changes in surface can affect wheel encoders.

Why is it important to reset the encoders and inertial sensors before starting odometry calculations?