forked from PKU-MARL/DexterousHands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadow_full.py
306 lines (239 loc) · 9.56 KB
/
shadow_full.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
Copyright (c) 2022, 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.
Spherical Joint
------------
- Demonstrates usage of spherical joints.
"""
import math
import numpy as np
from isaacgym import gymapi, gymutil
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import numpy as np
bridge = CvBridge()
import time
from std_msgs.msg import Float32MultiArray
def clamp(x, min_value, max_value):
return max(min(x, max_value), min_value)
# simple asset descriptor for selecting from a list
class AssetDesc:
def __init__(self, file_name, flip_visual_attachments=False):
self.file_name = file_name
self.flip_visual_attachments = flip_visual_attachments
asset_descriptors = [
# AssetDesc("urdf/spherical_joint.urdf", False),
# AssetDesc("mjcf/spherical_joint.xml", False),
# AssetDesc("mjcf/open_ai_assets/hand/shadow_hand.xml", False),
# AssetDesc("urdf/shadow_hand_description/shadowhand.urdf", False), # okay to use
# AssetDesc("urdf/shadow_hand_description/shadow_hand_right.urdf", False) #Nope
# AssetDesc("urdf/shadow_hand_description/shadowhand_with_fingertips.urdf", False), # okay to use
AssetDesc("mjcf/open_ai_assets/hand/shadow_test.xml", False),
]
args = gymutil.parse_arguments(
description="Shadwhand: Show example of controlling a shadow hand robot.",
)
# initialize gym
gym = gymapi.acquire_gym()
# configure sim
sim_params = gymapi.SimParams()
sim_params.dt = dt = 1.0 / 60.0
sim_params.gravity = gymapi.Vec3(0, 0, 0)
sim_params.up_axis = gymapi.UP_AXIS_Z
if args.physics_engine == gymapi.SIM_FLEX:
pass
elif args.physics_engine == gymapi.SIM_PHYSX:
sim_params.physx.solver_type = 1
sim_params.physx.num_position_iterations = 6
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.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
viewer = gym.create_viewer(sim, gymapi.CameraProperties())
if viewer is None:
print("*** Failed to create viewer")
quit()
# load asset
asset_root = "./assets"
asset_file = asset_descriptors[0].file_name
asset_options = gymapi.AssetOptions()
asset_options.fix_base_link = True
asset_options.flip_visual_attachments = asset_descriptors[0].flip_visual_attachments
asset_options.use_mesh_materials = True
print("Loading asset '%s' from '%s'" % (asset_file, asset_root))
asset = gym.load_asset(sim, asset_root, asset_file, asset_options)
# get array of DOF names
dof_names = gym.get_asset_dof_names(asset)
# get array of DOF properties
dof_props = gym.get_asset_dof_properties(asset)
# create an array of DOF states that will be used to update the actors
num_dofs = gym.get_asset_dof_count(asset)
dof_states = np.zeros(num_dofs, dtype=gymapi.DofState.dtype)
# get list of DOF types
dof_types = [gym.get_asset_dof_type(asset, i) for i in range(num_dofs)]
# get the position slice of the DOF state array
dof_positions = dof_states['pos']
# get the limit-related slices of the DOF properties array
stiffnesses = dof_props['stiffness']
dampings = dof_props['damping']
armatures = dof_props['armature']
has_limits = dof_props['hasLimits']
lower_limits = dof_props['lower']
upper_limits = dof_props['upper']
# initialize default positions, limits, and speeds (make sure they are in reasonable ranges)
defaults = np.zeros(num_dofs)
for i in range(num_dofs):
if has_limits[i]:
if dof_types[i] == gymapi.DOF_ROTATION:
lower_limits[i] = clamp(lower_limits[i], -math.pi, math.pi)
upper_limits[i] = clamp(upper_limits[i], -math.pi, math.pi)
# make sure our default position is in range
if lower_limits[i] > 0.0:
defaults[i] = lower_limits[i]
elif upper_limits[i] < 0.0:
defaults[i] = upper_limits[i]
else:
# set reasonable animation limits for unlimited joints
if dof_types[i] == gymapi.DOF_ROTATION:
# unlimited revolute joint
lower_limits[i] = -math.pi
upper_limits[i] = math.pi
elif dof_types[i] == gymapi.DOF_TRANSLATION:
# unlimited prismatic joint
lower_limits[i] = -1.0
upper_limits[i] = 1.0
else:
print("Unknown DOF type!")
exit()
# set DOF position to default
dof_positions[i] = defaults[i]
# Print DOF properties
for i in range(num_dofs):
print("DOF %d" % i)
print(" Name: '%s'" % dof_names[i])
print(" Type: %s" % gym.get_dof_type_string(dof_types[i]))
print(" Stiffness: %r" % stiffnesses[i])
print(" Damping: %r" % dampings[i])
print(" Armature: %r" % armatures[i])
print(" Limited? %r" % has_limits[i])
if has_limits[i]:
print(" Lower %f" % lower_limits[i])
print(" Upper %f" % upper_limits[i])
# set up the env grid
num_envs = 1
num_per_row = 6
spacing = 2.5
env_lower = gymapi.Vec3(-spacing, 0.0, -spacing)
env_upper = gymapi.Vec3(spacing, spacing, spacing)
# position the camera
# cam_pos = gymapi.Vec3(-0.8, -0.1, 0.2)
cam_pos = gymapi.Vec3(0.440, 0.256 , 0.629)
cam_target = gymapi.Vec3(0.0, 0.0, 0.0)
gym.viewer_camera_look_at(viewer, None, cam_pos, cam_target)
# cache useful handles
envs = []
actor_handles = []
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)
# add actor
pose = gymapi.Transform()
pose.p = gymapi.Vec3(0.0, 0.0, 0.0)
# pose.r = gymapi.Quat(-0.707107, 0.0, 0.0, 0.707107)
actor_handle = gym.create_actor(env, asset, pose, "actor", i, 1)
actor_handles.append(actor_handle)
props = gym.get_actor_dof_properties(env, actor_handle)
props["driveMode"] = (
gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS,
gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS,
gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS,
gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS,
gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS, gymapi.DOF_MODE_POS
)
props["stiffness"] = (
1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0
)
Tval = 0.1
Rval = 0.5
props["damping"] = (
Tval, Tval, Tval, Rval, Rval, Rval,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1
)
gym.set_actor_dof_properties(env, actor_handle, props)
# set default DOF positions
gym.set_actor_dof_states(env, actor_handle, dof_states, gymapi.STATE_ALL)
def random_quaternion():
"""Random quaternion of the form (x, y, z, w).
Returns:
np.ndarray: 4-element array.
"""
r1, r2, r3 = np.random.random(3)
q1 = math.sqrt(1.0 - r1) * (math.sin(2 * math.pi * r2))
q2 = math.sqrt(1.0 - r1) * (math.cos(2 * math.pi * r2))
q3 = math.sqrt(r1) * (math.sin(2 * math.pi * r3))
q4 = math.sqrt(r1) * (math.cos(2 * math.pi * r3))
quat_xyzw = np.array([q2, q3, q4, q1])
if quat_xyzw[-1] < 0:
quat_xyzw = -quat_xyzw
return quat_xyzw
def quat2expcoord(q):
"""Converts quaternion to exponential coordinates.
Args:
q (np.ndarray): Quaternion as a 4-element array of the form [x, y, z, w].
Returns:
np.ndarray: Exponential coordinate as 3-element array.
"""
if (q[-1] < 0):
q = -q
theta = 2. * math.atan2(np.linalg.norm(q[:-1]), q[-1])
w = (1. / np.sin(theta/2.0)) * q[:-1]
return w * theta
# Helper visualization for goal orientation
axes_geom = gymutil.AxesGeometry(0.5)
cnt = 0
while not gym.query_viewer_has_closed(viewer):
# step the physics
gym.simulate(sim)
gym.fetch_results(sim, True)
# Set new goal orientation
if cnt % 1000000000 == 0:
goal_quat = np.array([0.0, 0.0, 0.0, 1.0])
#print("New goal orientation:", goal_quat)
gym.clear_lines(viewer)
goal_viz_T = gymapi.Transform(r=gymapi.Quat(*goal_quat))
gymutil.draw_lines(axes_geom, gym, viewer, env, goal_viz_T)
dof_positions[:] = 0.0
#dof_positions[3:] = quat2expcoord(goal_quat)
for i in range(num_envs):
gym.set_actor_dof_position_targets(envs[i], actor_handles[i], dof_positions)
# 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)
cnt += 1
print("Done")
gym.destroy_viewer(viewer)
gym.destroy_sim(sim)