Runs the simulation loop for a single motion.
(sim: sim_utils.SimulationContext, scene: InteractiveScene, joint_names: list[str], motion_data: dict, motion_name: str)
| 222 | |
| 223 | |
| 224 | def run_simulator(sim: sim_utils.SimulationContext, scene: InteractiveScene, joint_names: list[str], motion_data: dict, motion_name: str): |
| 225 | """Runs the simulation loop for a single motion.""" |
| 226 | # Load motion |
| 227 | motion = PKLMotionLoader( |
| 228 | motion_data=motion_data, |
| 229 | input_fps=args_cli.input_fps, |
| 230 | output_fps=args_cli.output_fps, |
| 231 | device=sim.device, |
| 232 | ) |
| 233 | |
| 234 | # Extract scene entities |
| 235 | robot = scene["robot"] |
| 236 | robot_joint_indexes = robot.find_joints(joint_names, preserve_order=True)[0] |
| 237 | |
| 238 | # ------- data logger ------------------------------------------------------- |
| 239 | log = { |
| 240 | "fps": [args_cli.output_fps], |
| 241 | "joint_pos": [], |
| 242 | "joint_vel": [], |
| 243 | "body_pos_w": [], |
| 244 | "body_quat_w": [], |
| 245 | "body_lin_vel_w": [], |
| 246 | "body_ang_vel_w": [], |
| 247 | } |
| 248 | file_saved = False |
| 249 | # -------------------------------------------------------------------------- |
| 250 | |
| 251 | # Simulation loop |
| 252 | while simulation_app.is_running(): |
| 253 | ( |
| 254 | ( |
| 255 | motion_base_pos, |
| 256 | motion_base_rot, |
| 257 | motion_base_lin_vel, |
| 258 | motion_base_ang_vel, |
| 259 | motion_dof_pos, |
| 260 | motion_dof_vel, |
| 261 | ), |
| 262 | reset_flag, |
| 263 | ) = motion.get_next_state() |
| 264 | |
| 265 | # set root state |
| 266 | root_states = robot.data.default_root_state.clone() |
| 267 | root_states[:, :3] = motion_base_pos |
| 268 | root_states[:, :2] += scene.env_origins[:, :2] |
| 269 | root_states[:, 3:7] = motion_base_rot |
| 270 | root_states[:, 7:10] = motion_base_lin_vel |
| 271 | root_states[:, 10:] = motion_base_ang_vel |
| 272 | robot.write_root_state_to_sim(root_states) |
| 273 | |
| 274 | # set joint state |
| 275 | joint_pos = robot.data.default_joint_pos.clone() |
| 276 | joint_vel = robot.data.default_joint_vel.clone() |
| 277 | joint_pos[:, robot_joint_indexes] = motion_dof_pos |
| 278 | joint_vel[:, robot_joint_indexes] = motion_dof_vel |
| 279 | robot.write_joint_state_to_sim(joint_pos, joint_vel) |
| 280 | sim.render() # We don't want physic (sim.step()) |
| 281 | scene.update(sim.get_physics_dt()) |
no test coverage detected