-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathscene.py
561 lines (503 loc) · 24.7 KB
/
scene.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
from typing import List, Callable
import numpy as np
from pyrep import PyRep
from pyrep.const import ObjectType
from pyrep.errors import ConfigurationPathError
from pyrep.objects import Dummy
from pyrep.objects.shape import Shape
from pyrep.objects.vision_sensor import VisionSensor
from rlbench.backend.exceptions import (
WaypointError, BoundaryError, NoWaypointsError, DemoError)
from rlbench.backend.observation import Observation
from rlbench.backend.robot import Robot
from rlbench.backend.spawn_boundary import SpawnBoundary
from rlbench.backend.task import Task
from rlbench.backend.utils import rgb_handles_to_mask
from rlbench.demo import Demo
from rlbench.noise_model import NoiseModel
from rlbench.observation_config import ObservationConfig, CameraConfig
STEPS_BEFORE_EPISODE_START = 10
class Scene(object):
"""Controls what is currently in the vrep scene. This is used for making
sure that the tasks are easily reachable. This may be just replaced by
environment. Responsible for moving all the objects. """
def __init__(self,
pyrep: PyRep,
robot: Robot,
obs_config: ObservationConfig = ObservationConfig(),
robot_setup: str = 'panda'):
self.pyrep = pyrep
self.robot = robot
self.robot_setup = robot_setup
self.task = None
self._obs_config = obs_config
self._initial_task_state = None
self._start_arm_joint_pos = robot.arm.get_joint_positions()
self._starting_gripper_joint_pos = robot.gripper.get_joint_positions()
self._workspace = Shape('workspace')
self._workspace_boundary = SpawnBoundary([self._workspace])
self._cam_over_shoulder_left = VisionSensor('cam_over_shoulder_left')
self._cam_over_shoulder_right = VisionSensor('cam_over_shoulder_right')
self._cam_overhead = VisionSensor('cam_overhead')
self._cam_wrist = VisionSensor('cam_wrist')
self._cam_front = VisionSensor('cam_front')
self._cam_over_shoulder_left_mask = VisionSensor(
'cam_over_shoulder_left_mask')
self._cam_over_shoulder_right_mask = VisionSensor(
'cam_over_shoulder_right_mask')
self._cam_overhead_mask = VisionSensor('cam_overhead_mask')
self._cam_wrist_mask = VisionSensor('cam_wrist_mask')
self._cam_front_mask = VisionSensor('cam_front_mask')
self._has_init_task = self._has_init_episode = False
self._variation_index = 0
self._initial_robot_state = (robot.arm.get_configuration_tree(),
robot.gripper.get_configuration_tree())
# Set camera properties from observation config
self._set_camera_properties()
x, y, z = self._workspace.get_position()
minx, maxx, miny, maxy, _, _ = self._workspace.get_bounding_box()
self._workspace_minx = x - np.fabs(minx) - 0.2
self._workspace_maxx = x + maxx + 0.2
self._workspace_miny = y - np.fabs(miny) - 0.2
self._workspace_maxy = y + maxy + 0.2
self._workspace_minz = z
self._workspace_maxz = z + 1.0 # 1M above workspace
self.target_workspace_check = Dummy.create()
self._step_callback = None
self._robot_shapes = self.robot.arm.get_objects_in_tree(
object_type=ObjectType.SHAPE)
self._joint_position_action = None
def load(self, task: Task) -> None:
"""Loads the task and positions at the centre of the workspace.
:param task: The task to load in the scene.
"""
task.load() # Load the task in to the scene
# Set at the centre of the workspace
task.get_base().set_position(self._workspace.get_position())
self._initial_task_state = task.get_state()
self.task = task
self._initial_task_pose = task.boundary_root().get_orientation()
self._has_init_task = self._has_init_episode = False
self._variation_index = 0
def unload(self) -> None:
"""Clears the scene. i.e. removes all tasks. """
if self.task is not None:
self.robot.gripper.release()
if self._has_init_task:
self.task.cleanup_()
self.task.unload()
self.task = None
self._variation_index = 0
def init_task(self) -> None:
self.task.init_task()
self._initial_task_state = self.task.get_state()
self._has_init_task = True
self._variation_index = 0
def init_episode(self, index: int, randomly_place: bool=True,
max_attempts: int = 5, place_demo: bool = False) -> List[str]:
"""Calls the task init_episode and puts randomly in the workspace.
"""
self._variation_index = index
if not self._has_init_task:
self.init_task()
# Try a few times to init and place in the workspace
self._attempts = 0
descriptions = None
while self._attempts < max_attempts:
descriptions = self.task.init_episode(index)
try:
if (randomly_place and
not self.task.is_static_workspace()):
self._place_task()
if self.robot.arm.check_arm_collision():
raise BoundaryError()
if not place_demo:
self.task.validate()
break
else:
# Placing demo, run the number of attempts for correct demo reset
self._attempts += 1
except (BoundaryError, WaypointError) as e:
self.task.cleanup_()
self.task.restore_state(self._initial_task_state)
self._attempts += 1
if self._attempts >= max_attempts:
raise e
# Let objects come to rest
[self.pyrep.step() for _ in range(STEPS_BEFORE_EPISODE_START)]
self._has_init_episode = True
return descriptions
def reset(self) -> None:
"""Resets the joint angles. """
self.robot.gripper.release()
arm, gripper = self._initial_robot_state
self.pyrep.set_configuration_tree(arm)
self.pyrep.set_configuration_tree(gripper)
self.robot.arm.set_joint_positions(self._start_arm_joint_pos, disable_dynamics=True)
self.robot.arm.set_joint_target_velocities(
[0] * len(self.robot.arm.joints))
self.robot.gripper.set_joint_positions(
self._starting_gripper_joint_pos, disable_dynamics=True)
self.robot.gripper.set_joint_target_velocities(
[0] * len(self.robot.gripper.joints))
if self.task is not None and self._has_init_task:
self.task.cleanup_()
self.task.restore_state(self._initial_task_state)
self.task.set_initial_objects_in_scene()
def get_observation(self) -> Observation:
tip = self.robot.arm.get_tip()
joint_forces = None
if self._obs_config.joint_forces:
fs = self.robot.arm.get_joint_forces()
vels = self.robot.arm.get_joint_target_velocities()
joint_forces = self._obs_config.joint_forces_noise.apply(
np.array([-f if v < 0 else f for f, v in zip(fs, vels)]))
ee_forces_flat = None
if self._obs_config.gripper_touch_forces:
ee_forces = self.robot.gripper.get_touch_sensor_forces()
ee_forces_flat = []
for eef in ee_forces:
ee_forces_flat.extend(eef)
ee_forces_flat = np.array(ee_forces_flat)
lsc_ob = self._obs_config.left_shoulder_camera
rsc_ob = self._obs_config.right_shoulder_camera
oc_ob = self._obs_config.overhead_camera
wc_ob = self._obs_config.wrist_camera
fc_ob = self._obs_config.front_camera
lsc_mask_fn, rsc_mask_fn, oc_mask_fn, wc_mask_fn, fc_mask_fn = [
(rgb_handles_to_mask if c.masks_as_one_channel else lambda x: x
) for c in [lsc_ob, rsc_ob, oc_ob, wc_ob, fc_ob]]
def get_rgb_depth(sensor: VisionSensor, get_rgb: bool, get_depth: bool,
get_pcd: bool, rgb_noise: NoiseModel,
depth_noise: NoiseModel, depth_in_meters: bool):
rgb = depth = pcd = None
if sensor is not None and (get_rgb or get_depth):
sensor.handle_explicitly()
if get_rgb:
rgb = sensor.capture_rgb()
if rgb_noise is not None:
rgb = rgb_noise.apply(rgb)
rgb = np.clip((rgb * 255.).astype(np.uint8), 0, 255)
if get_depth or get_pcd:
depth = sensor.capture_depth(depth_in_meters)
if depth_noise is not None:
depth = depth_noise.apply(depth)
if get_pcd:
depth_m = depth
if not depth_in_meters:
near = sensor.get_near_clipping_plane()
far = sensor.get_far_clipping_plane()
depth_m = near + depth * (far - near)
pcd = sensor.pointcloud_from_depth(depth_m)
if not get_depth:
depth = None
return rgb, depth, pcd
def get_mask(sensor: VisionSensor, mask_fn):
mask = None
if sensor is not None:
sensor.handle_explicitly()
mask = mask_fn(sensor.capture_rgb())
return mask
left_shoulder_rgb, left_shoulder_depth, left_shoulder_pcd = get_rgb_depth(
self._cam_over_shoulder_left, lsc_ob.rgb, lsc_ob.depth, lsc_ob.point_cloud,
lsc_ob.rgb_noise, lsc_ob.depth_noise, lsc_ob.depth_in_meters)
right_shoulder_rgb, right_shoulder_depth, right_shoulder_pcd = get_rgb_depth(
self._cam_over_shoulder_right, rsc_ob.rgb, rsc_ob.depth, rsc_ob.point_cloud,
rsc_ob.rgb_noise, rsc_ob.depth_noise, rsc_ob.depth_in_meters)
overhead_rgb, overhead_depth, overhead_pcd = get_rgb_depth(
self._cam_overhead, oc_ob.rgb, oc_ob.depth, oc_ob.point_cloud,
oc_ob.rgb_noise, oc_ob.depth_noise, oc_ob.depth_in_meters)
wrist_rgb, wrist_depth, wrist_pcd = get_rgb_depth(
self._cam_wrist, wc_ob.rgb, wc_ob.depth, wc_ob.point_cloud,
wc_ob.rgb_noise, wc_ob.depth_noise, wc_ob.depth_in_meters)
front_rgb, front_depth, front_pcd = get_rgb_depth(
self._cam_front, fc_ob.rgb, fc_ob.depth, fc_ob.point_cloud,
fc_ob.rgb_noise, fc_ob.depth_noise, fc_ob.depth_in_meters)
left_shoulder_mask = get_mask(self._cam_over_shoulder_left_mask,
lsc_mask_fn) if lsc_ob.mask else None
right_shoulder_mask = get_mask(self._cam_over_shoulder_right_mask,
rsc_mask_fn) if rsc_ob.mask else None
overhead_mask = get_mask(self._cam_overhead_mask,
oc_mask_fn) if oc_ob.mask else None
wrist_mask = get_mask(self._cam_wrist_mask,
wc_mask_fn) if wc_ob.mask else None
front_mask = get_mask(self._cam_front_mask,
fc_mask_fn) if fc_ob.mask else None
obs = Observation(
left_shoulder_rgb=left_shoulder_rgb,
left_shoulder_depth=left_shoulder_depth,
left_shoulder_point_cloud=left_shoulder_pcd,
right_shoulder_rgb=right_shoulder_rgb,
right_shoulder_depth=right_shoulder_depth,
right_shoulder_point_cloud=right_shoulder_pcd,
overhead_rgb=overhead_rgb,
overhead_depth=overhead_depth,
overhead_point_cloud=overhead_pcd,
wrist_rgb=wrist_rgb,
wrist_depth=wrist_depth,
wrist_point_cloud=wrist_pcd,
front_rgb=front_rgb,
front_depth=front_depth,
front_point_cloud=front_pcd,
left_shoulder_mask=left_shoulder_mask,
right_shoulder_mask=right_shoulder_mask,
overhead_mask=overhead_mask,
wrist_mask=wrist_mask,
front_mask=front_mask,
joint_velocities=(
self._obs_config.joint_velocities_noise.apply(
np.array(self.robot.arm.get_joint_velocities()))
if self._obs_config.joint_velocities else None),
joint_positions=(
self._obs_config.joint_positions_noise.apply(
np.array(self.robot.arm.get_joint_positions()))
if self._obs_config.joint_positions else None),
joint_forces=(joint_forces
if self._obs_config.joint_forces else None),
gripper_open=(
(1.0 if self.robot.gripper.get_open_amount()[0] > 0.9 else 0.0)
if self._obs_config.gripper_open else None),
gripper_pose=(
np.array(tip.get_pose())
if self._obs_config.gripper_pose else None),
gripper_matrix=(
tip.get_matrix()
if self._obs_config.gripper_matrix else None),
gripper_touch_forces=(
ee_forces_flat
if self._obs_config.gripper_touch_forces else None),
gripper_joint_positions=(
np.array(self.robot.gripper.get_joint_positions())
if self._obs_config.gripper_joint_positions else None),
task_low_dim_state=(
self.task.get_low_dim_state() if
self._obs_config.task_low_dim_state else None),
misc=self._get_misc())
obs = self.task.decorate_observation(obs)
return obs
def step(self):
self.pyrep.step()
self.task.step()
if self._step_callback is not None:
self._step_callback()
def register_step_callback(self, func):
self._step_callback = func
def get_demo(self, record: bool = True,
callable_each_step: Callable[[Observation], None] = None,
randomly_place: bool = True) -> Demo:
"""Returns a demo (list of observations)"""
if not self._has_init_task:
self.init_task()
if not self._has_init_episode:
self.init_episode(self._variation_index,
randomly_place=randomly_place)
self._has_init_episode = False
waypoints = self.task.get_waypoints()
if len(waypoints) == 0:
raise NoWaypointsError(
'No waypoints were found.', self.task)
demo = []
if record:
self.pyrep.step() # Need this here or get_force doesn't work...
self._joint_position_action = None
gripper_open = 1.0 if self.robot.gripper.get_open_amount()[0] > 0.9 else 0.0
demo.append(self.get_observation())
while True:
success = False
for i, point in enumerate(waypoints):
point.start_of_path()
if point.skip:
continue
grasped_objects = self.robot.gripper.get_grasped_objects()
colliding_shapes = [s for s in self.pyrep.get_objects_in_tree(
object_type=ObjectType.SHAPE) if s not in grasped_objects
and s not in self._robot_shapes and s.is_collidable()
and self.robot.arm.check_arm_collision(s)]
[s.set_collidable(False) for s in colliding_shapes]
try:
path = point.get_path()
[s.set_collidable(True) for s in colliding_shapes]
except ConfigurationPathError as e:
[s.set_collidable(True) for s in colliding_shapes]
raise DemoError(
'Could not get a path for waypoint %d.' % i,
self.task) from e
ext = point.get_ext()
path.visualize()
done = False
success = False
while not done:
done = path.step()
self.step()
self._joint_position_action = np.append(path.get_executed_joint_position_action(), gripper_open)
self._demo_record_step(demo, record, callable_each_step)
success, term = self.task.success()
point.end_of_path()
path.clear_visualization()
if len(ext) > 0:
contains_param = False
start_of_bracket = -1
gripper = self.robot.gripper
if 'open_gripper(' in ext:
gripper.release()
start_of_bracket = ext.index('open_gripper(') + 13
contains_param = ext[start_of_bracket] != ')'
if not contains_param:
done = False
while not done:
gripper_open = 1.0
done = gripper.actuate(gripper_open, 0.04)
self.step()
self._joint_position_action = np.append(path.get_executed_joint_position_action(), gripper_open)
if self._obs_config.record_gripper_closing:
self._demo_record_step(
demo, record, callable_each_step)
elif 'close_gripper(' in ext:
start_of_bracket = ext.index('close_gripper(') + 14
contains_param = ext[start_of_bracket] != ')'
if not contains_param:
done = False
while not done:
gripper_open = 0.0
done = gripper.actuate(gripper_open, 0.04)
self.step()
self._joint_position_action = np.append(path.get_executed_joint_position_action(), gripper_open)
if self._obs_config.record_gripper_closing:
self._demo_record_step(
demo, record, callable_each_step)
if contains_param:
rest = ext[start_of_bracket:]
num = float(rest[:rest.index(')')])
done = False
while not done:
gripper_open = num
done = gripper.actuate(gripper_open, 0.04)
self.step()
self._joint_position_action = np.append(path.get_executed_joint_position_action(), gripper_open)
if self._obs_config.record_gripper_closing:
self._demo_record_step(
demo, record, callable_each_step)
if 'close_gripper(' in ext:
for g_obj in self.task.get_graspable_objects():
gripper.grasp(g_obj)
self._demo_record_step(demo, record, callable_each_step)
if not self.task.should_repeat_waypoints() or success:
break
# Some tasks may need additional physics steps
# (e.g. ball rowling to goal)
if not success:
for _ in range(10):
self.step()
self._joint_position_action = np.append(path.get_executed_joint_position_action(), gripper_open)
self._demo_record_step(demo, record, callable_each_step)
success, term = self.task.success()
if success:
break
success, term = self.task.success()
if not success:
raise DemoError('Demo was completed, but was not successful.',
self.task)
processed_demo = Demo(demo)
processed_demo.num_reset_attempts = self._attempts + 1
return processed_demo
def get_observation_config(self) -> ObservationConfig:
return self._obs_config
def check_target_in_workspace(self, target_pos: np.ndarray) -> bool:
x, y, z = target_pos
return (self._workspace_maxx > x > self._workspace_minx and
self._workspace_maxy > y > self._workspace_miny and
self._workspace_maxz > z > self._workspace_minz)
def _demo_record_step(self, demo_list, record, func):
if record:
demo_list.append(self.get_observation())
if func is not None:
func(self.get_observation())
def _set_camera_properties(self) -> None:
def _set_rgb_props(rgb_cam: VisionSensor,
rgb: bool, depth: bool, conf: CameraConfig):
if not (rgb or depth or conf.point_cloud):
rgb_cam.remove()
else:
rgb_cam.set_explicit_handling(1)
rgb_cam.set_resolution(conf.image_size)
rgb_cam.set_render_mode(conf.render_mode)
def _set_mask_props(mask_cam: VisionSensor, mask: bool,
conf: CameraConfig):
if not mask:
mask_cam.remove()
else:
mask_cam.set_explicit_handling(1)
mask_cam.set_resolution(conf.image_size)
_set_rgb_props(
self._cam_over_shoulder_left,
self._obs_config.left_shoulder_camera.rgb,
self._obs_config.left_shoulder_camera.depth,
self._obs_config.left_shoulder_camera)
_set_rgb_props(
self._cam_over_shoulder_right,
self._obs_config.right_shoulder_camera.rgb,
self._obs_config.right_shoulder_camera.depth,
self._obs_config.right_shoulder_camera)
_set_rgb_props(
self._cam_overhead,
self._obs_config.overhead_camera.rgb,
self._obs_config.overhead_camera.depth,
self._obs_config.overhead_camera)
_set_rgb_props(
self._cam_wrist, self._obs_config.wrist_camera.rgb,
self._obs_config.wrist_camera.depth,
self._obs_config.wrist_camera)
_set_rgb_props(
self._cam_front, self._obs_config.front_camera.rgb,
self._obs_config.front_camera.depth,
self._obs_config.front_camera)
_set_mask_props(
self._cam_over_shoulder_left_mask,
self._obs_config.left_shoulder_camera.mask,
self._obs_config.left_shoulder_camera)
_set_mask_props(
self._cam_over_shoulder_right_mask,
self._obs_config.right_shoulder_camera.mask,
self._obs_config.right_shoulder_camera)
_set_mask_props(
self._cam_overhead_mask,
self._obs_config.overhead_camera.mask,
self._obs_config.overhead_camera)
_set_mask_props(
self._cam_wrist_mask, self._obs_config.wrist_camera.mask,
self._obs_config.wrist_camera)
_set_mask_props(
self._cam_front_mask, self._obs_config.front_camera.mask,
self._obs_config.front_camera)
def _place_task(self) -> None:
self._workspace_boundary.clear()
# Find a place in the robot workspace for task
self.task.boundary_root().set_orientation(
self._initial_task_pose)
min_rot, max_rot = self.task.base_rotation_bounds()
self._workspace_boundary.sample(
self.task.boundary_root(),
min_rotation=min_rot, max_rotation=max_rot)
def _get_misc(self):
def _get_cam_data(cam: VisionSensor, name: str):
d = {}
if cam.still_exists():
d = {
'%s_extrinsics' % name: cam.get_matrix(),
'%s_intrinsics' % name: cam.get_intrinsic_matrix(),
'%s_near' % name: cam.get_near_clipping_plane(),
'%s_far' % name: cam.get_far_clipping_plane(),
}
return d
misc = _get_cam_data(self._cam_over_shoulder_left, 'left_shoulder_camera')
misc.update(_get_cam_data(self._cam_over_shoulder_right, 'right_shoulder_camera'))
misc.update(_get_cam_data(self._cam_overhead, 'overhead_camera'))
misc.update(_get_cam_data(self._cam_front, 'front_camera'))
misc.update(_get_cam_data(self._cam_wrist, 'wrist_camera'))
misc.update({"variation_index": self._variation_index})
if self._joint_position_action is not None:
# Store the actual requested joint positions during demo collection
misc.update({"joint_position_action": self._joint_position_action})
joint_poses = [j.get_pose() for j in self.robot.arm.joints]
misc.update({'joint_poses': joint_poses})
return misc