Using a camera with the Raspberry Pi
A camera turns a Raspberry Pi from a controller into a robot that can see. This unlocks line following, object detection and navigation, all from pixels.
Two kinds of camera
- Ribbon (CSI) cameras plug into the Pi's dedicated camera port and are handled by the modern
picamera2library. They are fast and low-latency. - USB cameras plug into a USB port and work with OpenCV's
VideoCapture. Convenient, slightly higher latency.
The core loop
Every vision program is a loop: grab a frame, process it, act.
frame = camera.capture_array() # get an image
# process: find the line, detect an object
# decide what the robot should doResolution is a trade-off
A bigger image has more detail but takes longer to process, which lowers your frame rate. Robots almost always shrink the image before processing (for example to a few hundred pixels wide). Lower resolution means a faster loop, and a fast loop lets the robot react in time. Detail you do not need is just slower.
Lighting is everything
Cameras are only as good as the light. Shadows and reflections ruin color detection. Serious robots add their own LED lighting and calibrate colors on the actual field, because the lighting at a competition is never the lighting in your lab.
From pixels to decisions
Once you have a frame you use image processing (OpenCV) or a neural network to find what matters: the line, a marker, a target. We cover the basics in the OpenCV lesson.