🦿 Legged Gym#

小技巧

模型和代码详见 MotrixSim Docs 仓库

在运行示例之前,请先参考 🛠️ 环境准备 完成环境准备。

我们在使用范例里,提供了一个简易的、类似 legged gym 的框架,方便用户将 legged gym 里训练的策略 sim2sim 到 MotrixSim 中进行测试。

关于 legged gym 训练框架,您可以点击 这里 了解更多信息。

在 MotrixSim 中附带了如下的 legged gym sim2sim 示例

介绍

命令

效果

Go1的控制移动

pdm run legged_gym/scripts/go1_play.py

Go1

T1的行走

pdm run legged_gym/scripts/T1_play.py

T1

Berkeley Humanoid Lite 行走

备注

行走策略来自 Berkeley-Humanoid-Lite 仓库。

pdm run legged_gym/scripts/BHL_play.py

BHL

双臂Berkeley Humanoid Lite 行走

备注

行走策略来自 Berkeley-Humanoid-Lite 仓库。

pdm run legged_gym/scripts/BHA_play.py

BHL

Custom Env#

我们提供的 legged gym sim2sim 框架,尽量在 Env 的设计上与 legged gym 保持一致,以减少您进行 sim2sim 时的理解成本。其目录结构如下:

  • legged_gym

    • envs: 自定的 Envs

    • policy: 策略文件

    • resources: mjcf 模型文件

    • scripts: play 脚本

    • utils: 工具函数

以 T1 的 sim2sim 为例,我们在 envs 下创建了一个名为 T1 的文件夹,里面包含了两个文件:T1.pyT1_config.py,分别继承自 legged_gym.envs.base.legged_robot.Legged_Robotlegged_gym.envs.base.legged_robot_config.LeggedRobotCfg 。 您可以重载 config 或者 env 来实现自定义的 observations 以及 actions 计算。

legged_gym/scripts/T1_play.py 中定义了 T1 的 play 脚本:

# Copyright (C) 2020-2025 Motphys Technology Co., Ltd. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# from legged_gym.envs.base.legged_robot import Legged_Robot
import numpy as np
import onnxruntime as ort

from legged_gym.addr import LEGGED_GYM_ENVS_DIR
from legged_gym.envs.T1.T1 import T1_env
from legged_gym.envs.T1.T1_config import T1Cfg
from legged_gym.utils import runner

policy_path = "/policy/booster_t1/booster_t1_10000.onnx"
env = T1_env(T1Cfg)
session = ort.InferenceSession(LEGGED_GYM_ENVS_DIR + policy_path, providers=["CPUExecutionProvider"])
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
actions = np.zeros(12)


def step():
    global actions
    env.step(actions)
    obs = env.get_observation()
    # print(obs)
    input_data = obs.reshape(1, 47).astype(np.float32)
    outputs = session.run([output_name], {input_name: input_data})
    # Read actions from output
    actions = outputs[0][0]


runner.loop(policy_step=step, render=env.render, policy_dt=env.config.sim.dt * env.config.control.decimation)