-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUltraBorg.py
executable file
·1792 lines (1504 loc) · 64.4 KB
/
UltraBorg.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: latin-1
"""
This module is designed to communicate with the UltraBorg
Use by creating an instance of the class, call the Init function, then command as desired, e.g.
import UltraBorg
UB = UltraBorg.UltraBorg()
UB.Init()
# User code here, use UB to control the board
Multiple boards can be used when configured with different I²C addresses by creating multiple instances, e.g.
import UltraBorg
UB1 = UltraBorg.UltraBorg()
UB2 = UltraBorg.UltraBorg()
UB1.i2cAddress = 0x44
UB2.i2cAddress = 0x45
UB1.Init()
UB2.Init()
# User code here, use UB1 and UB2 to control each board separately
For explanations of the functions available call the Help function, e.g.
import UltraBorg
UB = UltraBorg.UltraBorg()
UB.Help()
See the website at www.piborg.org/ultraborg for more details
"""
# Import the libraries we need
import smbus
import types
import time
# Constant values
I2C_MAX_LEN = 4
USM_US_TO_MM = 0.171500
PWM_MIN = 2000 # Should be a 1 ms burst, typical servo minimum
PWM_MAX = 4000 # Should be a 2 ms burst, typical servo maximum
DELAY_AFTER_EEPROM = 0.01 # Time to wait after updating an EEPROM value before reading
PWM_UNSET = 0xFFFF
READ_TOO_FAST = 0xED # We seem to get this for the first byte sometimes on the RPi v2
I2C_ID_SERVO_USM = 0x36
COMMAND_GET_TIME_USM1 = 1 # Get the time measured by ultrasonic #1 in us (0 for no detection)
COMMAND_GET_TIME_USM2 = 2 # Get the time measured by ultrasonic #2 in us (0 for no detection)
COMMAND_GET_TIME_USM3 = 3 # Get the time measured by ultrasonic #3 in us (0 for no detection)
COMMAND_GET_TIME_USM4 = 4 # Get the time measured by ultrasonic #4 in us (0 for no detection)
COMMAND_SET_PWM1 = 5 # Set the PWM duty cycle for drive #1 (16 bit)
COMMAND_GET_PWM1 = 6 # Get the PWM duty cycle for drive #1 (16 bit)
COMMAND_SET_PWM2 = 7 # Set the PWM duty cycle for drive #2 (16 bit)
COMMAND_GET_PWM2 = 8 # Get the PWM duty cycle for drive #2 (16 bit)
COMMAND_SET_PWM3 = 9 # Set the PWM duty cycle for drive #3 (16 bit)
COMMAND_GET_PWM3 = 10 # Get the PWM duty cycle for drive #3 (16 bit)
COMMAND_SET_PWM4 = 11 # Set the PWM duty cycle for drive #4 (16 bit)
COMMAND_GET_PWM4 = 12 # Get the PWM duty cycle for drive #4 (16 bit)
COMMAND_CALIBRATE_PWM1 = 13 # Set the PWM duty cycle for drive #1 (16 bit, ignores limit checks)
COMMAND_CALIBRATE_PWM2 = 14 # Set the PWM duty cycle for drive #2 (16 bit, ignores limit checks)
COMMAND_CALIBRATE_PWM3 = 15 # Set the PWM duty cycle for drive #3 (16 bit, ignores limit checks)
COMMAND_CALIBRATE_PWM4 = 16 # Set the PWM duty cycle for drive #4 (16 bit, ignores limit checks)
COMMAND_GET_PWM_MIN_1 = 17 # Get the minimum allowed PWM duty cycle for drive #1
COMMAND_GET_PWM_MAX_1 = 18 # Get the maximum allowed PWM duty cycle for drive #1
COMMAND_GET_PWM_BOOT_1 = 19 # Get the startup PWM duty cycle for drive #1
COMMAND_GET_PWM_MIN_2 = 20 # Get the minimum allowed PWM duty cycle for drive #2
COMMAND_GET_PWM_MAX_2 = 21 # Get the maximum allowed PWM duty cycle for drive #2
COMMAND_GET_PWM_BOOT_2 = 22 # Get the startup PWM duty cycle for drive #2
COMMAND_GET_PWM_MIN_3 = 23 # Get the minimum allowed PWM duty cycle for drive #3
COMMAND_GET_PWM_MAX_3 = 24 # Get the maximum allowed PWM duty cycle for drive #3
COMMAND_GET_PWM_BOOT_3 = 25 # Get the startup PWM duty cycle for drive #3
COMMAND_GET_PWM_MIN_4 = 26 # Get the minimum allowed PWM duty cycle for drive #4
COMMAND_GET_PWM_MAX_4 = 27 # Get the maximum allowed PWM duty cycle for drive #4
COMMAND_GET_PWM_BOOT_4 = 28 # Get the startup PWM duty cycle for drive #4
COMMAND_SET_PWM_MIN_1 = 29 # Set the minimum allowed PWM duty cycle for drive #1
COMMAND_SET_PWM_MAX_1 = 30 # Set the maximum allowed PWM duty cycle for drive #1
COMMAND_SET_PWM_BOOT_1 = 31 # Set the startup PWM duty cycle for drive #1
COMMAND_SET_PWM_MIN_2 = 32 # Set the minimum allowed PWM duty cycle for drive #2
COMMAND_SET_PWM_MAX_2 = 33 # Set the maximum allowed PWM duty cycle for drive #2
COMMAND_SET_PWM_BOOT_2 = 34 # Set the startup PWM duty cycle for drive #2
COMMAND_SET_PWM_MIN_3 = 35 # Set the minimum allowed PWM duty cycle for drive #3
COMMAND_SET_PWM_MAX_3 = 36 # Set the maximum allowed PWM duty cycle for drive #3
COMMAND_SET_PWM_BOOT_3 = 37 # Set the startup PWM duty cycle for drive #3
COMMAND_SET_PWM_MIN_4 = 38 # Set the minimum allowed PWM duty cycle for drive #4
COMMAND_SET_PWM_MAX_4 = 39 # Set the maximum allowed PWM duty cycle for drive #4
COMMAND_SET_PWM_BOOT_4 = 40 # Set the startup PWM duty cycle for drive #4
COMMAND_GET_FILTER_USM1 = 41 # Get the filtered time measured by ultrasonic #1 in us (0 for no detection)
COMMAND_GET_FILTER_USM2 = 42 # Get the filtered time measured by ultrasonic #2 in us (0 for no detection)
COMMAND_GET_FILTER_USM3 = 43 # Get the filtered time measured by ultrasonic #3 in us (0 for no detection)
COMMAND_GET_FILTER_USM4 = 44 # Get the filtered time measured by ultrasonic #4 in us (0 for no detection)
COMMAND_GET_ID = 0x99 # Get the board identifier
COMMAND_SET_I2C_ADD = 0xAA # Set a new I2C address
COMMAND_VALUE_FWD = 1 # I2C value representing forward
COMMAND_VALUE_REV = 2 # I2C value representing reverse
COMMAND_VALUE_ON = 1 # I2C value representing on
COMMAND_VALUE_OFF = 0 # I2C value representing off
def ScanForUltraBorg(busNumber = 1):
"""
ScanForUltraBorg([busNumber])
Scans the I²C bus for a UltraBorg boards and returns a list of all usable addresses
The busNumber if supplied is which I²C bus to scan, 0 for Rev 1 boards, 1 for Rev 2 boards, if not supplied the default is 1
"""
found = []
print 'Scanning I²C bus #%d' % (busNumber)
bus = smbus.SMBus(busNumber)
for address in range(0x03, 0x78, 1):
try:
i2cRecv = bus.read_i2c_block_data(address, COMMAND_GET_ID, I2C_MAX_LEN)
if len(i2cRecv) == I2C_MAX_LEN:
if i2cRecv[1] == I2C_ID_SERVO_USM:
print 'Found UltraBorg at %02X' % (address)
found.append(address)
else:
pass
else:
pass
except KeyboardInterrupt:
raise
except:
pass
if len(found) == 0:
print 'No UltraBorg boards found, is bus #%d correct (should be 0 for Rev 1, 1 for Rev 2)' % (busNumber)
elif len(found) == 1:
print '1 UltraBorg board found'
else:
print '%d UltraBorg boards found' % (len(found))
return found
def SetNewAddress(newAddress, oldAddress = -1, busNumber = 1):
"""
SetNewAddress(newAddress, [oldAddress], [busNumber])
Scans the I²C bus for the first UltraBorg and sets it to a new I2C address
If oldAddress is supplied it will change the address of the board at that address rather than scanning the bus
The busNumber if supplied is which I²C bus to scan, 0 for Rev 1 boards, 1 for Rev 2 boards, if not supplied the default is 1
Warning, this new I²C address will still be used after resetting the power on the device
"""
if newAddress < 0x03:
print 'Error, I²C addresses below 3 (0x03) are reserved, use an address between 3 (0x03) and 119 (0x77)'
return
elif newAddress > 0x77:
print 'Error, I²C addresses above 119 (0x77) are reserved, use an address between 3 (0x03) and 119 (0x77)'
return
if oldAddress < 0x0:
found = ScanForUltraBorg(busNumber)
if len(found) < 1:
print 'No UltraBorg boards found, cannot set a new I²C address!'
return
else:
oldAddress = found[0]
print 'Changing I²C address from %02X to %02X (bus #%d)' % (oldAddress, newAddress, busNumber)
bus = smbus.SMBus(busNumber)
try:
i2cRecv = bus.read_i2c_block_data(oldAddress, COMMAND_GET_ID, I2C_MAX_LEN)
if len(i2cRecv) == I2C_MAX_LEN:
if i2cRecv[1] == I2C_ID_SERVO_USM:
foundChip = True
print 'Found UltraBorg at %02X' % (oldAddress)
else:
foundChip = False
print 'Found a device at %02X, but it is not a UltraBorg (ID %02X instead of %02X)' % (oldAddress, i2cRecv[1], I2C_ID_SERVO_USM)
else:
foundChip = False
print 'Missing UltraBorg at %02X' % (oldAddress)
except KeyboardInterrupt:
raise
except:
foundChip = False
print 'Missing UltraBorg at %02X' % (oldAddress)
if foundChip:
bus.write_byte_data(oldAddress, COMMAND_SET_I2C_ADD, newAddress)
time.sleep(0.1)
print 'Address changed to %02X, attempting to talk with the new address' % (newAddress)
try:
i2cRecv = bus.read_i2c_block_data(newAddress, COMMAND_GET_ID, I2C_MAX_LEN)
if len(i2cRecv) == I2C_MAX_LEN:
if i2cRecv[1] == I2C_ID_SERVO_USM:
foundChip = True
print 'Found UltraBorg at %02X' % (newAddress)
else:
foundChip = False
print 'Found a device at %02X, but it is not a UltraBorg (ID %02X instead of %02X)' % (newAddress, i2cRecv[1], I2C_ID_SERVO_USM)
else:
foundChip = False
print 'Missing UltraBorg at %02X' % (newAddress)
except KeyboardInterrupt:
raise
except:
foundChip = False
print 'Missing UltraBorg at %02X' % (newAddress)
if foundChip:
print 'New I²C address of %02X set successfully' % (newAddress)
else:
print 'Failed to set new I²C address...'
# Class used to control UltraBorg
class UltraBorg:
"""
This module is designed to communicate with the UltraBorg
busNumber I²C bus on which the UltraBorg is attached (Rev 1 is bus 0, Rev 2 is bus 1)
bus the smbus object used to talk to the I²C bus
i2cAddress The I²C address of the UltraBorg chip to control
foundChip True if the UltraBorg chip can be seen, False otherwise
printFunction Function reference to call when printing text, if None "print" is used
"""
# Shared values used by this class
busNumber = 1 # Check here for Rev 1 vs Rev 2 and select the correct bus
i2cAddress = I2C_ID_SERVO_USM # I²C address, override for a different address
bus = None
foundChip = False
printFunction = None
# Default calibration adjustments to standard values
PWM_MIN_1 = PWM_MIN
PWM_MAX_1 = PWM_MAX
PWM_MIN_2 = PWM_MIN
PWM_MAX_2 = PWM_MAX
PWM_MIN_3 = PWM_MIN
PWM_MAX_3 = PWM_MAX
PWM_MIN_4 = PWM_MIN
PWM_MAX_4 = PWM_MAX
def Print(self, message):
"""
Print(message)
Wrapper used by the UltraBorg instance to print messages, will call printFunction if set, print otherwise
"""
if self.printFunction == None:
print message
else:
self.printFunction(message)
def NoPrint(self, message):
"""
NoPrint(message)
Does nothing, intended for disabling diagnostic printout by using:
UB = UltraBorg.UltraBorg()
UB.printFunction = UB.NoPrint
"""
pass
def Init(self, tryOtherBus = True):
"""
Init([tryOtherBus])
Prepare the I2C driver for talking to the UltraBorg
If tryOtherBus is True or omitted, this function will attempt to use the other bus if the UltraBorg devices can not be found on the current busNumber
"""
self.Print('Loading UltraBorg on bus %d, address %02X' % (self.busNumber, self.i2cAddress))
# Open the bus
self.bus = smbus.SMBus(self.busNumber)
# Check for UltraBorg
try:
i2cRecv = self.bus.read_i2c_block_data(self.i2cAddress, COMMAND_GET_ID, I2C_MAX_LEN)
if len(i2cRecv) == I2C_MAX_LEN:
if i2cRecv[1] == I2C_ID_SERVO_USM:
self.foundChip = True
self.Print('Found UltraBorg at %02X' % (self.i2cAddress))
else:
self.foundChip = False
self.Print('Found a device at %02X, but it is not a UltraBorg (ID %02X instead of %02X)' % (self.i2cAddress, i2cRecv[1], I2C_ID_SERVO_USM))
else:
self.foundChip = False
self.Print('Missing UltraBorg at %02X' % (self.i2cAddress))
except KeyboardInterrupt:
raise
except:
self.foundChip = False
self.Print('Missing UltraBorg at %02X' % (self.i2cAddress))
# See if we are missing chips
if not self.foundChip:
self.Print('UltraBorg was not found')
if tryOtherBus:
if self.busNumber == 1:
self.busNumber = 0
else:
self.busNumber = 1
self.Print('Trying bus %d instead' % (self.busNumber))
self.Init(False)
else:
self.Print('Are you sure your UltraBorg is properly attached, the correct address is used, and the I2C drivers are running?')
self.bus = None
else:
self.Print('UltraBorg loaded on bus %d' % (self.busNumber))
# Read the calibration settings from the UltraBorg
self.PWM_MIN_1 = self.GetWithRetry(self.GetServoMinimum1, 5)
self.PWM_MAX_1 = self.GetWithRetry(self.GetServoMaximum1, 5)
self.PWM_MIN_2 = self.GetWithRetry(self.GetServoMinimum2, 5)
self.PWM_MAX_2 = self.GetWithRetry(self.GetServoMaximum2, 5)
self.PWM_MIN_3 = self.GetWithRetry(self.GetServoMinimum3, 5)
self.PWM_MAX_3 = self.GetWithRetry(self.GetServoMaximum3, 5)
self.PWM_MIN_4 = self.GetWithRetry(self.GetServoMinimum4, 5)
self.PWM_MAX_4 = self.GetWithRetry(self.GetServoMaximum4, 5)
def GetWithRetry(self, function, count):
"""
value = GetWithRetry(function, count)
Attempts to read a value multiple times before giving up
Pass a get function with no parameters
e.g.
distance = GetWithRetry(UB.GetDistance1, 5)
Will try UB.GetDistance1() upto 5 times, returning when it gets a value
Useful for ensuring a read is successful
"""
value = None
for i in range(count):
okay = True
try:
value = function()
except KeyboardInterrupt:
raise
except:
okay = False
if okay:
break
return value
def SetWithRetry(self, setFunction, getFunction, value, count):
"""
worked = SetWithRetry(setFunction, getFunction, value, count)
Attempts to write a value multiple times before giving up
Pass a set function with one parameter, and a get function no parameters
The get function will be used to check if the set worked, if not it will be repeated
e.g.
worked = SetWithRetry(UB.SetServoMinimum1, UB.GetServoMinimum1, 2000, 5)
Will try UB.SetServoMinimum1(2000) upto 5 times, returning when UB.GetServoMinimum1 returns 2000.
Useful for ensuring a write is successful
"""
for i in range(count):
okay = True
try:
setFunction(value)
readValue = getFunction()
except KeyboardInterrupt:
raise
except:
okay = False
if okay:
if readValue == value:
break
else:
okay = False
return okay
def ReadWithCheck(self, address, command, length):
"""
i2cRecv = ReadWithCheck(address, command, length)
Attempts an I2C read, checks if the bus tried to read too fast and re-tries
Only intended for internal usage
"""
unfinished = True
while unfinished:
i2cRecv = self.bus.read_i2c_block_data(address, command, length)
if len(i2cRecv) > 0:
if i2cRecv[0] == READ_TOO_FAST:
self.Print('I2C read was too fast, retrying')
else:
unfinished = False
else:
self.Print('Empty I2C reply, retrying...')
return i2cRecv
def GetDistance1(self):
"""
distance = GetDistance1()
Gets the filtered distance for ultrasonic module #1 in millimeters
Returns 0 for no object detected or no ultrasonic module attached
If you need a faster response try GetRawDistance1 instead (no filtering)
e.g.
0 -> No object in range
25 -> Object 25 mm away
1000 -> Object 1000 mm (1 m) away
3500 -> Object 3500 mm (3.5 m) away
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_FILTER_USM1, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading ultrasonic #1 distance!')
return
time_us = (i2cRecv[1] << 8) + i2cRecv[2]
if time_us == 65535:
time_us = 0
return time_us * USM_US_TO_MM
def GetDistance2(self):
"""
distance = GetDistance2()
Gets the filtered distance for ultrasonic module #2 in millimeters
Returns 0 for no object detected or no ultrasonic module attached
If you need a faster response try GetRawDistance2 instead (no filtering)
e.g.
0 -> No object in range
25 -> Object 25 mm away
1000 -> Object 1000 mm (1 m) away
3500 -> Object 3500 mm (3.5 m) away
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_FILTER_USM2, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading ultrasonic #2 distance!')
return
time_us = (i2cRecv[1] << 8) + i2cRecv[2]
if time_us == 65535:
time_us = 0
return time_us * USM_US_TO_MM
def GetDistance3(self):
"""
distance = GetDistance3()
Gets the filtered distance for ultrasonic module #3 in millimeters
Returns 0 for no object detected or no ultrasonic module attached
If you need a faster response try GetRawDistance3 instead (no filtering)
e.g.
0 -> No object in range
25 -> Object 25 mm away
1000 -> Object 1000 mm (1 m) away
3500 -> Object 3500 mm (3.5 m) away
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_FILTER_USM3, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading ultrasonic #3 distance!')
return
time_us = (i2cRecv[1] << 8) + i2cRecv[2]
if time_us == 65535:
time_us = 0
return time_us * USM_US_TO_MM
def GetDistance4(self):
"""
distance = GetDistance4()
Gets the filtered distance for ultrasonic module #4 in millimeters
Returns 0 for no object detected or no ultrasonic module attached
If you need a faster response try GetRawDistance4 instead (no filtering)
e.g.
0 -> No object in range
25 -> Object 25 mm away
1000 -> Object 1000 mm (1 m) away
3500 -> Object 3500 mm (3.5 m) away
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_FILTER_USM4, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading ultrasonic #4 distance!')
return
time_us = (i2cRecv[1] << 8) + i2cRecv[2]
if time_us == 65535:
time_us = 0
return time_us * USM_US_TO_MM
def GetRawDistance1(self):
"""
distance = GetRawDistance1()
Gets the raw distance for ultrasonic module #1 in millimeters
Returns 0 for no object detected or no ultrasonic module attached
For a filtered (less jumpy) reading use GetDistance1
e.g.
0 -> No object in range
25 -> Object 25 mm away
1000 -> Object 1000 mm (1 m) away
3500 -> Object 3500 mm (3.5 m) away
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_TIME_USM1, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading ultrasonic #1 distance!')
return
time_us = (i2cRecv[1] << 8) + i2cRecv[2]
if time_us == 65535:
time_us = 0
return time_us * USM_US_TO_MM
def GetRawDistance2(self):
"""
distance = GetRawDistance2()
Gets the raw distance for ultrasonic module #2 in millimeters
Returns 0 for no object detected or no ultrasonic module attached
For a filtered (less jumpy) reading use GetDistance2
e.g.
0 -> No object in range
25 -> Object 25 mm away
1000 -> Object 1000 mm (1 m) away
3500 -> Object 3500 mm (3.5 m) away
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_TIME_USM2, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading ultrasonic #2 distance!')
return
time_us = (i2cRecv[1] << 8) + i2cRecv[2]
if time_us == 65535:
time_us = 0
return time_us * USM_US_TO_MM
def GetRawDistance3(self):
"""
distance = GetRawDistance3()
Gets the raw distance for ultrasonic module #3 in millimeters
Returns 0 for no object detected or no ultrasonic module attached
For a filtered (less jumpy) reading use GetDistance3
e.g.
0 -> No object in range
25 -> Object 25 mm away
1000 -> Object 1000 mm (1 m) away
3500 -> Object 3500 mm (3.5 m) away
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_TIME_USM3, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading ultrasonic #3 distance!')
return
time_us = (i2cRecv[1] << 8) + i2cRecv[2]
if time_us == 65535:
time_us = 0
return time_us * USM_US_TO_MM
def GetRawDistance4(self):
"""
distance = GetRawDistance4()
Gets the distance for ultrasonic module #4 in millimeters
Returns 0 for no object detected or no ultrasonic module attached
For a filtered (less jumpy) reading use GetDistance4
e.g.
0 -> No object in range
25 -> Object 25 mm away
1000 -> Object 1000 mm (1 m) away
3500 -> Object 3500 mm (3.5 m) away
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_TIME_USM4, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading ultrasonic #4 distance!')
return
time_us = (i2cRecv[1] << 8) + i2cRecv[2]
if time_us == 65535:
time_us = 0
return time_us * USM_US_TO_MM
def GetServoPosition1(self):
"""
position = GetServoPosition1()
Gets the drive position for servo output #1
0 is central, -1 is maximum left, +1 is maximum right
e.g.
0 -> Central
0.5 -> 50% to the right
1 -> 100% to the right
-0.75 -> 75% to the left
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM1, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo output #1!')
return
pwmDuty = (i2cRecv[1] << 8) + i2cRecv[2]
powerOut = (float(pwmDuty) - self.PWM_MIN_1) / (self.PWM_MAX_1 - self.PWM_MIN_1)
return (2.0 * powerOut) - 1.0
def GetServoPosition2(self):
"""
position = GetServoPosition2()
Gets the drive position for servo output #2
0 is central, -1 is maximum left, +1 is maximum right
e.g.
0 -> Central
0.5 -> 50% to the right
1 -> 100% to the right
-0.75 -> 75% to the left
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM2, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo output #2!')
return
pwmDuty = (i2cRecv[1] << 8) + i2cRecv[2]
powerOut = (float(pwmDuty) - self.PWM_MIN_2) / (self.PWM_MAX_2 - self.PWM_MIN_2)
return (2.0 * powerOut) - 1.0
def GetServoPosition3(self):
"""
position = GetServoPosition3()
Gets the drive position for servo output #3
0 is central, -1 is maximum left, +1 is maximum right
e.g.
0 -> Central
0.5 -> 50% to the right
1 -> 100% to the right
-0.75 -> 75% to the left
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM3, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo output #3!')
return
pwmDuty = (i2cRecv[1] << 8) + i2cRecv[2]
powerOut = (float(pwmDuty) - self.PWM_MIN_3) / (self.PWM_MAX_3 - self.PWM_MIN_3)
return (2.0 * powerOut) - 1.0
def GetServoPosition4(self):
"""
position = GetServoPosition4()
Gets the drive position for servo output #4
0 is central, -1 is maximum left, +1 is maximum right
e.g.
0 -> Central
0.5 -> 50% to the right
1 -> 100% to the right
-0.75 -> 75% to the left
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM4, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo output #4!')
return
pwmDuty = (i2cRecv[1] << 8) + i2cRecv[2]
powerOut = (float(pwmDuty) - self.PWM_MIN_4) / (self.PWM_MAX_4 - self.PWM_MIN_4)
return (2.0 * powerOut) - 1.0
def SetServoPosition1(self, position):
"""
SetServoPosition1(position)
Sets the drive position for servo output #1
0 is central, -1 is maximum left, +1 is maximum right
e.g.
0 -> Central
0.5 -> 50% to the right
1 -> 100% to the right
-0.75 -> 75% to the left
"""
powerOut = (position + 1.0) / 2.0
pwmDuty = int((powerOut * (self.PWM_MAX_1 - self.PWM_MIN_1)) + self.PWM_MIN_1)
pwmDutyLow = pwmDuty & 0xFF
pwmDutyHigh = (pwmDuty >> 8) & 0xFF
try:
self.bus.write_i2c_block_data(self.i2cAddress, COMMAND_SET_PWM1, [pwmDutyHigh, pwmDutyLow])
except KeyboardInterrupt:
raise
except:
self.Print('Failed sending servo output #1!')
def SetServoPosition2(self, position):
"""
SetServoPosition2(position)
Sets the drive position for servo output #2
0 is central, -1 is maximum left, +1 is maximum right
e.g.
0 -> Central
0.5 -> 50% to the right
1 -> 100% to the right
-0.75 -> 75% to the left
"""
powerOut = (position + 1.0) / 2.0
pwmDuty = int((powerOut * (self.PWM_MAX_2 - self.PWM_MIN_2)) + self.PWM_MIN_2)
pwmDutyLow = pwmDuty & 0xFF
pwmDutyHigh = (pwmDuty >> 8) & 0xFF
try:
self.bus.write_i2c_block_data(self.i2cAddress, COMMAND_SET_PWM2, [pwmDutyHigh, pwmDutyLow])
except KeyboardInterrupt:
raise
except:
self.Print('Failed sending servo output #2!')
def SetServoPosition3(self, position):
"""
SetServoPosition3(position)
Sets the drive position for servo output #3
0 is central, -1 is maximum left, +1 is maximum right
e.g.
0 -> Central
0.5 -> 50% to the right
1 -> 100% to the right
-0.75 -> 75% to the left
"""
powerOut = (position + 1.0) / 2.0
pwmDuty = int((powerOut * (self.PWM_MAX_3 - self.PWM_MIN_3)) + self.PWM_MIN_3)
pwmDutyLow = pwmDuty & 0xFF
pwmDutyHigh = (pwmDuty >> 8) & 0xFF
try:
self.bus.write_i2c_block_data(self.i2cAddress, COMMAND_SET_PWM3, [pwmDutyHigh, pwmDutyLow])
except KeyboardInterrupt:
raise
except:
self.Print('Failed sending servo output #3!')
def SetServoPosition4(self, position):
"""
SetServoPosition4(position)
Sets the drive position for servo output #4
0 is central, -1 is maximum left, +1 is maximum right
e.g.
0 -> Central
0.5 -> 50% to the right
1 -> 100% to the right
-0.75 -> 75% to the left
"""
powerOut = (position + 1.0) / 2.0
pwmDuty = int((powerOut * (self.PWM_MAX_4 - self.PWM_MIN_4)) + self.PWM_MIN_4)
pwmDutyLow = pwmDuty & 0xFF
pwmDutyHigh = (pwmDuty >> 8) & 0xFF
try:
self.bus.write_i2c_block_data(self.i2cAddress, COMMAND_SET_PWM4, [pwmDutyHigh, pwmDutyLow])
except KeyboardInterrupt:
raise
except:
self.Print('Failed sending servo output #1!')
def GetServoMinimum1(self):
"""
pwmLevel = GetServoMinimum1()
Gets the minimum PWM level for servo output #1
This corresponds to position -1
The value is an integer where 2000 represents a 1 ms servo burst
e.g.
2000 -> 1 ms servo burst, typical shortest burst
4000 -> 2 ms servo burst, typical longest burst
3000 -> 1.5 ms servo burst, typical centre
5000 -> 2.5 ms servo burst, higher than typical longest burst
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM_MIN_1, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo #1 minimum burst!')
return
return (i2cRecv[1] << 8) + i2cRecv[2]
def GetServoMinimum2(self):
"""
pwmLevel = GetServoMinimum2()
Gets the minimum PWM level for servo output #2
This corresponds to position -1
The value is an integer where 2000 represents a 1 ms servo burst
e.g.
2000 -> 1 ms servo burst, typical shortest burst
4000 -> 2 ms servo burst, typical longest burst
3000 -> 1.5 ms servo burst, typical centre
5000 -> 2.5 ms servo burst, higher than typical longest burst
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM_MIN_2, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo #2 minimum burst!')
return
return (i2cRecv[1] << 8) + i2cRecv[2]
def GetServoMinimum3(self):
"""
pwmLevel = GetServoMinimum3()
Gets the minimum PWM level for servo output #3
This corresponds to position -1
The value is an integer where 2000 represents a 1 ms servo burst
e.g.
2000 -> 1 ms servo burst, typical shortest burst
4000 -> 2 ms servo burst, typical longest burst
3000 -> 1.5 ms servo burst, typical centre
5000 -> 2.5 ms servo burst, higher than typical longest burst
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM_MIN_3, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo #3 minimum burst!')
return
return (i2cRecv[1] << 8) + i2cRecv[2]
def GetServoMinimum4(self):
"""
pwmLevel = GetServoMinimum4()
Gets the minimum PWM level for servo output #4
This corresponds to position -1
The value is an integer where 2000 represents a 1 ms servo burst
e.g.
2000 -> 1 ms servo burst, typical shortest burst
4000 -> 2 ms servo burst, typical longest burst
3000 -> 1.5 ms servo burst, typical centre
5000 -> 2.5 ms servo burst, higher than typical longest burst
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM_MIN_4, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo #4 minimum burst!')
return
return (i2cRecv[1] << 8) + i2cRecv[2]
def GetServoMaximum1(self):
"""
pwmLevel = GetServoMaximum1()
Gets the maximum PWM level for servo output #1
This corresponds to position +1
The value is an integer where 2000 represents a 1 ms servo burst
e.g.
2000 -> 1 ms servo burst, typical shortest burst
4000 -> 2 ms servo burst, typical longest burst
3000 -> 1.5 ms servo burst, typical centre
5000 -> 2.5 ms servo burst, higher than typical longest burst
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM_MAX_1, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo #1 maximum burst!')
return
return (i2cRecv[1] << 8) + i2cRecv[2]
def GetServoMaximum2(self):
"""
pwmLevel = GetServoMaximum2()
Gets the maximum PWM level for servo output #2
This corresponds to position +1
The value is an integer where 2000 represents a 1 ms servo burst
e.g.
2000 -> 1 ms servo burst, typical shortest burst
4000 -> 2 ms servo burst, typical longest burst
3000 -> 1.5 ms servo burst, typical centre
5000 -> 2.5 ms servo burst, higher than typical longest burst
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM_MAX_2, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo #2 maximum burst!')
return
return (i2cRecv[1] << 8) + i2cRecv[2]
def GetServoMaximum3(self):
"""
pwmLevel = GetServoMaximum3()
Gets the maximum PWM level for servo output #3
This corresponds to position +1
The value is an integer where 2000 represents a 1 ms servo burst
e.g.
2000 -> 1 ms servo burst, typical shortest burst
4000 -> 2 ms servo burst, typical longest burst
3000 -> 1.5 ms servo burst, typical centre
5000 -> 2.5 ms servo burst, higher than typical longest burst
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM_MAX_3, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo #3 maximum burst!')
return
return (i2cRecv[1] << 8) + i2cRecv[2]
def GetServoMaximum4(self):
"""
pwmLevel = GetServoMaximum4()
Gets the maximum PWM level for servo output #4
This corresponds to position +1
The value is an integer where 2000 represents a 1 ms servo burst
e.g.
2000 -> 1 ms servo burst, typical shortest burst
4000 -> 2 ms servo burst, typical longest burst
3000 -> 1.5 ms servo burst, typical centre
5000 -> 2.5 ms servo burst, higher than typical longest burst
"""
try:
i2cRecv = self.ReadWithCheck(self.i2cAddress, COMMAND_GET_PWM_MAX_4, I2C_MAX_LEN)
except KeyboardInterrupt:
raise
except:
self.Print('Failed reading servo #4 maximum burst!')
return
return (i2cRecv[1] << 8) + i2cRecv[2]
def GetServoStartup1(self):
"""
pwmLevel = GetServoStartup1()
Gets the startup PWM level for servo output #1
This can be anywhere in the minimum to maximum range