1

I have a XV-11 Lidar sensor from an old vacuum cleaner and I want to use it for a robot project. During my research, I saw a very interesting and simple approach using Matplotlib and display all the distances using scatter points. eg (https://udayankumar.com/2018/08/01/working-with-lidar/) but when I run this python code to RP3 indeed a Matplotlib window is popping up with all the distances but the refresh rate for data it's too slow and impossible to view in real time. I mean the matplotlib display is falling behind a few dozens of seconds with all the sensor readings. My next idea was to do something by myself with the following display lines but I have same result: Good readings but delayed a lot.

points =[]
plt.ion()
x = dist_mm*np.cos(angle_rad)
y = dist_mm*np.sin(angle_rad)
points.append([x,y])
points = np.array(points)
plt.scatter(points[:,0], points[:,1])
if angle == 356:
 plt.plot()
 plt.draw()
 plt.pause(0.0001)
 plt.clf()
 print ("-----------")

What I'm doing wrong or what I can improve in this case? My expectations are something like this Lidar animation, source: https://github.com/Hyun-je/pyrplidar but in this example it's used a different Lidar sensor

AlexandruM
  • 13
  • 3

1 Answers1

0

You are clearing and re-creating the axes, background etc. every time. At the very least you can limit this drawing/re-drawing to only the relevant plot points for a degree of improvement.

If you're not familiar with this I'd start with the animation guidance- https://matplotlib.org/stable/api/animation_api.html which introduces some basics like updating only parts of the figure.

If you're still churning out too much data to update then limiting the frequency with which you read your data or more specifically the rate at which you redraw might result in more stability too.

Probably worth hunting down more general guidance on realtime plotting e.g. update frame in matplotlib with live camera preview

tgrtim
  • 106
  • 5