File size: 6,421 Bytes
83e0ecd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
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.


Large mass-ratio test
-------------------------------
A test of the simulation stability stacks of boxes with large mass ratio. There are two scenarios available:
- Same size boxes, the next level boxes are 10 times heavier compared to the box it is standing on
- Inverted pyramid of boxes, they have the same density but the next level boxes are 4 times larger and 4*4*4 = 64 heavier than previous level one 
"""

from isaacgym import gymutil
from isaacgym import gymapi
from math import sqrt

# initialize gym
gym = gymapi.acquire_gym()

# parse arguments
args = gymutil.parse_arguments(
    description="Large Mass Ratio Test",
    custom_parameters=[
        {"name": "--num_envs", "type": int, "default": 100, "help": "Number of environments to create"},
        {"name": "--inverted_pyramid_test", "action": "store_true", "help": "Run test with stack of boxes of increasing sizes but the same density"}])

inverted_pyramid_test = args.inverted_pyramid_test

# configure sim
sim_params = gymapi.SimParams()
if args.physics_engine == gymapi.SIM_FLEX:
    sim_params.substeps = 6
    sim_params.flex.num_inner_iterations = 5
    sim_params.flex.num_inner_iterations = 24
    sim_params.flex.relaxation = 0.7
    sim_params.flex.warm_start = 0.25
    displacement = 0.02
elif args.physics_engine is gymapi.SIM_PHYSX:
    sim_params.substeps = 6
    sim_params.physx.solver_type = 1
    sim_params.physx.num_position_iterations = 100
    sim_params.physx.num_velocity_iterations = 0
    sim_params.physx.num_threads = args.num_threads
    sim_params.physx.use_gpu = args.use_gpu
    sim_params.physx.rest_offset = 0.001
    displacement = 0.002

sim_params.use_gpu_pipeline = False
if args.use_gpu_pipeline:
    print("WARNING: Forcing CPU pipeline.")

sim = gym.create_sim(args.compute_device_id, args.graphics_device_id, args.physics_engine, sim_params)

if sim is None:
    print("*** Failed to create sim")
    quit()

# create viewer using the default camera properties
viewer = gym.create_viewer(sim, gymapi.CameraProperties())
if viewer is None:
    raise ValueError('*** Failed to create viewer')

# add ground plane
plane_params = gymapi.PlaneParams()
gym.add_ground(sim, gymapi.PlaneParams())

# set up the env grid
num_envs = args.num_envs
num_per_row = int(sqrt(num_envs))
spacing = 4.5
if inverted_pyramid_test:
    spacing = 12.0

# look at the first env
cam_pos = gymapi.Vec3(8.0, 6.5, 3.0)
cam_target = gymapi.Vec3(10.0, 6.0, 10.0)
if not inverted_pyramid_test:
    cam_pos.y = 3.0
    cam_target.y = 1.9

gym.viewer_camera_look_at(viewer, None, cam_pos, cam_target)

box_color = gymapi.Vec3(0.1, 0.9, 0.05)
env_lower = gymapi.Vec3(-spacing, 0.0, -spacing)
env_upper = gymapi.Vec3(spacing, spacing, spacing)

# create list to mantain environment and asset handles
envs = []
box_handles = []
box_sizes = []
actor_handles = []

# create box assets w/ varying densities (measured in kg/m^3)
box_size = 0.5
box_density = 1

# AssetOptions enables loading assets with different properties
# Properties include density, angularDamping, maxAngularVelocity, linearDamping, maxLinearVelocity etc.
asset_options = gymapi.AssetOptions()
asset_options.thickness = 0.005

if not inverted_pyramid_test:
    stack_height = 5

    for dx in range(stack_height):
        asset_options.density = box_density
        asset_box = gym.create_box(sim, box_size, box_size, box_size, asset_options)
        box_handles.append(asset_box)
        box_density *= 10

    for i in range(num_envs):
        # create env
        env = gym.create_env(sim, env_lower, env_upper, num_per_row)
        envs.append(env)

        box_color = gymapi.Vec3(0.2, 0.75, 0.18)
        color_step = box_color.y / (stack_height - 1)

        # add moving boxes to env
        for b in range(stack_height):
            name = 'box_{}'.format(b)
            actor_handles.append(gym.create_actor(env, box_handles[b], gymapi.Transform(
                p=gymapi.Vec3(0., 0.5 * box_size + (box_size + displacement) * b + 0.001, 0.)), name, i, 0))
            gym.set_rigid_body_color(env, actor_handles[-1], 0, gymapi.MESH_VISUAL, box_color)
            box_color.x += 0.85 * color_step * b
            box_color.y -= color_step * b
            box_color.z -= 0.04 * b
else:
    stack_height = 5
    scale_factor = 2
    color_step = box_color.x / (stack_height - 1)

    new_box_size = box_size
    for dx in range(stack_height):
        asset_options.density = 100
        asset_box = gym.create_box(sim, new_box_size, new_box_size, new_box_size, asset_options)
        box_handles.append(asset_box)
        box_sizes.append(new_box_size)
        new_box_size *= scale_factor

    print('Creating %d environments' % num_envs)
    for i in range(num_envs):
        # create env
        env = gym.create_env(sim, env_lower, env_upper, num_per_row)
        envs.append(env)

        start_height = 0.001

        box_color = gymapi.Vec3(0.9, 0.25, 0.15)
        color_step = box_color.x / (stack_height - 1)

        # add moving boxes to env
        for b in range(stack_height):
            name = 'box_{}'.format(b)
            start_height += (0.5 * box_sizes[b] + displacement)
            actor_handles.append(gym.create_actor(env, box_handles[b], gymapi.Transform(p=gymapi.Vec3(0., start_height, 0.)), name, i, 0))

            box_color.x -= 0.9 * color_step * b
            box_color.y -= 0.02 * color_step * b
            box_color.z += 0.8 * color_step * b
            gym.set_rigid_body_color(env, actor_handles[-1], 0, gymapi.MESH_VISUAL, box_color)

            start_height += 0.5 * box_sizes[b]

while not gym.query_viewer_has_closed(viewer):

    # step the physics
    gym.simulate(sim)
    gym.fetch_results(sim, True)

    # update the viewer
    gym.step_graphics(sim)
    gym.draw_viewer(viewer, sim, True)

    # Wait for dt to elapse in real time.
    # This synchronizes the physics simulation with the rendering rate.
    gym.sync_frame_time(sim)

print('Done')

gym.destroy_viewer(viewer)
gym.destroy_sim(sim)