Nvidia Isaac Lab 8 - 在 Isaac Lab 中,训练第二个机器人 - 7. 评估结果
1. 运行策略
运行以下命令,注意使用的是之前创建的任务的 Play 版本。
python scripts/skrl/play.py --task Template-Reach-Play-v0提示
为提升性能,可能希望在回放期间进一步限制加载的环境数量。为此,可以使用 --num_envs 参数,例如:
python scripts/skrl/play.py --task Template-Reach-Play-v0 --num_envs 1002. 问题
虽然机器人移动到空间中正确的位置,但机器人的朝向没有改变,无法达到目标姿态。该如何解决这个问题呢?
3. 改进奖励
下面为朝向添加奖励项。
首先,通过在奖励管理器中添加新奖励项的方式,解决该问题。
将新 RewTerm 添加到 reach_env_cfg.py 文件中的 RewardsCfg 下方。
@configclass
class RewardsCfg:
"""Reward terms for the MDP."""
#
# ADD this code
#
end_effector_orientation_tracking = RewTerm(
func=mdp.orientation_command_error,
weight=-0.1,
params={"asset_cfg": SceneEntityCfg("robot", body_names=["ee_link"]), "command_name": "ee_pose"},
)
# task terms
end_effector_position_tracking = RewTerm(
func=mdp.position_command_error,
weight=-0.2,
params={"asset_cfg": SceneEntityCfg("robot", body_names=["ee_link"]), "command_name": "ee_pose"},
)
end_effector_position_tracking_fine_grained = RewTerm(
func=mdp.position_command_error_tanh,
weight=0.1,
params={"asset_cfg": SceneEntityCfg("robot", body_names=["ee_link"]), "std": 0.1, "command_name": "ee_pose"},
)
# action penalty
action_rate = RewTerm(func=mdp.action_rate_l2, weight=-0.0001)
joint_vel = RewTerm(下面添加与 func=mdp.orientation_command_error 参数相对应的函数实现。具体的数学计算逻辑在该函数中。
4. 添加朝向奖励
该函数用于计算来自命令的期望末端执行器朝向与末端执行器当前位置之间的误差。
将此代码添加到 Reach/tasks/manager_based/reach/mdp/rewards.py 文件中。
def orientation_command_error(env: ManagerBasedRLEnv, command_name: str, asset_cfg: SceneEntityCfg) -> torch.Tensor:
"""Penalize tracking orientation error using shortest path.
The function computes the orientation error between the desired orientation (from the command) and the
current orientation of the asset's body (in world frame). The orientation error is computed as the shortest
path between the desired and current orientations.
"""
# extract the asset (to enable type hinting)
asset: RigidObject = env.scene[asset_cfg.name]
command = env.command_manager.get_command(command_name)
# obtain the desired and current orientations
des_quat_b = command[:, 3:7]
des_quat_w = quat_mul(asset.data.root_state_w[:, 3:7], des_quat_b)
curr_quat_w = asset.data.body_state_w[:, asset_cfg.body_ids[0], 3:7] # type: ignore
return quat_error_magnitude(curr_quat_w, des_quat_w)5. 再次训练及审查策略
通过重新运行训练及审查结果,评估新奖励对策略的影响。
与之前一样,可以添加 --headless 参数,跳过 Isaac Sim 视口的显示,加速训练。也可以不使用该参数,实时观看训练过程。
不使用 --headless 运行方便调试,可以观察机器人在学习过程中的表现。尤其是刚开始时,观看学习过程的演变非常有趣。
5.1. 用无头模式训练
运行如下命令:
python scripts/skrl/train.py --task Template-Reach-v0 --headless5.2. 以可视化模式训练
运行如下命令:
python scripts/skrl/train.py --task Template-Reach-v05.3. 训练后审查结果
运行如下命令:
python scripts/skrl/play.py --task Template-Reach-Play-v0如果运行代码时遇到问题,可以打开本节附带的参考文件,将自己的代码与这些文件进行对比,或者直接覆盖自己的代码,这样就能得到经过验证的正确配置。
6. 奖励工程
6.1. 如何确定奖励函数?
这些奖励函数是怎么想出来的?虽然已经解释每个奖励,但定义这些奖励的想法从何而来?
上面编写的奖励不是解决该问题的唯一方案,只是对该机器人和该任务有效的特定方案而已。
除寻找能产生最优行为的奖励外,还可以从训练速度以及最终行为的鲁棒性角度,评估奖励的好坏。
如何为给定任务设计奖励是宏大的课题,并且是持续的工程性工作。这本身就可以构成完整的教程,而这里仅仅触及其表面,展示一些可行的可能性!
6.2. 如何加快训练速度?
可以采用多种策略,比如:优化碰撞体和网格,确保模型是可实例化的;或者使用更多的 GPU 资源、云资源等。
7. 训练、评估与迭代
7.1. 概述
至此,示例项目已经全部配置完成,下面先对机器人配置进行快速测试,然后开始训练和评估策略。
7.2. 使用 zero_agent 和 random_agent 进行测试
Isaac Lab 提供若干用于对配置进行基本测试的脚本。运行这些脚本可以帮助定位环境中是否存在问题。
运行以下命令:
python scripts/zero_agent.py --task Template-Reach-v0 --num_envs=10将打开环境,其中将加载多个机器人实例,但它们不会移动。该初步测试有助于验证机器人能否正常加载。
运行以下命令:
python scripts/random_agent.py --task Template-Reach-v0 --num_envs=10将给场景中的关节添加噪声,能看到所有关节都在运动。这是在开始训练之前进行的简单合理性检查。
对于上述命令,--num_envs 参数用于覆盖配置文件中设置的并行环境数。由于只是进行快速检查,通过将该数量减少到 10,可以加快场景加载速度。
任务名称 Template-Reach-v0 是随项目一起生成的,并且在安装过程中注册到 Isaac Lab 中。如果需要,可以在 source/Reach/Reach/tasks/manager_based/reach/__init__.py 中查看其定义位置。还可以在此处定义更多入口点,例如面向回放的配置。