How qiayuanl/legged_control works
Project Type and Purpose
• This is a full-stack ROS-based control framework (“legged_control”) for quadrupedal robots implementing Nonlinear Model Predictive Control (NMPC), Whole-Body Control (WBC), and state estimation.
• It provides:
• Core control algorithms (NMPC via OCS2, WBC via QP)
• A state estimator (linear Kalman filter)
• ROS controller plugins and hardware interfaces for both Gazebo simulation and real Unitree hardware
• Example URDFs, launch files, and configuration for A1, AlienGo, GO1
File Structure Analysis
Top-level packages (all built with catkin in a workspace):
• legged_common: shared interfaces (joint, contact sensor) and URDF/URDF‐generation scripts
• legged_interface: formulates NMPC problem, constraints & cost definitions (wrapping OCS2)
• legged_wbc: hierarchical whole-body QP solver (defines tasks, priorities)
• legged_estimation: state estimation (Kalman filter)
• legged_controllers: ROS controller plugins (switchable NMPC+WBC controller), gait/trajectory publisher, safety checks, controller configurations
• legged_hw: abstract hardware interface base (LeggedHW)
• legged_gazebo: Gazebo hardware simulator plugin (LeggedHWSim)
• legged_examples:
• legged_unitree_description: URDF, meshes, xacro, launch files for simulation
• legged_unitree_hw: UnitreeHW implementation for real robot, SDK wrappers, config & launch
• qpoases_catkin: QPOASES solver for QP in catkin
a. Sensors & Inputs
• IMU, joint_state publishers (from real robot or Gazebo)
• User commands: geometry_msgs/Twist or move_base goals
b. State Estimator (legged_estimation)
• Subscribes to IMU & joint_states
• Publishes filtered base pose & velocity, joint states
c. Trajectory & Gait Publisher (legged_controllers/TargetTrajectoriesPublisher)
• Subscribes to user commands
• Publishes desired COM trajectory & foot trajectories + gait phase
d. NMPC Node (legged_interface + OCS2)
• Subscribes to state_estimate, desired trajectories
• Solves optimal control problem via OCS2 backend
• Publishes optimal state & contact force trajectories
e. WBC Node (legged_wbc)
• Subscribes to NMPC outputs & current state
• Solves hierarchical QP for joint accelerations & torques
• Publishes torque feedforward + PD setpoints
f. ros_control Controller Manager (legged_controllers plugins)
• Loads “legged_controller” plugin
• Forwards torque + PD commands to hardware interface
g. Hardware Interface Layer
• Simulation: legged_gazebo’s LeggedHWSim plugin
• Real robot: legged_unitree_hw’s UnitreeHW plugin
• Implements read() (sensor feedback) and write() (send torques/commands)
h. Dependencies
• OCS2 for NMPC
• QPOASES / HPIPM for QP solving
• pinocchio, hpp-fcl for kinematics/collision
Relationships & Data Flow
• Sensors → State Estimator → {NMPC, WBC}
• User Cmds → Trajectory Publisher → NMPC
• NMPC → WBC → ros_control → Hardware Interface → Robot/SIM
• Hardware Interface → Sensors → State Estimator (loop)
Architectural Patterns & Principles
• Modular ROS nodes with clear separation of concerns
• Catkin multi-package workspace: each major function in its own package
• Dependency inversion: abstract LeggedHW vs. concrete simulation/real plugins
• Hierarchical optimization: OCS2 for long horizon, WBC for instantaneous control
• Use of established frameworks (ros_control, OCS2, pinocchio)
Detail Level
• Both simulation and real-robot branches
• Loops (feedback loops from hardware to estimator to controller)