-
Notifications
You must be signed in to change notification settings - Fork 4
/
RoboticArm.py
executable file
·487 lines (392 loc) · 20.1 KB
/
RoboticArm.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
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
# pylint: disable=trailing-whitespace
# pylint: disable=bad-whitespace
# pylint: disable=bad-continuation
# pylint: disable=invalid-name
"""
Written by Dr. Elishai Ezra Tsur
@ The Neuro-Biomorphoc Engineering Lab (NBEL-lab.com)
@ The Open University of Israel
@ 7.9.2020
This work is using Robotis' Dynamixel SDK package (pip install dynamixel-sdk)
and it is utilized here to control Trossen Robotics' arms.
Code was tested on the:
1. 5DOF WidowX 200 (https://www.trossenrobotics.com/widowx-200-robot-arm.aspx)
2. 6DOF ViperX 300 (https://www.trossenrobotics.com/viperx-300-robot-arm-6dof.aspx)
"""
import time
import numpy as np
from dynamixel_sdk import *
# Offset angles for the physical arm in relative to the IK mpdel
offset_relative_to_IK_Model = {1: 90, 2: 180, 3: 180, 4: 180,
5: 180, 6: 0, 7: 180, 8: 0, 9: 0}
def robot_to_model_position(robot_position):
return [ np.deg2rad(robot_position[1]-offset_relative_to_IK_Model[1]),
-1 * np.deg2rad(robot_position[2]-offset_relative_to_IK_Model[2]),
-1 * np.deg2rad(robot_position[4]-offset_relative_to_IK_Model[4]),
np.deg2rad(robot_position[6]-offset_relative_to_IK_Model[6]),
-1 * np.deg2rad(robot_position[7]-offset_relative_to_IK_Model[7])]
def model_to_robot_position(model_position):
f = [ np.rad2deg( model_position[0])+offset_relative_to_IK_Model[1],
np.rad2deg(-1 * model_position[1])+offset_relative_to_IK_Model[2],
np.rad2deg(-1 * model_position[2])+offset_relative_to_IK_Model[4],
np.rad2deg( model_position[3])+offset_relative_to_IK_Model[6],
np.rad2deg(-1 * model_position[4])+offset_relative_to_IK_Model[7]]
return {1: int(f[0]), 2: int(f[1]), 3: int(f[1]), 4: int(f[2]),
5: int(f[2]), 6: int(f[3]), 7: int(f[4]), 8: 180, 9: 180}
Robot = {'Real':{
'CMD':
{'Baud Rate' : {'Address': 8, 'Value': {9600 : 0,
57600 : 1,
115200 : 2,
1000000: 3,
2000000: 4,
3000000: 5,
4000000: 6,
4500000: 7}},
'Operating mode' : {'Address': 11, 'Value': {'Torque' : 0,
'Velocity' : 1,
'Position' : 3,
'Extended Position' : 4,
'PWM' : 16}},
'Torque Enable' : {'Address': 64, 'Value': {'OFF': 0, 'ON' : 1}},
'LED' : {'Address': 65, 'Value': {'OFF': 0, 'ON' : 1}},
'Goal Position' : {'Address': 116},
'Present Position' : {'Address': 132},
'Goal torque' : {'Address': 102},
'Ranges' : {1: range (-1000, 1000),
2: range (-1000, 1000),
3: range (-1000, 1000),
4: range (-1000, 1000),
5: range (-1000, 1000),
6: range (-1000, 1000),
7: range (-1000, 1000),
8: range (-1000, 1000),
9: range (-1000, 1000)},
'Homing Offset' : {'Address': 20},
'Limit velocity' : {'Address': 100, 'Value': 600}, # ranging [-885, 885]
'Limit torque' : {'Address': 38, 'Value': 250} # ranging [-1193, 1193], 2.69mA per step, 3.210A
},
'Priority': [[4, 5], [2, 3], [1], [6], [7], [8], [9]],
# Note that engines 2 and 5 were set to reverse mode to allow
# both to be configured similarly to their counterpart.
'Home' : {1:181, 2:180, 3:180, 4:180, 5:180, 6:0, 7:270, 8:180, 9:180},
'Nap' : {1:181, 2:288, 3:72, 4:90, 5:270, 6:0, 7:198, 8:180, 9:180}}
}
class RoboticArm:
def __init__ (self, CMD_dict=Robot, COM_ID = '/dev/ttyUSB0', PROTOCOL_VERSION = 2.0):
self.CMD = CMD_dict['Real']['CMD']
self.priority = CMD_dict['Real']['Priority']
self.home_position = CMD_dict['Real']['Home']
self.nap = CMD_dict['Real']['Nap']
self.encoder_unit = 11.37777778
#map sim to real
self.id1 = None
self.id2 = None
self.id3 = None
self.id4 = None
self.id5 = None
self.id6 = None
self.id7 = None
self.id8 = None
self.initialize(COM_ID, PROTOCOL_VERSION)
def initialize(self, COM_ID, PROTOCOL_VERSION):
self.portHandler = PortHandler (COM_ID)
self.packetHandler = PacketHandler(PROTOCOL_VERSION)
# Open port
if self.portHandler.openPort():
print("Succeeded to open the port")
else:
print("Failed to open the port")
# Broadcast ping the Dynamixel
ENGINES_list, COMM_result = self.packetHandler.broadcastPing(self.portHandler)
if COMM_result != COMM_SUCCESS:
print("%s" % self.packetHandler.getTxRxResult(COMM_result))
self.ENGINES = []
print("Detected Engines :")
for engine in ENGINES_list:
print("[ID:%03d] model version : %d | firmware version : %d" %
(engine, ENGINES_list.get(engine)[0], ENGINES_list.get(engine)[1]))
if ENGINES_list.get(engine)[0] > 1000: # Checking for identified engines (rather then controllers)
self.ENGINES.append(engine)
# # Set port baudrate
# print('Setting baud rate to: 1 Mbps')
# for ID in self.ENGINES:
# self.send_single_cmd(ID,
# self.CMD['Baud Rate']['Address'],
# self.CMD['Baud Rate']['Value'][1000000])
# Initializing mode and constraints
self.reset_state()
#Initializing syncronized actuation of coupled joints
self.groupSyncWrite = GroupSyncWrite(
self.portHandler, self.packetHandler, self.CMD['Goal Position']['Address'], 4) # 4 for full CMD
self.groupSyncWrite.clearParam()
def reset_state (self):
# Setting operation mode to position (default; getting into home position)
# This command has to be excuted first. Changing modes reset default values.
print('Releasing torque')
self.release_torque()
print('Setting operation mode to: position')
for ID in self.ENGINES:
if ID == 6:
self.send_single_cmd(ID,
self.CMD['Operating mode']['Address'],
self.CMD['Operating mode']['Value']['Extended Position'])
self.send_full_cmd(ID,
self.CMD['Homing Offset']['Address'],
round(180*self.encoder_unit))
else:
self.send_single_cmd(ID,
self.CMD['Operating mode']['Address'],
self.CMD['Operating mode']['Value']['Position'])
if ID <=5:
self.send_full_cmd(ID,
84,
500)
else:
self.send_full_cmd(ID,
84,
200)
# Limiting velocity
print('Limiting velocity to: {}%'.format(100*(self.CMD['Limit velocity']['Value']/885)))
for ID in self.ENGINES:
self.send_full_cmd(ID,
self.CMD['Limit velocity']['Address'],
self.CMD['Limit velocity']['Value'])
# Limiting torque
print('Limiting torque to: {}%'.format(100*(self.CMD['Limit torque']['Value']/1193)))
for ID in self.ENGINES:
self.send_half_cmd(ID,
self.CMD['Limit torque']['Address'],
self.CMD['Limit torque']['Value'])
def go_home (self) :
self.enable_torque()
print("Setting home position")
self.set_position(self.home_position)
def take_a_nap (self) :
self.enable_torque()
print("Setting napping position")
self.set_position(self.nap)
def reboot (self, IDs = 'all'):
if IDs == 'all':
IDs = self.ENGINES
for DXL_ID in IDs:
if DXL_ID < 12: # Assuming less than 12 engines' arm.
dxl_comm_result, dxl_error = self.packetHandler.reboot(self.portHandler, DXL_ID)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % self.packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % self.packetHandler.getRxPacketError(dxl_error))
def enable_torque (self, IDs = 'all'):
if IDs == 'all':
IDs = self.ENGINES
for DXL_ID in IDs:
self.send_single_cmd(DXL_ID,
self.CMD['Torque Enable']['Address'],
self.CMD['Torque Enable']['Value']['ON'])
def release_torque (self, IDs = 'all'):
if IDs == 'all':
IDs = self.ENGINES
for DXL_ID in IDs:
self.send_single_cmd(DXL_ID,
self.CMD['Torque Enable']['Address'],
self.CMD['Torque Enable']['Value']['OFF'])
def play_by_priority (self, sequance, delay, mode='position'):
if mode == 'position':
for seq in sequance:
self.set_position_by_priority(seq)
time.sleep(delay)
elif mode == 'torque':
targets_TDs = []
for seq in sequance:
for ID in seq:
if ID not in targets_TDs:
targets_TDs.append(ID)
# Setting targets to torque control mode
print('Setting operation mode of engines {} to: torque'.format(targets_TDs))
for ID in targets_TDs:
self.release_torque(targets_TDs) # Before changing mode, torque have to be released
self.send_single_cmd(ID,
self.CMD['Operating mode']['Address'],
self.CMD['Operating mode']['Value']['Torque'])
self.enable_torque(targets_TDs)
for seq in sequance:
self.set_torque(seq)
time.sleep(delay)
else:
print('Working mode is not recognized. Supported modes: position / torque')
def set_torque (self, torque_dict):
for IDs in self.priority:
for ID in IDs:
if ID not in torque_dict:
continue
target_torque = round(torque_dict[ID])
print('Setting {} to {}'.format(ID, target_torque))
self.send_half_cmd(ID,
self.CMD['Goal torque']['Address'],
target_torque)
def watch_for_execution(self, ID, target):
watchdog = time.time()
lapsed_time = 0
while True:
lapsed_time = time.time() - watchdog
current_position = self.get_position(ID)
error = round(target-current_position)
print('{} Deviation is {}'.format(ID, error))
con = abs(round(target-current_position)) < 500
if con:
return True
if lapsed_time > 2.5:
print('watch dog executed. ID: {} is at {} instead of {}'.format(ID, current_position, target))
# Set target to current position.
self.send_full_cmd(ID,
self.CMD['Goal Position']['Address'],
self.get_position(ID))
return False
def set_position(self, position_dict):
for ID in position_dict:
target_position = round(position_dict[ID])
target_position = round(target_position * self.encoder_unit)
target_position_byte_array = [DXL_LOBYTE(DXL_LOWORD(target_position)),
DXL_HIBYTE(DXL_LOWORD(target_position)),
DXL_LOBYTE(DXL_HIWORD(target_position)),
DXL_HIBYTE(DXL_HIWORD(target_position))]
self.groupSyncWrite.addParam(ID, target_position_byte_array)
# print('Setting position at {}'.format(position_dict))
self.groupSyncWrite.txPacket()
# for ID in position_dict:
# self.watch_for_execution(ID, round(position_dict[ID] * self.encoder_unit))
self.groupSyncWrite.clearParam()
def set_position_by_priority (self, position_dict):
for IDs in self.priority:
if len(IDs) > 1: # Synchronized actuation
for ID in IDs:
target_position = round(position_dict[ID] * self.encoder_unit)
target_position_byte_array = [DXL_LOBYTE(DXL_LOWORD(target_position)),
DXL_HIBYTE(DXL_LOWORD(target_position)),
DXL_LOBYTE(DXL_HIWORD(target_position)),
DXL_HIBYTE(DXL_HIWORD(target_position))]
self.groupSyncWrite.addParam(ID, target_position_byte_array)
print('Setting {} to {}'.format(IDs, target_position))
self.groupSyncWrite.txPacket()
for ID in IDs:
self.watch_for_execution(ID, target_position)
else:
ID = IDs[0]
if ID not in position_dict:
continue
target_position = round(position_dict[ID])
if target_position not in self.CMD['Ranges'][ID]:
print('{} is not in range. Engine {} is constrained to {}'.format(
position_dict[ID], ID, self.CMD['Ranges'][ID]))
continue
# Multiplying angle by self.encoder_unit to convert to register value
target_position = round(target_position * self.encoder_unit)
print('Setting {} to {}'.format(ID, target_position))
self.send_full_cmd(ID,
self.CMD['Goal Position']['Address'],
target_position)
self.watch_for_execution(ID, target_position)
def get_position(self, ID):
dxl_present_position, dxl_comm_result, dxl_error = self.packetHandler.read4ByteTxRx(
self.portHandler, ID, self.CMD['Present Position']['Address'])
if dxl_comm_result != COMM_SUCCESS:
print("%s" % self.packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % self.packetHandler.getRxPacketError(dxl_error))
return dxl_present_position
def destruct(self):
self.reboot()
self.portHandler.closePort()
def send_full_cmd(self, ID, adr, val):
dxl_comm_result, dxl_error = self.packetHandler.write4ByteTxRx(
self.portHandler, ID, adr, val)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % self.packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % self.packetHandler.getRxPacketError(dxl_error))
print('ID: {}, Address: {}, value: {}'.format(ID, adr, val))
# else:
# print("[ID:%03d] CMD executed successfully" % ID)
def send_half_cmd(self, ID, adr, val):
dxl_comm_result, dxl_error = self.packetHandler.write2ByteTxRx(
self.portHandler, ID, adr, val)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % self.packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % self.packetHandler.getRxPacketError(dxl_error))
print('ID: {}, Address: {}, value: {}'.format(ID, adr, val))
# else:
# print("[ID:%03d] CMD executed successfully" % ID)
def send_single_cmd(self, ID, adr, val):
dxl_comm_result, dxl_error = self.packetHandler.write1ByteTxRx(
self.portHandler, ID, adr, val)
if dxl_comm_result != COMM_SUCCESS:
print("%s" % self.packetHandler.getTxRxResult(dxl_comm_result))
elif dxl_error != 0:
print("%s" % self.packetHandler.getRxPacketError(dxl_error))
print('ID: {}, Address: {}, value: {}'.format(ID, adr, val))
# else:
# print("[ID:%03d] CMD executed successfully" % ID)
def get_positions_in_rad(self):
res = {}
for i in range(1, 10):
j = self.get_position(i)
res[i] = np.deg2rad(j/self.encoder_unit)
return res
def set_map_from_nap(self, sim_nap):
real_nap = self.get_positions_in_rad()
self.id1 = real_nap[1]-sim_nap[0]
self.id2 = real_nap[2]+sim_nap[1]
self.id3 = self.id2
self.id4 = real_nap[4]+sim_nap[2]
self.id5 = real_nap[5]-sim_nap[2]
self.id6 = real_nap[6]+sim_nap[3]
self.id7 = real_nap[7]+sim_nap[4]
self.id8 = real_nap[8]-sim_nap[5]
def map_sim_to_real(self, sim_positions = None):
if self.id1 is None:
print("Mapping is not set!!")
exit(1)
real_joints = {
1: np.rad2deg(self.id1 + sim_positions[0]),
2: np.rad2deg(self.id2 - sim_positions[1]),
3: np.rad2deg(self.id3 + sim_positions[1]),
4: np.rad2deg(self.id4 - sim_positions[2]),
5: np.rad2deg(self.id5 + sim_positions[2]),
6: np.rad2deg(self.id6 - sim_positions[3]),
7: np.rad2deg(self.id7 - sim_positions[4]),
8: np.rad2deg(self.id8 + sim_positions[5])
}
return real_joints
def map_to_angles(self, sim_positions = None):
real_joints = {
1: np.rad2deg(sim_positions[0]),
2: np.rad2deg(sim_positions[1]),
3: np.rad2deg(sim_positions[1]),
4: np.rad2deg(sim_positions[2]),
5: np.rad2deg(sim_positions[2]),
6: np.rad2deg(sim_positions[3]),
7: np.rad2deg(sim_positions[4]),
8: np.rad2deg(sim_positions[5])
}
return real_joints
def map_adjustments(self):
adjustmets = {
1: np.rad2deg(self.id1),
2: np.rad2deg(self.id2),
3: np.rad2deg(self.id3),
4: np.rad2deg(self.id4),
5: np.rad2deg(self.id5),
6: np.rad2deg(self.id6),
7: np.rad2deg(self.id7),
8: np.rad2deg(self.id8)
}
return adjustmets
def set_position_from_sim(self, sim_positions):
# print("Sim : ", self.map_to_angles(sim_positions))
# print("Real: ", self.map_sim_to_real(sim_positions) )
# print("Adju: ", self.map_adjustments() )
self.set_position(self.map_sim_to_real(sim_positions))