Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
O

ocs2_legged_robot_notes

> 编程语言
Open source

- ocs2leggedrobotnotes - MIT 控制框架 - ETH 控制框架 - ROS 节点图 - Class List - Formulation - Implementation - API - Example - 代价 LeggedRobotStateInp…

193 stars0 likes1 views
WebsiteGitHub

About

- ocs2leggedrobotnotes - MIT 控制框架 - ETH 控制框架 - ROS 节点图 - Class List - Formulation - Implementation - API - Example - 代价 LeggedRobotStateInp…

  • ocs2_legged_robot_notes
    • MIT 控制框架
    • ETH 控制框架
      • ROS 节点图
      • Class List
    • Formulation
      • Implementation
        • API
        • Example
          • 代价 LeggedRobotStateInputQuadraticCost
          • 等式约束 ZeroForceConstraint
          • 动力学模型 LeggedRobotDynamicsAD
          • LeggedRobotPreComputation
          • [ReferenceManager 足式机器人的例子是一个开关系统问题。它实现了一个四足机器人Anymal运动控制的MPC方法。机器人的步态由用户定义,在执行过程中可以通过求解器的同步模块 (GaitReceiver) 进行修改。模式序列和目标轨迹是通过一个参考管理器模块 (SwitchedModelReferenceManager) 定义的。代价函数是一个二次罚函数,以跟踪机器人身体位置和偏航指令,并将机器人的重量平均分配到触地腿上。该问题有几个随模式变化的约束条件:摆动腿的地面作用力为零;触地腿的速度为零;摩擦锥约束;为避免足端和地面摩擦,摆动腿末端须跟踪一个预定的Z向运动。

系统动力学的建模有两种方式,可以从配置文件中选择。(1) 单一刚体动力学 (SRBD),这个模型假设系统具有恒定的惯性,而不管其关节位置如何,它也包括了系统的全部运动学;(2) 全中心动力学 (FCD)。这个模型使用中心动力学,它包括了机器人四肢的运动,与 SRBD 类似,它考虑了机器人的全部运动学。

子文件夹 功能
command 键盘控制
common 常用功能
constraint 约束类型
cost 代价类型
dynamics 动力学模型 (by Pinocchio)
foot_planner 摆动腿规划
gait 步态设置
initialization 初始化
synchronized_module 优化器参数同步
visualization 可视化

MIT 控制框架

ETH 控制框架

ROS 节点图

Class List

  • LeggedRobotModeSequenceKeyboard: 检测用户指令更新步态改变 Switched Systems 的模式序列 (ModeSequence)
  • EndEffectorLinearConstraint: 一阶足端位置/速度的线性约束
  • FrictionConeConstraint: 二阶触地足端摩擦锥不等式约束
  • NormalVelocityConstraintCppAd: 一阶摆动腿足端Z向速度(=摆动轨腿迹规划的Z向速度 LeggedRobotPreComputation::request)等式约束 (CppAd版)
  • ZeroForceConstraint: 一阶摆动腿足端零地面反作用力等式约束
  • ZeroVelocityConstraintCppAd: 一阶触地足端零速度等式约束 (CppAd版)
  • LeggedRobotStateInputQuadraticCost: 状态与输入的二次代价
  • LeggedRobotDynamicsAD: 动力学约束 (CppAd版)
  • CubicSpline: 三次样条曲线
  • SplineCpg: 摆动腿曲线 (CPG = Central Pattern Generator?)
  • SwingTrajectoryPlanner: 摆动腿Z向高度规划
  • GaitReceiver: 步态更新接收器 (A Solver synchronized module is updated once before and once after a problem is solved)
  • GaitSchedule: 步态定义类
  • LeggedRobotInitializer: 初始化类
  • SwitchedModelReferenceManager: Manages the ModeSchedule and the TargetTrajectories for switched systems
  • LeggedRobotVisualizer: 可视化类
  • LeggedRobotInterface: MPC 实现接口
  • LeggedRobotPreComputation: Request callback are called before getting the value or approximation

Formulation

X = [ linear_momentum / mass, angular_momentum / mass, base_position, base_orientation_zyx, joint_positions ]

U = [ contact_forces, contact_wrenches, joint_velocities ]

Implementation

API

  1. 定义 OptimalControlProblem
    • 代价
    • 软约束/硬约束
    • 动力学模型
    • PreComputation
  2. 定义 ReferenceManagerInterface
  3. 定义 SolverSynchronizedModule

Example

代价 LeggedRobotStateInputQuadraticCost
等式约束 ZeroForceConstraint
动力学模型 LeggedRobotDynamicsAD
LeggedRobotPreComputation
ReferenceManager Augmented Lagrangian |

| 不等式约束 | Relaxed barrier methods -> Augmented Lagrangian |

实现细节

核心 Class

  • MPC_BASE checkConvergence(unreliableControllerIncrement, performanceIndexHistory_.back(), performanceIndex_); unreliableControllerIncrement = false; } // end of while loop

