2

I am running an RPLidar a1m8 connected to my computer over USB. I am trying to continually collect data from it and graph it using matplotlib. This works for approximately 25 lines but then I get the error, if anyone knows a solution to this it would be greatly appreciated. Code Printing Properly

x is  -424.2619428142333 y is  -103.69361783394488
x is  -425.2488450237623 y is  -113.94508460638478
x is  -426.76680234448065 y is  -122.49937312764901
x is  -428.54349054785354 y is  -132.29787114334755
x is  -429.7437088814488 y is  -142.4882703130912
x is  -430.94972519517984 y is  -154.32885943399893
x is  -433.00149846184235 y is  -163.239899625671
x is  -433.649229732051 y is  -174.65564849955132
x is  -434.9438784567277 y is  -185.8838954643981
x is  -436.894825594578 y is  -196.37696241841425
x is  -439.7883509307727 y is  -205.66102301017438

Error:

Too many bytes in the input buffer: 4030/3000. Cleaning buffer...
Traceback (most recent call last):
  File "/home/garb/TESTING/lidar_testing/plotting.py", line 24, in <module>
    for scan in enumerate(lidar.iter_scans()):
  File "/usr/local/lib/python3.10/dist-packages/rplidar.py", line 446, in iter_scans
    for new_scan, quality, angle, distance in iterator:
  File "/usr/local/lib/python3.10/dist-packages/rplidar.py", line 394, in iter_measures
    self.start(self.scanning[2])
  File "/usr/local/lib/python3.10/dist-packages/rplidar.py", line 319, in start
    status, error_code = self.get_health()
  File "/usr/local/lib/python3.10/dist-packages/rplidar.py", line 279, in get_health
    dsize, is_single, dtype = self._read_descriptor()
  File "/usr/local/lib/python3.10/dist-packages/rplidar.py", line 216, in _read_descriptor
    raise RPLidarException('Incorrect descriptor starting bytes')
rplidar.RPLidarException: Incorrect descriptor starting bytes

My Code:

from rplidar import RPLidar
from pprint import pprint
import csv
from math import sin, cos, radians
lidar = RPLidar('/dev/ttyUSB0')
from math import sin, cos, radians
import time
import numpy as np
import matplotlib.pyplot as plt


def polar2cart(angle, distance):
        length = distance
        angle = angle
        angle = radians(angle)
        x,y = (length * cos(angle)), (length * sin(angle))
        print('x is ', str(x) + ' y is ', str(y))
        plt.xlim(-300, 300)
        plt.ylim(-300, 300)
        plt.scatter(x, y, color="black")
        plt.pause(0.05)
        
for scan in enumerate(lidar.iter_scans()):
    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]
                    polar2cart(angle, distance)

        elif isinstance(data, int):
            print("int")
if KeyboardInterrupt:
    lidar.stop()
    lidar.stop_motor()
    lidar.disconnect()
    scan()

plt.show()

1 Answer 1

2

This happens because the buffer is full. You can solve it by using a try except method and then clear input to clean the buffer:

try:

. . .

. . .

except RPLidarException:

lidar.clear_input()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.