-
Notifications
You must be signed in to change notification settings - Fork 3
/
barrett_manager.py
344 lines (289 loc) · 13.7 KB
/
barrett_manager.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
import roslib; roslib.load_manifest( "trajectory_planner" )
import rospy
from numpy import pi, eye, dot, cross, linalg, sqrt, ceil, size
from numpy import hstack, vstack, mat, array, arange, fabs
import geometry_msgs
from std_srvs.srv import Empty as EmptySrv
from std_msgs.msg import Empty as EmptyMsg
from tf import transformations as tr
import pr_msgs.msg
import pr_msgs.srv
from OWDUtil import *
from ft_manager import *
def move_hand(angles, blocking = True):
"""@brief - set joint angles of BarrettHand
@param angles - target joint angles.
@param blocking - if true, block until the hand stops moving
Returns whether movement succeeded, why it failed if necessary, and the final position for the joint motions.
"""
success = MoveHandSrv(1, angles)
if not success:
return success, "MovehandSrv Failed", []
if blocking:
success, reason, position = WaitForHand(0)
else:
reason = "nonblocking"
position = []
return success, reason, position
def move_hand_velocity(velocities, blocking = True):
"""@brief - set joint velocities of BarrettHand
@param velocities - target joint velocities.
@param blocking - if true, block until the hand stops moving
Returns whether movement succeeded, why it failed if necessary, and the final position for the joint motions.
"""
success = MoveHandSrv(2, velocities)
if not success:
return success, "MovehandSrv in velocitymode failed", []
if blocking:
success, reason, position = WaitForHand(0)
else:
reason = "nonblocking"
position = []
return success, reason, position
def move_hand_percentage(percentage):
"""@brief - set joint angles of BarrettHand relative to current position
@param percentage - target relative positions.
"""
jnts = get_barrett_joints()
move_hand(array([jnts[0] * percentage, jnts[1] * percentage, jnts[2] * percentage, jnts[3]]))
def get_barrett_joints():
"""@brief - Get the current position of the hand.
Returns the current joint position of the hand as a list.
"""
msg = rospy.wait_for_message("/bhd/handstate", pr_msgs.msg.BHState, 5)
return msg.positions
def stop_barrett():
"""@brief - Set desired hand position to current hand position, stopping the hand
Returns true of motion is successfully stopped.
"""
try:
msg = rospy.wait_for_message("/bhd/handstate", pr_msgs.msg.BHState, 5)
MoveHandSrv(1,msg.positions)
return 1, msg.positions
except:
return 0, []
def relax_barrett():
"""@brief - Stop applying torque to the hand.
"""
return RelaxHandSrv()
def open_barrett(open_aperture = 0):
"""@brief - open the fingers without changing the spread angle
"""
success = 0
reason = "Exception in open barrett"
positions = []
try:
msg = rospy.wait_for_message("/bhd/handstate", pr_msgs.msg.BHState, 5)
success,reason,positions = move_hand([open_aperture,open_aperture,open_aperture,msg.positions[3]])
except:
pass
return success,reason, positions
def close_barrett():
"""@brief - move fingers completely closed in position mode.
"""
return open_barrett(2)
def set_handstate_callback(global_data, callback_func):
"""@brief - Helper function to activate hand state callback
@param global_data - data structure for storing handstate subscriber.
@param callback_func - A function that process handstate messages.
"""
if not global_data.hs_sub == []:
sub = global_data.hs_sub
global_data.hs_sub = []
sub.unregister()
del sub
global_data.hs_sub = rospy.Subscriber("/bhd/handstate", pr_msgs.msg.BHState, callback_func)
def unset_handstate_callback(global_data):
"""@brief - Helper function to deactivate hand state callback function
@param global_data - data structure for storing handstate subscriber.
Returns true if there is an active callback function, false otherwise.
"""
if not global_data.hs_sub == []:
sub = global_data.hs_sub
global_data.hs_sub = []
sub.unregister()
del sub
return True
return False
def guarded_close_single(active_fingers, desired_positions, tactile_threshold, ft_threshold, sleep_len = 5):
"""@brief - Close BarrettHand fingers until they reach a certain position or contact is made, one finger at a time.
@param active_fingers - set which fingers are allowed to move.
@param desired_positions - set desired positions for fingers.
@param tactile_threshold - the reading for the tactile sensors which indicates contact
@param ft_threshold - the reading for the force-torque sensor which indicates contact
@param sleep_len - time to wait for completion of motion.
Returns list of fingers that have not made contact and current position of fingers.
"""
output_active_fingers = cp.deepcopy(active_fingers)
for i in range(len(active_fingers)):
my_active_fingers = [0,0,0]
my_active_fingers[i] = active_fingers[i]
ftm = FtFingerMonitor(my_active_fingers, desired_positions, tactile_threshold, ft_threshold)
sleep(sleep_len)
output_active_fingers[i] = ftm.active_fingers[i]
ftm.close()
return output_active_fingers, ftm.current_position
def guarded_stepwise_close(active_fingers, starting_positions, desired_positions, tactile_threshold, ft_threshold, num_steps = 10):
"""@brief - Close BarrettHand fingers incrementally until they reach a certain position or contact is made, one finger at a time.
@param active_fingers - set which fingers are allowed to move.
@param desired_positions - set desired positions for fingers.
@param tactile_threshold - the reading for the tactile sensors which indicates contact
@param ft_threshold - the reading for the force-torque sensor which indicates contact
@param sleep_len - time to wait for completion of motion.
Returns list of fingers that have not made contact and current position of fingers.
"""
for i in range(num_steps):
next_desired_positions = [((i+1.0)*(desired_positions[j]-starting_positions[j])/(float(num_steps)) +starting_positions[j]) for j in range(3)]
next_desired_positions.append(desired_positions[3])
print next_desired_positions
active_fingers, current_position = guarded_close_single(active_fingers, next_desired_positions, tactile_threshold, ft_threshold, .5)
if not any(active_fingers):
break
return active_fingers, current_position
def get_tactile_threshold(multiplier = 5, offset = 1):
def modify_data_tuple(t,m,o):
return [r*m+o for r in t]
tm = rospy.wait_for_message("/bhd/tactile", pr_msgs.msg.BHTactile, 5)
tm.finger1 = modify_data_tuple(tm.finger1,multiplier, offset)
tm.finger2 = modify_data_tuple(tm.finger2,multiplier, offset)
tm.finger3 = modify_data_tuple(tm.finger3,multiplier, offset)
tm.palm = modify_data_tuple(tm.palm,multiplier, offset)
return tm
class FingerMotionMonitor():
"""@brief - A monitor that is activated by the state of the barrett hand. It monitors both the joint state and
the tactile sensors of the hand.
"""
def __init__(self, active_fingers, desired_position, tactile_threshold):
"""@brief - Create a new monitor
@param active_fingers - Which fingers are active right now
@param desired_position - The hand state that the monitor is waiting to achieve.
@param tactile_threshold - The limit at which the tactile sensors are considered activaed.
"""
self.active_fingers = active_fingers
self.desired_positions = desired_position
self.current_position = []
self.current_strain = []
self.current_tactile = []
#Parameters for linear model of strain that depends on finger position.
self.strain_model = [3000.0,0]
hs_msg = rospy.wait_for_message("/bhd/handstate", pr_msgs.msg.BHState)
#initial call of hand state functor. Necessary to intialize current strain and position.
self.hand_state_func(hs_msg)
self.tactile_threshold = tactile_threshold
self.handstate_subscriber = rospy.Subscriber("/bhd/handstate", pr_msgs.msg.BHState, self.hand_state_func)
self.tactile_subscriber = rospy.Subscriber("/bhd/tactile", pr_msgs.msg.BHTactile, self.tactile_state_func)
self.reset_desired_position()
def reset_desired_position(self):
"""@brief - Set the desired positions for the hand to the current position
"""
desired_positions = list(self.current_position)
desired_positions[-1] = self.desired_positions[-1]
for i in range(len(self.active_fingers)):
if self.active_fingers[i] == 1:
desired_positions[i] = self.desired_positions[i]
self.desired_positions = desired_positions
print "ft_finger_move desired positions:"
print self.desired_positions
MoveHandSrv(1,self.desired_positions)
def hand_state_func(self, hs_msg):
"""@brief - The callback function for the handstate listener.
Reads the current state of hand positions, strains, and whether the state of the
hand has gone beyond some of the set thresholds.
@param hs_msg - The input handstate message.
"""
self.current_position = hs_msg.positions
self.current_strain = hs_msg.strain
any_change = False
for i in range(len(self.current_strain)):
if not self.active_fingers[i]:
continue
if (self.current_strain[i] > self.strain_model[1] * self.current_position[i]
+ self.strain_model[0]):
print "high strain on finger %i: %d"%(i, self.current_strain[i])
self.active_fingers[i] = 0
any_change = True
if any_change:
self.reset_desired_position()
def tactile_state_func(self, tac_msg):
"""@brief - The callback function for the tactile sensors of the hand.
@param tac_msg - The input tactile message.
"""
def any_greater(l1, l2):
return [i for i in range(len(l1)) if l1[i] > l2[i]] != []
#If any of the taxels are above the predetermined threshold, set the finger to inactive.
any_change = False
if any_greater(tac_msg.finger1, self.tactile_threshold.finger1):
print "high tactile 1"
any_change = True
self.active_fingers[0] = 0
if any_greater(tac_msg.finger2, self.tactile_threshold.finger2):
print "high tactile 2"
any_change = True
self.active_fingers[1] = 0
if any_greater(tac_msg.finger3, self.tactile_threshold.finger3):
print "high tactile 3"
any_change = True
self.active_fingers[2] = 0
if any_change:
self.reset_desired_position()
def close(self):
"""@brief - Unsubscribe function that is part of the destructor for this function.
"""
try:
self.handstate_subscriber.unregister()
self.tactile_subscriber.unregister()
except:
print "unregister failed"
class FtFingerMonitor(FingerMotionMonitor):
"""@brief - FingerMotionMonitor with added capabilities to monitor the force torque sensor as well.
"""
def __init__(self, active_fingers, desired_position, tactile_threshold, ft_threshold):
"""@brief - Create new FtFingerMonitor
@param active_fingers - The fingers that are allowed to move
@param desired_position - The goal pose of the hand.
@param tactile_threshold - The threshold for tactile sensors to inactivate their respective finger.
@param ft_threshold - The threshold for the force-torque sensor force level to deactivate the hand.
"""
self.ft_sub = []
self.handstate_subscriber = []
self.tactile_subscriber = []
self.active_fingers = []
self.desired_positions = []
self.current_position = []
self.current_strain = []
self.current_tactile = []
self.strain_model = [3000.0]
tare_FT()
FingerMotionMonitor.__init__(self,active_fingers, desired_position, tactile_threshold)
self.ft_sub = rospy.Subscriber("ForceTorque/Readings", geometry_msgs.msg.Wrench, self.ft_callback)
self.hits = 5
self.delay_num = 5
self.ft_threshold = ft_threshold
def ft_callback(self, wrench_msg):
"""@brief - Test if the total force is above some threshold.
Because the sensor is noisy, we make sure that the threshold is crossed for a certain number of sensor
readings.
"""
if linalg.norm(array([wrench_msg.force.x, wrench_msg.force.y, wrench_msg.force.z])) > self.ft_threshold:
self.hits -= 1
print self.hits
else:
self.hits = self.delay_num
if self.hits <= 0:
self.active_fingers = [0,0,0]
self.reset_desired_position()
def reset_desired_position(self):
"""@brief - Test if all fingers have been inactivated. If it has, stop the monitor.
"""
FingerMotionMonitor.reset_desired_position(self)
if not any(self.active_fingers):
self.close()
def close(self):
"""@brief - Unregister all active subscribers before destroying the monitor.
"""
print 'closing'
try:
self.ft_sub.unregister()
except:
print "ft_already_closed"
FingerMotionMonitor.close(self)