-
Notifications
You must be signed in to change notification settings - Fork 8
/
pyrplidar.py
181 lines (136 loc) · 6.58 KB
/
pyrplidar.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
from pyrplidar_serial import PyRPlidarSerial
from pyrplidar_protocol import *
import pyrplidar_protocol
import time
class PyRPlidar:
def __init__(self):
self.lidar_serial = None
self.measurements = None
def __del__(self):
self.disconnect()
def connect(self, port="/dev/ttyUSB0", baudrate=115200, timeout=3):
self.lidar_serial = PyRPlidarSerial()
self.lidar_serial.open(port, baudrate, timeout)
print("PyRPlidar Info : device is connected")
def disconnect(self):
if self.lidar_serial is not None:
self.lidar_serial.close()
self.lidar_serial = None
print("PyRPlidar Info : device is disconnected")
def send_command(self, cmd, payload=None):
if self.lidar_serial == None:
raise PyRPlidarConnectionError("PyRPlidar Error : device is not connected")
self.lidar_serial.send_data(PyRPlidarCommand(cmd, payload).raw_bytes)
def receive_discriptor(self):
if self.lidar_serial == None:
raise PyRPlidarConnectionError("PyRPlidar Error : device is not connected")
discriptor = PyRPlidarResponse(self.lidar_serial.receive_data(RPLIDAR_DESCRIPTOR_LEN))
if discriptor.sync_byte1 != RPLIDAR_SYNC_BYTE1[0] or discriptor.sync_byte2 != RPLIDAR_SYNC_BYTE2[0]:
raise PyRPlidarProtocolError("PyRPlidar Error : sync bytes are mismatched", hex(discriptor.sync_byte1), hex(discriptor.sync_byte2))
return discriptor
def receive_data(self, discriptor):
if self.lidar_serial == None:
raise PyRPlidarConnectionError("PyRPlidar Error : received data length is mismatched")
data = self.lidar_serial.receive_data(discriptor.data_length)
if len(data) != discriptor.data_length:
raise PyRPlidarProtocolError()
return data
def stop(self):
self.send_command(RPLIDAR_CMD_STOP)
time.sleep(.02)
def clear_buffer(self):
'''Clears input buffer by reading all available data'''
self.lidar_serial.clear_input()
def reset(self):
self.send_command(RPLIDAR_CMD_RESET)
def set_motor_pwm(self, pwm): #Modified for A1 rplidar
if (pwm !=0):
self.lidar_serial.set_dtr(False)
else:
self.lidar_serial.set_dtr(True)
# self.send_command(RPLIDAR_CMD_SET_MOTOR_PWM, struct.pack("<H", pwm)) not available on A1 model
def get_info(self):
self.send_command(RPLIDAR_CMD_GET_INFO)
discriptor = self.receive_discriptor()
data = self.receive_data(discriptor)
return PyRPlidarDeviceInfo(data)
def get_health(self):
self.send_command(RPLIDAR_CMD_GET_HEALTH)
discriptor = self.receive_discriptor()
data = self.receive_data(discriptor)
return PyRPlidarHealth(data)
def get_samplerate(self):
self.send_command(RPLIDAR_CMD_GET_SAMPLERATE)
discriptor = self.receive_discriptor()
data = self.receive_data(discriptor)
return PyRPlidarSamplerate(data)
def get_lidar_conf(self, payload):
self.send_command(RPLIDAR_CMD_GET_LIDAR_CONF, payload)
discriptor = self.receive_discriptor()
data = self.receive_data(discriptor)
return data
def get_scan_mode_count(self):
data = self.get_lidar_conf(struct.pack("<I", RPLIDAR_CONF_SCAN_MODE_COUNT))
count = struct.unpack("<H", data[4:6])[0]
return count
def get_scan_mode_typical(self):
data = self.get_lidar_conf(struct.pack("<I", RPLIDAR_CONF_SCAN_MODE_TYPICAL))
typical_mode = struct.unpack("<H", data[4:6])[0]
return typical_mode
def get_scan_modes(self):
scan_modes = []
scan_mode_count = self.get_scan_mode_count()
for mode in range(scan_mode_count):
scan_mode = PyRPlidarScanMode(
self.get_lidar_conf(struct.pack("<IH", RPLIDAR_CONF_SCAN_MODE_NAME, mode)),
self.get_lidar_conf(struct.pack("<IH", RPLIDAR_CONF_SCAN_MODE_MAX_DISTANCE, mode)),
self.get_lidar_conf(struct.pack("<IH", RPLIDAR_CONF_SCAN_MODE_US_PER_SAMPLE, mode)),
self.get_lidar_conf(struct.pack("<IH", RPLIDAR_CONF_SCAN_MODE_ANS_TYPE, mode)))
#print(scan_mode)
scan_modes.append(scan_mode)
return scan_modes
def start_scan(self): #this is the legacy scan mode
self.send_command(RPLIDAR_CMD_SCAN)
discriptor = self.receive_discriptor()
def scan_generator():
while True:
data = self.receive_data(discriptor)
#yield PyRPlidarMeasurement(data)
yield PyRPlidarMeasurementRaw(raw_bytes=True, measurement_hq=None).get_all()
return scan_generator
def start_scan_express(self, mode, t_return):
self.send_command(RPLIDAR_CMD_EXPRESS_SCAN, struct.pack("<BI", mode, 0x00000000))
discriptor = self.receive_discriptor()
print (discriptor)
if discriptor.data_type == 0x82:
capsule_type = PyRPlidarScanCapsule
elif discriptor.data_type == 0x84:
capsule_type = PyRPlidarScanUltraCapsule
elif discriptor.data_type == 0x85:
capsule_type = PyRPlidarScanDenseCapsule
else:
raise PyRPlidarProtocolError("RPlidar Error : scan data type is not supported")
def scan_generator():
data = self.receive_data(discriptor)
capsule_prev = capsule_type(data)
capsule_current = None
while (True ):
data = self.receive_data(discriptor)
capsule_current = capsule_type(data)
nodes = capsule_type._parse_capsule(capsule_prev, capsule_current)
if t_return != "raw":
for index, node in enumerate(nodes):
yield PyRPlidarMeasurement(raw_bytes=None, measurement_hq=node)
else:
for index, node in enumerate(nodes):
yield PyRPlidarMeasurementRaw(raw_bytes=None, measurement_hq=node).get_all()
capsule_prev = capsule_current
return scan_generator
def force_scan(self):
self.send_command(RPLIDAR_CMD_FORCE_SCAN)
discriptor = self.receive_discriptor()
def scan_generator():
while True:
data = self.receive_data(discriptor)
yield PyRPlidarMeasurement(data)
return scan_generator