genesis-world
下载 genesis-world-main.zip 解压缩到 D:\AI
pypi: genesis-world
pip install torch torchvision torchaudio
pip install genesis-world
genesis_world-1.4.5-py3-none-any.whl 83.6MB

运行 cmd
cd D:\AI\genesis-world-main
python examples/rigid/single_franka.py -v

RL algorithms = Reinforcement Learning algorithms,强化学习算法

一句话:
强化学习(RL)是机器学习三大分支之一(另外两个:监督学习、无监督学习);RL algorithms 就是用来实现强化学习的各类算法。

核心思想:智能体(agent)在环境里不断试错,通过拿到的奖励(reward)去学习一套动作策略,让长期累积奖励最大化。

典型用途

机器人控制、游戏 AI(AlphaGo、OpenAI Five)、自动驾驶、推荐系统、资源调度、大模型里的 RLHF(基于人类反馈的强化学习)。

说明:下面给两套脚本:

  1. 简易开环三角步态(直接跑,不用RL预训练权重,Unitree Go2),适合快速看行走效果;
  2. 官方原生RL训练/评估(go2_train.py / go2_eval.py,和仓库原版对齐),适合做PPO训练步态

前置安装

# 先装pytorch,再装genesis-world
pip install torch torchvision 
pip install genesis-world 
# 如果要跑RL版本:
pip install rsl-rl-lib tensorboard

注意:genesis-world 要求 Python >=3.10


脚本1:简易开环步态示例(go2_openloop_walk.py)

原理:Trot 对角步态,正弦轨迹生成关节目标角度,PD控制,不需要训练权重,开箱可视化。

import genesis as gs
import torch
import math

def main():
    gs.init(backend=gs.cpu) # 换成 gs.gpu 如果有cuda

    scene = gs.Scene(
        sim_options=gs.options.SimOptions(dt=0.02, substeps=2),
        viewer_options=gs.options.ViewerOptions(
            camera_pos=(3, 0, 2),
            camera_lookat=(0,0,0.5),
            camera_fov=40,
        ),
        show_viewer=True,
    )

    # 地面
    plane = scene.add_entity(gs.morphs.Plane())
    # 加载Go2 URDF,genesis内置asset
    robot = scene.add_entity(
        gs.morphs.URDF(
            file="urdf/go2/urdf/go2.urdf",
            pos=(0, 0, 0.4),
            quat=(1, 0, 0, 0),
            fixed=False,
        )
    )

    scene.build()

    # 关节名称顺序对应Go2
    joint_names = [
        "FR_hip_joint", "FR_thigh_joint", "FR_calf_joint",
        "FL_hip_joint", "FL_thigh_joint", "FL_calf_joint",
        "RR_hip_joint", "RR_thigh_joint", "RR_calf_joint",
        "RL_hip_joint", "RL_thigh_joint", "RL_calf_joint",
    ]
    # 官方标准方式获取dof索引,替换掉不存在的 get_dofs_index
    dofs_idx = [robot.get_joint(name).dofs_idx_local[0] for name in joint_names]
    n_dof = len(dofs_idx)

    # PD gains
    kp = torch.tensor([40.0]*n_dof, device=scene.device)
    kd = torch.tensor([2.0]*n_dof, device=scene.device)

    t = 0.0
    gait_freq = 2.0  # 步态频率 Hz
    step_len = 0.12
    height = 0.08

    while True:
        # Trot对角步态:FR & RL 一组,FL & RR 一组,相位差pi
        phase1 = 2 * math.pi * gait_freq * t
        phase2 = phase1 + math.pi

        def leg_traj(phase):
            swing = max(0, math.sin(phase))
            hip = 0.15
            thigh = -0.4 + swing * step_len
            calf = 0.8 - swing * height
            return [hip, thigh, calf]

        q_target = []
        q_target.extend(leg_traj(phase1))   # FR
        q_target.extend(leg_traj(phase2))   # FL
        q_target.extend(leg_traj(phase2))   # RR
        q_target.extend(leg_traj(phase1))   # RL

        q_target = torch.tensor(q_target, device=scene.device, dtype=torch.float32)

        robot.set_dofs_target(
            dofs_idx=dofs_idx,
            target_pos=q_target,
            kp=kp,
            kd=kd,
        )

        scene.step()
        t += 0.02

if __name__ == "__main__":
    main()

运行:

