Skip to main content
Donkeycar supports computer vision (CV) based autopilot as an alternative to deep learning. This approach uses classical computer vision techniques like color detection, edge detection, and PID control to follow lines or paths.

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
Use cases:
  • 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

  1. Capture: Get camera image
  2. Slice: Extract horizontal slice at configured Y position
  3. Convert: Transform RGB to HSV color space
  4. Threshold: Apply color mask to find target color
  5. Detect: Calculate histogram to find line position
  6. Control: Use PID to steer toward target position
  7. Speed: Adjust throttle based on steering correction

Template Setup

The CV control template is available at donkeycar/templates/cv_control.py:
This creates a car configured for computer vision autopilot.

Configuration

Configure CV parameters in myconfig.py:

LineFollower Implementation

The LineFollower class processes images and generates control signals:
Key methods:

Color Calibration

Find the correct HSV color range for your line: Using HSV color picker:
Common HSV ranges:
Tips:
  • 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:
  1. Start with P only: PID_P=0.01, PID_I=0, PID_D=0
  2. Increase P until oscillation starts
  3. Add D to dampen oscillations: PID_D=0.001
  4. Optionally add I to eliminate steady-state error
Live tuning with buttons:
PID effect:
  • 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 in donkeycar/parts/cv.py:

Color Space Conversion

Image Filtering

Edge Detection

Image Masking

Image Transformations

Custom CV Controller

Create your own CV-based autopilot:
Register in myconfig.py:

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

  1. Start simple: Use LineFollower before custom implementations
  2. Test color range: Calibrate under actual track lighting
  3. Tune PID carefully: Start with P only, add D for smoothing
  4. Use overlay: Enable OVERLAY_IMAGE for debugging
  5. Adjust scan region: Position SCAN_Y where line is clearest
  6. Set confidence threshold: Ignore weak detections
  7. Combine with DL: Use CV as backup or training aid

Next Steps