How legubiao/quadruped_ros2_control works
Purpose and Runtime
A ROS 2 Jazzy workspace for ros2_control-based quadruped locomotion across simulation and real Unitree hardware. It combines robot descriptions, ros2_control hardware adapters, command publishers, and interchangeable controllers: classical Unitree Guide, OCS2 MPC/WBC, RL policy, and leg PD. ROS 2 launch files are the operational entry points; build uses colcon/CMake.
System Entry Points
Typical startup loads a robot description plus hardware adapter and controller via:
• controllers/unitree_guide_controller/launch/gazebo.launch.py
• controllers/unitree_guide_controller/launch/mujoco.launch.py
• libraries/gz_quadruped_playground/launch/ocs2.launch.py
Inputs are separate ROS nodes, notably keyboard and joystick publishers:
• commands/keyboard_input/src/KeyboardInput.cpp
• commands/control_input_msgs/msg/Inputs.msg
• commands/joystick_input/launch/joystick.launch.py
Robot Model and ros2_control Boundary
Each supported robot is a package containing URDF/Xacro, transmissions, Gazebo integration, and controller parameters. The description is the contract binding controller joint/foot assumptions to simulation or hardware interfaces. Go2 is the primary documented deployment target:
• descriptions/unitree/go2_description/xacro/robot.xacro
• descriptions/unitree/go2_description/xacro/ros2_control.xacro
• descriptions/unitree/go2_description/config/robot_control.yaml
Equivalent model packages cover Unitree, Deep Robotics, Xiaomi/Magiclab, and ANYmal variants.
Hardware and Simulation Adapters
ros2_control controllers operate against hardware-system plugins rather than simulator- or device-specific APIs. Gazebo Harmonic uses a custom GZ system/plugin; real Unitree control uses Unitree SDK2 with its DDS/network stack:
• hardwares/gz_quadruped_hardware/src/gz_system.cpp
• hardwares/gz_quadruped_hardware/src/gz_quadruped_plugin.cpp
• hardwares/hardware_unitree_sdk2/src/HardwareUnitree.cpp
MuJoCo support is launched through controller packages and depends on the external unitree_mujoco environment. The README identifies an RMW compatibility boundary between CycloneDDS and unitree_sdk2.
Controller Families
The controller packages are alternative ros2_control execution cores, each translating state and command inputs into joint commands:
• Unitree Guide FSM locomotion: controllers/unitree_guide_controller/src/UnitreeGuideController.cpp, controllers/unitree_guide_controller/src/FSM/StateTrotting.cpp
• OCS2 MPC locomotion: controllers/ocs2_quadruped_controller/src/Ocs2QuadrupedController.cpp, controllers/ocs2_quadruped_controller/src/FSM/StateOCS2.cpp
• RL inference locomotion: controllers/rl_quadruped_controller/src/RlQuadrupedController.cpp, controllers/rl_quadruped_controller/src/FSM/StateRL.cpp
• Direct leg PD control: controllers/leg_pd_controller/src/LegPdController.cpp
Shared FSM interfaces and safe stand/passive states live in libraries/controller_common/include/controller_common/FSM/.
Locomotion Data Flow
Command nodes publish Inputs; selected controllers consume commands alongside ros2_control joint/IMU/contact state. The Unitree Guide path estimates state, selects FSM behavior, generates gait phase and foot targets, solves balance control, then emits leg commands. Its key stages are:
• controllers/unitree_guide_controller/src/control/Estimator.cpp
• controllers/unitree_guide_controller/src/gait/GaitGenerator.cpp
• controllers/unitree_guide_controller/src/control/BalanceCtrl.cpp
The RL path buffers filtered observations and evaluates packaged PyTorch policies associated with robot descriptions, e.g. descriptions/unitree/go2_description/config/legged_gym/policy.pt.
OCS2 Planning, Constraints, and Perception
OCS2 is the advanced planning/control pipeline: target and gait managers establish references; a legged model interface constructs MPC costs and constraints; WBC converts planned motion into executable joint-level commands. Safety and perceptive terrain extensions are explicit layers:
• controllers/ocs2_quadruped_controller/src/control/TargetManager.cpp
• controllers/ocs2_quadruped_controller/src/interface/LeggedInterface.cpp
• controllers/ocs2_quadruped_controller/src/wbc/HierarchicalWbc.cpp
Perceptive locomotion consumes planar terrain/elevation data, chooses convex foothold regions, and applies foot-placement/collision constraints. Gazebo playground provides worlds, camera/lidar models, and Cartographer or FAST-LIO launch/configuration:
• controllers/ocs2_quadruped_controller/src/perceptive/synchronize/PlanarTerrainReceiver.cpp
• libraries/gz_quadruped_playground/launch/slam/fast_lio.launch.py
• libraries/gz_quadruped_playground/worlds/warehouse.sdf
Main entry points: Inputs.msg Inputs · UnitreeGuideController.cpp Unitree Guide FSM · Ocs2QuadrupedController.cpp OCS2 MPC controller · RlQuadrupedController.cpp RL controller
Components 22
Launch & Inputs
- Gazebo launch · Starts the Guide controller simulation stack.
controllers/unitree_guide_controller/launch/gazebo.launch.py - MuJoCo launch · Starts controller packages against external unitree_mujoco.
controllers/unitree_guide_controller/launch/mujoco.launch.py - OCS2 launch · Starts the OCS2 playground stack.
libraries/gz_quadruped_playground/launch/ocs2.launch.py - Keyboard input
commands/keyboard_input/src/KeyboardInput.cpp - Joystick launch
commands/joystick_input/launch/joystick.launch.py - Inputs · Shared locomotion command message.
commands/control_input_msgs/msg/Inputs.msg
Robot Contract
- Go2 robot description · Primary robot model and controller joint contract.
descriptions/unitree/go2_description/xacro/robot.xacro - ros2_control contract · Declares the hardware-system interface for the model.
descriptions/unitree/go2_description/xacro/ros2_control.xacro - Controller parameters
descriptions/unitree/go2_description/config/robot_control.yaml
Controller Cores
- Unitree Guide FSM · Classical FSM locomotion execution core.
controllers/unitree_guide_controller/src/UnitreeGuideController.cpp - Guide estimation, gait & balance · Estimates state, generates foot targets, and computes balanced leg commands.
controllers/unitree_guide_controller/src/control/Estimator.cpp - OCS2 MPC controller · MPC/WBC locomotion execution core.
controllers/ocs2_quadruped_controller/src/Ocs2QuadrupedController.cpp - OCS2 planning & WBC · Builds references, constraints, MPC costs, and executable whole-body commands.
controllers/ocs2_quadruped_controller/src/control/TargetManager.cpp - RL controller · Runs filtered observations through a packaged policy.
controllers/rl_quadruped_controller/src/RlQuadrupedController.cpp - Go2 RL policy
descriptions/unitree/go2_description/config/legged_gym/policy.pt - Leg PD controller · Direct leg-level alternative controller.
controllers/leg_pd_controller/src/LegPdController.cpp
Execution Backends
- ros2_control manager · Routes controller state and joint commands through hardware-system plugins.
- Gazebo hardware system
hardwares/gz_quadruped_hardware/src/gz_system.cpp - Unitree SDK2 hardware · Connects real hardware through SDK2 DDS/network transport.
hardwares/hardware_unitree_sdk2/src/HardwareUnitree.cpp
Perception
- Planar terrain receiver · Supplies terrain/elevation data to perceptive locomotion.
controllers/ocs2_quadruped_controller/src/perceptive/synchronize/PlanarTerrainReceiver.cpp - FAST-LIO launch
libraries/gz_quadruped_playground/launch/slam/fast_lio.launch.py - Warehouse world
libraries/gz_quadruped_playground/worlds/warehouse.sdf
Connections 25
- Keyboard input → Inputs: publishes
- Joystick launch → Inputs: publishes
- Gazebo launch → Go2 robot description: loads
- Gazebo launch → Unitree Guide FSM: starts
- MuJoCo launch → Unitree Guide FSM: starts
- OCS2 launch → OCS2 MPC controller: starts
- OCS2 launch → FAST-LIO launch: optionally starts
- Go2 robot description → ros2_control contract: includes
- Controller parameters → ros2_control manager: configures
- ros2_control contract → ros2_control manager: declares interfaces
- ros2_control manager → Gazebo hardware system: uses
- ros2_control manager → Unitree SDK2 hardware: uses
- ros2_control manager → Unitree Guide FSM: state / commands
- ros2_control manager → OCS2 MPC controller: state / commands
- ros2_control manager → RL controller: state / commands
- ros2_control manager → Leg PD controller: state / commands
- Inputs → Unitree Guide FSM: commands
- Inputs → OCS2 MPC controller: commands
- Inputs → RL controller: commands
- Unitree Guide FSM → Guide estimation, gait & balance: executes
- OCS2 MPC controller → OCS2 planning & WBC: executes
- Planar terrain receiver → OCS2 planning & WBC: terrain constraints
- FAST-LIO launch → Planar terrain receiver: terrain mapping
- Warehouse world → FAST-LIO launch: simulated environment
- RL controller → Go2 RL policy: evaluates