OpenDuckMini强化学习框架入门教程-站立环境
纠错,疑问,交流: 请进入讨论区或 请点击进入页面,扫码加入微信群或Q群进行交流
获取最新文章: 扫一扫加入“创客智造”公众号
欢迎加入我们的openduckmini交流群,微信扫描右侧二维码立即进群交流
站立环境
- 理解机械鸭是如何实现站立和控制,包含类结构和方法,配置参数,奖励系统,命令采样,观测构建,训练方法等
概述
Standing 类(定义在 standing.py 中)与 Joystick 环境互补,专注于让 Open Duck Mini V2 机器人学会稳定站立。在此环境中,机器人不需要移动,而是保持身体姿态和平衡,同时可以控制头部姿态。
类结构
class Standing(open_duck_mini_v2_base.OpenDuckMiniV2Env):
"""Standing policy"""
def __init__(self, task="flat_terrain", config=default_config(), config_overrides=None):
super().__init__(xml_path=..., config=config, ...)
self._post_init()
与 Joystick 环境的关键区别
| 特性 | Joystick | Standing |
|---|---|---|
| 运动目标 | 速度跟踪 | 姿态保持 |
| 运动命令 | 随机采样 (lin_vel, ang_vel) | 始终为零 |
| 模仿奖励 | 可用 (USE_IMITATION_REWARD=True) |
禁用 (USE_IMITATION_REWARD=False) |
| 奖励重点 | 速度跟踪 + 能量效率 | 姿态保持 + 平衡 |
| 头部控制 | 与运动同时 | 独立控制 |
| 速度限制 | 启用 (USE_MOTOR_SPEED_LIMITS=True) |
未启用 |
配置参数
Standing 环境的配置更简单,因为不需要速度跟踪相关的参数:
def default_config() -> config_dict.ConfigDict:
return config_dict.create(
ctrl_dt=0.02,
sim_dt=0.002,
episode_length=1000,
action_repeat=1,
action_scale=0.25,
dof_vel_scale=0.05,
soft_joint_pos_limit_factor=0.95,
# ... 噪声配置同 Joystick ...
)
奖励权重配置:
| 奖励项 | 权重 | 说明 |
|---|---|---|
orientation |
-0.5 | 姿态偏离惩罚(保持直立) |
torques |
-1.0e-3 | 力矩惩罚(节省能量) |
action_rate |
-0.375 | 动作平滑性惩罚 |
stand_still |
-0.3 | 静止偏离默认姿态惩罚 |
alive |
+20.0 | 存活奖励 |
head_pos |
-2.0 | 头部位置跟踪惩罚 |
奖励系统
Standing 环境的奖励计算专注于保持平衡:
def _get_reward(self, data, action, info, ...):
return {
"orientation": cost_orientation(self.get_gravity(data)),
"torques": cost_torques(data.actuator_force),
"action_rate": cost_action_rate(action, info["last_act"]),
"alive": reward_alive(),
"stand_still": cost_stand_still(info["command"],
actuator_qpos, actuator_qvel,
default_actuator, ignore_head=True),
"head_pos": cost_head_pos(actuator_qpos, actuator_qvel,
info["command"]),
}
姿态奖励 (cost_orientation)
奖励机器人保持直立姿态。通过检查躯干 Z 轴在水平面上的分量实现:
def cost_orientation(torso_zaxis: jax.Array) -> jax.Array:
return sum(square(torso_zaxis[:2]))
当机器人完全直立时,Z 轴在水平面的投影为零,代价最小。
站立静止奖励 (cost_stand_still)
由于运动命令始终为零,cost_stand_still 持续生效,惩罚关节位置偏离默认姿态和关节速度。在 Standing 环境中,ignore_head=True,意味着头部关节的偏离不计入惩罚。
头部位置控制 (cost_head_pos)
Standing 环境支持通过命令控制头部姿态:
def cost_head_pos(joints_qpos, joints_qvel, cmd):
move_cmd_norm = norm(cmd[:3]) # 始终接近 0
head_cmd = cmd[3:] # 头部目标位置
head_pos = joints_qpos[5:9] # 实际头部位置
head_pos_error = sum(square(head_pos - head_cmd))
return head_pos_error * (move_cmd_norm > 0.01)
注意:仅当运动命令接近零时才计算头部位置代价——这正是 Standing 环境的情况。
命令采样
Standing 环境的命令采样只生成头部姿态命令:
def sample_command(self, rng):
# 运动命令始终为零
return jp.hstack([
0.0, 0.0, 0.0, # lin_vel_x, lin_vel_y, ang_vel_yaw = 0
neck_pitch, # 随机颈部姿态
head_pitch, # 随机头部俯仰
head_yaw, # 随机头部偏航
head_roll, # 随机头部滚动
])
头部姿态范围:
| 关节 | 范围 |
|---|---|
| neck_pitch | [-0.34, 1.1] rad |
| head_pitch | [-0.78, 0.78] rad |
| head_yaw | [-2.7, 2.7] rad |
| head_roll | [-0.5, 0.5] rad |
也有 10% 的概率全部置零。
观测构建
Standing 环境的观测与 Joystick 类似,但不包含 imitation_phase:
标准观测 (44 维):
noisy_gyro(3) + noisy_accelerometer(3) + command(3) +
(joint_angles - default)(10) + joint_vel * scale(10) +
last_act(10) + last_last_act(10) + last_last_last_act(10) +
contact(2) + ref_motion(0) # 模仿奖励已禁用,ref_motion 为空
特权观测 (额外维度):
标准观测 +
gyro_true(3) + accelerometer_true(3) + gravity(3) +
linvel(3) + global_angvel(3) +
joint_angles - default(10) + joint_vel(10) +
root_height(1) + actuator_force(10) + contact(2) +
feet_vel(12) + feet_air_time(2) + ref_motion(0)
训练方法
Standing 环境的训练命令:
uv run playground/open_duck_mini_v2/runner.py \
--env standing \
--task flat_terrain_backlash \
--num_timesteps 150000000
平衡策略的学习
训练过程中,策略需要学会:
- 对抗重力:持续输出适当的关节力矩,保持机器人直立
- 抵抗扰动:对随机推力做出反应,快速恢复平衡
- 默认姿态保持:回到并保持在预设的 "home" 姿态
- 头部姿态控制:在保持平衡的同时控制头部朝向
训练进度指标
| 指标 | 说明 |
|---|---|
cost/orientation |
姿态偏离程度(越低越好) |
cost/torques |
力矩消耗(越低越好) |
cost/action_rate |
动作变化率(越低越平滑) |
cost/stand_still |
姿态保持代价 |
cost/head_pos |
头部位置跟踪误差 |
reward/alive |
存活率 |
swing_peak |
足部摆动峰值高度 |
使用场景
Standing 环境适用于:
- 初始化策略:先训练站立作为基础能力
- 安全策略:跌倒恢复或紧急停止时使用
- 头部跟踪:需要稳定平台来控制摄像头朝向
- 多任务学习:作为多任务训练的一部分,与移动策略切换
纠错,疑问,交流: 请进入讨论区或 请点击进入页面,扫码加入微信群或Q群进行交流
获取最新文章: 扫一扫加入“创客智造”公众号
欢迎加入我们的openduckmini交流群,微信扫描右侧二维码立即进群交流


















