-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_data_with_space_mouse.py
291 lines (227 loc) · 8.93 KB
/
collect_data_with_space_mouse.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
"""Teleoperating robot arm with a SpaceMouse to collect demonstration data"""
import argparse
import json
import os
import pickle
import threading
import time
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
# from rpl_vision_utils.networking.camera_redis_interface import \
# CameraRedisSubInterface
from deoxys_vision.networking.camera_redis_interface import CameraRedisSubInterface
from deoxys_vision.camera.rs_interface import RSInterface
from deoxys_vision.utils.img_utils import preprocess_color, preprocess_depth
from deoxys_vision.utils.camera_utils import assert_camera_ref_convention, get_camera_info
from deoxys import config_root
from deoxys.franka_interface import FrankaInterface
from deoxys.utils import YamlConfig
from deoxys.utils.config_utils import robot_config_parse_args
from deoxys.utils.input_utils import input2action
# from deoxys.k4a_interface import K4aInterface
from deoxys.utils.io_devices import SpaceMouse
from deoxys.utils.log_utils import get_deoxys_example_logger
import cv2
from threading import Thread
logger = get_deoxys_example_logger()
import beepy as beep
beep_start = lambda : beep.beep('coin')
beep_end= lambda : beep.beep('ready')
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--interface-cfg", type=str, default="charmander.yml")
parser.add_argument("--folder", type=Path, default="/home/franka_deoxys/data_franka/imgsd_demo/")
parser.add_argument("--controller-type", type=str, default="OSC_POSE")
parser.add_argument(
"--controller-cfg", type=str, default="osc-position-controller.yml"
)
parser.add_argument("--use-depth", action="store_true",
help="increase output verbosity")
parser.add_argument(
"--horizon",
type=int,
default=1000,
)
parser.add_argument(
"--vendor_id",
type=int,
default=9583,
)
parser.add_argument(
"--product_id",
type=int,
default=50734,
)
# robot_config_parse_args(parser)
return parser.parse_args()
def main():
args = parse_args()
use_depth = args.use_depth
horizon = args.horizon
print(f'-----------collecting data with {horizon} max steps')
args.folder.mkdir(parents=True, exist_ok=True)
experiment_id = 0
logger.info(f"Saving to {args.folder}")
# Create a folder that saves the demonstration raw states.
for path in args.folder.glob("run*"):
if not path.is_dir():
continue
try:
folder_id = int(str(path).split("run")[-1])
if folder_id > experiment_id:
experiment_id = folder_id
except BaseException:
pass
experiment_id += 1
folder = str(args.folder / f"run{experiment_id}")
device = SpaceMouse(vendor_id=args.vendor_id, product_id=args.product_id)
device.start_control()
cfg_file = os.path.join(config_root, args.interface_cfg)
print('cfg_file=', cfg_file)
# Franka Interface
robot_interface = FrankaInterface(cfg_file)
camera_ids = [0, 1]
# camera_ids = [0]
cr_interfaces = {}
for camera_id in camera_ids:
camera_ref=f"rs_{camera_id}"
assert_camera_ref_convention(camera_ref)
camera_info = get_camera_info(camera_ref)
print('---------****-----------')
print(camera_info)
print('--------------------------')
# camera_info= {'camera_id': 0, 'camera_type': 'rs', 'camera_name': 'camera_rs_0'}
#
cr_interface = CameraRedisSubInterface(camera_info=camera_info, use_depth=use_depth, redis_host='127.0.0.1')
cr_interface.start()
cr_interfaces[camera_id] = cr_interface
controller_cfg = YamlConfig(
os.path.join(config_root, args.controller_cfg)
).as_easydict()
# demo_file = h5py.File(demo_file_name, "w")
controller_type = args.controller_type
data = {"action": [], "ee_states": [], "joint_states": [], "gripper_states": []}
for camera_id in camera_ids:
data[f"camera_{camera_id}"] = []
data[f"camera_{camera_id}_color"] = []
data[f"camera_{camera_id}_depth"] = []
i = 0
start = False
previous_state_dict = None
# beep_start()
time.sleep(2)
print('------------------------record--------now----------------------')
tb=Thread(target=beep_start)
tb.start()
while i < horizon:
i += 1
start_time = time.time_ns()
action, grasp = input2action(
device=device,
controller_type=controller_type,
)
if action is None:
break
# set unused orientation dims to 0
if controller_type == "OSC_YAW":
action[3:5] = 0.0
elif controller_type == "OSC_POSITION":
action[3:6] = 0.0
robot_interface.control(
controller_type=controller_type,
action=action,
controller_cfg=controller_cfg,
)
if len(robot_interface._state_buffer) == 0:
continue
last_state = robot_interface._state_buffer[-1]
last_gripper_state = robot_interface._gripper_state_buffer[-1]
if np.linalg.norm(action[:-1]) < 1e-3 and not start:
continue
start = True
# print(action.shape)
# Record ee pose, joints
data["action"].append(action)
state_dict = {
"ee_states": np.array(last_state.O_T_EE),
"joint_states": np.array(last_state.q),
"gripper_states": np.array(last_gripper_state.width),
}
if previous_state_dict is not None:
for proprio_key in state_dict.keys():
proprio_state = state_dict[proprio_key]
if np.sum(np.abs(proprio_state)) <= 1e-6:
proprio_state = previous_state_dict[proprio_key]
state_dict[proprio_key] = np.copy(proprio_state)
for proprio_key in state_dict.keys():
data[proprio_key].append(state_dict[proprio_key])
previous_state_dict = state_dict
# data["ee_states"].append(np.array(last_state.O_T_EE))
# joints = np.array(last_state.q)
# if np.sum(np.abs(joints)) < 1e-6:
# print("Joints missing!!!!")
# data["joint_states"].append(np.array(last_state.q))
# data["gripper_states"].append(np.array(last_gripper_state.width))
# Get img info
for camera_id in camera_ids:
img_info = cr_interfaces[camera_id].get_img_info()
data[f"camera_{camera_id}"].append(img_info)
imgs = cr_interfaces[camera_id].get_img()
# print('keys: ', imgs.keys())
color_img = imgs["color"][..., ::-1]
color_img = cv2.resize(color_img, None, fx=0.5, fy=0.5)
data[f"camera_{camera_id}_color"].append(color_img)
if use_depth:
depth_img = imgs["depth"]
depth_img = cv2.resize(depth_img, None, fx=0.5, fy=0.5)
data[f"camera_{camera_id}_depth"].append(depth_img)
# cv2.imshow(f'color_img_{camera_id}', color_img)
# cv2.waitKey(1)
# color_img = cv2.resize(color_img, (224, 224))
# depth_img = cv2.resize(depth_img, (224, 224))
# scale by half.
# TODO: Test if we can directly save img (probably not)
# img = cr_interface.get_img()
end_time = time.time_ns()
print(f"Time profile: {(end_time - start_time) / 10 ** 9} steps: {i}/{horizon}")
# cv2.destroyAllWindows()
os.makedirs(folder, exist_ok=True)
with open(f"{folder}/config.json", "w") as f:
config_dict = {
"controller_cfg": dict(controller_cfg),
"controller_type": controller_type,
}
json.dump(config_dict, f)
np.savez(f"{folder}/testing_demo_action", data=np.array(data["action"]))
np.savez(f"{folder}/testing_demo_ee_states", data=np.array(data["ee_states"]))
np.savez(
f"{folder}/testing_demo_joint_states", data=np.array(data["joint_states"])
)
np.savez(
f"{folder}/testing_demo_gripper_states",
data=np.array(data["gripper_states"]),
)
for camera_id in camera_ids:
np.savez(
f"{folder}/testing_demo_camera_{camera_id}_color",
data=np.array(data[f"camera_{camera_id}_color"]),
)
if use_depth:
np.savez(
f"{folder}/testing_demo_camera_{camera_id}_depth",
data=np.array(data[f"camera_{camera_id}_depth"]),
)
for camera_id in camera_ids:
np.savez(
f"{folder}/testing_demo_camera_{camera_id}",
data=np.array(data[f"camera_{camera_id}"]),
)
cr_interfaces[camera_id].stop()
robot_interface.close()
print("Total length of the trajectory: ", len(data["action"]))
# beep_end()
tb=Thread(target=beep_end)
tb.start()
if __name__ == "__main__":
main()