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 synchronouslyrun_threaded()- Returns cached values from a background threadupdate()- Runs continuously in a background thread to update cached valuesshutdown()- Cleanup when the vehicle stops
Part Types
Synchronous Parts: Simple parts that execute in the main vehicle loopUsing Parts in Your Vehicle
Parts are added to the vehicle inmanage.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
- Initialization: Parts are created with configuration parameters
- Addition to Vehicle: Parts are added to the vehicle with inputs/outputs defined
- Start: Vehicle starts, threaded parts begin their update loops
- Run Loop: Vehicle loop calls
run()orrun_threaded()on each part - 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
- Explore specific part categories:
- Cameras - Image capture
- Controllers - User input
- Actuators - Motors and servos
- Sensors - IMU, GPS, encoders
- Models - AI pilots
- Data Stores - Recording data
- Learn to create custom parts
