4. isaac sim4.2 教程-Core API-Hello robot
本教程详细介绍了如何在 Omniverse Isaac Sim 的扩展应用中添加并移动一个移动机器人。完成本教程后,您将了解如何向仿真环境中添加机器人,并使用 Python 对其轮子施加动作。
1. 前置条件
在开始本教程前,请先学习 Hello World 教程。
本教程基于 Hello World 教程中开发的 Awesome Example 源码继续扩展。
2. 添加一个机器人
首先,在场景中添加一个 NVIDIA Jetbot。这样,您就可以通过 Python 访问 Omniverse Nucleus 服务器上的 Isaac Sim 机器人、传感器和环境资源库,并利用 Content 窗口 进行浏览。
在拖动添加的时候,直接把他拖到world下就可以,如果直接拖到场景中是与world并列的。
接下来看看怎么用代码添加
from omni.isaac.examples.base_sample import BaseSample
from omni.isaac.core import Worldfrom omni.isaac.core.utils.nucleus import get_assets_root_path
from omni.isaac.core.utils.stage import add_reference_to_stage
from omni.isaac.core.robots import Robot
import carbclass MyHelloWorld(BaseSample): # 继承自basesampledef __init__(self) -> None:super().__init__()returndef setup_scene(self):world = self.get_world()world.scene.add_default_ground_plane()# 拿到资源的根路径assets_root_path = get_assets_root_path()if assets_root_path is None:# Use carb to 记录日志 warnings, errors and infos in your application (shown on terminal)carb.log_error("Could not find nucleus server with /Isaac folder")# 拿到具体资源路径:根路径+具体资源asset_path = assets_root_path + "/Isaac/Robots/Jetbot/jetbot.usd"# 这将会创建 a new XFormPrim and 把它添加到场景world中add_reference_to_stage(usd_path=asset_path, prim_path="/World/Fancy_Robot")# 将机器人封装为Robot对象并添加到场景# Note: this call doesn't create the Jetbot in the stage window, it was already# created with the add_reference_to_stagejetbot_robot = world.scene.add(Robot(prim_path="/World/Fancy_Robot", name="fancy_robot"))# 物理系统初始化前无法获取动力学信息print("Num of degrees of freedom before first reset: " + str(jetbot_robot.num_dof)) # prints Nonereturn# 初始化时机:# 发生在 setup_scene() 完成之后# 发生在 setup_post_load() 执行之前async def setup_post_load(self):self._world = self.get_world()self._jetbot = self._world.scene.get_object("fancy_robot")# 物理系统初始化后可获取动力学参数print("Num of degrees of freedom after first reset: " + str(self._jetbot.num_dof)) # prints 2print("Joint Positions after first reset: " + str(self._jetbot.get_joint_positions()))return
再次load:
这就通过代码加进来了。
3. 移动机器人
把代码改成:
from omni.isaac.examples.base_sample import BaseSample
from omni.isaac.core import Worldfrom omni.isaac.core.utils.nucleus import get_assets_root_path
from omni.isaac.core.utils.stage import add_reference_to_stage
from omni.isaac.core.robots import Robot
import carbfrom omni.isaac.core.utils.types import ArticulationAction # 我们要给机器人发送动作,动作类型就是ArticulationAction
import numpy as npclass MyHelloWorld(BaseSample): # 继承自basesampledef __init__(self) -> None:super().__init__()returndef setup_scene(self):world = self.get_world()world.scene.add_default_ground_plane()# 拿到资源的根路径assets_root_path = get_assets_root_path()if assets_root_path is None:# Use carb to 记录日志 warnings, errors and infos in your application (shown on terminal)carb.log_error("Could not find nucleus server with /Isaac folder")# 拿到具体资源路径:根路径+具体资源asset_path = assets_root_path + "/Isaac/Robots/Jetbot/jetbot.usd"# 这将会创建 a new XFormPrim and 把它添加到场景world中add_reference_to_stage(usd_path=asset_path, prim_path="/World/Fancy_Robot")# 将机器人封装为Robot对象并添加到场景# Note: this call doesn't create the Jetbot in the stage window, it was already# created with the add_reference_to_stagejetbot_robot = world.scene.add(Robot(prim_path="/World/Fancy_Robot", name="fancy_robot"))# 物理系统初始化前无法获取动力学信息print("Num of degrees of freedom before first reset: " + str(jetbot_robot.num_dof)) # prints Nonereturn# 初始化时机:# 发生在 setup_scene() 完成之后# 发生在 setup_post_load() 执行之前async def setup_post_load(self):self._world = self.get_world()self._jetbot = self._world.scene.get_object("fancy_robot")# 物理系统初始化后可获取动力学参数# print("Num of degrees of freedom after first reset: " + str(self._jetbot.num_dof)) # prints 2# print("Joint Positions after first reset: " + str(self._jetbot.get_joint_positions()))# 首先要拿到机器人控制器(遥控器)self._jetbot_articulation_controller = self._jetbot.get_articulation_controller()# 设置自动执行的任务--通过回调函数# 每一个物理步长都会调用一次这个回调函数self._world.add_physics_callback("sending_actions", callback_fn=self.send_robot_actions)returndef send_robot_actions(self, step_size):# 每个关节控制器(articulation controller)都提供了 apply_action 方法。# 该方法用于向机器人发送动作指令,其参数是 ArticulationAction 类型,# 可以包含 joint_positions(关节位置)、joint_efforts(关节力矩)、joint_velocities(关节速度)这三个可选参数。self._jetbot_articulation_controller.apply_action(ArticulationAction(joint_positions=None, # 不关心joint_efforts=None, # 不关心joint_velocities=5 * np.random.rand(2,) # 只关注速度))return
3.1 使用 WheeledRobot Class
Omniverse Isaac Sim 也有robot-specific extensions that 提供更适配的函数 and access to other controllers and tasks (more on this later). Now you’ll re-write the previous code using the WheeledRobot class to make it simpler.
from omni.isaac.wheeled_robots.robots import WheeledRobot #轮式机器人class MyHelloWorld(BaseSample): # 继承自basesampledef __init__(self) -> None:super().__init__()returndef setup_scene(self):world = self.get_world()world.scene.add_default_ground_plane()# # 拿到资源的根路径# assets_root_path = get_assets_root_path()# if assets_root_path is None:# # Use carb to 记录日志 warnings, errors and infos in your application (shown on terminal)# carb.log_error("Could not find nucleus server with /Isaac folder")# # 拿到具体资源路径:根路径+具体资源# asset_path = assets_root_path + "/Isaac/Robots/Jetbot/jetbot.usd"# # 这将会创建 a new XFormPrim and 把它添加到场景world中# add_reference_to_stage(usd_path=asset_path, prim_path="/World/Fancy_Robot")# # 将机器人封装为Robot对象并添加到场景# # Note: this call doesn't create the Jetbot in the stage window, it was already# # created with the add_reference_to_stage# jetbot_robot = world.scene.add(Robot(prim_path="/World/Fancy_Robot", name="fancy_robot"))# # 物理系统初始化前无法获取动力学信息# print("Num of degrees of freedom before first reset: " + str(jetbot_robot.num_dof)) # prints Noneassets_root_path = get_assets_root_path()jetbot_asset_path = assets_root_path + "/Isaac/Robots/Jetbot/jetbot.usd"self._jetbot = world.scene.add(WheeledRobot(prim_path="/World/Fancy_Robot", #树状结构的哪一块name="fancy_robot",wheel_dof_names=["left_wheel_joint", "right_wheel_joint"], # 轮子关节的名称create_robot=True,usd_path=jetbot_asset_path,))return# 初始化时机:# 发生在 setup_scene() 完成之后# 发生在 setup_post_load() 执行之前async def setup_post_load(self):self._world = self.get_world()self._jetbot = self._world.scene.get_object("fancy_robot")# 物理系统初始化后可获取动力学参数# print("Num of degrees of freedom after first reset: " + str(self._jetbot.num_dof)) # prints 2# print("Joint Positions after first reset: " + str(self._jetbot.get_joint_positions()))# 首先要拿到机器人控制器(遥控器),如果用wheelrobot类就不用这个控制器了# self._jetbot_articulation_controller = self._jetbot.get_articulation_controller()# 设置自动执行的任务--通过回调函数# 每一个物理步长都会调用一次这个回调函数self._world.add_physics_callback("sending_actions", callback_fn=self.send_robot_actions)returndef send_robot_actions(self, step_size):# 每个关节控制器(articulation controller)都提供了 apply_action 方法。# 该方法用于向机器人发送动作指令,其参数是 ArticulationAction 类型,# 可以包含 joint_positions(关节位置)、joint_efforts(关节力矩)、joint_velocities(关节速度)这三个可选参数。# self._jetbot_articulation_controller.apply_action(ArticulationAction(# joint_positions=None, # 不关心# joint_efforts=None, # 不关心# joint_velocities=5 * np.random.rand(2,) # 只关注速度# ))# 直接用wheelrobot类就可以了,不用对controller进行操作self._jetbot.apply_wheel_actions(ArticulationAction(joint_positions=None,joint_efforts=None,joint_velocities=5 * np.random.rand(2,)))return
4. 增加一个自定义控制器
本教程将介绍如何创建并使用自定义控制器来移动移动机器人,随后详细说明如何在Omniverse Isaac Sim中运用各类可用控制器。完成本教程后,您将能更轻松地在Omniverse Isaac Sim中添加与控制机器人。
from omni.isaac.examples.base_sample import BaseSample
from omni.isaac.core.utils.nucleus import get_assets_root_path
from omni.isaac.wheeled_robots.robots import WheeledRobot
from omni.isaac.core.utils.types import ArticulationAction
from omni.isaac.core.controllers import BaseController
import numpy as npclass CoolController(BaseController):def __init__(self):super().__init__(name="my_cool_controller")# An open loop controller that uses a unicycle modelself._wheel_radius = 0.03 # 轮子半径3cmself._wheel_base = 0.1125 # 轴距11.25cmreturndef forward(self, command): # 这个函数是用来接收命令的# command will have two elements, first element is the forward velocity# second element is the angular velocity (yaw only).joint_velocities = [0.0, 0.0]# 差速驱动模型joint_velocities[0] = ((2 * command[0]) - (command[1] * self._wheel_base)) / (2 * self._wheel_radius)joint_velocities[1] = ((2 * command[0]) + (command[1] * self._wheel_base)) / (2 * self._wheel_radius)# A controller has to return an ArticulationActionreturn ArticulationAction(joint_velocities=joint_velocities)class MyHelloWorld(BaseSample): # 继承自basesampledef __init__(self) -> None:super().__init__()returndef setup_scene(self):world = self.get_world()world.scene.add_default_ground_plane()assets_root_path = get_assets_root_path()jetbot_asset_path = assets_root_path + "/Isaac/Robots/Jetbot/jetbot.usd"world.scene.add(WheeledRobot(prim_path="/World/Fancy_Robot", #树状结构的哪一块name="fancy_robot",wheel_dof_names=["left_wheel_joint", "right_wheel_joint"], # 轮子关节的名称create_robot=True,usd_path=jetbot_asset_path,))returnasync def setup_post_load(self):self._world = self.get_world()self._jetbot = self._world.scene.get_object("fancy_robot")self._world.add_physics_callback("sending_actions", callback_fn=self.send_robot_actions)# 初始化自己的控制器self._my_controller = CoolController()returndef send_robot_actions(self, step_size):# 让控制器去计算动作self._jetbot.apply_action(self._my_controller.forward(command=[0.20, np.pi / 4])) # 前进速度0.2m/s,角速度pi/4returnasync def setup_pre_reset(self): # reset之前,场景重置之前保存/暂停等returnasync def setup_post_reset(self): # 重置之后,恢复初始化状态returndef world_cleanup(self):# 释放内存等return
4.1 Using the Available Controllers
Omniverse Isaac Sim also provides different controllers under many robot extensions. Re-write the previous code using the DifferentialController
class and add a WheelBasePoseController
刚才我们用的就是右边的,给一个高层的命令,然后通过控制器去计算,驱动。
现在来用下已经实现好的轮姿态控制器和差速控制器,wheelbase就是要知道目标的位置和方向才能移动到制定位置,时应高层的命令。差速控制器是精确到控制轮子的速度。
isaacsim4.2/exts/omni.isaac.wheeled_robots/omni/isaac/wheeled_robots/controllers/__init__.py中还有其他控制器
# Using the Available Controllers
from omni.isaac.examples.base_sample import BaseSample
from omni.isaac.core.utils.nucleus import get_assets_root_path
from omni.isaac.wheeled_robots.robots import WheeledRobot
# This extension includes several generic controllers that could be used with multiple robots
from omni.isaac.wheeled_robots.controllers.wheel_base_pose_controller import WheelBasePoseController
# Robot specific controller
from omni.isaac.wheeled_robots.controllers.differential_controller import DifferentialController
import numpy as npclass MyHelloWorld(BaseSample): # 继承自basesampledef __init__(self) -> None:super().__init__()returndef setup_scene(self):world = self.get_world()world.scene.add_default_ground_plane()assets_root_path = get_assets_root_path()jetbot_asset_path = assets_root_path + "/Isaac/Robots/Jetbot/jetbot.usd"world.scene.add(WheeledRobot(prim_path="/World/Fancy_Robot", #树状结构的哪一块name="fancy_robot",wheel_dof_names=["left_wheel_joint", "right_wheel_joint"], # 轮子关节的名称create_robot=True,usd_path=jetbot_asset_path,))returnasync def setup_post_load(self):self._world = self.get_world()self._jetbot = self._world.scene.get_object("fancy_robot")self._world.add_physics_callback("sending_actions", callback_fn=self.send_robot_actions)# 初始化自己的控制器self._my_controller = WheelBasePoseController(name="cool_controller",open_loop_wheel_controller=DifferentialController( # 开环就是不会判断是否真的到达目标位置,闭环会有反馈name="simple_control",wheel_radius=0.03,wheel_base=0.1125),is_holonomic=False # 非全向轮)returndef send_robot_actions(self, step_size):# 获取当前机器人的位姿position, orientation = self._jetbot.get_world_pose()self._jetbot.apply_action(self._my_controller.forward(# 开始时的位姿start_position=position,start_orientation=orientation,# 目标位置goal_position=np.array([0.8, 0.8])))return
可以看到小车一开始是转变了一下朝向才开始移动的: