-
Notifications
You must be signed in to change notification settings - Fork 2
/
LRF3K1LS-samplecode.py
155 lines (128 loc) · 4.7 KB
/
LRF3K1LS-samplecode.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
# =============================================================================
# The sample code will use the pyserial lib. Please use the following command to install the required packages.
# $ sudo pip install pyserial
# =============================================================================
import serial.tools.list_ports
import sys
import threading
#declare
global_flag=True
#List available COMport
ports = serial.tools.list_ports.comports()
portlist=[]
index=0
for port, desc, hwid in ports:
portlist.append(port)
print(str(index)+") "+desc+"|"+port)
index+=1
if portlist == []:
print("No serial ports detected")
sys.exit()
#Select COMport
portname=input("Please input the number of the desired port: \n")
try:
ser = serial.Serial(
port=portlist[int(portname)],\
baudrate=19200,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=3)
except Exception as e:
print(str(e))
sys.exit()
def receive():
while(global_flag):
#Read all data from the buffer
data=ser.read_all()
if(len(data)>=4):
print('Module response: ' +str(data))
#Depending on the response, perform different tasks
#When reading a range finder result
if data[1]==0x01:
#Data 2nd byte's 7th bit states if distance is invalid
if((data[2]>>7)&1)==1:
print("Distance is invalid")
continue
#Data 2nd byte's 6th bit states if angle is invalid
if((data[2]>>6)&1)==1:
print("Angle is invalid")
continue
#Data 2nd byte's 5th bit states measurement's data resolution
if((data[2]>>5)&1)==0:
dataResolution=0.5
else:
dataResolution=0.1
#Calculate distance
distance=(data[3]*256+data[4])*dataResolution
print("Distance: "+str(round(distance,2))+ "mm")
#Calculate angle
if(data[5]<180):
angle=data[5]
else:
angle=data[5]-256
print("Angle: "+ str(round(angle,2))+" degree")
#When reading the status of the module
elif data[1]==0x20:
info=''
#Data 2nd byte's 7th bit displays measurement state
if((data[2]>>7)&1)==0:
info+='Finish ranging, '
else:
info+='Laser rangefinder is busy, '
#Data 2nd byte's 6th bit states if an error occured
if((data[2]>>6)&1)==0:
info+='No Error\n'
else:
info+='In Error\n'
#Data 2nd byte's 1st bit displays tilt angle sensor state
if((data[2]>>1)&1)==0:
info+='Tilt angle sensor off, '
else:
info+='Tilt angle sensor ON, '
#Data 2nd byte's 0th bit states tilt angle sensor status
if((data[2]>>0)&1)==0:
info+='Tilt angle sensor nornal'
else:
info+='Tilt angle sensor abnormal'
print(info)
def main(args=None):
global global_flag
t = threading.Thread(target = receive)
t.start()
#console operation
while global_flag:
#Provide instructions for the user
key=input("\nPlease enter the operation command:\n -s single measurement (meter)\n\
-c continuous measurement (meter)\n -x stop measurement\n\
-r read status\n -q Exit\n")
#Single measurement (meter)
if key=='s':
ser.reset_input_buffer()
cmd=bytearray(b'\x10\x83\x00\x7D')
ser.write(cmd)
#Continuous measurement (meter)
elif key=='c':
ser.reset_input_buffer()
cmd=bytearray(b'\x10\x83\x40\x3D')
ser.write(cmd)
#Stop measurement
elif key=='x':
ser.reset_input_buffer()
cmd=bytearray(b'\x10\x84\x7C')
ser.write(cmd)
#Read status
elif key=='r':
ser.reset_input_buffer()
cmd=bytearray(b'\x10\x80\x80')
ser.write(cmd)
#Exit
elif key=='q':
global_flag = False
ser.close()
sys.exit()
#Incorrect Input
else:
print("Invalid option. Please enter one of the above options.")
if __name__ == '__main__':
main()