(model_path: str, args: SimulationArgs)
| 251 | |
| 252 | |
| 253 | def runMujocoXML(model_path: str, args: SimulationArgs): |
| 254 | if not mujoco_imported: |
| 255 | print("Can't run, need to install mujoco") |
| 256 | if model_path.endswith(".xml"): |
| 257 | m = mujoco.MjModel.from_xml_path(model_path) |
| 258 | else: |
| 259 | m = mlr(model_path) |
| 260 | m.opt.cone = 1 # Elliptic |
| 261 | m.opt.solver = 2 # Newton |
| 262 | m.opt.timestep = args.dt |
| 263 | m.opt.iterations = args.maxit |
| 264 | m.opt.tolerance = args.tol |
| 265 | m.opt.ls_iterations = 50 |
| 266 | m.opt.ls_tolerance = 1e-2 |
| 267 | d = mujoco.MjData(m) |
| 268 | d.qpos = m.qpos0 |
| 269 | q0 = d.qpos.copy() |
| 270 | v0 = d.qvel.copy() |
| 271 | a0 = d.qacc.copy() |
| 272 | |
| 273 | print(f"{q0=}") |
| 274 | print(f"{v0=}") |
| 275 | |
| 276 | step_timings = np.zeros(args.horizon) |
| 277 | for t in range(args.horizon): |
| 278 | start_time = time.time() |
| 279 | mujoco.mj_step(m, d) |
| 280 | end_time = time.time() |
| 281 | step_timings[t] = end_time - start_time |
| 282 | printSimulationPerfStats(step_timings) |
| 283 | |
| 284 | d.qpos = q0 |
| 285 | d.qvel = v0 |
| 286 | d.qacc = a0 |
| 287 | show_ui = False |
| 288 | display_contacts = False |
| 289 | with mujoco.viewer.launch_passive( |
| 290 | m, d, show_left_ui=show_ui, show_right_ui=show_ui |
| 291 | ) as viewer: |
| 292 | input("[Press enter to display trajectory]") |
| 293 | while True: |
| 294 | d.qpos = q0 |
| 295 | d.qvel = v0 |
| 296 | d.qacc = a0 |
| 297 | for t in range(args.horizon): |
| 298 | step_start = time.time() |
| 299 | # mj_step can be replaced with code that also evaluates |
| 300 | # a policy and applies a control signal before stepping the physics. |
| 301 | mujoco.mj_step(m, d) |
| 302 | |
| 303 | # Example modification of a viewer option: toggle contact points every two seconds. |
| 304 | if display_contacts: |
| 305 | with viewer.lock(): |
| 306 | viewer.opt.flags[mujoco.mjtVisFlag.mjVIS_CONSTRAINT] = ( |
| 307 | 1 # int(d.time % 2) |
| 308 | ) |
| 309 | viewer.opt.flags[mujoco.mjtVisFlag.mjVIS_CONTACTPOINT] = ( |
| 310 | 1 # int(d.time % 2) |
no test coverage detected