python go2_openloop_walk.py

缺点:开环步态很容易摔倒,仅用于理解关节控制;稳定行走需要RL策略(下面官方版本)


脚本2:官方RL版本(Go2 PPO行走,仓库原版结构)

分成两个文件:go2_env.py(环境) + go2_train.py(训练),训练完用eval跑

运行 cmd
cd D:\AI\genesis-world-main
python examples/locomotion/go2_env.py
python examples/locomotion/go2_backflip.py # 后空翻
python examples/locomotion/go2_train.py --max-iterations 11
python examples/locomotion/go2_eval.py -e go2-walking --ckpt 10

go2_env.py

import torch
import genesis as gs

class Go2Env:
    def __init__(self, num_envs=1, device="cpu"):
        self.num_envs = num_envs
        self.device = device
        self.dt = 0.02
        self.substeps = 2

        self.scene = gs.Scene(
            sim_options=gs.options.SimOptions(dt=self.dt, substeps=self.substeps),
            rigid_options=gs.options.RigidOptions(enable_self_collision=False),
            viewer_options=gs.options.ViewerOptions(),
            show_viewer=(num_envs == 1),
        )
        self.scene.add_entity(gs.morphs.Plane())
        self.robot = self.scene.add_entity(
            gs.morphs.URDF(file="urdf/go2/urdf/go2.urdf", pos=(0,0,0.4))
        )
        self.scene.build(n_envs=num_envs)

        self.joint_names = [
            "FR_hip_joint", "FR_thigh_joint", "FR_calf_joint",
            "FL_hip_joint", "FL_thigh_joint", "FL_calf_joint",
            "RR_hip_joint", "RR_thigh_joint", "RR_calf_joint",
            "RL_hip_joint", "RL_thigh_joint", "RL_calf_joint",
        ]
        # 官方标准方式获取dof索引,替换掉不存在的 get_dofs_index
        self.dofs_idx = [self.robot.get_joint(name).dofs_idx_local[0] for name in self.joint_names]
        self.n_dof = len(self.dofs_idx)

        self.default_joint_pos = torch.tensor([
            0.1, 0.8, -1.5,
            -0.1, 0.8, -1.5,
            0.1, 0.8, -1.5,
            -0.1, 0.8, -1.5,
        ], device=device)

        self.kp = torch.full((num_envs, self.n_dof), 40.0, device=device)
        self.kd = torch.full((num_envs, self.n_dof), 2.0, device=device)

        self.commands = torch.zeros((num_envs,3), device=device) # vx, vy, yaw
        self.obs_buf = torch.zeros((num_envs, 48), device=device)
        self.reset_buf = torch.ones(num_envs, device=device, dtype=torch.bool)

    def reset_idx(self, env_ids):
        self.robot.set_dofs_pos(
            self.dofs_idx,
            self.default_joint_pos[None, :].repeat(len(env_ids),1),
            env_indices=env_ids
        )
        self.robot.set_dofs_vel(self.dofs_idx, torch.zeros(len(env_ids), self.n_dof, device=self.device), env_indices=env_ids)
        self.commands[env_ids, 0] = 0.6  # 前向速度指令
        self.reset_buf[env_ids] = False

    def step(self, actions):
        actions = torch.clip(actions, -1, 1) * 0.25
        target_pos = self.default_joint_pos[None, :] + actions
        self.robot.set_dofs_target(
            self.dofs_idx, target_pos,
            kp=self.kp, kd=self.kd
        )
        self.scene.step()

        base_pos = self.robot.get_base_pos()
        base_lin_vel = self.robot.get_base_lin_vel()
        base_ang_vel = self.robot.get_base_ang_vel()
        dof_pos = self.robot.get_dofs_pos(self.dofs_idx)
        dof_vel = self.robot.get_dofs_vel(self.dofs_idx)

        self.obs_buf = torch.cat([
            base_lin_vel, base_ang_vel, self.commands,
            dof_pos - self.default_joint_pos, dof_vel
        ], dim=-1)

        reward = self._compute_reward(base_lin_vel)
        termination = (base_pos[:,2] < 0.25)
        self.reset_buf |= termination
        self.reset_idx(torch.where(self.reset_buf)[0])
        return self.obs_buf, reward, self.reset_buf, {}

    def _compute_reward(self, base_lin_vel):
        err = torch.square(self.commands[:,0] - base_lin_vel[:,0])
        r = torch.exp(-err / 0.25)
        return r

