Line Tracing¶
examples/line-tracing.ipynb drives the car along the yellow track line with no
machine learning at all: an HSV color filter finds the line's pixels, their centroid
gives the line's offset from image center, and steering is simply proportional to that
offset. Color segmentation, image moments, P-control — the classic recipe behind every
line follower, and the best first autonomous drive.
Inside the notebook¶
| Cell | What happens |
|---|---|
| 1. Setup | camera() and drive() helpers over the robot's Web API, plus one snapshot to check the view |
| 2. See the line | find_line_offset() — mask yellow, keep only the road ahead, collapse to one offset number; camera and mask shown side by side |
| 3. Drive | the live web view, then the driving loop: camera → offset → steer, ~15 times a second |
Run it¶
- Open
examples/line-tracing.ipynband pick the Python 3 (PhysiCar AI) kernel. - Run the cells top to bottom. The driving cell loops until you press ⏹ (interrupt) — the notebook then stops the car for you.
- To watch the car think, open
app.physicarand switch to the MYAPP tab: every pixel that passed the filter is repainted magenta — exactly what the code steers by — with the image center and the detected line drawn on top.
How it steers¶
find_line_offset() masks yellow with cv2.inRange(hsv, YELLOW_LO, YELLOW_HI), keeps
only the lower half of the image (the road just ahead), and collapses the centroid
(cv2.moments) into one number: the line's offset from center, normalized to [-1, 1].
Then:
If almost no yellow pixels are found, it searches: drive(0.3, 15) — creep forward
while turning left.
The code only talks to the robot's local Web API
(GET /camera, POST /speed, POST /steering), so the same notebook runs unchanged on
a real kit.
Make it yours¶
SPEED(0.5 m/s) — raise it and see where the corners break.STEER_GAIN(20.0) — too low drifts wide in corners, too high zigzags. Feel P-control.YELLOW_LO/YELLOW_HI— the HSV window. Retarget it to follow another color.- The mask's blanked upper half in
find_line_offset()— the region of interest. Try looking nearer or further.
Watch out
- The car needs a yellow line in view — if the HSV range doesn't match your track
(or, on a real kit, your room's lighting), nothing is detected. Tune
YELLOW_LO/HIfirst. - When the line is lost the car always turns left — lose it to the right and it can circle forever.
- The Web API's
/steeringtakes radians; this code thinks in degrees and converts insidedrive(). Don't mix the units when calling the API yourself. - If port 5000 is busy, the web view is skipped (with a notice) — driving still works.
🛠 Mission: make the search smart — remember which side the line was last seen on and turn that way instead of always left.
Learn more
Robot endpoints → Web API. Camera and sensors → Robot anatomy. Want the car to learn the track instead of following rules → Racing Deep Learning.