-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkuka_utils.py
190 lines (162 loc) · 6.66 KB
/
kuka_utils.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
# -*- coding: utf8 -*-
import os.path
from matplotlib import cm
import numpy as np
import pydrake
from pydrake.all import (
AddFlatTerrainToWorld,
AddModelInstancesFromSdfString,
AddModelInstanceFromUrdfFile,
AddModelInstanceFromUrdfStringSearchingInRosPackages,
FloatingBaseType,
LeafSystem,
PortDataType,
RigidBodyFrame,
RollPitchYaw,
RotationMatrix
)
import meshcat
import meshcat.transformations as tf
import meshcat.geometry as g
def extract_position_indices(rbt, controlled_joint_names):
''' Given a RigidBodyTree and a list of
joint names, returns, in separate lists, the
position indices (i.e. offsets into the RBT positions vector)
corresponding to those joints, and the rest of the
position indices. '''
controlled_config_inds = []
other_config_inds = []
for i in range(rbt.get_num_bodies()):
body = rbt.get_body(i)
if body.has_joint():
joint = body.getJoint()
if joint.get_name() in controlled_joint_names:
controlled_config_inds += range(
body.get_position_start_index(),
body.get_position_start_index() +
joint.get_num_positions())
else:
other_config_inds += range(
body.get_position_start_index(),
body.get_position_start_index() +
joint.get_num_positions())
if len(controlled_joint_names) != len(controlled_config_inds):
raise ValueError("Didn't find all "
"requested controlled joint names.")
return controlled_config_inds, other_config_inds
def setup_kuka(rbt):
iiwa_urdf_path = os.path.join(
pydrake.getDrakePath(),
"manipulation", "models", "iiwa_description", "urdf",
"iiwa14_polytope_collision.urdf")
wsg50_sdf_path = os.path.join(
pydrake.getDrakePath(),
"manipulation", "models", "wsg_50_description", "sdf",
"schunk_wsg_50.sdf")
table_sdf_path = os.path.join(
pydrake.getDrakePath(),
"examples", "kuka_iiwa_arm", "models", "table",
"extra_heavy_duty_table_surface_only_collision.sdf")
object_urdf_path = os.path.join(
pydrake.getDrakePath(),
"examples", "kuka_iiwa_arm", "models", "objects",
"block_for_pick_and_place.urdf")
AddFlatTerrainToWorld(rbt)
table_frame_robot = RigidBodyFrame(
"table_frame_robot", rbt.world(),
[0.0, 0, 0], [0, 0, 0])
AddModelInstancesFromSdfString(
open(table_sdf_path).read(), FloatingBaseType.kFixed,
table_frame_robot, rbt)
table_frame_fwd = RigidBodyFrame(
"table_frame_fwd", rbt.world(),
[0.8, 0, 0], [0, 0, 0])
AddModelInstancesFromSdfString(
open(table_sdf_path).read(), FloatingBaseType.kFixed,
table_frame_fwd, rbt)
table_top_z_in_world = 0.736 + 0.057 / 2
robot_base_frame = RigidBodyFrame(
"robot_base_frame", rbt.world(),
[0.0, 0, table_top_z_in_world], [0, 0, 0])
AddModelInstanceFromUrdfFile(iiwa_urdf_path, FloatingBaseType.kFixed,
robot_base_frame, rbt)
object_init_frame = RigidBodyFrame(
"object_init_frame", rbt.world(),
[0.8, 0, table_top_z_in_world+0.1], [0, 0, 0])
AddModelInstanceFromUrdfFile(object_urdf_path,
FloatingBaseType.kRollPitchYaw,
object_init_frame, rbt)
# Add gripper
gripper_frame = rbt.findFrame("iiwa_frame_ee")
AddModelInstancesFromSdfString(
open(wsg50_sdf_path).read(), FloatingBaseType.kFixed,
gripper_frame, rbt)
def render_system_with_graphviz(system, output_file="system_view.gz"):
''' Renders the Drake system (presumably a diagram,
otherwise this graph will be fairly trivial) using
graphviz to a specified file. '''
from graphviz import Source
string = system.GetGraphvizString()
src = Source(string)
src.render(output_file, view=False)
class RgbdCameraMeshcatVisualizer(LeafSystem):
def __init__(self,
camera,
rbt,
draw_timestep=0.033333,
prefix="RBCameraViz",
zmq_url="tcp://127.0.0.1:6000"):
LeafSystem.__init__(self)
self.set_name('camera meshcat visualization')
self.timestep = draw_timestep
self._DeclarePeriodicPublish(draw_timestep, 0.0)
self.camera = camera
self.rbt = rbt
self.prefix = prefix
self.camera_input_port = \
self._DeclareInputPort(PortDataType.kAbstractValued,
camera.depth_image_output_port().size())
self.state_input_port = \
self._DeclareInputPort(PortDataType.kVectorValued,
rbt.get_num_positions() +
rbt.get_num_velocities())
# Set up meshcat
self.vis = meshcat.Visualizer(zmq_url=zmq_url)
self.vis[prefix].delete()
def _DoPublish(self, context, event):
u_data = self.EvalAbstractInput(context, 0).get_value()
x = self.EvalVectorInput(context, 1).get_value()
w, h, _ = u_data.data.shape
depth_image = u_data.data[:, :, 0]
# Convert depth image to point cloud, with +z being
# camera "forward"
Kinv = np.linalg.inv(
self.camera.depth_camera_info().intrinsic_matrix())
U, V = np.meshgrid(np.arange(h), np.arange(w))
points_in_camera_frame = np.vstack([
U.flatten(),
V.flatten(),
np.ones(w*h)])
points_in_camera_frame = Kinv.dot(points_in_camera_frame) * \
depth_image.flatten()
# The depth camera has some offset from the camera's root frame,
# so take than into account.
pose_mat = self.camera.depth_camera_optical_pose().matrix()
points_in_camera_frame = pose_mat[0:3, 0:3].dot(points_in_camera_frame)
points_in_camera_frame += np.tile(pose_mat[0:3, 3], [w*h, 1]).T
kinsol = self.rbt.doKinematics(x[:self.rbt.get_num_positions()])
points_in_world_frame = self.rbt.transformPoints(
kinsol,
points_in_camera_frame,
self.camera.frame().get_frame_index(),
0)
# Color points according to their normalized height
min_height = 0.0
max_height = 2.0
colors = cm.jet(
(points_in_world_frame[2, :]-min_height)/(max_height-min_height)
).T[0:3, :]
self.vis[self.prefix]["points"].set_object(
g.PointCloud(position=points_in_world_frame,
color=colors,
size=0.005))