// Description: Runs a single iteration of Gauss-Newton DDP void GaussNewtonDDP::runIteration(bool unreliableControllerIncrement) { // disable Eigen multi-threading Eigen::setNbThreads(1);

// finding the optimal stepLength searchStrategyTimer_.startTimer(); // the controller which is designed solely based on operation trajectories possibly has invalid feedforward. // Therefore the expected cost/merit (calculated by the Riccati solution) is not reliable as well. scalar_t expectedCost = unreliableControllerIncrement ? performanceIndex_.merit : sTrajectoryStock_[initActivePartition_].front(); runSearchStrategy(expectedCost); searchStrategyTimer_.endTimer();

// update the constraint penalty coefficients updateConstraintPenalties(performanceIndex_.stateEqConstraintISE, performanceIndex_.stateEqFinalConstraintSSE, performanceIndex_.stateInputEqConstraintISE);

// linearizing the dynamics and quadratizing the cost function along nominal trajectories linearQuadraticApproximationTimer_.startTimer(); approximateOptimalControlProblem(); linearQuadraticApproximationTimer_.endTimer();

// solve Riccati equations backwardPassTimer_.startTimer(); avgTimeStepBP_ = solveSequentialRiccatiEquations(heuristics_.dfdxx, heuristics_.dfdx, heuristics_.f); backwardPassTimer_.endTimer();

// calculate controller computeControllerTimer_.startTimer(); // cache controller cachedControllersStock_.swap(nominalControllersStock_); // update nominal controller calculateController(); computeControllerTimer_.endTimer();

// display if (ddpSettings_.displayInfo_) { printRolloutInfo(); }

// TODO(mspieler): this is not exception safe // restore default Eigen thread number Eigen::setNbThreads(0); }


## State-only Foot Placement Constraints [TODO]

> For each contact phase within the MPC horizon, the terrain segment is selected that is closest to the reference end-effector position determined by , evaluated at the middle of the stance phase.

### Illustration

