|
| 1 | +import argparse |
| 2 | + |
| 3 | +import genesis as gs |
| 4 | +import numpy as np |
| 5 | +import trimesh |
| 6 | +from genesis.sensors import NPZFileWriter, RigidContactForceGridSensor, SensorDataRecorder, VideoFileWriter |
| 7 | +from genesis.utils.misc import tensor_to_array |
| 8 | +from tqdm import tqdm |
| 9 | + |
| 10 | + |
| 11 | +def visualize_grid_sensor(scene: gs.Scene, sensor: RigidContactForceGridSensor, min_force=0.0, max_force=1.0): |
| 12 | + """ |
| 13 | + Draws debug objects on scene to visualize the contact grid sensor data. |
| 14 | +
|
| 15 | + Note: This method is very inefficient and purely for demo/debugging purposes. |
| 16 | + This processes the grid data from the sensor, which means the transformation from global-> local frame is undone to |
| 17 | + revert back to into global frame to draw the debug objects. |
| 18 | + """ |
| 19 | + grid_data = sensor.read() |
| 20 | + |
| 21 | + link_pos = tensor_to_array(scene._sim.rigid_solver.get_links_pos(links_idx=sensor.link_idx).squeeze(axis=1)) |
| 22 | + link_quat = tensor_to_array(scene._sim.rigid_solver.get_links_quat(links_idx=sensor.link_idx).squeeze(axis=1)) |
| 23 | + |
| 24 | + sensor_dims = sensor.max_bounds - sensor.min_bounds |
| 25 | + grid_cell_size = sensor_dims / np.array(sensor.grid_size) |
| 26 | + |
| 27 | + debug_objs = [] |
| 28 | + |
| 29 | + for x in range(grid_data.shape[1]): |
| 30 | + for y in range(grid_data.shape[2]): |
| 31 | + for z in range(grid_data.shape[3]): |
| 32 | + force = grid_data[0, x, y, z] |
| 33 | + force_magnitude = np.linalg.norm(force) |
| 34 | + |
| 35 | + color_intensity = np.clip(force_magnitude, min_force, max_force) / max_force |
| 36 | + color = np.array([color_intensity, 0.0, 1.0 - color_intensity, 0.4]) |
| 37 | + |
| 38 | + mesh = trimesh.creation.box(extents=grid_cell_size) |
| 39 | + mesh.visual = trimesh.visual.ColorVisuals(vertex_colors=np.tile(color, [len(mesh.vertices), 1])) |
| 40 | + |
| 41 | + local_pos = sensor.min_bounds + (np.array([x, y, z]) + 0.5) * grid_cell_size |
| 42 | + |
| 43 | + T = trimesh.transformations.quaternion_matrix(link_quat) |
| 44 | + T[:3, 3] = link_pos |
| 45 | + |
| 46 | + local_T = np.eye(4) |
| 47 | + local_T[:3, 3] = local_pos |
| 48 | + final_T = T @ local_T |
| 49 | + |
| 50 | + debug_objs.append(scene.draw_debug_mesh(mesh, T=final_T)) |
| 51 | + |
| 52 | + return debug_objs |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + parser = argparse.ArgumentParser() |
| 57 | + parser.add_argument("--seconds", "-t", type=float, default=1, help="Number of seconds to simulate") |
| 58 | + parser.add_argument("--dt", type=float, default=1e-2, help="Simulation time step") |
| 59 | + parser.add_argument( |
| 60 | + "--substeps", |
| 61 | + type=int, |
| 62 | + default=1, |
| 63 | + help="Number of substeps", |
| 64 | + ) |
| 65 | + parser.add_argument("--n_envs", type=int, default=0, help="Number of environments (default: 0)") |
| 66 | + parser.add_argument("--cpu", action="store_true", help="Use CPU instead of GPU") |
| 67 | + parser.add_argument("--vis", "-v", action="store_true", help="Show visualization GUI") |
| 68 | + parser.add_argument( |
| 69 | + "--debug", "-d", action="store_true", default=True, help="Draw the contact grid sensor debug objects" |
| 70 | + ) |
| 71 | + |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + gs.init(backend=gs.gpu, logging_level=None) |
| 75 | + |
| 76 | + scene = gs.Scene( |
| 77 | + sim_options=gs.options.SimOptions( |
| 78 | + dt=args.dt, |
| 79 | + substeps=args.substeps, |
| 80 | + gravity=(0, 0, -9.81), |
| 81 | + ), |
| 82 | + rigid_options=gs.options.RigidOptions( |
| 83 | + use_gjk_collision=True, |
| 84 | + constraint_timeconst=max(0.01, 2 * args.dt / args.substeps), |
| 85 | + ), |
| 86 | + profiling_options=gs.options.ProfilingOptions( |
| 87 | + show_FPS=False, |
| 88 | + ), |
| 89 | + show_viewer=args.vis, |
| 90 | + ) |
| 91 | + |
| 92 | + scene.add_entity(gs.morphs.Plane()) |
| 93 | + |
| 94 | + block = scene.add_entity( |
| 95 | + morph=gs.morphs.Box( |
| 96 | + pos=(0.0, 0.0, 0.2), |
| 97 | + euler=(0, 10, 0), |
| 98 | + size=(2.0, 2.0, 0.05), |
| 99 | + visualization=not args.debug, |
| 100 | + ), |
| 101 | + material=gs.materials.Rigid( |
| 102 | + gravity_compensation=1.0, |
| 103 | + ), |
| 104 | + ) |
| 105 | + |
| 106 | + sphere1 = scene.add_entity( |
| 107 | + gs.morphs.Sphere(pos=(0.7, 0.4, 0.4), radius=0.1), |
| 108 | + surface=gs.surfaces.Default( |
| 109 | + color=(1.0, 0.0, 0.0, 1.0), |
| 110 | + ), |
| 111 | + ) |
| 112 | + sphere2 = scene.add_entity( |
| 113 | + gs.morphs.Sphere(pos=(-0.5, -0.3, 0.5), radius=0.1), |
| 114 | + surface=gs.surfaces.Default( |
| 115 | + color=(0.0, 1.0, 0.0, 1.0), |
| 116 | + ), |
| 117 | + ) |
| 118 | + sphere3 = scene.add_entity( |
| 119 | + gs.morphs.Sphere(pos=(-0.2, 0.7, 0.6), radius=0.1), |
| 120 | + surface=gs.surfaces.Default( |
| 121 | + color=(0.0, 0.0, 1.0, 1.0), |
| 122 | + ), |
| 123 | + ) |
| 124 | + |
| 125 | + steps = int(args.seconds / args.dt) |
| 126 | + cam = scene.add_camera(res=(640, 480), pos=(-2, 3, 1.5), lookat=(0.0, 0.0, 0.1), fov=30, GUI=args.vis) |
| 127 | + grid_sensor = RigidContactForceGridSensor(block, grid_size=(4, 4, 2)) |
| 128 | + |
| 129 | + scene.build(n_envs=args.n_envs) |
| 130 | + |
| 131 | + data_recorder = SensorDataRecorder() |
| 132 | + data_recorder.add_sensor(cam, VideoFileWriter(filename="grid_test.mp4")) |
| 133 | + data_recorder.add_sensor(grid_sensor, NPZFileWriter(filename="grid_test.npz")) |
| 134 | + data_recorder.start_recording() |
| 135 | + |
| 136 | + try: |
| 137 | + for _ in tqdm(range(steps), total=steps): |
| 138 | + scene.step() |
| 139 | + |
| 140 | + if args.debug: |
| 141 | + scene.clear_debug_objects() |
| 142 | + visualize_grid_sensor(scene, grid_sensor) |
| 143 | + |
| 144 | + data_recorder.step() |
| 145 | + |
| 146 | + except KeyboardInterrupt: |
| 147 | + gs.logger.info("Simulation interrupted, exiting.") |
| 148 | + finally: |
| 149 | + gs.logger.info("Simulation finished.") |
| 150 | + |
| 151 | + data_recorder.stop_recording() |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + main() |
0 commit comments