Line Following¶
This guide explains how to implement a line-following robot using the LineTracker sensor and
the cooperative Task pattern.
Hardware overview¶
The Raspbot V2 has a 4-channel IR reflective sensor array mounted at the front-bottom.
| Channel | Position |
|---|---|
x1 |
Left-most |
x2 |
Centre-left |
x3 |
Centre-right |
x4 |
Right-most |
A channel reads True when it detects a dark/black surface (the line).
A channel reads False when it detects a light surface (the floor).
The sensors produce a byte from register 0x0A.
The LineTracker.read() method returns a frozen LineState dataclass with named attributes.
Line-following logic¶
The classic proportional approach:
- If both centre sensors (
x2,x3) see the line: drive straight. - If the left sensor (
x1) sees the line but not the right (x4): steer left. - If the right sensor (
x4) sees the line but not the left (x1): steer right. - If no sensor sees the line: maintain last direction (or stop).
Minimal line-following example¶
Adding obstacle avoidance¶
Combine line following with the ultrasonic sensor:
Tuning tips¶
- Speed: Reduce speed if the robot overshoots turns. A good starting point is 100-150 for forward and 80-120 for turns.
- Control rate: 20-50 Hz (
rate=0.02torate=0.05) works well. Faster is better for tight curves. - Track width: The Raspbot V2 sensor spacing works best with a track line approximately 20-25 mm wide (standard black electrical tape).
x1/x4weight: If the robot oscillates, reduce turn speed or add a hysteresis condition (only turn if the outer sensor has been active for 2+ consecutive reads).
Reading raw sensor data¶
For debugging, print the LineState string directly:
The four binary digits correspond to x1 x2 x3 x4 from left to right.