-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
350 lines (288 loc) · 10.3 KB
/
test.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
import numpy as np
import sys
import time
sys.path.append("..")
sys.path.insert(1, "/home/eeg/deoxys_control/deoxys")
from primitive_skills_noir import PrimitiveSkill
from environments.solo_envs.tablesetting_env import TablesettingEnv
from environments.solo_envs.realrobot_env_general import RealRobotEnvGeneral
from environments.realrobot_env_multi import RealRobotEnvMulti
from deoxys.utils.config_utils import (get_default_controller_config, verify_controller_config)
from deoxys.franka_interface import FrankaInterface
from deoxys.experimental.motion_utils import reset_joints_to
from utils.camera_utils import project_points_from_base_to_camera, get_camera_image
from deoxys.camera_redis_interface import CameraRedisSubInterface
import utils.transformation_utils as T
from utils.detection_utils import DetectionUtils
# from utils.detection_utils_eeg import DetectionUtils
from PIL import Image, ImageDraw, ImageFont, ImageTk
import cv2
import pdb
from getkey import getkey
import json
import tkinter as tk
with open('config/task_obj_skills.json') as json_file:
task_dict = json.load(json_file)
with open('config/skill_config.json') as json_file:
skill_dict = json.load(json_file)
num_skills = len(skill_dict.keys())
camera_interfaces = {
0 : CameraRedisSubInterface(camera_id=0),
1 : CameraRedisSubInterface(camera_id=1),
2 : CameraRedisSubInterface(camera_id=2),
}
for id in camera_interfaces.keys():
camera_interfaces[id].start()
################# TEST MULTI ENV ####################
# env = RealRobotEnvMulti(
# controller_type="OSC_POSE",
# )
# skill_selection_vec = np.zeros(env.skill.num_skills)
# skill_selection_vec[0] = 1
# params = [0.4, 0.0, 0.1]
# action = np.concatenate([skill_selection_vec, params])
# obs, reward, done, info = env.step(action)
################## TEST OBJ 3D POS ESTIMATE ######################
# env = RealRobotEnvGeneral(
# env_name="Sweeping",
# )
# obs = env.reset()
# handle_pos = obs["world_coords"]["red lego block"]
# skill_selection_vec = np.zeros(env.num_skills)
# skill_selection_vec[0] = 1
# params = handle_pos
# action = np.concatenate([skill_selection_vec, params])
# obs, reward, done, info = env.step(action)
################## TEST Z DISCRETIZATION #####################
# # get world xy from top down view
# detection_utils = DetectionUtils()
# pix = (158., 315.)
# # world_xy = detection_utils.get_world_xy_from_topdown_view(pix)
# world_xy = np.array([0.5, 0.0, 0.25])
# print(world_xy)
# camera_id = 1
# detection_utils.get_points_on_z(world_xy=world_xy, camera_interface=camera_interfaces[camera_id], camera_id=camera_id, max_height=0.3)
############### TEST EEG SIDE DETECTION UTILS #################
# detection_utils = DetectionUtils()
# img0 = get_camera_image(camera_interface=camera_interfaces[0])
# img1 = get_camera_image(camera_interface=camera_interfaces[1])
# trim_low = [90, 130]
# trim_high = [450, 370]
# img2 = get_camera_image(camera_interface=camera_interfaces[2])
# img2 = img2[trim_low[1]:trim_high[1], trim_low[0]:trim_high[0]]
# pix0 = detection_utils.get_obj_pixel_coord(
# img_array=img0,
# texts=["red mug"],
# save_filename="testing",
# )
# breakpoint()
# pix1 = detection_utils.get_obj_pixel_coord(
# img_array=img1,
# texts=["red mug"],
# save_filename="testing",
# )
# breakpoint()
# pix2 = detection_utils.get_obj_pixel_coord(
# img_array=img2,
# texts=["red mug"],
# save_filename="testing",
# )
# breakpoint()
# world = detection_utils.get_object_world_coords(
# cam0_img=img0,
# cam1_img=img1,
# texts=["red mug"],
# )
# breakpoint()
# xy = detection_utils.get_world_xy_from_topdown_view(
# pix_coords=(11.0, 35.0),
# img_array=img2,
# )
# breakpoint()
################### CAMERA TEST ########################
# while True:
# raw_image = get_camera_image(camera_interfaces[2])
# rgb_image = raw_image[:,:,::-1] # convert from bgr to rgb
# image = Image.fromarray(np.uint8(rgb_image))
# # Create a draw object
# draw = ImageDraw.Draw(image)
# # Set the grid size and color
# grid_size = 10
# grid_color = (0, 255, 0)
# # Draw the grid
# width, height = image.size
# for x in range(0, width, grid_size):
# draw.line((x, 0, x, height), fill=grid_color)
# for y in range(0, height, grid_size):
# draw.line((0, y, width, y), fill=grid_color)
# # Save the image with the grid
# image.save("image_with_grid.jpg")
# print("saved image")
# time.sleep(0.5)
################### SKILLS WITHOUT ENVIRONMENT ####################
# setup robot interface
controller_type = "OSC_POSE"
controller_config = get_default_controller_config(controller_type)
robot_interface = FrankaInterface(
general_cfg_file="config/charmander.yml",
control_freq=20,
)
# setup skills
skill = PrimitiveSkill(
controller_type=controller_type,
controller_config=controller_config,
robot_interface=robot_interface,
waypoint_height=0.25,
workspace_limits={"x" : (0.35, 0.55), "y" : (-0.15, 0.25), "z" : (0.03, 0.45)},
)
# get one-hot skill selection vector
skill_name = "place_from_diag"
skill_idx = skill_dict[skill_name]["default_idx"]
skill_selection_vec = np.zeros(num_skills)
skill_selection_vec[skill_idx] = 1
params = [0.5, 0.0, 0.2]
# execute action
action = np.concatenate([skill_selection_vec, params])
skill.execute_skill(action)
# skill._wipe_y(params=[0.45, 0.15, 0.2])
# skill._rehome_pos_quat(params=np.concatenate([skill.from_top_reset_eef_pos, skill.from_side_quat, [1, 1]]))
# skill._pick_from_top(params=np.array([0.5, 0.0, 0.2]))
# skill._gripper_action([1])
# skill._rehome(params=np.append(skill.reset_joint_positions["from_top"], 0.0))
# skill._rehome(params=np.append(skill.reset_joint_positions["from_top"], 1.0))
# skill._rehome(params=np.append(skill.reset_joint_positions["from_top"], -1.0))
# skill._move_to(params=np.concatenate([[0.5, 0.0, 0.2], skill.from_top_quat, [1, 0]]))
# skill._reset_joints()
####################### TEST ENVIRONMENT ##########################
# env = RealRobotEnvGeneral(
# env_name="TableSetting",
# controller_type="OSC_POSE"
# )
# env.skill._gripper_action([1])
# move_params = np.concatenate([[0.5, 0.2, 0.2], env.skill.from_top_quat, [1.0, 0.0]])
# env.skill._move_to(params=move_params)
# robot_interface = env.robot_interface
# reset_joint_positions = env.skill.reset_joint_positions
# controller_type = "JOINT_IMPEDANCE"
# controller_cfg = get_default_controller_config(controller_type)
# action = reset_joint_positions + [1.0]
# time.sleep(1)
# while True:
# if len(robot_interface._state_buffer) > 0:
# print(robot_interface._state_buffer[-1].q)
# print(robot_interface._state_buffer[-1].q_d)
# print("-----------------------")
# if (
# np.max(
# np.abs(
# np.array(robot_interface._state_buffer[-1].q)
# - np.array(reset_joint_positions)
# )
# )
# < 1e-3
# ):
# break
# robot_interface.control(controller_type=controller_type, action=action, controller_cfg=controller_cfg)
# breakpoint()
# env.robot_interface.control(
# controller_type="JOINT_IMPEDANCE",
# action=env.skill.reset_joint_positions + [1.0],
# controller_cfg=env.controller_config,
# )
# breakpoint()
# obs = env.reset()
# print("initial obs\n", obs)
# world_coords = obs["world_coords"]
# # pick_pos = [cup_pos[0], cup_pos[1]+0.05, cup_pos[2]]
# skill_selection_vec = np.zeros(env.num_skills)
# skill_selection_vec[env.skill.skills["pick_from_top"]["default_idx"]] = 1
# obs, reward, done, info = env.step(action=np.concatenate([skill_selection_vec, pick_pos]))
# pdb.set_trace()
# reset_joint_positions = [
# 0.09162008114028396,
# -0.19826458111314524,
# -0.01990020486871322,
# -2.4732269941140346,
# -0.01307073642274261,
# 2.30396583422025,
# 0.8480939705504309,
# ]
# env = TablesettingEnv(
# normalized_params=False
# )
# obs = env.reset()
# cup_pos = obs["shiny silver cup"]
# pick_pos = [cup_pos[0], cup_pos[1]+0.05, cup_pos[2]]
# pick_pos = [0.54657, 0.01063, 0.15]
# skill_selection_vec = np.zeros(env.num_skills)
# skill_selection_vec[env.skill.skills["pick_from_top"]["default_idx"]] = 1
# obs, reward, done, info = env.step(action=np.concatenate([skill_selection_vec, pick_pos]))
# pdb.set_trace()
# controller_type = "OSC_POSE"
# robot_interface = FrankaInterface(
# general_cfg_file="config/charmander.yml",
# control_freq=20,
# )
# reset_joints_to(robot_interface, reset_joint_positions)
# quat, pos = robot_interface.last_eef_quat_and_pos
# print("initial pos, quat", pos, quat)
# skills = PrimitiveSkill(
# controller_type=controller_type,
# controller_config=get_default_controller_config(controller_type),
# robot_interface=robot_interface,
# )
# pos1 = [0.46871761, -0.12582393, 0.005]
# place_pos = [0.48755184, 0.13649393, 0.2]
# skills._pick_from_top(params=pos1)
# skills._pick_from_side(params=handle_pos)
# skills._pour_from_side(params=place_pos)
# pdb.set_trace()
# skills._place_from_top(params=place_pos)
# pos = [0.5, -0.1, 0.1]
# while True:
# skills._pick_from_top(pos)
# print("finished executing skill")
# pdb.set_trace()
### Test everything in series ###
# pos = [0.5, 0.0, 0.1]
# skills._reset_joints()
# skills._pick_from_top(pos)
# pdb.set_trace()
# skills._place_from_top(pos)
# pdb.set_trace()
# skills._reset_joints()
# pdb.set_trace()
# skills._pick_from_side(pos)
# pdb.set_trace()
# skills._place_from_side(pos)
# pdb.set_trace()
# skills._reset_joints()
# pdb.set_trace()
# skills._push_z(np.concatenate([pos, [-0.1, 90]]))
# pdb.set_trace()
# skills._push_xy(np.concatenate([pos, [0.05, -0.1, 45]]))
# pdb.set_trace()
# skills._reset_joints()
# pdb.set_trace()
# skills._wipe_xy(np.concatenate([pos, [0.05, -0.1, 0.0]]))
# pdb.set_trace()
# skills._draw_x(pos)
# pdb.set_trace()
# skills._reset_joints()
# pdb.set_trace()
# skills._screw(np.concatenate([pos, [45]]))
# pdb.set_trace()
# skills._reset_joints()
# pdb.set_trace()
# skills._pour_from_top(pos)
# pdb.set_trace()
# skills._pour_from_side(pos)
# pdb.set_trace()
# #### Test draw ####
# skills._pick_from_top([0.5, 0.0, 0.25])
# skills._draw_x([0.5, 0.0, 0.11])
#### Test screw ####
# skills._screw([0.5, 0.0, 0.2, 180])
#### Test pour ####
# skills._pick_from_top(params=[0.5, 0.0, 0.2])
# skills._pour_from_top(params=[0.5, 0.0, 0.2])