How mpc-berkeley/racing-lmpc-ros2 works
Project Type and Purpose
• This is a full-stack ROS2 application in C++ implementing Learning Model Predictive Control (LMPC) for autonomous racing.
• It comprises ROS2 packages for simulation, state estimation, various controllers (LMPC, MPC, LQR, vanilla), vehicle dynamics models, common message definitions, and utility tools.
• The system runs under ROS2 Humble, uses CasADi for optimization, and integrates with Foxglove Studio (or RViz2) for visualization.
File-Structure Analysis
• Top-level “src” contains multiple ROS2 packages, each with its own package.xml and CMakeLists.txt.
• src/common: Shared custom message definitions (lmpc_msgs and mpclab_msgs).
• src/vehicle_dynamics_models: Several vehicle model implementations (kinematic bicycle, single/double-track, etc.) and a factory to choose at runtime.
• src/state_estimators/ekf_state_estimator: Implements an EKF node.
• src/mpc: Core optimization packages: racing_lqr, racing_mpc.
• src/controllers: High-level controllers wrapping the MPC solvers into ROS2 nodes (e.g., racing_lmpc, vanilla_controller).
• src/simulation/racing_simulator: Simulator node publishing synthetic sensor data and accepting control inputs.
• src/tools: Helper libraries (transforms, profiling, logging, parameter helpers).
• src/launch/racing_lmpc_launch: ROS2 launch scripts tying together simulator, controller, and Foxglove Bridge.
• Configuration (param) and launch (launch/*.launch.py) subdirectories provide YAML parameter sets and launch descriptions.
README Insights
• The “Run LMPC” instructions show the runtime pipeline:
simulator node → controller node → Foxglove Bridge → visualization.
• Emphasizes CasADi dependency for on-the-fly optimization.
• Lists required ROS2 topics (telemetry, trajectory commands) and launch sequences.
Core Components
a. ROS2 Middleware (DDS) – backbone for pub/sub
b. Common Messages
• lmpc_msgs: telemetry ∔ trajectory_cmd
• mpclab_msgs: raw sensor ∔ actuation/status
c. Simulator Node
• Publishes simulated sensor data to topics under mpclab_msgs
• Subscribes to control commands (TrajectoryCommand.msg)
d. State Estimator Node (EKF)
• Subscribes: raw sensors → publishes estimated state
e. Vehicle Dynamics Model Library
• Underlies both simulator and controllers; loaded via a factory
f. Controller Nodes
• LMPC (racing_lmpc_node)
• Classic MPC (racing_mpc_node)
• LQR (racing_lqr library)
• Vanilla PID-style controller
• Each subscribes: estimated state + reference trajectory, publishes: control trajectory
g. MPC Solver Backend
• CasADi JIT + Ipopt for nonlinear optimization
h. Tools & Utilities
• Parameter loaders, logging, transform helpers
i. Launch Manager
• ROS2 launch files orchestrating simulator, controllers, Foxglove Bridge
j. Foxglove Bridge & Studio
• Bridge node converts ROS2 topics → WebSocket → Foxglove Studio UI
Relationships and Data Flow
• Simulator → publishes sensor topics → EKF → publishes /vehicle_state
• Reference trajectory (from simulator or prerecorded maps) → Controller
• Controller → computes optimal trajectory/actuation → publishes /trajectory_command
• Simulator subscribes /trajectory_command, advances simulation
• Foxglove Bridge subscribes key topics (telemetry, state, control), streams to UI
• All nodes communicate via ROS2 DDS (pub/sub)
Architectural Patterns & Design Principles
• Modular ROS2-package structure: each functional area in its own package
• Publisher‐Subscriber decoupling via common message definitions
• Parameterization through YAML + ROS2 parameters (loaded with ros_param_loader)
• Factory pattern for selecting vehicle models at runtime
• CI/CD via GitHub Actions, reproducible builds with colcon + CMake