pythonraspberry-piraspberry-pi3lidar

RPLidarException: Works fine on PC but multiple errors when using on Raspberry Pi


I am trying to use an rplidar to write lidar scan data to a csv, and it works fine when it is plugged into my PC via USB - however, as soon as I plug it into my Raspberry Pi(3b+), it throws multiple errors despite the health reporting being fine. If you knows what the cause of this might be, the assistance would be greatly appreciated! Python Code and traceback below!

Python:

from rplidar import RPLidar
from math import sin, cos, radians
import multiprocessing as mp
import csv

lidar = RPLidar("/dev/ttyUSB0")


def scan():
    try:
        for scan in enumerate(lidar.iter_scans(max_buf_meas=False)):
            list_version_data = list(scan)
            for data in list_version_data:
                if isinstance(data, list):
                    for indiv_data_points in data:
                        if isinstance(indiv_data_points, tuple):
                            list_indiv_data_points = list(indiv_data_points)
                            list_indiv_data_points.pop(0)
                            # print(list_indiv_data_points)
                            # Angle is first, distance is second
                            # Angle is in degrees, distance is in mm
                            angle = list_indiv_data_points[0]
                            distance = list_indiv_data_points[1]
                            length = distance
                            angle = radians(angle)
                            x, y = (length * cos(angle)), (length * sin(angle))
                            print(str(x), str(y))
                            with open("lidar03.csv", "a") as f:
                                writer = csv.writer(f)
                                writer.writerow([x, y])
                elif isinstance(data, int):
                    print("int")
        if KeyboardInterrupt:
            lidar.stop()
            lidar.stop_motor()
            lidar.disconnect()
            scan()
    except Exception as e:
        lidar.stop()
        lidar.stop_motor()
        lidar.disconnect()
        lidar.reset()
        print("error")
        print(e)
        pass


if __name__ == "__main__":
    print(lidar.get_health())
    print(lidar.get_info())
    print(lidar.reset())
    scan()

Full Traceback:

('Good', 0)
{'model': 24, 'firmware': (1, 29), 'hardware': 7, 'serialnumber': '95BD99F6C9E59AD4C5E59CF7696F3414'}
None
Traceback (most recent call last):
  File "/home/garb/robot/TESTING/lidar_testing/live_write_csv.py", line 11, in scan
    for scan in enumerate(lidar.iter_scans(max_buf_meas=False)):
  File "/home/garb/.local/lib/python3.9/site-packages/rplidar.py", line 357, in iter_scans
    for new_scan, quality, angle, distance in iterator:
  File "/home/garb/.local/lib/python3.9/site-packages/rplidar.py", line 300, in iter_measurments
    status, error_code = self.get_health()
  File "/home/garb/.local/lib/python3.9/site-packages/rplidar.py", line 245, in get_health
    dsize, is_single, dtype = self._read_descriptor()
  File "/home/garb/.local/lib/python3.9/site-packages/rplidar.py", line 189, in _read_descriptor
    raise RPLidarException('Incorrect descriptor starting bytes')
rplidar.RPLidarException: Incorrect descriptor starting bytes

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/garb/robot/TESTING/lidar_testing/live_write_csv.py", line 53, in <module>
    scan()
  File "/home/garb/robot/TESTING/lidar_testing/live_write_csv.py", line 43, in scan
    lidar.reset()
  File "/home/garb/.local/lib/python3.9/site-packages/rplidar.py", line 273, in reset
    self._send_cmd(RESET_BYTE)
  File "/home/garb/.local/lib/python3.9/site-packages/rplidar.py", line 179, in _send_cmd
    self._serial_port.write(req)
  File "/home/garb/.local/lib/python3.9/site-packages/serial/serialposix.py", line 615, in write
    raise PortNotOpenError()
serial.serialutil.PortNotOpenError: Attempting to use a port that is not open

Solution

  • I solved it: I used pip3 install rplidar when the package I should have used was sudo pip3 install rplidar-roboticia. There are multiple packages for rplidar out there and this was the only one I found that worked really well.