go2_train.py

import torch
from rsl_rl.runners import OnPolicyRunner
from go2_env import Go2Env

def main():
    device = "cpu"
    env = Go2Env(num_envs=4096, device=device)
    cfg = {
        "policy": {"init_noise_std": 1.0, "hidden_dims": [512,256,128]},
        "runner": {"max_iterations": 1000},
    }
    runner = OnPolicyRunner(env, cfg, log_dir="logs/go2_walk", device=device)
    runner.learn()

if __name__ == "__main__":
    main()

训练:

python go2_train.py

训练后加载权重eval(你可以在eval脚本里打开viewer,单env看行走)

官方原版仓库:https://github.com/Genesis-Embodied-AI/genesis-world/tree/main/examples/locomotion


常见问题

  1. 找不到 urdf/go2/urdf/go2.urdf:genesis内置asset,新版会自动拉取;如果缺失,从官方仓库assets目录复制
  2. 开环步态容易倒:属于正常现象,开环没有状态反馈;稳定必须RL
  3. 切换GPU:gs.init(backend=gs.gpu)

如果你想要,可以再给你一个训练完成后,加载ckpt单独可视化行走的eval脚本。

下面是配套的 eval 可视化脚本,加载训练好的 ckpt,单环境、打开 viewer,直接看机器狗行走。

注意:和上面 go2_env.py 保持在同一个目录,权重路径改成你训练输出的路径。

# go2_eval.py
import torch
import genesis as gs
from rsl_rl.runners import OnPolicyRunner
from go2_env import Go2Env

def main():
    # -------------------------- 配置区 --------------------------
    ckpt_path = "logs/go2_walk/model_1000.pt"  # 修改成你的ckpt路径
    device = "cpu"  # 有cuda就改成 "cuda:0"
    # 指令:vx, vy, yaw(前进速度、横向速度、转向角速度)
    cmd_vx = 0.6
    cmd_vy = 0.0
    cmd_yaw = 0.0
    # -----------------------------------------------------------

    gs.init(backend=gs.cpu if device=="cpu" else gs.gpu)

    # 评估只用 1 个环境,开启可视化
    env = Go2Env(num_envs=1, device=device)
    # 覆盖指令,让机器狗往前走
    env.commands[0, 0] = cmd_vx
    env.commands[0, 1] = cmd_vy
    env.commands[0, 2] = cmd_yaw

    # 加载runner和policy
    cfg = {
        "policy": {"init_noise_std": 1.0, "hidden_dims": [512,256,128]},
        "runner": {"max_iterations": 1000},
    }
    runner = OnPolicyRunner(env, cfg, log_dir="logs/go2_walk", device=device)
    runner.load(ckpt_path)
    policy = runner.get_inference_policy(device=device)
    policy.eval()

    obs, _ = env.reset_idx(torch.tensor([0], device=device))
    obs = env.obs_buf.clone()

    with torch.no_grad():
        while True:
            actions = policy(obs)
            obs, reward, done, info = env.step(actions)
            # 如果摔倒重置
            if done.any():
                env.reset_idx(torch.where(done)[0])
                obs = env.obs_buf.clone()

if __name__ == "__main__":
    main()

使用方法

  1. 先跑训练脚本 go2_train.py,训练结束后,logs/go2_walk/ 下会生成 model_xxxx.pt
  2. 修改 ckpt_path 指向你对应的权重文件
  3. 运行:
python go2_eval.py

几个可改的地方

  • 想让机器狗转弯:把 cmd_yaw 改成正数/负数(比如 0.8)
  • 想原地转向:cmd_vx=0, cmd_yaw=0.8
  • 换成GPU:device="cuda:0",同时 gs.init(backend=gs.gpu)
  • 如果训练时 policy 网络结构改了,eval 里的 cfg["policy"] 必须和训练完全一致,否则加载失败

重要提醒

  1. rsl-rl 加载权重时,训练和评估的观测维度、动作维度、网络结构必须严格一致
  2. 如果训练时加了噪声、动作缩放、奖励函数改动,eval 的环境类 go2_env.py 不能和训练版本不一样
  3. 若出现 KeyError 权重加载失败,大概率是 cfg 不匹配
Logo

聚焦RDK与具身智能,汇集机器人开发教程、开源案例和技术交流

更多推荐