ocs2_ros2¶
OCS2(Optimal Control for Switched Systems,切换系统最优控制)的 ROS 2 移植。
仓库: legubiao/ocs2_ros2 (branch: ros2)
用途¶
OCS2 是一个模型预测控制(MPC)库,提供:
实时轨迹优化
约束处理
支持多种机器人(机械臂、移动底盘、四足机器人)
安装¶
OCS2 不在 ROS apt 索引中。通过部署工作空间安装(或在源码 ↔ deb 之间切换):
cd ~/open-deploy-ws # or fa-deploy-ws
./init_repo.sh
# 1) init: choose d (deb) or s (source) for OCS2
# 2) 切换模块安装方式 — change an existing OCS2 path
# 3) ./scripts/install_core_debs.sh --only ocs2
# 4) ./scripts/uninstall_core_debs.sh --only ocs2
open-deploy-ws 默认 OCS2=d(经 scripts/install_core_debs.sh 的 GitHub Release .deb)。然后 colcon build。
手动回退
没有部署工作空间时:从 ocs2_ros2 Releases 下载 ros-jazzy-ocs2_*_<arch>.deb 并 sudo dpkg -i。源码则把 ros2 分支克隆进 src/,再 colcon build --packages-up-to ocs2。
使用方法¶
OCS2 通常通过更高层的控制器(如 ocs2_arm_controller)使用。直接使用方式:
在软件包中引入¶
find_package(ocs2_core REQUIRED)
find_package(ocs2_mpc REQUIRED)
find_package(ocs2_robotic_tools REQUIRED)
target_link_libraries(my_controller
ocs2_core::ocs2_core
ocs2_mpc::ocs2_mpc
)
示例¶
该仓库包含以下示例软件包:
示例 |
描述 |
|---|---|
|
简单一维系统 |
|
经典控制示例 |
|
移动机器人示例 |
|
机械臂 + 底盘 |
|
四足机器人 |
运行双积分器示例¶
ros2 launch ocs2_double_integrator double_integrator.launch.py
核心概念¶
MPC 问题¶
OCS2 求解最优控制问题:
状态:机器人构型(位置、速度)
输入:控制指令
代价:跟踪误差 + 控制输入
约束:关节限位、碰撞避免
DDP 求解器¶
使用微分动态规划(Differential Dynamic Programming):
前向传递:仿真轨迹
反向传递:计算最优增益
迭代直至收敛
实时运行¶
MPC 在独立线程中运行:
可配置的更新频率
始终可获取最新解
能够应对时序波动
配置¶
MPC 参数¶
mpc:
dt: 0.01 # MPC timestep
horizon: 1.0 # Prediction horizon (seconds)
iterations: 1 # DDP iterations per update
任务配置¶
task:
targetTrackingWeight: [100, 100, 100, 10, 10, 10] # Position, orientation
inputWeight: [1, 1, 1, 1, 1, 1] # Control effort