-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion_primitives.py
More file actions
351 lines (273 loc) · 11.9 KB
/
motion_primitives.py
File metadata and controls
351 lines (273 loc) · 11.9 KB
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
"""
Based on Ben/Harry's `motion_primitives.py` script and redone for MM.
"""
import time
import click
from numpy import diff
import geometry_msgs.msg
import moveit_commander
import numpy as np
# from mp_flow_pred import calc_rot
import rospy
import tf
from geometry_msgs.msg import Point, PoseStamped
from intera_core_msgs.msg import DigitalOutputCommand
from intera_interface import Limb
from scipy.spatial.transform import Rotation as R
class FlowbotController:
"""TODO
"""
def __init__(self):
# MoveIt! Stuff
self.robot = moveit_commander.RobotCommander()
scene = moveit_commander.PlanningSceneInterface()
# init_scene(scene)
self.group = moveit_commander.MoveGroupCommander("right_arm")
# Vacuum stuff.
self.publishVacuumCommand = rospy.Publisher(
"/robot/digital_io/command", DigitalOutputCommand, queue_size=30
)
# Intera SDK stuff.
self.limb = Limb()
self.joint_names = self.limb.joint_names()
self.group.set_max_velocity_scaling_factor(0.6)
self.group.set_max_acceleration_scaling_factor(0.6)
self.group.set_planner_id("RRTstarkConfigDefault")
self.group.set_planning_time(1.0)
# For experiments. TODO(daniel) how to get EE positions? Might be easier.
# This is what Ben and Harry used.
self.home_jas = [-1.90256, -2.57107, 0.92954, 2.22587, -2.37902, -1.5741, 3.81140]
# NOTE(daniel): just set this by manually adjusting the robot and querying j-angles.
# But, for some reason MoveIt! keeps telling me abotu out of bounds joints.
#self.home_jas = [-1.54608, -0.65492, 1.7265, -0.70596, 1.03651, -1.9498, 4.71302]
def moveit_to_pose(self, t, r):
pose_target = geometry_msgs.msg.Pose()
pose_target.position.x = t[0]
pose_target.position.y = t[1]
pose_target.position.z = t[2]
pose_target.orientation.x = r[0]
pose_target.orientation.y = r[1]
pose_target.orientation.z = r[2]
pose_target.orientation.w = r[3]
# Planning
self.group.set_start_state(self.robot.get_current_state())
self.group.set_pose_target(pose_target)
plan = self.group.plan()
pts = plan.joint_trajectory.points
if not pts:
for _ in range(10):
plan = self.group.plan()
pts = plan.joint_trajectory.points
if pts:
break
self.group.execute(plan, wait=True)
self.group.stop()
# It is always good to clear your targets after planning with poses.
# Note: there is no equivalent function for clear_joint_value_targets()
self.group.clear_pose_targets()
def go_to_experiment_home(self):
"""NOTE(daniel): Ben/Harry use this at the start of each trial."""
self.group.set_start_state(self.robot.get_current_state())
self.group.set_joint_value_target(self.home_jas)
# NOTE(daniel) be careful! Maybe always have some kind of query / check.
import pdb; pdb.set_trace() # pause then continue
plan = self.group.plan()
self.group.execute(plan, wait=True)
self.group.stop()
# It is always good to clear your targets after planning with poses.
# Note: there is no equivalent function for clear_joint_value_targets()
self.group.clear_pose_targets()
def move_until_contact(self, vector, speed, timeout, contact_force=15.0):
# Get the force exerted on the endpoint.
f = self.limb.endpoint_effort()["force"]
fnorm = np.linalg.norm(f)
t0 = rospy.get_time()
r = rospy.Rate(50)
# The vector is just a direction, so we set a particular speed.
vector = vector / np.linalg.norm(vector) * speed
while fnorm < contact_force:
# Compute the estimated joint velocities.
x_des = np.array([vector[0], vector[1], vector[2], 0, 0, 0])
curr_ja = self.group.get_current_joint_values()
jacobian = np.array(self.group.get_jacobian_matrix(curr_ja))
th_ex = np.matmul(np.linalg.pinv(jacobian), x_des)
# Give the command.
cmd = {name: th for name, th in zip(self.joint_names, th_ex)}
self.limb.set_joint_velocities(cmd)
# Sleep for a bit.
r.sleep()
# Compute the force exerted on the end-effector.
f = self.limb.endpoint_effort()["force"]
f = np.array([f.x, f.y, f.z])
fnorm = np.linalg.norm(f)
if rospy.get_time() - t0 > timeout:
rospy.loginfo("timeout occurred before contact")
return
rospy.loginfo("detected contact!")
return
def move_with_compliance(self, vector, speed, dist, timeout):
init_xyz = self.limb.endpoint_pose()["position"]
init_xyz = np.array([init_xyz.x, init_xyz.y, init_xyz.z])
curr_dist = 0.0
t0 = rospy.get_time()
r = rospy.Rate(50)
# TODO: implement compliance lol.
# The vector is just a direction, so we set a particular speed.
vector = vector / np.linalg.norm(vector) * speed
while curr_dist < dist:
# Compute the estimated joint velocities.
x_des = np.array([vector[0], vector[1], vector[2], 0, 0, 0])
curr_ja = self.group.get_current_joint_values()
jacobian = np.array(self.group.get_jacobian_matrix(curr_ja))
th_ex = np.matmul(np.linalg.pinv(jacobian), x_des)
# Give the command.
cmd = {name: th for name, th in zip(self.joint_names, th_ex)}
self.limb.set_joint_velocities(cmd)
# Sleep for a bit.
r.sleep()
# Compute the force exerted on the end-effector.
curr_xyz = self.limb.endpoint_pose()["position"]
curr_xyz = np.array([curr_xyz.x, curr_xyz.y, curr_xyz.z])
curr_dist = np.linalg.norm(curr_xyz - init_xyz)
if rospy.get_time() - t0 > timeout:
rospy.loginfo("timeout occurred before distance reached")
return
rospy.loginfo("distance moved")
return
def rot_align(self, v1, v2):
# Aligns v1 to v2
v1 = v1 / np.linalg.norm(v1)
v2 = v2 / np.linalg.norm(v2)
angle = np.arccos(np.dot(v1, v2))
axis = np.cross(v1, v2)
axis = axis/np.linalg.norm(axis)
qx = axis[0] * np.sin(angle/2)
qy = axis[1] * np.sin(angle/2)
qz = axis[2] * np.sin(angle/2)
qw = np.cos(angle/2)
quat = np.array([qx, qy, qz, qw])
quat = quat / np.linalg.norm(quat)
return quat
def move_with_flow(self, vector, hand_axis, tip_rot, speed, dist, timeout):
init_xyz = self.limb.endpoint_pose()["position"]
init_xyz = np.array([init_xyz.x, init_xyz.y, init_xyz.z])
init_quat = self.limb.endpoint_pose()["orientation"]
init_quat = np.array([init_quat.x, init_quat.y, init_quat.z, init_quat.w])
# Calculate goal rotation
hand_rot = R.from_quat(tip_rot).as_dcm()
diff_quat = self.rot_align(hand_axis, -vector)
diff_rot = R.from_quat(diff_quat).as_euler('xyz')
curr_dist = 0.0
t0 = rospy.get_time()
r = rospy.Rate(50)
# The vector is just a direction, so we set a particular speed.
vector = vector / np.linalg.norm(vector) * speed
rot_speed = 0.8 * diff_rot
while curr_dist < dist:
# Compute the estimated joint velocities.
# x_des = np.array([vector[0], vector[1], vector[2], goal_rpy[0], goal_rpy[1], goal_rpy[2]])
x_des = np.array([vector[0], vector[1], vector[2], rot_speed[0], rot_speed[1], rot_speed[2]
])
curr_ja = self.group.get_current_joint_values()
jacobian = np.array(self.group.get_jacobian_matrix(curr_ja))
th_ex = np.matmul(np.linalg.pinv(jacobian), x_des)
# Give the command.
cmd = {name: th for name, th in zip(self.joint_names, th_ex)}
self.limb.set_joint_velocities(cmd)
# Sleep for a bit.
r.sleep()
# Compute the force exerted on the end-effector.
curr_xyz = self.limb.endpoint_pose()["position"]
curr_xyz = np.array([curr_xyz.x, curr_xyz.y, curr_xyz.z])
curr_dist = np.linalg.norm(curr_xyz - init_xyz)
if rospy.get_time() - t0 > timeout:
rospy.loginfo("timeout occurred before distance reached")
return
rospy.loginfo("distance moved")
return
def move_with_dagger(self, vector, hand_axis, tip_rot, speed, dist, timeout):
init_xyz = self.limb.endpoint_pose()["position"]
init_xyz = np.array([init_xyz.x, init_xyz.y, init_xyz.z])
curr_dist = 0.0
t0 = rospy.get_time()
r = rospy.Rate(50)
# The vector is just a direction, so we set a particular speed.
vector = vector / np.linalg.norm(vector) * speed
while curr_dist < dist:
# Compute the estimated joint velocities.
# x_des = np.array([vector[0], vector[1], vector[2], goal_rpy[0], goal_rpy[1], goal_rpy[2]])
x_des = np.array([vector[0, 0], vector[0, 1], vector[0, 2], 0, 0, 0
])
curr_ja = self.group.get_current_joint_values()
jacobian = np.array(self.group.get_jacobian_matrix(curr_ja))
th_ex = np.matmul(np.linalg.pinv(jacobian), x_des)
# Give the command.
cmd = {name: th for name, th in zip(self.joint_names, th_ex)}
self.limb.set_joint_velocities(cmd)
# Sleep for a bit.
r.sleep()
# Compute the force exerted on the end-effector.
curr_xyz = self.limb.endpoint_pose()["position"]
curr_xyz = np.array([curr_xyz.x, curr_xyz.y, curr_xyz.z])
curr_dist = np.linalg.norm(curr_xyz - init_xyz)
if rospy.get_time() - t0 > timeout:
rospy.loginfo("timeout occurred before distance reached")
return
rospy.loginfo("distance moved")
return
def begin_suction(self):
cmd_1a = DigitalOutputCommand()
cmd_1b = DigitalOutputCommand()
cmd_1a.name = "right_valve_1a"
cmd_1b.name = "right_valve_1b"
cmd_1a.value = 0
cmd_1b.value = 1
self.publishVacuumCommand.publish(cmd_1a)
self.publishVacuumCommand.publish(cmd_1b)
cmd_1a.value = 1
cmd_1b.value = 0
self.publishVacuumCommand.publish(cmd_1a)
self.publishVacuumCommand.publish(cmd_1b)
def end_suction(self):
cmd_1a = DigitalOutputCommand()
cmd_1b = DigitalOutputCommand()
cmd_1a.name = "right_valve_1a"
cmd_1b.name = "right_valve_1b"
cmd_1a.value = 1
cmd_1b.value = 0
self.publishVacuumCommand.publish(cmd_1a)
self.publishVacuumCommand.publish(cmd_1b)
cmd_1a.value = 0
cmd_1b.value = 1
self.publishVacuumCommand.publish(cmd_1a)
self.publishVacuumCommand.publish(cmd_1b)
@click.group()
def cli():
pass
@cli.command()
def compliance_demo():
print("compliance")
# Get current pose.
# Compute a velocity.
# Move in that velocity until a distance has been reached.
pass
@cli.command()
def contact_demo():
rospy.init_node("contact_demo")
robot = FlowbotController()
print('Created the flowbot controller!')
robot.move_until_contact([0, 0, -1], 0.001, 10.0)
print('Done moving...')
time.sleep(10)
#robot.begin_suction()
#time.sleep(1)
#robot.move_with_compliance([0, 0, 1], 0.03, 0.01, 5.0)
#time.sleep(1)
#robot.move_with_compliance([0, 0, 1], 0.03, 0.01, 5.0)
#time.sleep(1)
#robot.move_with_compliance([0, 0, 1], 0.03, 0.01, 5.0)
#robot.end_suction()
rospy.signal_shutdown("done")
exit(0)
if __name__ == "__main__":
cli()