| """ |
| Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. |
| |
| NVIDIA CORPORATION and its licensors retain all intellectual property |
| and proprietary rights in and to this software, related documentation |
| and any modifications thereto. Any use, reproduction, disclosure or |
| distribution of this software and related documentation without an express |
| license agreement from NVIDIA CORPORATION is strictly prohibited. |
| |
| |
| Apply Forces (apply_forces.py) |
| ---------------------------- |
| This example shows how to apply forces and torques to rigid bodies using the tensor API. |
| """ |
|
|
| from isaacgym import gymutil |
| from isaacgym import gymapi |
| from isaacgym import gymtorch |
|
|
| import numpy as np |
| import torch |
|
|
| |
| gym = gymapi.acquire_gym() |
|
|
| |
| args = gymutil.parse_arguments( |
| description="Example of applying forces and torques to bodies") |
|
|
| |
| sim_params = gymapi.SimParams() |
| sim_params.up_axis = gymapi.UP_AXIS_Z |
| sim_params.gravity = gymapi.Vec3(0.0, 0.0, -9.81) |
| if args.physics_engine == gymapi.SIM_PHYSX: |
| sim_params.substeps = 1 |
| sim_params.physx.solver_type = 1 |
| sim_params.physx.num_position_iterations = 4 |
| sim_params.physx.num_velocity_iterations = 1 |
| sim_params.physx.num_threads = args.num_threads |
| sim_params.physx.use_gpu = args.use_gpu |
| elif args.physics_engine == gymapi.SIM_FLEX and not args.use_gpu_pipeline: |
| sim_params.flex.shape_collision_margin = 0.25 |
| sim_params.flex.num_outer_iterations = 4 |
| sim_params.flex.num_inner_iterations = 10 |
| else: |
| raise Exception("GPU pipeline is only available with PhysX") |
|
|
| sim_params.use_gpu_pipeline = args.use_gpu_pipeline |
| device = args.sim_device if args.use_gpu_pipeline else 'cpu' |
|
|
| sim = gym.create_sim(args.compute_device_id, args.graphics_device_id, args.physics_engine, sim_params) |
| if sim is None: |
| raise Exception("Failed to create sim") |
|
|
| |
| plane_params = gymapi.PlaneParams() |
| plane_params.normal = gymapi.Vec3(0, 0, 1) |
| gym.add_ground(sim, plane_params) |
|
|
| |
| viewer = gym.create_viewer(sim, gymapi.CameraProperties()) |
| if viewer is None: |
| raise Exception("Failed to create viewer") |
|
|
| |
| asset_root = "../../assets" |
| asset_file = "mjcf/nv_ant.xml" |
| asset = gym.load_asset(sim, asset_root, asset_file, gymapi.AssetOptions()) |
|
|
| num_bodies = gym.get_asset_rigid_body_count(asset) |
| print('num_bodies', num_bodies) |
|
|
| |
| pose = gymapi.Transform() |
| pose.p.z = 1.0 |
|
|
| |
| num_envs = 4 |
| num_per_row = int(np.sqrt(num_envs)) |
| env_spacing = 2.0 |
| env_lower = gymapi.Vec3(-env_spacing, -env_spacing, 0.0) |
| env_upper = gymapi.Vec3(env_spacing, env_spacing, env_spacing) |
|
|
| |
| np.random.seed(17) |
|
|
| envs = [] |
| handles = [] |
| for i in range(num_envs): |
| |
| env = gym.create_env(sim, env_lower, env_upper, num_per_row) |
| envs.append(env) |
|
|
| |
| c = 0.5 + 0.5 * np.random.random(3) |
| color = gymapi.Vec3(c[0], c[1], c[2]) |
|
|
| ahandle = gym.create_actor(env, asset, pose, "actor", i, 1) |
| handles.append(ahandle) |
| gym.set_rigid_body_color(env, ahandle, 0, gymapi.MESH_VISUAL_AND_COLLISION, color) |
|
|
| gym.viewer_camera_look_at(viewer, None, gymapi.Vec3(20, 20, 5), gymapi.Vec3(0, 0, 1)) |
|
|
| gym.prepare_sim(sim) |
|
|
| torque_amt = 100 |
|
|
| frame_count = 0 |
| while not gym.query_viewer_has_closed(viewer): |
|
|
| if (frame_count - 99) % 200 == 0: |
| |
| forces = torch.zeros((num_envs, num_bodies, 3), device=device, dtype=torch.float) |
| torques = torch.zeros((num_envs, num_bodies, 3), device=device, dtype=torch.float) |
| forces[:, 0, 2] = 300 |
| torques[:, 0, 2] = torque_amt |
| gym.apply_rigid_body_force_tensors(sim, gymtorch.unwrap_tensor(forces), gymtorch.unwrap_tensor(torques), gymapi.ENV_SPACE) |
|
|
| torque_amt = -torque_amt |
|
|
| |
| gym.simulate(sim) |
| gym.fetch_results(sim, True) |
|
|
| |
| gym.step_graphics(sim) |
| gym.draw_viewer(viewer, sim, True) |
|
|
| |
| |
| gym.sync_frame_time(sim) |
|
|
| frame_count += 1 |
|
|
| gym.destroy_viewer(viewer) |
| gym.destroy_sim(sim) |
|
|