-
Notifications
You must be signed in to change notification settings - Fork 53
/
main.py
382 lines (362 loc) · 19.5 KB
/
main.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
import torch
import numpy as np
import json
import os
import argparse
from environment import ReKepOGEnv
from keypoint_proposal import KeypointProposer
from constraint_generation import ConstraintGenerator
from ik_solver import IKSolver
from subgoal_solver import SubgoalSolver
from path_solver import PathSolver
from visualizer import Visualizer
import transform_utils as T
from omnigibson.robots.fetch import Fetch
from utils import (
bcolors,
get_config,
load_functions_from_txt,
get_linear_interpolation_steps,
spline_interpolate_poses,
get_callable_grasping_cost_fn,
print_opt_debug_dict,
)
class Main:
def __init__(self, scene_file, visualize=False):
global_config = get_config(config_path="./configs/config.yaml")
self.config = global_config['main']
self.bounds_min = np.array(self.config['bounds_min'])
self.bounds_max = np.array(self.config['bounds_max'])
self.visualize = visualize
# set random seed
np.random.seed(self.config['seed'])
torch.manual_seed(self.config['seed'])
torch.cuda.manual_seed(self.config['seed'])
# initialize keypoint proposer and constraint generator
self.keypoint_proposer = KeypointProposer(global_config['keypoint_proposer'])
self.constraint_generator = ConstraintGenerator(global_config['constraint_generator'])
# initialize environment
self.env = ReKepOGEnv(global_config['env'], scene_file, verbose=False)
# setup ik solver (for reachability cost)
assert isinstance(self.env.robot, Fetch), "The IK solver assumes the robot is a Fetch robot"
ik_solver = IKSolver(
robot_description_path=self.env.robot.robot_arm_descriptor_yamls[self.env.robot.default_arm],
robot_urdf_path=self.env.robot.urdf_path,
eef_name=self.env.robot.eef_link_names[self.env.robot.default_arm],
reset_joint_pos=self.env.reset_joint_pos,
world2robot_homo=self.env.world2robot_homo,
)
# initialize solvers
self.subgoal_solver = SubgoalSolver(global_config['subgoal_solver'], ik_solver, self.env.reset_joint_pos)
self.path_solver = PathSolver(global_config['path_solver'], ik_solver, self.env.reset_joint_pos)
# initialize visualizer
if self.visualize:
self.visualizer = Visualizer(global_config['visualizer'], self.env)
def perform_task(self, instruction, rekep_program_dir=None, disturbance_seq=None):
self.env.reset()
cam_obs = self.env.get_cam_obs()
rgb = cam_obs[self.config['vlm_camera']]['rgb']
points = cam_obs[self.config['vlm_camera']]['points']
mask = cam_obs[self.config['vlm_camera']]['seg']
# ====================================
# = keypoint proposal and constraint generation
# ====================================
if rekep_program_dir is None:
keypoints, projected_img = self.keypoint_proposer.get_keypoints(rgb, points, mask)
print(f'{bcolors.HEADER}Got {len(keypoints)} proposed keypoints{bcolors.ENDC}')
if self.visualize:
self.visualizer.show_img(projected_img)
metadata = {'init_keypoint_positions': keypoints, 'num_keypoints': len(keypoints)}
rekep_program_dir = self.constraint_generator.generate(projected_img, instruction, metadata)
print(f'{bcolors.HEADER}Constraints generated{bcolors.ENDC}')
# ====================================
# = execute
# ====================================
self._execute(rekep_program_dir, disturbance_seq)
def _update_disturbance_seq(self, stage, disturbance_seq):
if disturbance_seq is not None:
if stage in disturbance_seq and not self.applied_disturbance[stage]:
# set the disturbance sequence, the generator will yield and instantiate one disturbance function for each env.step until it is exhausted
self.env.disturbance_seq = disturbance_seq[stage](self.env)
self.applied_disturbance[stage] = True
def _execute(self, rekep_program_dir, disturbance_seq=None):
# load metadata
with open(os.path.join(rekep_program_dir, 'metadata.json'), 'r') as f:
self.program_info = json.load(f)
self.applied_disturbance = {stage: False for stage in range(1, self.program_info['num_stages'] + 1)}
# register keypoints to be tracked
self.env.register_keypoints(self.program_info['init_keypoint_positions'])
# load constraints
self.constraint_fns = dict()
for stage in range(1, self.program_info['num_stages'] + 1): # stage starts with 1
stage_dict = dict()
for constraint_type in ['subgoal', 'path']:
load_path = os.path.join(rekep_program_dir, f'stage{stage}_{constraint_type}_constraints.txt')
get_grasping_cost_fn = get_callable_grasping_cost_fn(self.env) # special grasping function for VLM to call
stage_dict[constraint_type] = load_functions_from_txt(load_path, get_grasping_cost_fn) if os.path.exists(load_path) else []
self.constraint_fns[stage] = stage_dict
# bookkeeping of which keypoints can be moved in the optimization
self.keypoint_movable_mask = np.zeros(self.program_info['num_keypoints'] + 1, dtype=bool)
self.keypoint_movable_mask[0] = True # first keypoint is always the ee, so it's movable
# main loop
self.last_sim_step_counter = -np.inf
self._update_stage(1)
while True:
scene_keypoints = self.env.get_keypoint_positions()
self.keypoints = np.concatenate([[self.env.get_ee_pos()], scene_keypoints], axis=0) # first keypoint is always the ee
self.curr_ee_pose = self.env.get_ee_pose()
self.curr_joint_pos = self.env.get_arm_joint_postions()
self.sdf_voxels = self.env.get_sdf_voxels(self.config['sdf_voxel_size'])
self.collision_points = self.env.get_collision_points()
# ====================================
# = decide whether to backtrack
# ====================================
backtrack = False
if self.stage > 1:
path_constraints = self.constraint_fns[self.stage]['path']
for constraints in path_constraints:
violation = constraints(self.keypoints[0], self.keypoints[1:])
if violation > self.config['constraint_tolerance']:
backtrack = True
break
if backtrack:
# determine which stage to backtrack to based on constraints
for new_stage in range(self.stage - 1, 0, -1):
path_constraints = self.constraint_fns[new_stage]['path']
# if no constraints, we can safely backtrack
if len(path_constraints) == 0:
break
# otherwise, check if all constraints are satisfied
all_constraints_satisfied = True
for constraints in path_constraints:
violation = constraints(self.keypoints[0], self.keypoints[1:])
if violation > self.config['constraint_tolerance']:
all_constraints_satisfied = False
break
if all_constraints_satisfied:
break
print(f"{bcolors.HEADER}[stage={self.stage}] backtrack to stage {new_stage}{bcolors.ENDC}")
self._update_stage(new_stage)
else:
# apply disturbance
self._update_disturbance_seq(self.stage, disturbance_seq)
# ====================================
# = get optimized plan
# ====================================
if self.last_sim_step_counter == self.env.step_counter:
print(f"{bcolors.WARNING}sim did not step forward within last iteration (HINT: adjust action_steps_per_iter to be larger or the pos_threshold to be smaller){bcolors.ENDC}")
next_subgoal = self._get_next_subgoal(from_scratch=self.first_iter)
next_path = self._get_next_path(next_subgoal, from_scratch=self.first_iter)
self.first_iter = False
self.action_queue = next_path.tolist()
self.last_sim_step_counter = self.env.step_counter
# ====================================
# = execute
# ====================================
# determine if we proceed to the next stage
count = 0
while len(self.action_queue) > 0 and count < self.config['action_steps_per_iter']:
next_action = self.action_queue.pop(0)
precise = len(self.action_queue) == 0
self.env.execute_action(next_action, precise=precise)
count += 1
if len(self.action_queue) == 0:
if self.is_grasp_stage:
self._execute_grasp_action()
elif self.is_release_stage:
self._execute_release_action()
# if completed, save video and return
if self.stage == self.program_info['num_stages']:
self.env.sleep(2.0)
save_path = self.env.save_video()
print(f"{bcolors.OKGREEN}Video saved to {save_path}\n\n{bcolors.ENDC}")
return
# progress to next stage
self._update_stage(self.stage + 1)
def _get_next_subgoal(self, from_scratch):
subgoal_constraints = self.constraint_fns[self.stage]['subgoal']
path_constraints = self.constraint_fns[self.stage]['path']
subgoal_pose, debug_dict = self.subgoal_solver.solve(self.curr_ee_pose,
self.keypoints,
self.keypoint_movable_mask,
subgoal_constraints,
path_constraints,
self.sdf_voxels,
self.collision_points,
self.is_grasp_stage,
self.curr_joint_pos,
from_scratch=from_scratch)
subgoal_pose_homo = T.convert_pose_quat2mat(subgoal_pose)
# if grasp stage, back up a bit to leave room for grasping
if self.is_grasp_stage:
subgoal_pose[:3] += subgoal_pose_homo[:3, :3] @ np.array([-self.config['grasp_depth'] / 2.0, 0, 0])
debug_dict['stage'] = self.stage
print_opt_debug_dict(debug_dict)
if self.visualize:
self.visualizer.visualize_subgoal(subgoal_pose)
return subgoal_pose
def _get_next_path(self, next_subgoal, from_scratch):
path_constraints = self.constraint_fns[self.stage]['path']
path, debug_dict = self.path_solver.solve(self.curr_ee_pose,
next_subgoal,
self.keypoints,
self.keypoint_movable_mask,
path_constraints,
self.sdf_voxels,
self.collision_points,
self.curr_joint_pos,
from_scratch=from_scratch)
print_opt_debug_dict(debug_dict)
processed_path = self._process_path(path)
if self.visualize:
self.visualizer.visualize_path(processed_path)
return processed_path
def _process_path(self, path):
# spline interpolate the path from the current ee pose
full_control_points = np.concatenate([
self.curr_ee_pose.reshape(1, -1),
path,
], axis=0)
num_steps = get_linear_interpolation_steps(full_control_points[0], full_control_points[-1],
self.config['interpolate_pos_step_size'],
self.config['interpolate_rot_step_size'])
dense_path = spline_interpolate_poses(full_control_points, num_steps)
# add gripper action
ee_action_seq = np.zeros((dense_path.shape[0], 8))
ee_action_seq[:, :7] = dense_path
ee_action_seq[:, 7] = self.env.get_gripper_null_action()
return ee_action_seq
def _update_stage(self, stage):
# update stage
self.stage = stage
self.is_grasp_stage = self.program_info['grasp_keypoints'][self.stage - 1] != -1
self.is_release_stage = self.program_info['release_keypoints'][self.stage - 1] != -1
# can only be grasp stage or release stage or none
assert self.is_grasp_stage + self.is_release_stage <= 1, "Cannot be both grasp and release stage"
if self.is_grasp_stage: # ensure gripper is open for grasping stage
self.env.open_gripper()
# clear action queue
self.action_queue = []
# update keypoint movable mask
self._update_keypoint_movable_mask()
self.first_iter = True
def _update_keypoint_movable_mask(self):
for i in range(1, len(self.keypoint_movable_mask)): # first keypoint is ee so always movable
keypoint_object = self.env.get_object_by_keypoint(i - 1)
self.keypoint_movable_mask[i] = self.env.is_grasping(keypoint_object)
def _execute_grasp_action(self):
pregrasp_pose = self.env.get_ee_pose()
grasp_pose = pregrasp_pose.copy()
grasp_pose[:3] += T.quat2mat(pregrasp_pose[3:]) @ np.array([self.config['grasp_depth'], 0, 0])
grasp_action = np.concatenate([grasp_pose, [self.env.get_gripper_close_action()]])
self.env.execute_action(grasp_action, precise=True)
def _execute_release_action(self):
self.env.open_gripper()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--task', type=str, default='pen', help='task to perform')
parser.add_argument('--use_cached_query', action='store_true', help='instead of querying the VLM, use the cached query')
parser.add_argument('--apply_disturbance', action='store_true', help='apply disturbance to test the robustness')
parser.add_argument('--visualize', action='store_true', help='visualize each solution before executing (NOTE: this is blocking and needs to press "ESC" to continue)')
args = parser.parse_args()
if args.apply_disturbance:
assert args.task == 'pen' and args.use_cached_query, 'disturbance sequence is only defined for cached scenario'
# ====================================
# = pen task disturbance sequence
# ====================================
def stage1_disturbance_seq(env):
"""
Move the pen in stage 0 when robot is trying to grasp the pen
"""
pen = env.og_env.scene.object_registry("name", "pen_1")
holder = env.og_env.scene.object_registry("name", "pencil_holder_1")
# disturbance sequence
pos0, orn0 = pen.get_position_orientation()
pose0 = np.concatenate([pos0, orn0])
pos1 = pos0 + np.array([-0.08, 0.0, 0.0])
orn1 = T.quat_multiply(T.euler2quat(np.array([0, 0, np.pi/4])), orn0)
pose1 = np.concatenate([pos1, orn1])
pos2 = pos1 + np.array([0.10, 0.0, 0.0])
orn2 = T.quat_multiply(T.euler2quat(np.array([0, 0, -np.pi/2])), orn1)
pose2 = np.concatenate([pos2, orn2])
control_points = np.array([pose0, pose1, pose2])
pose_seq = spline_interpolate_poses(control_points, num_steps=25)
def disturbance(counter):
if counter < len(pose_seq):
pose = pose_seq[counter]
pos, orn = pose[:3], pose[3:]
pen.set_position_orientation(pos, orn)
counter += 1
counter = 0
while True:
yield disturbance(counter)
counter += 1
def stage2_disturbance_seq(env):
"""
Take the pen out of the gripper in stage 1 when robot is trying to reorient the pen
"""
apply_disturbance = env.is_grasping()
pen = env.og_env.scene.object_registry("name", "pen_1")
holder = env.og_env.scene.object_registry("name", "pencil_holder_1")
# disturbance sequence
pos0, orn0 = pen.get_position_orientation()
pose0 = np.concatenate([pos0, orn0])
pose1 = np.array([-0.30, -0.15, 0.71, -0.7071068, 0, 0, 0.7071068])
control_points = np.array([pose0, pose1])
pose_seq = spline_interpolate_poses(control_points, num_steps=25)
def disturbance(counter):
if apply_disturbance:
if counter < 20:
if counter > 15:
env.robot.release_grasp_immediately() # force robot to release the pen
else:
pass # do nothing for the other steps
elif counter < len(pose_seq) + 20:
env.robot.release_grasp_immediately() # force robot to release the pen
pose = pose_seq[counter - 20]
pos, orn = pose[:3], pose[3:]
pen.set_position_orientation(pos, orn)
counter += 1
counter = 0
while True:
yield disturbance(counter)
counter += 1
def stage3_disturbance_seq(env):
"""
Move the holder in stage 2 when robot is trying to drop the pen into the holder
"""
pen = env.og_env.scene.object_registry("name", "pen_1")
holder = env.og_env.scene.object_registry("name", "pencil_holder_1")
# disturbance sequence
pos0, orn0 = holder.get_position_orientation()
pose0 = np.concatenate([pos0, orn0])
pos1 = pos0 + np.array([-0.02, -0.15, 0.0])
orn1 = orn0
pose1 = np.concatenate([pos1, orn1])
control_points = np.array([pose0, pose1])
pose_seq = spline_interpolate_poses(control_points, num_steps=5)
def disturbance(counter):
if counter < len(pose_seq):
pose = pose_seq[counter]
pos, orn = pose[:3], pose[3:]
holder.set_position_orientation(pos, orn)
counter += 1
counter = 0
while True:
yield disturbance(counter)
counter += 1
task_list = {
'pen': {
'scene_file': './configs/og_scene_file_pen.json',
'instruction': 'reorient the white pen and drop it upright into the black pen holder',
'rekep_program_dir': './vlm_query/pen',
'disturbance_seq': {1: stage1_disturbance_seq, 2: stage2_disturbance_seq, 3: stage3_disturbance_seq},
},
}
task = task_list['pen']
scene_file = task['scene_file']
instruction = task['instruction']
main = Main(scene_file, visualize=args.visualize)
main.perform_task(instruction,
rekep_program_dir=task['rekep_program_dir'] if args.use_cached_query else None,
disturbance_seq=task.get('disturbance_seq', None) if args.apply_disturbance else None)