-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bosch_LRM30.py
141 lines (110 loc) · 3.21 KB
/
Bosch_LRM30.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
"""this file is the computer side application for logging the data output from a
Bosch LRM30 OEM laser range finder."""
import serial
import serial.tools.list_ports
import io
import sys
import time
import os
sampleTime = 60
#Bosch specific constants
CRC8_POLYNOMIAL = 0xA6
CRC8_INITIAL_VALUE = 0xAA
RET_NOERR = 1
RET_ERR_TIMEOUT= -1
RET_ERR_STATUS = -2
LASERON, LASEROFF, MEAS_INFO, MEAS_NORMAL, MEAS_CONT_START, MEAS_FAST_05HZ, MEAS_FAST_10HZ, MEAS_FAST_20HZ, MEAS_FAST_30HZ, MEAS_CONT_STOP = range(10)
commands = [
(0xC0, 0x41, 0x00),
(0xC0, 0x42, 0x00),
(0xC0, 0x73, 0x00),
(0xC0, 0x40, 0x01, 0x00),
(0xC0, 0x40, 0x01, 0x01),
(0xC0, 0x40, 0x01, 0x05),
(0xC0, 0x40, 0x01, 0x0D),
(0xC0, 0x40, 0x01, 0x15),
(0xC0, 0x40, 0x01, 0x1D),
(0xC0, 0x40, 0x01, 0x02)
]
def calcCRC8(data, init):
for i in range(8):
if (((init & 0x80) != 0) != ((data >> (7-i)) & 1)):
init = (init << 1) ^ CRC8_POLYNOMIAL
else:
init <<= 1
return init
def calcCRC8FromTuple(pData, init):
for d in pData:
init = calcCRC8(d, init)
return init & 0xFF
def sendCommand(cmd, comport):
command = commands[cmd]
comport.flushInput()
comport.flushOutput()
cksum = calcCRC8FromTuple(command, CRC8_INITIAL_VALUE)
comport.write(command)
comport.write([cksum])
def measureDistance(comport, command, timeout):
sendCommand(command, comport)
startTime = time.time()
done = 0
while not done:
if time.time() - startTime > timeout:
done = 2
if comport.inWaiting() > 0:
done = 1
if done == 2:
print "timeout error"
return -1
n = comport.inWaiting()
msg = comport.read(n)
msg_bytes = bytearray(msg)
value = 0
#check for the status
if msg_bytes[0] == 0:
length = msg_bytes[1]
payload = msg_bytes[2:2+length]
for i in range(length):
value += payload[i] << (i*8)
#to get mm from the returned value:
if value == 0:
value = -1
else:
value = value * 50e-3 - 50
if command > 1 and command < 9:
message = str(value)
print message
log.write(message + "\n")
return value
"""find the correct port in the list of ports. Assume that the arduino is the
only serial usb device connected."""
print "searching ports for connected devices"
ports = serial.tools.list_ports.comports()
laserAddress = ""
for port in ports:
#print port
typ = port[0]
dev = port[2]
if "USB VID:PID=483" in dev:
print "range finder found."
laserAddress = typ
"""if we still don't have a port, abort."""
if laserAddress == "":
print "no ranger found"
sys.abort()
path = 'logfile.csv'
if len(sys.argv) == 2:
path = sys.argv[1]
arcom = serial.Serial(laserAddress, 115200)
"""get the current system path and create a log file for the sensor data."""
log = open(path, 'w')
startTime = time.time()
lastTime = 0
measureDistance(arcom, MEAS_CONT_START, 2)
while time.time() - startTime < sampleTime+1:
if time.time() - lastTime > 0.035:
measureDistance(arcom, MEAS_FAST_30HZ, 2)
lastTime = time.time()
measureDistance(arcom, LASEROFF, 2)
log.close()
arcom.close()