medium Beta Tested end to end; we're still polishing it. line followinglocalizationodometry

Line Following with Localization

Follow a line with an e-puck while estimating its pose from wheel odometry.

Free. Runs in your browser; you'll be asked to sign in first.

The Line Following with Localization simulation in Webots

Line Following with Odometry

Overview

In this lesson, the robot follows a floor line while also estimating its pose from wheel encoder data. The robot must keep the line under control and, at the same time, maintain an estimate of its own position and heading.

The lesson combines reactive line following with encoder-based pose estimation.

Theoretical Background

Line Following

A simple line follower uses the ground sensors to decide whether the robot should move forward, turn left, or turn right.

Odometry Localization

Odometry estimates motion by integrating wheel rotations over time.

For a differential-drive robot, the wheel angular speeds are computed from the encoder increments:

\[\omega_l = \frac{\Delta \theta_l}{\Delta t}\]
\[\omega_r = \frac{\Delta \theta_r}{\Delta t}\]

The robot linear and angular speeds are then:

\[u = \frac{R}{2}(\omega_r + \omega_l)\]
\[\omega = \frac{R}{D}(\omega_r - \omega_l)\]

where \(R\) is the wheel radius and \(D\) is the distance between the wheels.

A simple pose update is:

\[\phi_{k+1} = \phi_k + \omega \Delta t\]
\[x_{k+1} = x_k + u\cos(\phi_{k+1})\Delta t\]
\[y_{k+1} = y_k + u\sin(\phi_{k+1})\Delta t\]

Odometry provides a running pose estimate without external position sensors, but it also accumulates drift over time.

Task Description

Objective

Implement four functions in line_following_with_localization.py:

  1. get_line_following_speeds(line_left, line_right, max_speed): the wheel speeds that keep the robot on the line (turn toward the side where only one sensor sees the line, otherwise drive straight).
  2. get_wheels_speed(encoder_values, old_encoder_values, dt): the wheel angular speeds from the encoder increments.
  3. get_robot_speeds(wl, wr, r, d): the robot's linear speed \(u\) and angular speed \(\omega\).
  4. get_robot_pose(u, w, x_old, y_old, phi_old, dt): the integrated pose, with the heading wrapped to \([-\pi, \pi)\).

The main loop calls them in this order and prints the estimated pose about twice a second.

System Configuration

  • Robot: e-puck differential-drive robot (wheel speeds up to about 6.28 rad/s)
  • Sensors: wheel encoders (get_wheel_encoder_positions(), rad) and three ground sensors, gs0 (left), gs1 (centre) and gs2 (right); a reading below 600 means the sensor sees the dark line
  • Actuators: left and right wheel motors
  • Parameters: wheel radius \(R = 0.020\) m, wheel separation \(D = 0.057\) m (the values in the code)
  • Initial pose: get_current_pose() gives the true starting pose once, to initialise the estimate
  • Environment: a closed oval of dark line (about 4.7 cm wide) on a bright floor; the robot starts on the line

Performance Metrics

A supervisor follows the robot's real position and compares it with the centre of the painted line. The run ends after one full lap, when the robot is more than 10 cm from the line for 5 s (line_lost), when it stops moving for 5 s (stopped), or after 90 s. It writes:

Metric Meaning
success one lap completed, on the line at least 90% of the time
ended_reason lap_completed, line_lost, stopped or time_limit
time_s simulated time of the run [s]
laps laps driven along the line
distance_m path length of the robot [m]
average_speed_m_s distance_m / time_s [m/s]
mean_line_error_m, max_line_error_m distance from the robot's centre to the line's centre [m]
on_line_ratio fraction of the time within 4 cm of the line's centre
score 0 to 100: 100 * min(1, laps) * on_line_ratio

The odometry is not graded: check it in the console. After one lap the robot is back near its starting point, so the printed pose should be close to the initial one; the difference is the accumulated drift.

Suggested Algorithm

At each control step:

  1. Read the ground sensors and choose wheel commands for line following.
  2. Apply the wheel commands.
  3. Read the wheel encoder positions.
  4. Compute the wheel angular speeds.
  5. Compute the robot linear and angular speeds.
  6. Integrate the pose estimate.
  7. Print or log the estimated pose.

Validation Checklist

  • Straight motion changes position mainly along the current heading.
  • Turning produces a nonzero angular speed estimate.
  • The heading stays bounded to a wrapped angular range.
  • The pose estimate updates once per control cycle.
  • The robot continues to follow the line while odometry runs in parallel.

References

Extension Ideas

  • Add the center ground sensor for more precise line tracking.
  • Plot the estimated pose over time to study odometry drift.
  • Add a correction source such as a compass, GPS, or landmark detector.