Skip to main content
The 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.
Attributes:
  • mem - The Memory instance used for data storage
  • parts - List of part entries in the vehicle pipeline
  • on - Boolean flag controlling the drive loop
  • threads - List of active threads for threaded parts
  • profiler - 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.
Example: Basic Part
Example: Part with Inputs and Outputs
Example: Conditional Execution
Example: Complete Pipeline from basic.py

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.
Returns:
int
Total number of loops executed
float
Total time elapsed in seconds
Example: Basic Usage
Example: Testing with Loop Count
Example: Verbose Mode for Debugging
Example: From complete.py Template

update_parts()

Executes one iteration of the drive loop. Called automatically by start(). This method:
  1. Loops through all parts in order
  2. Checks each part’s run condition
  3. Retrieves inputs from memory
  4. Calls the part’s run() or run_threaded() method
  5. Stores outputs back to memory
  6. Profiles execution time
Part Execution Flow:

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
Example: Custom Part with Shutdown

remove(part)

Removes a part from the vehicle pipeline.
object
required
The part instance to remove from the pipeline
Removing parts while the vehicle is running may cause unexpected behavior. It’s recommended to stop the vehicle before modifying the pipeline.

Performance Profiling

The Vehicle automatically tracks execution time for each part using a PartProfiler. Enable verbose mode to see performance statistics:
Profile reports show:
  • max - Longest execution time for the part
  • min - Shortest execution time
  • avg - Average execution time
  • 50%/90%/99%/99.9% - Percentile execution times
Use this to identify slow parts that may be causing the vehicle to miss its target loop rate.

Memory and Data Flow

The Vehicle uses a shared Memory instance for data flow:

See Also