Vehicle class is the heart of Donkeycar. It manages the main drive loop that runs all parts, handles data flow between parts through a shared memory system, and orchestrates the execution of your robotic vehicle.
Overview
The Vehicle creates a processing pipeline where each part runs sequentially in the order added. Parts communicate through a shared memory system, where outputs from one part can be inputs to another.Constructor
__init__(mem=None)
Creates a new Vehicle instance.
Memory
default:"None"
Optional Memory instance for storing part data. If not provided, a new Memory instance is created automatically.
mem- The Memory instance used for data storageparts- List of part entries in the vehicle pipelineon- Boolean flag controlling the drive loopthreads- List of active threads for threaded partsprofiler- PartProfiler instance for performance monitoring
Methods
add(part, inputs=[], outputs=[], threaded=False, run_condition=None)
Adds a part to the vehicle drive loop. Parts are executed in the order they are added.
object
required
A part instance with either a
run() or run_threaded() method. The part will be called on every loop iteration.list
default:"[]"
List of memory keys to retrieve and pass as arguments to the part’s run method. Values are retrieved from memory in the order specified.
list
default:"[]"
List of memory keys where the part’s return values will be stored. If the part returns a tuple, values are stored in order.
bool
default:"False"
If True, the part’s
update() method runs in a separate thread. The main loop calls run_threaded() to get the latest output. Use for I/O-bound operations like camera capture.str
default:"None"
Memory key that determines if this part should run. The part only executes when the value at this key is truthy.
start(rate_hz=10, max_loop_count=None, verbose=False)
Starts the vehicle’s main drive loop. This is a blocking call that runs until interrupted or max_loop_count is reached.
int
default:"10"
Target frequency for the drive loop in Hz. The actual frequency may be lower if parts take too long to process.
int
default:"None"
Maximum number of loop iterations. Used for testing. None means run indefinitely until interrupted.
bool
default:"False"
If True, prints debug information including timing violations and periodic performance reports every 200 loops.
int
Total number of loops executed
float
Total time elapsed in seconds
update_parts()
Executes one iteration of the drive loop. Called automatically by start(). This method:
- Loops through all parts in order
- Checks each part’s run condition
- Retrieves inputs from memory
- Calls the part’s
run()orrun_threaded()method - Stores outputs back to memory
- Profiles execution time
stop()
Stops the vehicle and shuts down all parts. Automatically called when the drive loop exits.
This method:
- Calls
shutdown()on each part (if the method exists) - Prints a performance profiler report
- Handles exceptions gracefully for parts without shutdown methods
remove(part)
Removes a part from the vehicle pipeline.
object
required
The part instance to remove from the pipeline
Performance Profiling
The Vehicle automatically tracks execution time for each part using aPartProfiler. Enable verbose mode to see performance statistics:
- max - Longest execution time for the part
- min - Shortest execution time
- avg - Average execution time
- 50%/90%/99%/99.9% - Percentile execution times
Memory and Data Flow
The Vehicle uses a shared Memory instance for data flow:See Also
- Memory - Shared memory system for data flow
- Config - Configuration management
- Creating Parts - How to create custom parts
- Templates - Example vehicle configurations
