This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
609_plan_placements.py
90 lines (67 loc) · 3.23 KB
/
609_plan_placements.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
import math
import os
import helpers
from compas_fab.backends import RosClient
from compas_fab.robots import PlanningScene
import compas
from compas.geometry import Translation
HERE = os.path.dirname(__file__)
MAX_STEP = 0.01
# Load assembly
filename = os.path.join(HERE, 'assembly.json')
assembly = compas.json_load(filename)
with RosClient() as client:
robot = client.load_robot()
scene = PlanningScene(robot)
# Prepare scene for planning
helpers.attach_vacuum_gripper(scene)
helpers.add_static_objects(scene)
# Get sequence
assembly_sequence = helpers.traversal_linearly_ordered(assembly)
print('Sequence: {}'.format(assembly_sequence))
built = []
for key in assembly_sequence:
e = assembly.element(key)
if e.trajectory:
print('Skipping already planned element {}'.format(key))
continue
print('Planning element {}:'.format(key))
print(' - Preparing scene: ', end='')
# Add built elements to the scene
helpers.add_built_elements(scene, assembly, built)
print()
place_t0cf_frame, approach_t0cf_frame = robot.from_tcf_to_t0cf([e.frame, e.approach_frame])
# Apply place tolerance
x, y, z = 0, 0, assembly.attributes['place_tolerance']
place_t0cf_frame = place_t0cf_frame.transformed(Translation.from_vector([x, y, z]))
tolerance_position = 0.001
tolerance_axes = [math.radians(1)] * 3
# create goal constraints from frame
goal_constraints = robot.constraints_from_frame(approach_t0cf_frame,
tolerance_position,
tolerance_axes)
# get start configuration
start_configuration = helpers.get_last_config(assembly.pick_trajectory, robot)
print(' - Planning kinematic motion: ', end='', flush=True)
trajectory = robot.plan_motion(goal_constraints,
start_configuration,
options=dict(planner_id='RRTstarkConfigDefault'))
print('OK ({} points)'.format(len(trajectory.points)))
# Plan cartesian insertion
print(' - Planning cartesian insertion: ', end='', flush=True)
frames = [approach_t0cf_frame, place_t0cf_frame]
start_configuration = helpers.get_last_config(trajectory, robot)
place_trajectory = robot.plan_cartesian_motion(frames,
start_configuration,
options=dict(
max_step=MAX_STEP,
avoid_collisions=True,
))
if place_trajectory and place_trajectory.fraction < 1.0:
raise Exception('Incomplete trajectory. Fraction={}'.format(place_trajectory.fraction))
print('OK ({} points)'.format(len(place_trajectory.points)))
# Merge and store
e.trajectory = trajectory.points + place_trajectory.points
built.append(key)
# Save assembly
compas.json_dump(assembly, filename, pretty=True)