-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadRangefinders.py
63 lines (51 loc) · 1.33 KB
/
readRangefinders.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
from Serial.serialPi import SerialU
class AdamRangefinders():
def __init__(self):
super().__init__()
self.ser = SerialU('/dev/ttyAMA0', 115200)
self.sensors=[0, 0, 0, 0]
def CRC8(self, mas):
st_byt = 0
crc = 0
while st_byt < len(mas):
dat = mas[st_byt]
for i in range(8):
fb = crc ^ dat
fb &= 1
crc >>= 1
dat >>= 1
if fb == 1:
crc ^= 0x8c
st_byt += 1
return crc
def distance(self, sensorId):
data = [18, sensorId, 0, 0, 0, 0, 255]
data.append(self.CRC8(data[1:]))
data.append(36)
self.ser.write(data, len(data))
time.sleep(0.02)
if self.ser.avail():
text = self.ser.readByte('$', 100, 100)
time.sleep(0.02)
text=text.decode('utf-8')
t=''
t=text[:text.find('$')]
t=t.split(',')
dataSens=[]
try:
for n in t:
dataSens.append(int(n))
except:
pass
if len(dataSens)>1:
if dataSens[0]==sensorId:
self.sensors=dataSens
return self.sensors
def getDistance(self, sensorId, isTest=False):
if isTest:
return [sensorId, 10, 20, 30]
else:
return self.distance(sensorId)