Overview
The computer vision autopilot:- Uses OpenCV for image processing
- Detects colored lines or features in real-time
- Uses PID control to steer toward targets
- Requires no training data
- Works immediately after configuration
- Line following on marked tracks
- Quick prototyping without training
- Educational demonstrations
- Backup autopilot system
Line Follower
The LineFollower is the primary CV autopilot that follows colored lines using HSV color detection.How It Works
- Capture: Get camera image
- Slice: Extract horizontal slice at configured Y position
- Convert: Transform RGB to HSV color space
- Threshold: Apply color mask to find target color
- Detect: Calculate histogram to find line position
- Control: Use PID to steer toward target position
- Speed: Adjust throttle based on steering correction
Template Setup
The CV control template is available atdonkeycar/templates/cv_control.py:
Configuration
Configure CV parameters inmyconfig.py:
LineFollower Implementation
The LineFollower class processes images and generates control signals:Color Calibration
Find the correct HSV color range for your line: Using HSV color picker:- Test under actual lighting conditions
- HSV is more robust to lighting than RGB
- Increase S (saturation) min to ignore white/gray
- Adjust V (value) for brightness variations
PID Tuning
Tune PID parameters for smooth control: Tuning process:- Start with P only:
PID_P=0.01, PID_I=0, PID_D=0 - Increase P until oscillation starts
- Add D to dampen oscillations:
PID_D=0.001 - Optionally add I to eliminate steady-state error
- P (Proportional): Larger P = stronger correction, but can oscillate
- I (Integral): Eliminates steady-state error, but can cause overshoot
- D (Derivative): Dampens oscillations, smooths control
OpenCV Parts
Donkeycar includes many OpenCV-based image processing parts indonkeycar/parts/cv.py:
Color Space Conversion
Image Filtering
Edge Detection
Image Masking
Image Transformations
Custom CV Controller
Create your own CV-based autopilot:CV Pipeline Example
Chain multiple CV operations:Debugging CV
Display Overlay
The LineFollower includes an overlay display for debugging:Test CV Parts
Test individual CV operations:Advantages and Limitations
Advantages
- No training required: Works immediately after configuration
- Interpretable: Easy to understand and debug
- Fast: Real-time processing with low latency
- Predictable: Deterministic behavior
- Resource efficient: Runs on limited hardware
Limitations
- Requires marked track: Needs clear lines or features
- Sensitive to lighting: HSV helps but not perfect
- Limited generalization: Works only on similar conditions
- Manual tuning: Requires PID and color calibration
- Less robust: Can’t handle complex scenarios like DL
Best Practices
- Start simple: Use LineFollower before custom implementations
- Test color range: Calibrate under actual track lighting
- Tune PID carefully: Start with P only, add D for smoothing
- Use overlay: Enable OVERLAY_IMAGE for debugging
- Adjust scan region: Position SCAN_Y where line is clearest
- Set confidence threshold: Ignore weak detections
- Combine with DL: Use CV as backup or training aid
Next Steps
- Deep Learning Models - Train neural network autopilots
- Get Driving - Drive with CV autopilot
- Calibration - Tune your car for better control
