Drake
Drake is an open-source C++ toolbox with extensive Python bindings for robot design, simulation, and analysis, emphasizing model-based design and formal verification. It provides tools for dynamics, control, planning, and visualization. As of version 1.52.0, Drake maintains a monthly release cycle, offering continuous improvements and new features.
Common errors
-
ModuleNotFoundError: No module named 'pydrake'
cause The `drake` package or its Python bindings (`pydrake`) are not installed or not accessible in the current Python environment.fixEnsure `drake` is installed in your active environment: `pip install drake`. If using a virtual environment, activate it first. -
ImportError: libdrake.so: cannot open shared object file: No such file or directory
cause The operating system cannot locate Drake's shared C++ libraries at runtime. This often happens if Drake was installed in a non-standard location, or environment variables like `LD_LIBRARY_PATH` (Linux) / `DYLD_LIBRARY_PATH` (macOS) are not correctly set.fixIf using a system-level installation, ensure your shell environment variables are correctly configured to point to Drake's `lib` directory (e.g., by sourcing the setup script). If using `pip install drake`, this error is less common but may indicate a corrupted installation or a missing system dependency. -
RuntimeError: There are no solvers available for the specified mathematical program. You must build/install at least one supported solver.
cause Your Drake installation does not have access to any configured mathematical programming solvers required by the optimization problem you are trying to solve. `pip install drake` does not include proprietary solvers.fixInstall and configure a compatible solver (e.g., IPOPT, SNOPT, Mosek, Gurobi) according to the Drake documentation (drake.mit.edu/solvers.html). This often involves a system-level installation or specific build flags for Drake. -
AttributeError: 'DiagramBuilder' object has no attribute 'AddLeafSystem'
cause You are attempting to use an outdated method `AddLeafSystem` on `DiagramBuilder`. The correct method for adding a system to the builder is `AddSystem`.fixReplace `builder.AddLeafSystem(...)` with `builder.AddSystem(...)`. -
RuntimeError: Failed to find Drake installation at conventional locations...
cause You are attempting to use a C++ utility (like `drake-visualizer` or `meshcat-server`) that is not included with `pip install drake`, or your system-level Drake installation path is not discoverable.fixThe `pip install drake` package does *not* include C++ tools like `drake-visualizer`. To use such tools, you must install Drake via its pre-compiled binaries or build from source, then ensure your environment variables are correctly sourced to make the installation discoverable.
Warnings
- breaking Drake 1.52.0 and newer officially requires Python >= 3.12. Users on older Python versions will encounter installation errors or runtime issues.
- gotcha The `pip install drake` package provides only a subset of Drake's functionality (Python bindings and core C++ libraries). It explicitly *does not* include visualization tools (like `drake-visualizer` or `meshcat-server`), C++ headers, or command-line utilities.
- breaking Drake's API undergoes frequent changes, especially between monthly releases, due to active development. Code written for older versions (e.g., a few months old) may encounter `AttributeError` or `TypeError` due to renamed classes, modified function signatures, or deprecated methods.
- gotcha Many advanced Drake functionalities, particularly those involving mathematical programming and optimization (e.g., `MathematicalProgram`, `DirectCollocation`), require specific third-party solvers (e.g., Mosek, Gurobi, SNOPT, IPOPT). These solvers often need to be installed and configured separately, as they are not bundled with Drake or `pip install`.
Install
-
pip install drake
Imports
- DiagramBuilder
from pydrake.systems.framework import DiagramBuilder
- Simulator
from pydrake.systems.framework import Simulator
- MultibodyPlant
from pydrake.multibody.plant import MultibodyPlant
- SceneGraph
from pydrake.geometry import SceneGraph
- AcrobotPlant
from pydrake.examples.acrobot import AcrobotPlant
- LogVectorOutput
from pydrake.systems.primitives import LogVectorOutput
- all
import drake
from pydrake.all import *
Quickstart
import numpy as np
from pydrake.systems.framework import DiagramBuilder, Simulator
from pydrake.examples.acrobot import AcrobotPlant
from pydrake.systems.primitives import LogVectorOutput
# Create a DiagramBuilder
builder = DiagramBuilder()
# Add the Acrobot plant (a classic underactuated system)
acrobot = builder.AddSystem(AcrobotPlant())
# Add a logger to record the state of the Acrobot
logger = LogVectorOutput(acrobot.get_output_port(0), builder)
# Build the diagram, connecting the components
diagram = builder.Build()
# Create a simulator for the diagram
simulator = Simulator(diagram)
context = simulator.get_mutable_context()
# Set initial state: (theta1, theta2, theta1_dot, theta2_dot)
# e.g., inverted upright with a small perturbation
context.SetContinuousState([np.pi/2, 0.1, 0.0, 0.0])
# Simulate for 2 seconds
simulator.AdvanceTo(2.0)
# Retrieve and print some logged data (optional verification)
log = logger.FindLog(context)
time = log.sample_times()
positions = log.data()[:2, :]
velocities = log.data()[2:, :]
print(f"Simulation finished. Logged {len(time)} time steps.")
print(f"Initial positions: {positions[:, 0]}, Final positions: {positions[:, -1]}")