|
| 1 | +# ---------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2021-2025 DexForce Technology Co., Ltd. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ---------------------------------------------------------------------------- |
| 16 | + |
| 17 | +""" |
| 18 | +This script demonstrates how to create a simulation scene using SimulationManager. |
| 19 | +It shows the basic setup of simulation context, adding objects, and sensors. |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +import time |
| 24 | + |
| 25 | +from embodichain.lab.sim import SimulationManager, SimulationManagerCfg |
| 26 | +from embodichain.lab.sim.cfg import RigidBodyAttributesCfg |
| 27 | +from embodichain.lab.sim.shapes import CubeCfg, MeshCfg |
| 28 | +from embodichain.lab.sim.objects import RigidObject, RigidObjectCfg,ArticulationCfg,Articulation |
| 29 | +from dexsim.utility.path import get_resources_data_path |
| 30 | + |
| 31 | + |
| 32 | +def main(): |
| 33 | + """Main function to create and run the simulation scene.""" |
| 34 | + |
| 35 | + # Parse command line arguments |
| 36 | + parser = argparse.ArgumentParser( |
| 37 | + description="Create a simulation scene with SimulationManager" |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + "--headless", |
| 41 | + action="store_true", |
| 42 | + default=False, |
| 43 | + help="Run simulation in headless mode", |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--device", type=str, default="cpu", help="Simulation device (cuda or cpu)" |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--enable_rt", |
| 50 | + action="store_true", |
| 51 | + default=True, |
| 52 | + help="Enable ray tracing for better visuals", |
| 53 | + ) |
| 54 | + args = parser.parse_args() |
| 55 | + |
| 56 | + # Configure the simulation |
| 57 | + sim_cfg = SimulationManagerCfg( |
| 58 | + width=1920, |
| 59 | + height=1080, |
| 60 | + headless=True, |
| 61 | + physics_dt=1.0 / 100.0, # Physics timestep (100 Hz) |
| 62 | + sim_device=args.device, |
| 63 | + enable_rt=args.enable_rt, # Enable ray tracing for better visuals |
| 64 | + num_envs=1, |
| 65 | + arena_space=3.0, |
| 66 | + ) |
| 67 | + |
| 68 | + # Create the simulation instance |
| 69 | + sim = SimulationManager(sim_cfg) |
| 70 | + # Open window when the scene has been set up |
| 71 | + if not args.headless: |
| 72 | + sim.open_window() |
| 73 | + |
| 74 | + |
| 75 | + cube: RigidObject = sim.add_rigid_object( |
| 76 | + cfg=RigidObjectCfg( |
| 77 | + uid="cube", |
| 78 | + shape=CubeCfg(size=[0.1, 0.1, 0.1]), |
| 79 | + body_type="dynamic", |
| 80 | + attrs=RigidBodyAttributesCfg( |
| 81 | + mass=1.0, |
| 82 | + dynamic_friction=0.5, |
| 83 | + static_friction=0.5, |
| 84 | + restitution=0.1, |
| 85 | + ), |
| 86 | + init_pos=[0.0, 0.0, 1.0], |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + usdpath="/home/xiemh/model/004_sugar_box/004_sugar_box_xmh.usda" |
| 91 | + sugar_box: RigidObject = sim.add_rigid_object( |
| 92 | + cfg=RigidObjectCfg( |
| 93 | + uid="sugar_box", |
| 94 | + shape=MeshCfg(fpath=usdpath), |
| 95 | + body_type="dynamic", |
| 96 | + init_pos=[0.2, 0.2, 1.0], |
| 97 | + use_usd_properties=True, |
| 98 | + ) |
| 99 | + ) |
| 100 | + |
| 101 | + # Add objects to the scene |
| 102 | + h1 :Articulation= sim.add_articulation( |
| 103 | + cfg=ArticulationCfg( |
| 104 | + uid="h1", |
| 105 | + # fpath="/home/xiemh/model/Collected_ur10/ur10.usd", |
| 106 | + fpath="/home/xiemh/model/Collected_h1/h1.usda", |
| 107 | + build_pk_chain=False, |
| 108 | + init_pos=[-0.2, -0.2, 1.0], |
| 109 | + use_usd_properties=False, |
| 110 | + ) |
| 111 | + ) |
| 112 | + |
| 113 | + print("[INFO]: Scene setup complete!") |
| 114 | + print("[INFO]: Press Ctrl+C to stop the simulation") |
| 115 | + |
| 116 | + # Run the simulation |
| 117 | + run_simulation(sim) |
| 118 | + |
| 119 | + |
| 120 | +def run_simulation(sim: SimulationManager): |
| 121 | + """Run the simulation loop. |
| 122 | +
|
| 123 | + Args: |
| 124 | + sim: The SimulationManager instance to run |
| 125 | + """ |
| 126 | + |
| 127 | + # Initialize GPU physics if using CUDA |
| 128 | + if sim.is_use_gpu_physics: |
| 129 | + sim.init_gpu_physics() |
| 130 | + |
| 131 | + step_count = 0 |
| 132 | + |
| 133 | + try: |
| 134 | + last_time = time.time() |
| 135 | + last_step = 0 |
| 136 | + while True: |
| 137 | + # Update physics simulation |
| 138 | + sim.update(step=1) |
| 139 | + time.sleep(0.03) # Sleep to limit update rate (optional) |
| 140 | + step_count += 1 |
| 141 | + |
| 142 | + # Print FPS every second |
| 143 | + if step_count % 100 == 0: |
| 144 | + current_time = time.time() |
| 145 | + elapsed = current_time - last_time |
| 146 | + fps = ( |
| 147 | + sim.num_envs * (step_count - last_step) / elapsed |
| 148 | + if elapsed > 0 |
| 149 | + else 0 |
| 150 | + ) |
| 151 | + # print(f"[INFO]: Simulation step: {step_count}, FPS: {fps:.2f}") |
| 152 | + last_time = current_time |
| 153 | + last_step = step_count |
| 154 | + |
| 155 | + except KeyboardInterrupt: |
| 156 | + print("\n[INFO]: Stopping simulation...") |
| 157 | + finally: |
| 158 | + # Clean up resources |
| 159 | + sim.destroy() |
| 160 | + print("[INFO]: Simulation terminated successfully") |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments