-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
55 lines (43 loc) · 1.43 KB
/
test.py
File metadata and controls
55 lines (43 loc) · 1.43 KB
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
import serial
import termios
import fcntl
from rplidar import RPLidar, RPLidarException
PORT_NAME = '/dev/ttyUSB0'
CUSTOM_BAUD = 256000
def set_custom_baud(serial_port, baud):
"""Set a custom baud rate on Linux using BOTHER"""
fd = serial_port.fileno()
attrs = termios.tcgetattr(fd)
# BOTHER = 0x1000 (from asm-generic/termios.h)
BOTHER = 0x1000
# Input/output speeds
attrs[4] = BOTHER # ispeed
attrs[5] = BOTHER # ospeed
# c_cflag holds the actual baud rate in c_ospeed/c_ispeed when BOTHER is set
attrs[2] &= ~termios.CBAUD # Clear old baud flags
attrs[2] |= BOTHER
# Set the file descriptor
termios.tcsetattr(fd, termios.TCSANOW, attrs)
# Use ioctl to set exact baud
fcntl.ioctl(fd, termios.TIOCSSERIAL, baud)
# Open serial port manually
ser = serial.Serial(PORT_NAME, baudrate=115200, timeout=1, dsrdtr=True)
set_custom_baud(ser, CUSTOM_BAUD)
# Pass this serial object to RPLidar
lidar = RPLidar(PORT_NAME, baudrate=CUSTOM_BAUD)
try:
info = lidar.get_info()
print("Lidar info:", info)
print("Scanning for 5 seconds...")
import time
start = time.time()
for scan in lidar.iter_scans():
for (_, angle, distance) in scan:
print(f"Angle: {angle:.2f}, Distance: {distance} mm")
if time.time() - start > 5:
break
finally:
lidar.stop()
lidar.disconnect()
ser.close()
print("Lidar disconnected.")