创建可视化标记

创建可视化标记#

可视化标记对调试环境的状态非常有用。它们可以用来可视化仿真中的帧、命令和其他信息。

虽然 Isaac Sim 提供了自己的 omni.isaac.debug_draw 扩展,但它仅限于渲染点、线和样条线。对于需要渲染更复杂形状的情况,您可以使用 markers.VisualizationMarkers 类。

本指南附带了一个名为 markers.py 的示例脚本,位于 IsaacLab/source/standalone/demos 目录中。

markers.py 的代码
  1# Copyright (c) 2022-2024, The Isaac Lab Project Developers.
  2# All rights reserved.
  3#
  4# SPDX-License-Identifier: BSD-3-Clause
  5
  6"""This script demonstrates different types of markers.
  7
  8.. code-block:: bash
  9
 10    # Usage
 11    ./isaaclab.sh -p source/standalone/demos/markers.py
 12
 13"""
 14
 15"""Launch Isaac Sim Simulator first."""
 16
 17import argparse
 18
 19from omni.isaac.lab.app import AppLauncher
 20
 21# add argparse arguments
 22parser = argparse.ArgumentParser(description="This script demonstrates different types of markers.")
 23# append AppLauncher cli args
 24AppLauncher.add_app_launcher_args(parser)
 25# parse the arguments
 26args_cli = parser.parse_args()
 27
 28# launch omniverse app
 29app_launcher = AppLauncher(args_cli)
 30simulation_app = app_launcher.app
 31
 32"""Rest everything follows."""
 33
 34import torch
 35
 36import omni.isaac.lab.sim as sim_utils
 37from omni.isaac.lab.markers import VisualizationMarkers, VisualizationMarkersCfg
 38from omni.isaac.lab.sim import SimulationContext
 39from omni.isaac.lab.utils.assets import ISAAC_NUCLEUS_DIR, ISAACLAB_NUCLEUS_DIR
 40from omni.isaac.lab.utils.math import quat_from_angle_axis
 41
 42
 43def define_markers() -> VisualizationMarkers:
 44    """Define markers with various different shapes."""
 45    marker_cfg = VisualizationMarkersCfg(
 46        prim_path="/Visuals/myMarkers",
 47        markers={
 48            "frame": sim_utils.UsdFileCfg(
 49                usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/UIElements/frame_prim.usd",
 50                scale=(0.5, 0.5, 0.5),
 51            ),
 52            "arrow_x": sim_utils.UsdFileCfg(
 53                usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/UIElements/arrow_x.usd",
 54                scale=(1.0, 0.5, 0.5),
 55                visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(0.0, 1.0, 1.0)),
 56            ),
 57            "cube": sim_utils.CuboidCfg(
 58                size=(1.0, 1.0, 1.0),
 59                visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 0.0, 0.0)),
 60            ),
 61            "sphere": sim_utils.SphereCfg(
 62                radius=0.5,
 63                visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(0.0, 1.0, 0.0)),
 64            ),
 65            "cylinder": sim_utils.CylinderCfg(
 66                radius=0.5,
 67                height=1.0,
 68                visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(0.0, 0.0, 1.0)),
 69            ),
 70            "cone": sim_utils.ConeCfg(
 71                radius=0.5,
 72                height=1.0,
 73                visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 1.0, 0.0)),
 74            ),
 75            "mesh": sim_utils.UsdFileCfg(
 76                usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/Blocks/DexCube/dex_cube_instanceable.usd",
 77                scale=(10.0, 10.0, 10.0),
 78            ),
 79            "mesh_recolored": sim_utils.UsdFileCfg(
 80                usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/Blocks/DexCube/dex_cube_instanceable.usd",
 81                scale=(10.0, 10.0, 10.0),
 82                visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 0.25, 0.0)),
 83            ),
 84            "robot_mesh": sim_utils.UsdFileCfg(
 85                usd_path=f"{ISAACLAB_NUCLEUS_DIR}/Robots/ANYbotics/ANYmal-D/anymal_d.usd",
 86                scale=(2.0, 2.0, 2.0),
 87                visual_material=sim_utils.GlassMdlCfg(glass_color=(0.0, 0.1, 0.0)),
 88            ),
 89        },
 90    )
 91    return VisualizationMarkers(marker_cfg)
 92
 93
 94def main():
 95    """Main function."""
 96    # Load kit helper
 97    sim = SimulationContext(sim_utils.SimulationCfg(dt=0.01))
 98    # Set main camera
 99    sim.set_camera_view([0.0, 18.0, 12.0], [0.0, 3.0, 0.0])
