Skip to main content

What are Parts?

Parts are the building blocks of a Donkeycar vehicle. They represent modular, reusable components that handle specific functions like cameras, motors, sensors, and AI models. Parts follow a standardized interface that makes them easy to compose into a complete autonomous vehicle.

The Parts Interface

Every Donkeycar part implements a simple interface:

Basic Methods

  • run() - Called in the vehicle loop to execute the part’s logic synchronously
  • run_threaded() - Returns cached values from a background thread
  • update() - Runs continuously in a background thread to update cached values
  • shutdown() - Cleanup when the vehicle stops

Part Types

Synchronous Parts: Simple parts that execute in the main vehicle loop
Threaded Parts: Parts that run continuously in the background

Using Parts in Your Vehicle

Parts are added to the vehicle in manage.py:

The Parts Library

Donkeycar includes a comprehensive library of parts:

Input Parts

  • Cameras: Image capture from various cameras
  • Controllers: Joystick and web-based control
  • Sensors: IMU, GPS, encoders, ultrasonic

Output Parts

  • Actuators: Motor controllers and servos
  • Indicators: LEDs, displays, sound

Processing Parts

  • Models: Keras and PyTorch neural networks
  • Computer Vision: Image processing and transformation
  • Filters: Throttle filters, smoothing

Data Parts

  • Data Stores: Recording and playback (Tub format)
  • Telemetry: Logging and monitoring

Part Communication

Parts communicate through the vehicle’s memory:

Creating Custom Parts

See Custom Parts for a complete guide to building your own parts.

Part Lifecycle

  1. Initialization: Parts are created with configuration parameters
  2. Addition to Vehicle: Parts are added to the vehicle with inputs/outputs defined
  3. Start: Vehicle starts, threaded parts begin their update loops
  4. Run Loop: Vehicle loop calls run() or run_threaded() on each part
  5. Shutdown: Vehicle stops, parts clean up resources

Best Practices

  • Keep parts simple: Each part should have a single, well-defined responsibility
  • Use threading wisely: Only use threaded parts for I/O-bound operations
  • Handle errors gracefully: Parts should handle exceptions and provide useful error messages
  • Document your parts: Include docstrings with parameter descriptions and usage examples
  • Make parts configurable: Use constructor parameters for configuration rather than hardcoding values

Next Steps