```c++
void SwitchedModelReferenceManager::modifyReferences(
    scalar_t initTime, scalar_t finalTime, const vector_t& initState,
    TargetTrajectories& targetTrajectories, ModeSchedule& modeSchedule) {
  const auto timeHorizon = finalTime - initTime;
  modeSchedule = gaitSchedulePtr_->getModeSchedule(initTime - timeHorizon,
                                                   finalTime + timeHorizon);

  const scalar_t terrainHeight = 0.0;
  swingTrajectoryPtr_->update(modeSchedule, terrainHeight);
}

执行顺序

cpp
// SolverBase::run
void SolverBase::run(scalar_t initTime, const vector_t& initState, scalar_t finalTime, const scalar_array_t& partitioningTimes) {
  preRun(initTime, initState, finalTime);
  runImpl(initTime, initState, finalTime, partitioningTimes);
  postRun();
}
void SolverBase::run(scalar_t initTime, const vector_t& initState, scalar_t finalTime, const scalar_array_t& partitioningTimes,
                     const std::vector& controllersPtrStock) {
  preRun(initTime, initState, finalTime);
  runImpl(initTime, initState, finalTime, partitioningTimes, controllersPtrStock);
  postRun();
}

// SolverBase::preRun
void SolverBase::preRun(scalar_t initTime, const vector_t& initState, scalar_t finalTime) {
  referenceManagerPtr_->preSolverRun(initTime, finalTime, initState);

  for (auto& module : synchronizedModules_) {
    module->preSolverRun(initTime, finalTime, initState, *referenceManagerPtr_);
  }
}

// ReferenceManager::preSolverRun
void ReferenceManager::preSolverRun(scalar_t initTime, scalar_t finalTime, const vector_t& initState) {
  targetTrajectories_.updateFromBuffer();
  modeSchedule_.updateFromBuffer();
  modifyReferences(initTime, finalTime, initState, targetTrajectories_.get(), modeSchedule_.get());
}

构造 LQ 问题

cpp
void LinearQuadraticApproximator::approximateLQProblem(const scalar_t& time, const vector_t& state, const vector_t& input,
                                                       ModelData& modelData) const {
  constexpr auto request = Request::Cost + Request::SoftConstraint + Request::Constraint + Request::Dynamics + Request::Approximation;
  problemPtr_->preComputationPtr->request(request, time, state, input);

  // dynamics
  approximateDynamics(time, state, input, modelData);

  // constraints
  approximateConstraints(time, state, input, modelData);

  // cost
  approximateCost(time, state, input, modelData);
}

void LinearQuadraticApproximator::approximateDynamics(const scalar_t& time, const vector_t& state, const vector_t& input,
                                                      ModelData& modelData) const {
  // get results
  modelData.dynamics_ = problemPtr_->dynamicsPtr->linearApproximation(time, state, input, *problemPtr_->preComputationPtr);
  modelData.dynamicsCovariance_ = problemPtr_->dynamicsPtr->dynamicsCovariance(time, state, input);

  // checking the numerical stability
  if (checkNumericalCharacteristics_) {
    std::string err = modelData.checkDynamicsDerivativsProperties();
    if (!err.empty()) {
      std::cerr equalityConstraintPtr->getLinearApproximation(time, state, input, *problemPtr_->preComputationPtr);
  if (modelData.stateInputEqConstr_.f.rows() > input.rows()) {
    throw std::runtime_error("Number of active state-input equality constraints should be less-equal to the input dimension.");
  }

  // State-only equality constraint
  modelData.stateEqConstr_ = problemPtr_->stateEqualityConstraintPtr->getLinearApproximation(time, state, *problemPtr_->preComputationPtr);
  if (modelData.stateEqConstr_.f.rows() > input.rows()) {
    throw std::runtime_error("Number of active state-only equality constraints should be less-equal to the input dimension.");
  }

  // Inequality constraint
  modelData.ineqConstr_ =
      problemPtr_->inequalityConstraintPtr->getQuadraticApproximation(time, state, input, *problemPtr_->preComputationPtr);

  if (checkNumericalCharacteristics_) {
    std::string err = modelData.checkConstraintProperties();
    if (!err.empty()) {
      std::cerr getQuadraticApproximation(time, state, input, targetTrajectories, preComputation);

  if (!problem.softConstraintPtr->empty()) {
    cost += problem.softConstraintPtr->getQuadraticApproximation(time, state, input, targetTrajectories, preComputation);
  }

  // get the state only cost approximations
  if (!problem.stateCostPtr->empty()) {
    auto stateCost = problem.stateCostPtr->getQuadraticApproximation(time, state, targetTrajectories, preComputation);
    cost.f += stateCost.f;
    cost.dfdx += stateCost.dfdx;
    cost.dfdxx += stateCost.dfdxx;
  }

  if (!problem.stateSoftConstraintPtr->empty()) {
    auto stateCost = problem.stateSoftConstraintPtr->getQuadraticApproximation(time, state, targetTrajectories, preComputation);
    cost.f += stateCost.f;
    cost.dfdx += stateCost.dfdx;
    cost.dfdxx += stateCost.dfdxx;
  }

  return cost;
}

Note: OCS2 把软约束看作 Cost,需要用户提供二阶近似函数,还有一个地方需要提供二阶近似函数的是 inequalityConstraintPtr,即一般不等式约束;而状态方程和等式约束只需要提供一阶近似函数。

References

  • Tutorial on OCS2 Toolbox
  • Multi-Layered Safety for Legged Robots via Control Barrier Functions and Model Predictive Control
  • Fast nonlinear Model Predictive Control for unified trajectory optimization and tracking
  • 数值最优化方法
  • Numerical Optimization
  • Software Tools for Real-Time Optimal Control
    • TrajectoryOptimization.jl
    • OCS2
    • Crocoddyl
    • SCP Toolbox
  • Introduction to Control Lyapunov Functions and Control Barrier Functions
  • Iterative Linear Quadratic Regulator

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言