-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamixel_io.py
1085 lines (921 loc) · 42.5 KB
/
dynamixel_io.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Software License Agreement (BSD License)
#
# Copyright (c) 2010-2011, Cody Jorgensen, Antons Rebguns.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of University of Arizona nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
__author__ = 'Cody Jorgensen, Antons Rebguns'
__copyright__ = 'Copyright (c) 2010-2011 Cody Jorgensen, Antons Rebguns'
__license__ = 'BSD'
__maintainer__ = 'Antons Rebguns'
__email__ = '[email protected]'
import time
import serial
from array import array
from binascii import b2a_hex
from threading import Lock
#Iñigo
import RPi.GPIO as GPIO
#
from dynamixel_const import *
exception = None
class DynamixelIO(object):
""" Provides low level IO with the Dynamixel servos through pyserial. Has the
ability to write instruction packets, request and read register value
packets, send and receive a response to a ping packet, and send a SYNC WRITE
multi-servo instruction packet.
"""
def __init__(self, port, baudrate, readback_echo=False):
""" Constructor takes serial port and baudrate as arguments. """
try:
self.serial_mutex = Lock()
self.ser = None
self.ser = serial.Serial(port)
self.ser.setTimeout(0.015)
self.ser.baudrate = baudrate
self.port_name = port
self.readback_echo = readback_echo
#
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
#
except SerialOpenError:
raise SerialOpenError(port, baudrate)
def __del__(self):
""" Destructor calls DynamixelIO.close """
self.close()
def close(self):
"""
Be nice, close the serial port.
"""
if self.ser:
self.ser.flushInput()
self.ser.flushOutput()
self.ser.close()
#
GPIO.cleanup()
def __write_serial(self, data):
#put output GPIO ready to write
GPIO.output(18, GPIO.HIGH)
#
self.ser.flushInput()
self.ser.flushOutput()
self.ser.write(data)
if self.readback_echo:
self.ser.read(len(data))
#Leave it as RX
time.sleep(0.00001)
GPIO.output(18, GPIO.LOW)
def __read_response(self, servo_id):
data = []
try:
data.extend(self.ser.read(4))
if not data[0:2] == ['\xff', '\xff']: raise Exception('Wrong packet prefix %s' % data[0:2])
data.extend(self.ser.read(ord(data[3])))
data = array('B', ''.join(data)).tolist() # [int(b2a_hex(byte), 16) for byte in data]
except Exception, e:
raise DroppedPacketError('Invalid response received from motor %d. %s' % (servo_id, e))
# verify checksum
checksum = 255 - sum(data[2:-1]) % 256
if not checksum == data[-1]: raise ChecksumError(servo_id, data, checksum)
return data
def read(self, servo_id, address, size):
""" Read "size" bytes of data from servo with "servo_id" starting at the
register with "address". "address" is an integer between 0 and 57. It is
recommended to use the constants in module dynamixel_const for readability.
To read the position from servo with id 1, the method should be called
like:
read(1, DXL_GOAL_POSITION_L, 2)
"""
# Number of bytes following standard header (0xFF, 0xFF, id, length)
length = 4 # instruction, address, size, checksum
# directly from AX-12 manual:
# Check Sum = ~ (ID + LENGTH + INSTRUCTION + PARAM_1 + ... + PARAM_N)
# If the calculated value is > 255, the lower byte is the check sum.
checksum = 255 - ( (servo_id + length + DXL_READ_DATA + address + size) % 256 )
# packet: FF FF ID LENGTH INSTRUCTION PARAM_1 ... CHECKSUM
packet = [0xFF, 0xFF, servo_id, length, DXL_READ_DATA, address, size, checksum]
packetStr = array('B', packet).tostring() # same as: packetStr = ''.join([chr(byte) for byte in packet])
with self.serial_mutex:
self.__write_serial(packetStr)
# wait for response packet from the motor
timestamp = time.time()
time.sleep(0.0013)#0.00235)
# read response
data = self.__read_response(servo_id)
data.append(timestamp)
return data
def write(self, servo_id, address, data):
""" Write the values from the "data" list to the servo with "servo_id"
starting with data[0] at "address", continuing through data[n-1] at
"address" + (n-1), where n = len(data). "address" is an integer between
0 and 49. It is recommended to use the constants in module dynamixel_const
for readability. "data" is a list/tuple of integers.
To set servo with id 1 to position 276, the method should be called
like:
write(1, DXL_GOAL_POSITION_L, (20, 1))
"""
# Number of bytes following standard header (0xFF, 0xFF, id, length)
length = 3 + len(data) # instruction, address, len(data), checksum
# directly from AX-12 manual:
# Check Sum = ~ (ID + LENGTH + INSTRUCTION + PARAM_1 + ... + PARAM_N)
# If the calculated value is > 255, the lower byte is the check sum.
checksum = 255 - ((servo_id + length + DXL_WRITE_DATA + address + sum(data)) % 256)
# packet: FF FF ID LENGTH INSTRUCTION PARAM_1 ... CHECKSUM
packet = [0xFF, 0xFF, servo_id, length, DXL_WRITE_DATA, address]
packet.extend(data)
packet.append(checksum)
packetStr = array('B', packet).tostring() # packetStr = ''.join([chr(byte) for byte in packet])
with self.serial_mutex:
self.__write_serial(packetStr)
# wait for response packet from the motor
timestamp = time.time()
time.sleep(0.0013)
# read response
data = self.__read_response(servo_id)
data.append(timestamp)
return data
def sync_write(self, address, data):
""" Use Broadcast message to send multiple servos instructions at the
same time. No "status packet" will be returned from any servos.
"address" is an integer between 0 and 49. It is recommended to use the
constants in module dynamixel_const for readability. "data" is a tuple of
tuples. Each tuple in "data" must contain the servo id followed by the
data that should be written from the starting address. The amount of
data can be as long as needed.
To set servo with id 1 to position 276 and servo with id 2 to position
550, the method should be called like:
sync_write(DXL_GOAL_POSITION_L, ( (1, 20, 1), (2 ,38, 2) ))
"""
# Calculate length and sum of all data
flattened = [value for servo in data for value in servo]
# Number of bytes following standard header (0xFF, 0xFF, id, length) plus data
length = 4 + len(flattened)
checksum = 255 - ((DXL_BROADCAST + length + \
DXL_SYNC_WRITE + address + len(data[0][1:]) + \
sum(flattened)) % 256)
# packet: FF FF ID LENGTH INSTRUCTION PARAM_1 ... CHECKSUM
packet = [0xFF, 0xFF, DXL_BROADCAST, length, DXL_SYNC_WRITE, address, len(data[0][1:])]
packet.extend(flattened)
packet.append(checksum)
packetStr = array('B', packet).tostring() # packetStr = ''.join([chr(byte) for byte in packet])
with self.serial_mutex:
self.__write_serial(packetStr)
def ping(self, servo_id):
""" Ping the servo with "servo_id". This causes the servo to return a
"status packet". This can tell us if the servo is attached and powered,
and if so, if there are any errors.
"""
# Number of bytes following standard header (0xFF, 0xFF, id, length)
length = 2 # instruction, checksum
# directly from AX-12 manual:
# Check Sum = ~ (ID + LENGTH + INSTRUCTION + PARAM_1 + ... + PARAM_N)
# If the calculated value is > 255, the lower byte is the check sum.
checksum = 255 - ((servo_id + length + DXL_PING) % 256)
# packet: FF FF ID LENGTH INSTRUCTION CHECKSUM
packet = [0xFF, 0xFF, servo_id, length, DXL_PING, checksum]
packetStr = array('B', packet).tostring()
with self.serial_mutex:
self.__write_serial(packetStr)
# wait for response packet from the motor
timestamp = time.time()
time.sleep(0.0013)
# read response
try:
response = self.__read_response(servo_id)
response.append(timestamp)
except Exception, e:
response = []
if response:
self.exception_on_error(response[4], servo_id, 'ping')
return response
def test_bit(self, number, offset):
mask = 1 << offset
return (number & mask)
######################################################################
# These function modify EEPROM data which persists after power cycle #
######################################################################
def set_id(self, old_id, new_id):
"""
Sets a new unique number to identify a motor. The range from 1 to 253
(0xFD) can be used.
"""
response = self.write(old_id, DXL_ID, [new_id])
if response:
self.exception_on_error(response[4], old_id, 'setting id to %d' % new_id)
return response
def set_baud_rate(self, servo_id, baud_rate):
"""
Sets servo communication speed. The range from 0 to 254.
"""
response = self.write(servo_id, DXL_BAUD_RATE, [baud_rate])
if response:
self.exception_on_error(response[4], servo_id, 'setting baud rate to %d' % baud_rate)
return response
def set_return_delay_time(self, servo_id, delay):
"""
Sets the delay time from the transmission of Instruction Packet until
the return of Status Packet. 0 to 254 (0xFE) can be used, and the delay
time per data value is 2 usec.
"""
response = self.write(servo_id, DXL_RETURN_DELAY_TIME, [delay])
if response:
self.exception_on_error(response[4], servo_id, 'setting return delay time to %d' % delay)
return response
def set_angle_limit_cw(self, servo_id, angle_cw):
"""
Set the min (CW) angle of rotation limit.
"""
loVal = int(angle_cw % 256)
hiVal = int(angle_cw >> 8)
response = self.write(servo_id, DXL_CW_ANGLE_LIMIT_L, (loVal, hiVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting CW angle limits to %d' % angle_cw)
return response
def set_angle_limit_ccw(self, servo_id, angle_ccw):
"""
Set the max (CCW) angle of rotation limit.
"""
loVal = int(angle_ccw % 256)
hiVal = int(angle_ccw >> 8)
response = self.write(servo_id, DXL_CCW_ANGLE_LIMIT_L, (loVal, hiVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting CCW angle limits to %d' % angle_ccw)
return response
def set_angle_limits(self, servo_id, min_angle, max_angle):
"""
Set the min (CW) and max (CCW) angle of rotation limits.
"""
loMinVal = int(min_angle % 256)
hiMinVal = int(min_angle >> 8)
loMaxVal = int(max_angle % 256)
hiMaxVal = int(max_angle >> 8)
# set 4 register values with low and high bytes for min and max angles
response = self.write(servo_id, DXL_CW_ANGLE_LIMIT_L, (loMinVal, hiMinVal, loMaxVal, hiMaxVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting CW and CCW angle limits to %d and %d' %(min_angle, max_angle))
return response
def set_drive_mode(self, servo_id, is_slave=False, is_reverse=False):
"""
Sets the drive mode for EX-106 motors
"""
drive_mode = (is_slave << 1) + is_reverse
response = self.write(servo_id, DXL_DRIVE_MODE, [drive_mode])
if response:
self.exception_on_error(response[4], servo_id, 'setting drive mode to %d' % drive_mode)
return response
def set_voltage_limit_min(self, servo_id, min_voltage):
"""
Set the minimum voltage limit.
NOTE: the absolute min is 5v
"""
if min_voltage < 5: min_voltage = 5
minVal = int(min_voltage * 10)
response = self.write(servo_id, DXL_DOWN_LIMIT_VOLTAGE, [minVal])
if response:
self.exception_on_error(response[4], servo_id, 'setting minimum voltage level to %d' % min_voltage)
return response
def set_voltage_limit_max(self, servo_id, max_voltage):
"""
Set the maximum voltage limit.
NOTE: the absolute min is 25v
"""
if max_voltage > 25: max_voltage = 25
maxVal = int(max_voltage * 10)
response = self.write(servo_id, DXL_UP_LIMIT_VOLTAGE, [maxVal])
if response:
self.exception_on_error(response[4], servo_id, 'setting maximum voltage level to %d' % max_voltage)
return response
def set_voltage_limits(self, servo_id, min_voltage, max_voltage):
"""
Set the min and max voltage limits.
NOTE: the absolute min is 5v and the absolute max is 25v
"""
if min_voltage < 5: min_voltage = 5
if max_voltage > 25: max_voltage = 25
minVal = int(min_voltage * 10)
maxVal = int(max_voltage * 10)
response = self.write(servo_id, DXL_DOWN_LIMIT_VOLTAGE, (minVal, maxVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting min and max voltage levels to %d and %d' %(min_voltage, max_voltage))
return response
###############################################################
# These functions can send a single command to a single servo #
###############################################################
def set_torque_enabled(self, servo_id, enabled):
"""
Sets the value of the torque enabled register to 1 or 0. When the
torque is disabled the servo can be moved manually while the motor is
still powered.
"""
response = self.write(servo_id, DXL_TORQUE_ENABLE, [enabled])
if response:
self.exception_on_error(response[4], servo_id, '%sabling torque' % 'en' if enabled else 'dis')
return response
def set_compliance_margin_cw(self, servo_id, margin):
"""
The error between goal position and present position in CW direction.
The range of the value is 0 to 255, and the unit is the same as Goal Position.
The greater the value, the more difference occurs.
"""
response = self.write(servo_id, DXL_CW_COMPLIANCE_MARGIN, [margin])
if response:
self.exception_on_error(response[4], servo_id, 'setting CW compliance margin to %d' % margin)
return response
def set_compliance_margin_ccw(self, servo_id, margin):
"""
The error between goal position and present position in CCW direction.
The range of the value is 0 to 255, and the unit is the same as Goal Position.
The greater the value, the more difference occurs.
"""
response = self.write(servo_id, DXL_CCW_COMPLIANCE_MARGIN, [margin])
if response:
self.exception_on_error(response[4], servo_id, 'setting CCW compliance margin to %d' % margin)
return response
def set_compliance_margins(self, servo_id, margin_cw, margin_ccw):
"""
The error between goal position and present position in CCW direction.
The range of the value is 0 to 255, and the unit is the same as Goal Position.
The greater the value, the more difference occurs.
"""
response = self.write(servo_id, DXL_CW_COMPLIANCE_MARGIN, (margin_cw, margin_ccw))
if response:
self.exception_on_error(response[4], servo_id, 'setting CW and CCW compliance margins to values %d and %d' %(margin_cw, margin_ccw))
return response
def set_compliance_slope_cw(self, servo_id, slope):
"""
Sets the level of Torque near the goal position in CW direction.
Compliance Slope is set in 7 steps, the higher the value, the more flexibility is obtained.
"""
response = self.write(servo_id, DXL_CW_COMPLIANCE_SLOPE, [slope])
if response:
self.exception_on_error(response[4], servo_id, 'setting CW compliance slope to %d' % slope)
return response
def set_compliance_slope_ccw(self, servo_id, slope):
"""
Sets the level of Torque near the goal position in CCW direction.
Compliance Slope is set in 7 steps, the higher the value, the more flexibility is obtained.
"""
response = self.write(servo_id, DXL_CCW_COMPLIANCE_SLOPE, [slope])
if response:
self.exception_on_error(response[4], servo_id, 'setting CCW compliance slope to %d' % slope)
return response
def set_compliance_slopes(self, servo_id, slope_cw, slope_ccw):
"""
Sets the level of Torque near the goal position in CW/CCW direction.
Compliance Slope is set in 7 steps, the higher the value, the more flexibility is obtained.
"""
response = self.write(servo_id, DXL_CW_COMPLIANCE_SLOPE, (slope_cw, slope_ccw))
if response:
self.exception_on_error(response[4], servo_id, 'setting CW and CCW compliance slopes to %d and %d' %(slope_cw, slope_ccw))
return response
def set_d_gain(self, servo_id, d_gain):
"""
Sets the value of derivative action of PID controller.
Gain value is in range 0 to 254.
"""
response = self.write(servo_id, DXL_D_GAIN, [d_gain])
if response:
self.exception_on_error(response[4], servo_id, 'setting D gain value of PID controller to %d' % slope)
return response
def set_i_gain(self, servo_id, i_gain):
"""
Sets the value of integral action of PID controller.
Gain value is in range 0 to 254.
"""
response = self.write(servo_id, DXL_I_GAIN, [i_gain])
if response:
self.exception_on_error(response[4], servo_id, 'setting I gain value of PID controller to %d' % slope)
return response
def set_p_gain(self, servo_id, p_gain):
"""
Sets the value of proportional action of PID controller.
Gain value is in range 0 to 254.
"""
response = self.write(servo_id, DXL_P_GAIN, [p_gain])
if response:
self.exception_on_error(response[4], servo_id, 'setting P gain value of PID controller to %d' % slope)
return response
def set_punch(self, servo_id, punch):
"""
Sets the limit value of torque being reduced when the output torque is
decreased in the Compliance Slope area. In other words, it is the mimimum
torque. The initial value is 32 (0x20) and can be extended up to 1023
(0x3FF). (Refer to Compliance margin & Slope)
"""
loVal = int(punch % 256)
hiVal = int(punch >> 8)
response = self.write(servo_id, DXL_PUNCH_L, (loVal, hiVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting punch to %d' % punch)
return response
def set_acceleration(self, servo_id, acceleration):
"""
Sets the acceleration. The unit is 8.583 Degree / sec^2.
0 - acceleration control disabled, 1-254 - valid range for acceleration.
"""
model = self.get_model_number(servo_id)
if not model in DXL_MODEL_TO_PARAMS:
raise UnsupportedFeatureError(model, DXL_GOAL_ACCELERATION)
if DXL_GOAL_ACCELERATION in DXL_MODEL_TO_PARAMS[model]['features']:
response = self.write(servo_id, DXL_GOAL_ACCELERATION, (acceleration, ))
if response:
self.exception_on_error(response[4], servo_id, 'setting acceleration to %d' % acceleration)
return response
else:
raise UnsupportedFeatureError(model, DXL_GOAL_ACCELERATION)
def set_position(self, servo_id, position):
"""
Set the servo with servo_id to the specified goal position.
Position value must be positive.
"""
loVal = int(position % 256)
hiVal = int(position >> 8)
response = self.write(servo_id, DXL_GOAL_POSITION_L, (loVal, hiVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting goal position to %d' % position)
return response
def set_speed(self, servo_id, speed):
"""
Set the servo with servo_id to the specified goal speed.
Speed can be negative only if the dynamixel is in "freespin" mode.
"""
# split speed into 2 bytes
if speed >= 0:
loVal = int(speed % 256)
hiVal = int(speed >> 8)
else:
loVal = int((1023 - speed) % 256)
hiVal = int((1023 - speed) >> 8)
# set two register values with low and high byte for the speed
response = self.write(servo_id, DXL_GOAL_SPEED_L, (loVal, hiVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting moving speed to %d' % speed)
return response
def set_torque_limit(self, servo_id, torque):
"""
Sets the value of the maximum torque limit for servo with id servo_id.
Valid values are 0 to 1023 (0x3FF), and the unit is about 0.1%.
For example, if the value is 512 only 50% of the maximum torque will be used.
If the power is turned on, the value of Max Torque (Address 14, 15) is used as the initial value.
"""
loVal = int(torque % 256)
hiVal = int(torque >> 8)
response = self.write(servo_id, DXL_TORQUE_LIMIT_L, (loVal, hiVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting torque limit to %d' % torque)
return response
def set_goal_torque(self, servo_id, torque):
"""
Set the servo to torque control mode (similar to wheel mode, but controlling the torque)
Valid values are from -1023 to 1023.
Anything outside this range or 'None' disables torque control.
"""
model = self.get_model_number(servo_id)
if not model in DXL_MODEL_TO_PARAMS:
raise UnsupportedFeatureError(model, DXL_TORQUE_CONTROL_MODE)
valid_torque = torque is not None and torque >= -1023 and torque <= 1023
if torque is not None and torque < 0:
torque = 1024 - torque
if DXL_TORQUE_CONTROL_MODE in DXL_MODEL_TO_PARAMS[model]['features']:
if valid_torque:
loVal = int(torque % 256); hiVal = int(torque >> 8);
response = self.write(servo_id, DXL_GOAL_TORQUE_L, (loVal, hiVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting goal torque to %d' % torque)
response = self.write(servo_id, DXL_TORQUE_CONTROL_MODE, (int(valid_torque), ))
if response:
self.exception_on_error(response[4], servo_id, 'enabling torque mode')
return response
else:
raise UnsupportedFeatureError(model, DXL_TORQUE_CONTROL_MODE)
def set_position_and_speed(self, servo_id, position, speed):
"""
Set the servo with servo_id to specified position and speed.
Speed can be negative only if the dynamixel is in "freespin" mode.
"""
# split speed into 2 bytes
if speed >= 0:
loSpeedVal = int(speed % 256)
hiSpeedVal = int(speed >> 8)
else:
loSpeedVal = int((1023 - speed) % 256)
hiSpeedVal = int((1023 - speed) >> 8)
# split position into 2 bytes
loPositionVal = int(position % 256)
hiPositionVal = int(position >> 8)
response = self.write(servo_id, DXL_GOAL_POSITION_L, (loPositionVal, hiPositionVal, loSpeedVal, hiSpeedVal))
if response:
self.exception_on_error(response[4], servo_id, 'setting goal position to %d and moving speed to %d' %(position, speed))
return response
def set_led(self, servo_id, led_state):
"""
Turn the LED of servo motor on/off.
Possible boolean state values:
True - turn the LED on,
False - turn the LED off.
"""
response = self.write(servo_id, DXL_LED, [led_state])
if response:
self.exception_on_error(response[4], servo_id,
'setting a LED to %s' % led_state)
return response
#################################################################
# These functions can send multiple commands to multiple servos #
# These commands are used in ROS wrapper as they don't send a #
# response packet, ROS wrapper gets motor states at a set rate #
#################################################################
def set_multi_torque_enabled(self, valueTuples):
"""
Method to set multiple servos torque enabled.
Should be called as such:
set_multi_servos_to_torque_enabled( (id1, True), (id2, True), (id3, True) )
"""
# use sync write to broadcast multi servo message
self.sync_write(DXL_TORQUE_ENABLE, tuple(valueTuples))
def set_multi_compliance_margin_cw(self, valueTuples):
"""
Set different CW compliance margin for multiple servos.
Should be called as such:
set_multi_compliance_margin_cw( ( (id1, margin1), (id2, margin2), (id3, margin3) ) )
"""
self.sync_write(DXL_CW_COMPLIANCE_MARGIN, tuple(valueTuples))
def set_multi_compliance_margin_ccw(self, valueTuples):
"""
Set different CCW compliance margin for multiple servos.
Should be called as such:
set_multi_compliance_margin_ccw( ( (id1, margin1), (id2, margin2), (id3, margin3) ) )
"""
self.sync_write(DXL_CCW_COMPLIANCE_MARGIN, tuple(valueTuples))
def set_multi_compliance_margins(self, valueTuples):
"""
Set different CW and CCW compliance margins for multiple servos.
Should be called as such:
set_multi_compliance_margins( ( (id1, cw_margin1, ccw_margin1), (id2, cw_margin2, ccw_margin2) ) )
"""
self.sync_write(DXL_CW_COMPLIANCE_MARGIN, tuple(valueTuples))
def set_multi_compliance_slope_cw(self, valueTuples):
"""
Set different CW compliance slope for multiple servos.
Should be called as such:
set_multi_compliance_slope_cw( ( (id1, slope1), (id2, slope2), (id3, slope3) ) )
"""
self.sync_write(DXL_CW_COMPLIANCE_SLOPE, tuple(valueTuples))
def set_multi_compliance_slope_ccw(self, valueTuples):
"""
Set different CCW compliance slope for multiple servos.
Should be called as such:
set_multi_compliance_slope_ccw( ( (id1, slope1), (id2, slope2), (id3, slope3) ) )
"""
self.sync_write(DXL_CCW_COMPLIANCE_SLOPE, tuple(valueTuples))
def set_multi_compliance_slopes(self, valueTuples):
"""
Set different CW and CCW compliance slopes for multiple servos.
Should be called as such:
set_multi_compliance_slopes( ( (id1, cw_slope1, ccw_slope1), (id2, cw_slope2, ccw_slope2) ) )
"""
self.sync_write(DXL_CW_COMPLIANCE_SLOPE, tuple(valueTuples))
def set_multi_punch(self, valueTuples):
"""
Set different punch values for multiple servos.
NOTE: according to documentation, currently this value is not being used.
Should be called as such:
set_multi_punch( ( (id1, punch1), (id2, punch2), (id3, punch3) ) )
"""
# prepare value tuples for call to syncwrite
writeableVals = []
for sid,punch in valueTuples:
# split punch into 2 bytes
loVal = int(punch % 256)
hiVal = int(punch >> 8)
writeableVals.append( (sid, loVal, hiVal) )
# use sync write to broadcast multi servo message
self.sync_write(DXL_PUNCH_L, writeableVals)
def set_multi_position(self, valueTuples):
"""
Set different positions for multiple servos.
Should be called as such:
set_multi_position( ( (id1, position1), (id2, position2), (id3, position3) ) )
"""
# prepare value tuples for call to syncwrite
writeableVals = []
for vals in valueTuples:
sid = vals[0]
position = vals[1]
# split position into 2 bytes
loVal = int(position % 256)
hiVal = int(position >> 8)
writeableVals.append( (sid, loVal, hiVal) )
# use sync write to broadcast multi servo message
self.sync_write(DXL_GOAL_POSITION_L, writeableVals)
def set_multi_speed(self, valueTuples):
"""
Set different speeds for multiple servos.
Should be called as such:
set_multi_speed( ( (id1, speed1), (id2, speed2), (id3, speed3) ) )
"""
# prepare value tuples for call to syncwrite
writeableVals = []
for vals in valueTuples:
sid = vals[0]
speed = vals[1]
# split speed into 2 bytes
if speed >= 0:
loVal = int(speed % 256)
hiVal = int(speed >> 8)
else:
loVal = int((1023 - speed) % 256)
hiVal = int((1023 - speed) >> 8)
writeableVals.append( (sid, loVal, hiVal) )
# use sync write to broadcast multi servo message
self.sync_write(DXL_GOAL_SPEED_L, writeableVals)
def set_multi_torque_limit(self, valueTuples):
"""
Set different torque limits for multiple servos.
Should be called as such:
set_multi_torque_limit( ( (id1, torque1), (id2, torque2), (id3, torque3) ) )
"""
# prepare value tuples for call to syncwrite
writeableVals = []
for sid,torque in valueTuples:
# split torque into 2 bytes
loVal = int(torque % 256)
hiVal = int(torque >> 8)
writeableVals.append( (sid, loVal, hiVal) )
# use sync write to broadcast multi servo message
self.sync_write(DXL_TORQUE_LIMIT_L, writeableVals)
def set_multi_position_and_speed(self, valueTuples):
"""
Set different positions and speeds for multiple servos.
Should be called as such:
set_multi_position_and_speed( ( (id1, position1, speed1), (id2, position2, speed2), (id3, position3, speed3) ) )
"""
# prepare value tuples for call to syncwrite
writeableVals = []
for vals in valueTuples:
sid = vals[0]
position = vals[1]
speed = vals[2]
# split speed into 2 bytes
if speed >= 0:
loSpeedVal = int(speed % 256)
hiSpeedVal = int(speed >> 8)
else:
loSpeedVal = int((1023 - speed) % 256)
hiSpeedVal = int((1023 - speed) >> 8)
# split position into 2 bytes
loPositionVal = int(position % 256)
hiPositionVal = int(position >> 8)
writeableVals.append( (sid, loPositionVal, hiPositionVal, loSpeedVal, hiSpeedVal) )
# use sync write to broadcast multi servo message
self.sync_write(DXL_GOAL_POSITION_L, tuple(writeableVals))
#################################
# Servo status access functions #
#################################
def get_model_number(self, servo_id):
""" Reads the servo's model number (e.g. 12 for AX-12+). """
response = self.read(servo_id, DXL_MODEL_NUMBER_L, 2)
if response:
self.exception_on_error(response[4], servo_id, 'fetching model number')
return response[5] + (response[6] << 8)
def get_firmware_version(self, servo_id):
""" Reads the servo's firmware version. """
response = self.read(servo_id, DXL_VERSION, 1)
if response:
self.exception_on_error(response[4], servo_id, 'fetching firmware version')
return response[5]
def get_return_delay_time(self, servo_id):
""" Reads the servo's return delay time. """
response = self.read(servo_id, DXL_RETURN_DELAY_TIME, 1)
if response:
self.exception_on_error(response[4], servo_id, 'fetching return delay time')
return response[5]
def get_angle_limits(self, servo_id):
"""
Returns the min and max angle limits from the specified servo.
"""
# read in 4 consecutive bytes starting with low value of clockwise angle limit
response = self.read(servo_id, DXL_CW_ANGLE_LIMIT_L, 4)
if response:
self.exception_on_error(response[4], servo_id, 'fetching CW/CCW angle limits')
# extract data valus from the raw data
cwLimit = response[5] + (response[6] << 8)
ccwLimit = response[7] + (response[8] << 8)
# return the data in a dictionary
return {'min':cwLimit, 'max':ccwLimit}
def get_drive_mode(self, servo_id):
""" Reads the servo's drive mode. """
response = self.read(servo_id, DXL_DRIVE_MODE, 1)
if response:
self.exception_on_error(response[4], servo_id, 'fetching drive mode')
return response[5]
def get_voltage_limits(self, servo_id):
"""
Returns the min and max voltage limits from the specified servo.
"""
response = self.read(servo_id, DXL_DOWN_LIMIT_VOLTAGE, 2)
if response:
self.exception_on_error(response[4], servo_id, 'fetching voltage limits')
# extract data valus from the raw data
min_voltage = response[5] / 10.0
max_voltage = response[6] / 10.0
# return the data in a dictionary
return {'min':min_voltage, 'max':max_voltage}
def get_position(self, servo_id):
""" Reads the servo's position value from its registers. """
response = self.read(servo_id, DXL_PRESENT_POSITION_L, 2)
if response:
self.exception_on_error(response[4], servo_id, 'fetching present position')
position = response[5] + (response[6] << 8)
return position
def get_speed(self, servo_id):
""" Reads the servo's speed value from its registers. """
response = self.read(servo_id, DXL_PRESENT_SPEED_L, 2)
if response:
self.exception_on_error(response[4], servo_id, 'fetching present speed')
speed = response[5] + (response[6] << 8)
if speed > 1023:
return 1023 - speed
return speed
def get_voltage(self, servo_id):
""" Reads the servo's voltage. """
response = self.read(servo_id, DXL_PRESENT_VOLTAGE, 1)
if response:
self.exception_on_error(response[4], servo_id, 'fetching supplied voltage')
return response[5] / 10.0
def get_current(self, servo_id):
""" Reads the servo's current consumption (if supported by model) """
model = self.get_model_number(servo_id)
if not model in DXL_MODEL_TO_PARAMS:
raise UnsupportedFeatureError(model, DXL_CURRENT_L)
if DXL_CURRENT_L in DXL_MODEL_TO_PARAMS[model]['features']:
response = self.read(servo_id, DXL_CURRENT_L, 2)
if response:
self.exception_on_error(response[4], servo_id, 'fetching sensed current')
current = response[5] + (response[6] << 8)
return 0.0045 * (current - 2048)
if DXL_SENSED_CURRENT_L in DXL_MODEL_TO_PARAMS[model]['features']:
response = self.read(servo_id, DXL_SENSED_CURRENT_L, 2)
if response:
self.exception_on_error(response[4], servo_id, 'fetching sensed current')
current = response[5] + (response[6] << 8)
return 0.01 * (current - 512)
else:
raise UnsupportedFeatureError(model, DXL_CURRENT_L)
def get_feedback(self, servo_id):
"""
Returns the id, goal, position, error, speed, load, voltage, temperature
and moving values from the specified servo.
"""
# read in 17 consecutive bytes starting with low value for goal position
response = self.read(servo_id, DXL_GOAL_POSITION_L, 17)
if response:
self.exception_on_error(response[4], servo_id, 'fetching full servo status')
if len(response) == 24:
# extract data values from the raw data
goal = response[5] + (response[6] << 8)
position = response[11] + (response[12] << 8)
error = position - goal
speed = response[13] + ( response[14] << 8)
if speed > 1023: speed = 1023 - speed
load_raw = response[15] + (response[16] << 8)
load_direction = 1 if self.test_bit(load_raw, 10) else 0
load = (load_raw & int('1111111111', 2)) / 1024.0
if load_direction == 1: load = -load
voltage = response[17] / 10.0
temperature = response[18]
moving = response[21]
timestamp = response[-1]
# return the data in a dictionary
return { 'timestamp': timestamp,
'id': servo_id,
'goal': goal,
'position': position,
'error': error,
'speed': speed,
'load': load,
'voltage': voltage,
'temperature': temperature,
'moving': bool(moving) }
def get_led(self, servo_id):
"""
Get status of the LED. Boolean return values:
True - LED is on,
False - LED is off.
"""
response = self.read(servo_id, DXL_LED, 1)
if response:
self.exception_on_error(response[4], servo_id,
'fetching LED status')
return bool(response[5])
def exception_on_error(self, error_code, servo_id, command_failed):
global exception
exception = None
ex_message = '[servo #%d on %s@%sbps]: %s failed' % (servo_id, self.ser.port, self.ser.baudrate, command_failed)
if not isinstance(error_code, int):
msg = 'Communcation Error ' + ex_message