100
101    # Spawn things into stage
102    # Lights
103    cfg = sim_utils.DomeLightCfg(intensity=3000.0, color=(0.75, 0.75, 0.75))
104    cfg.func("/World/Light", cfg)
105
106    # create markers
107    my_visualizer = define_markers()
108
109    # define a grid of positions where the markers should be placed
110    num_markers_per_type = 5
111    grid_spacing = 2.0
112    # Calculate the half-width and half-height
113    half_width = (num_markers_per_type - 1) / 2.0
114    half_height = (my_visualizer.num_prototypes - 1) / 2.0
115    # Create the x and y ranges centered around the origin
116    x_range = torch.arange(-half_width * grid_spacing, (half_width + 1) * grid_spacing, grid_spacing)
117    y_range = torch.arange(-half_height * grid_spacing, (half_height + 1) * grid_spacing, grid_spacing)
118    # Create the grid
119    x_grid, y_grid = torch.meshgrid(x_range, y_range, indexing="ij")
120    x_grid = x_grid.reshape(-1)
121    y_grid = y_grid.reshape(-1)
122    z_grid = torch.zeros_like(x_grid)
123    # marker locations
124    marker_locations = torch.stack([x_grid, y_grid, z_grid], dim=1)
125    marker_indices = torch.arange(my_visualizer.num_prototypes).repeat(num_markers_per_type)
126
127    # Play the simulator
128    sim.reset()
129    # Now we are ready!
130    print("[INFO]: Setup complete...")
131
132    # Yaw angle
133    yaw = torch.zeros_like(marker_locations[:, 0])
134    # Simulate physics
135    while simulation_app.is_running():
136        # rotate the markers around the z-axis for visualization
137        marker_orientations = quat_from_angle_axis(yaw, torch.tensor([0.0, 0.0, 1.0]))
138        # visualize
139        my_visualizer.visualize(marker_locations, marker_orientations, marker_indices=marker_indices)
140        # roll corresponding indices to show how marker prototype can be changed
141        if yaw[0].item() % (0.5 * torch.pi) < 0.01:
142            marker_indices = torch.roll(marker_indices, 1)
143        # perform step
144        sim.step()
145        # increment yaw
146        yaw += 0.01
147
148
149if __name__ == "__main__":
150    # run the main function
151    main()
152    # close sim app
153    simulation_app.close()

配置标记#

VisualizationMarkersCfg 类提供了一个简单的界面来配置不同类型的标记。它接受以下参数:

  • prim_path :标记类的对应 prim 路径。

  • markers :一个指定由类处理的不同标记原型的字典。键是标记原型的名称,值是其生成配置。

备注

如果标记原型指定了具有物理属性的配置,则会将其删除。这是因为这些标记不打算进行模拟。

这里展示了可以配置的所有不同类型的标记。这些范围从简单形状如圆锥和球体到更复杂的几何形状如框架或箭头。标记原型也可以从 USD 文件中配置。

marker_cfg = VisualizationMarkersCfg(
    prim_path="/Visuals/myMarkers",
    markers={
        "frame": sim_utils.UsdFileCfg(
            usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/UIElements/frame_prim.usd",
            scale=(0.5, 0.5, 0.5),
        ),
        "arrow_x": sim_utils.UsdFileCfg(
            usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/UIElements/arrow_x.usd",
            scale=(1.0, 0.5, 0.5),
            visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(0.0, 1.0, 1.0)),
        ),
        "cube": sim_utils.CuboidCfg(
            size=(1.0, 1.0, 1.0),
            visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 0.0, 0.0)),
        ),
        "sphere": sim_utils.SphereCfg(
            radius=0.5,
            visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(0.0, 1.0, 0.0)),
        ),
        "cylinder": sim_utils.CylinderCfg(
            radius=0.5,
            height=1.0,
            visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(0.0, 0.0, 1.0)),
        ),
        "cone": sim_utils.ConeCfg(
            radius=0.5,
            height=1.0,
            visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 1.0, 0.0)),
        ),
        "mesh": sim_utils.UsdFileCfg(
            usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/Blocks/DexCube/dex_cube_instanceable.usd",
            scale=(10.0, 10.0, 10.0),
        ),
        "mesh_recolored": sim_utils.UsdFileCfg(
            usd_path=f"{ISAAC_NUCLEUS_DIR}/Props/Blocks/DexCube/dex_cube_instanceable.usd",
            scale=(10.0, 10.0, 10.0),
            visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 0.25, 0.0)),
        ),
        "robot_mesh": sim_utils.UsdFileCfg(
            usd_path=f"{ISAACLAB_NUCLEUS_DIR}/Robots/ANYbotics/ANYmal-D/anymal_d.usd",
            scale=(2.0, 2.0, 2.0),
            visual_material=sim_utils.GlassMdlCfg(glass_color=(0.0, 0.1, 0.0)),
        ),
    },
)

绘制标记#

要绘制标记,我们调用 visualize 方法。该方法接受标记的姿势和要绘制的相应标记原型作为参数。

# rotate the markers around the z-axis for visualization
marker_orientations = quat_from_angle_axis(yaw, torch.tensor([0.0, 0.0, 1.0]))
# visualize
my_visualizer.visualize(marker_locations, marker_orientations, marker_indices=marker_indices)
# roll corresponding indices to show how marker prototype can be changed
if yaw[0].item() % (0.5 * torch.pi) < 0.01:
    marker_indices = torch.roll(marker_indices, 1)

执行脚本#

要运行附带的脚本,请执行以下命令:

./isaaclab.sh -p source/standalone/demos/markers.py

仿真应该启动,并且您可以观察到不同类型的标记排列成网格图案。标记将围绕其各自的轴旋转。另外,每隔几次旋转,它们将在网格上向前滚动。

要停止仿真,请关闭窗口,或在终端中使用 Ctrl+C