Line Following with Ground Sensors
Follow a dark line with an e-puck's ground sensors and a simple state machine.
Free. Runs in your browser; you'll be asked to sign in first.
Finite-State Line Following
Overview
In this lesson, the robot must follow a dark line on a bright floor using a finite-state machine. The goal is to turn simple ground-sensor observations into stable motion decisions.
The lesson covers reactive control with discrete state transitions and wheel-speed commands.
Theoretical Background
Line-Following Problem
The robot must detect where the line appears under its body and adjust the two wheel speeds so the line stays centered.
The sensing situation is intentionally simple:
- Sensor input: left, center, and right ground sensors
- Control output: left and right wheel speeds
- Main difficulty: the sensors provide discrete line information rather than a continuous lateral error
Because of that, the controller does not directly know the exact distance from the line center. Instead, it infers whether the line is centered, shifted left, or shifted right.
Finite-State Machine Control
A finite-state machine maps sensor observations into motion states:
A simple version uses three states:
| State | Sensor interpretation | Wheel action |
|---|---|---|
| Forward | line centered | \(v_L = v_R\) |
| Turn left | line shifted left | \(v_R > v_L\) |
| Turn right | line shifted right | \(v_L > v_R\) |
A short turn counter helps reduce oscillation by keeping the robot in a turn state for a few control cycles before reconsidering the transition.
Task Description
Objective
Implement a finite-state controller in line_following_sm.py:
update_state(current_state, turn_counter, line_left, line_right): fromFORWARD, switch toTURN_LEFTorTURN_RIGHTwhen only one side sensor sees the line; in a turn state, count the control cycles and return toFORWARDafterCOUNTER_MAXcycles.get_wheel_speeds(state, max_speed): the wheel speeds of each state (equal inFORWARD, the outer wheel faster in a turn).
The main loop reads the sensors, calls both functions and sets the motors.
System Configuration
- Robot: e-puck differential-drive robot (wheel speeds up to about 6.28 rad/s)
- Sensors: three ground sensors under the front of the robot,
gs0(left),gs1(centre) andgs2(right). A reading below 600 means the sensor sees the dark line;get_left_ground_sensors_state()and its siblings returnTruethen. - Actuators: left and right wheel motors
- 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 |
Suggested Algorithm
At each control step:
- Read the left, center, and right ground sensors.
- Decide whether the line is centered, left, or right.
- Update the active state.
- Apply wheel commands for that state.
- Use a short turn counter if needed to avoid rapid left-right oscillation.
Validation Checklist
- The robot drives straight when the line is centered.
- The robot turns left when the line moves left.
- The robot turns right when the line moves right.
- The controller does not bounce between left and right every step.
- The robot can continue through several curves without losing the line.
References
Extension Ideas
- Add a stop state when the line is fully lost.
- Add a search state for line recovery.
- Compare the finite-state solution with a proportional line-following controller.