forked from MarkFzp/mobile-aloha
-
Notifications
You must be signed in to change notification settings - Fork 6
/
record_episodes.py
executable file
·418 lines (372 loc) · 14 KB
/
record_episodes.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env python3
import argparse
import os
import time
from aloha.constants import (
DT,
FOLLOWER_GRIPPER_JOINT_CLOSE,
FOLLOWER_GRIPPER_JOINT_OPEN,
FPS,
IS_MOBILE,
LEADER_GRIPPER_CLOSE_THRESH,
LEADER_GRIPPER_JOINT_MID,
START_ARM_POSE,
TASK_CONFIGS,
)
from aloha.real_env import (
get_action,
make_real_env
)
from aloha.robot_utils import (
disable_gravity_compensation,
enable_gravity_compensation,
get_arm_gripper_positions,
ImageRecorder,
move_arms,
move_grippers,
Recorder,
torque_off,
torque_on,
)
import cv2
import h5py
from interbotix_common_modules.common_robot.robot import (
create_interbotix_global_node,
robot_shutdown,
robot_startup,
)
from interbotix_xs_modules.xs_robot.arm import InterbotixManipulatorXS
import numpy as np
import rclpy
from tqdm import tqdm
def opening_ceremony(
leader_bot_left: InterbotixManipulatorXS,
leader_bot_right: InterbotixManipulatorXS,
follower_bot_left: InterbotixManipulatorXS,
follower_bot_right: InterbotixManipulatorXS,
gravity_compensation: bool = False,
):
"""Move all 4 robots to a pose where it is easy to start demonstration."""
# reboot gripper motors, and set operating modes for all motors
follower_bot_left.core.robot_reboot_motors('single', 'gripper', True)
follower_bot_left.core.robot_set_operating_modes('group', 'arm', 'position')
follower_bot_left.core.robot_set_operating_modes('single', 'gripper', 'current_based_position')
leader_bot_left.core.robot_set_operating_modes('group', 'arm', 'position')
leader_bot_left.core.robot_set_operating_modes('single', 'gripper', 'position')
follower_bot_left.core.robot_set_motor_registers('single', 'gripper', 'current_limit', 300)
follower_bot_right.core.robot_reboot_motors('single', 'gripper', True)
follower_bot_right.core.robot_set_operating_modes('group', 'arm', 'position')
follower_bot_right.core.robot_set_operating_modes(
'single', 'gripper', 'current_based_position'
)
leader_bot_right.core.robot_set_operating_modes('group', 'arm', 'position')
leader_bot_right.core.robot_set_operating_modes('single', 'gripper', 'position')
follower_bot_left.core.robot_set_motor_registers('single', 'gripper', 'current_limit', 300)
torque_on(follower_bot_left)
torque_on(leader_bot_left)
torque_on(follower_bot_right)
torque_on(leader_bot_right)
# move arms to starting position
start_arm_qpos = START_ARM_POSE[:6]
move_arms(
[leader_bot_left, follower_bot_left, leader_bot_right, follower_bot_right],
[start_arm_qpos] * 4,
moving_time=1.5,
)
# move grippers to starting position
move_grippers(
[leader_bot_left, follower_bot_left, leader_bot_right, follower_bot_right],
[LEADER_GRIPPER_JOINT_MID, FOLLOWER_GRIPPER_JOINT_CLOSE] * 2,
moving_time=0.5,
)
# press gripper to start data collection
# disable torque for only gripper joint of leader robot to allow user movement
leader_bot_left.core.robot_torque_enable('single', 'gripper', False)
leader_bot_right.core.robot_torque_enable('single', 'gripper', False)
print('Close the gripper to start')
pressed = False
while rclpy.ok() and not pressed:
gripper_pos_left = get_arm_gripper_positions(leader_bot_left)
gripper_pos_right = get_arm_gripper_positions(leader_bot_right)
pressed = (
(gripper_pos_left < LEADER_GRIPPER_CLOSE_THRESH) and
(gripper_pos_right < LEADER_GRIPPER_CLOSE_THRESH)
)
time.sleep(DT/10)
if gravity_compensation:
enable_gravity_compensation(leader_bot_left)
enable_gravity_compensation(leader_bot_right)
else:
torque_off(leader_bot_left)
torque_off(leader_bot_right)
print('Started!')
def capture_one_episode(
dt,
max_timesteps,
camera_names,
dataset_dir,
dataset_name,
overwrite,
torque_base: bool = False,
gravity_compensation: bool = False,
):
print(f'Dataset name: {dataset_name}')
node = create_interbotix_global_node('aloha')
# source of data
leader_bot_left = InterbotixManipulatorXS(
robot_model='wx250s',
robot_name='leader_left',
node=node,
iterative_update_fk=False,
)
leader_bot_right = InterbotixManipulatorXS(
robot_model='wx250s',
robot_name='leader_right',
node=node,
iterative_update_fk=False,
)
env = make_real_env(
node=node,
setup_robots=False,
setup_base=IS_MOBILE,
torque_base=torque_base,
)
robot_startup(node)
# saving dataset
if not os.path.isdir(dataset_dir):
os.makedirs(dataset_dir)
dataset_path = os.path.join(dataset_dir, dataset_name)
if os.path.isfile(dataset_path) and not overwrite:
print(f'Dataset already exist at \n{dataset_path}\nHint: set overwrite to True.')
exit()
# move all 4 robots to a starting pose where it is easy to start teleoperation, then wait till
# both gripper closed
opening_ceremony(
leader_bot_left,
leader_bot_right,
env.follower_bot_left,
env.follower_bot_right,
gravity_compensation=gravity_compensation,
)
# Data collection
ts = env.reset(fake=True)
timesteps = [ts]
actions = []
actual_dt_history = []
time0 = time.time()
DT = 1 / FPS
for t in tqdm(range(max_timesteps)):
t0 = time.time()
action = get_action(leader_bot_left, leader_bot_right)
t1 = time.time()
ts = env.step(action, get_base_vel=IS_MOBILE)
t2 = time.time()
timesteps.append(ts)
actions.append(action)
actual_dt_history.append([t0, t1, t2])
time.sleep(max(0, DT - (time.time() - t0)))
print(f'Avg fps: {max_timesteps / (time.time() - time0)}')
# End the teleoperation
if gravity_compensation:
disable_gravity_compensation(leader_bot_left)
disable_gravity_compensation(leader_bot_right)
else:
torque_on(leader_bot_left)
torque_on(leader_bot_right)
# Open follower grippers
env.follower_bot_left.core.robot_set_operating_modes('single', 'gripper', 'position')
env.follower_bot_right.core.robot_set_operating_modes('single', 'gripper', 'position')
move_grippers(
[env.follower_bot_left, env.follower_bot_right],
[FOLLOWER_GRIPPER_JOINT_OPEN] * 2,
moving_time=0.5
)
freq_mean = print_dt_diagnosis(actual_dt_history)
if freq_mean < 30:
print(f'\n\nfreq_mean is {freq_mean}, lower than 30, re-collecting... \n\n\n\n')
return False
"""
For each timestep:
observations
- images
- cam_high (480, 640, 3) 'uint8'
- cam_low (480, 640, 3) 'uint8' (on Stationary)
- cam_left_wrist (480, 640, 3) 'uint8'
- cam_right_wrist (480, 640, 3) 'uint8'
- qpos (14,) 'float64'
- qvel (14,) 'float64'
action (14,) 'float64'
base_action (2,) 'float64' (on Mobile)
"""
data_dict = {
'/observations/qpos': [],
'/observations/qvel': [],
'/observations/effort': [],
'/action': [],
}
if IS_MOBILE:
data_dict['/base_action'] = []
for cam_name in camera_names:
data_dict[f'/observations/images/{cam_name}'] = []
# len(action): max_timesteps, len(time_steps): max_timesteps + 1
while actions:
action = actions.pop(0)
ts = timesteps.pop(0)
data_dict['/observations/qpos'].append(ts.observation['qpos'])
data_dict['/observations/qvel'].append(ts.observation['qvel'])
data_dict['/observations/effort'].append(ts.observation['effort'])
data_dict['/action'].append(action)
if IS_MOBILE:
data_dict['/base_action'].append(ts.observation['base_vel'])
for cam_name in camera_names:
data_dict[f'/observations/images/{cam_name}'].append(
ts.observation['images'][cam_name]
)
COMPRESS = True
if COMPRESS:
# JPEG compression
t0 = time.time()
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50] # tried as low as 20, seems fine
compressed_len = []
for cam_name in camera_names:
image_list = data_dict[f'/observations/images/{cam_name}']
compressed_list = []
compressed_len.append([])
for image in image_list:
# 0.02 sec # cv2.imdecode(encoded_image, 1)
result, encoded_image = cv2.imencode('.jpg', image, encode_param)
compressed_list.append(encoded_image)
compressed_len[-1].append(len(encoded_image))
data_dict[f'/observations/images/{cam_name}'] = compressed_list
print(f'compression: {time.time() - t0:.2f}s')
# pad so it has same length
t0 = time.time()
compressed_len = np.array(compressed_len)
padded_size = compressed_len.max()
for cam_name in camera_names:
compressed_image_list = data_dict[f'/observations/images/{cam_name}']
padded_compressed_image_list = []
for compressed_image in compressed_image_list:
padded_compressed_image = np.zeros(padded_size, dtype='uint8')
image_len = len(compressed_image)
padded_compressed_image[:image_len] = compressed_image
padded_compressed_image_list.append(padded_compressed_image)
data_dict[f'/observations/images/{cam_name}'] = padded_compressed_image_list
print(f'padding: {time.time() - t0:.2f}s')
# HDF5
t0 = time.time()
with h5py.File(dataset_path + '.hdf5', 'w', rdcc_nbytes=1024**2*2) as root:
root.attrs['sim'] = False
root.attrs['compress'] = COMPRESS
obs = root.create_group('observations')
image = obs.create_group('images')
for cam_name in camera_names:
if COMPRESS:
_ = image.create_dataset(cam_name, (max_timesteps, padded_size), dtype='uint8',
chunks=(1, padded_size), )
else:
_ = image.create_dataset(cam_name, (max_timesteps, 480, 640, 3), dtype='uint8',
chunks=(1, 480, 640, 3), )
_ = obs.create_dataset('qpos', (max_timesteps, 14))
_ = obs.create_dataset('qvel', (max_timesteps, 14))
_ = obs.create_dataset('effort', (max_timesteps, 14))
_ = root.create_dataset('action', (max_timesteps, 14))
if IS_MOBILE:
_ = root.create_dataset('base_action', (max_timesteps, 2))
for name, array in data_dict.items():
root[name][...] = array
if COMPRESS:
_ = root.create_dataset('compress_len', (len(camera_names), max_timesteps))
root['/compress_len'][...] = compressed_len
print(f'Saving: {time.time() - t0:.1f} secs')
robot_shutdown()
return True
def main(args: dict):
task_config = TASK_CONFIGS[args['task_name']]
dataset_dir = task_config['dataset_dir']
max_timesteps = task_config['episode_len']
camera_names = task_config['camera_names']
torque_base = args.get('enable_base_torque', False)
gravity_compensation = args.get('gravity_compensation', False)
if args['episode_idx'] is not None:
episode_idx = args['episode_idx']
else:
episode_idx = get_auto_index(dataset_dir)
overwrite = True
dataset_name = f'episode_{episode_idx}'
print(dataset_name + '\n')
while True:
is_healthy = capture_one_episode(
DT,
max_timesteps,
camera_names,
dataset_dir,
dataset_name,
overwrite,
torque_base,
gravity_compensation,
)
if is_healthy:
break
def get_auto_index(dataset_dir, dataset_name_prefix='', data_suffix='hdf5'):
max_idx = 1000
if not os.path.isdir(dataset_dir):
os.makedirs(dataset_dir)
for i in range(max_idx+1):
if not os.path.isfile(
os.path.join(dataset_dir, f'{dataset_name_prefix}episode_{i}.{data_suffix}')
):
return i
raise Exception(f'Error getting auto index, or more than {max_idx} episodes')
def print_dt_diagnosis(actual_dt_history):
actual_dt_history = np.array(actual_dt_history)
get_action_time = actual_dt_history[:, 1] - actual_dt_history[:, 0]
step_env_time = actual_dt_history[:, 2] - actual_dt_history[:, 1]
total_time = actual_dt_history[:, 2] - actual_dt_history[:, 0]
dt_mean = np.mean(total_time)
# dt_std = np.std(total_time)
freq_mean = 1 / dt_mean
print((
f'Avg freq: {freq_mean:.2f} Get action: {np.mean(get_action_time):.3f} '
f'Step env: {np.mean(step_env_time):.3f}')
)
return freq_mean
def debug():
print('====== Debug mode ======')
recorder = Recorder('right', is_debug=True)
image_recorder = ImageRecorder(init_node=False, is_debug=True)
while True:
time.sleep(1)
recorder.print_diagnostics()
image_recorder.print_diagnostics()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--task_name',
action='store',
type=str,
help='Task name.',
required=True,
)
parser.add_argument(
'--episode_idx',
action='store',
type=int,
help='Episode index.',
default=None,
required=False,
)
parser.add_argument(
'-b', '--enable_base_torque',
action='store_true',
help=(
'If set, mobile base will be torqued on during episode recording, allowing the use of'
' a joystick controller or some other manual method.'
),
)
parser.add_argument(
'-g', '--gravity_compensation',
action='store_true',
help='If set, gravity compensation will be enabled for the leader robots when teleop starts.',
)
main(vars(parser.parse_args()))