Technicboon
Industrial & Scientific

Tutorial: Raspberry Pi Lidar Visualization notes

I’ve tried matplotlib which has a very convenient polar coordinate mode,

Xiaomi lidar sensor pinout

but the output looked very wrong, so there must be a few mistakes in here:

#!/usr/bin/python
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import serial
import numpy as np
 
com_port = "/dev/ttyACM0"
baudrate = 115200
ser = serial.Serial(com_port, baudrate)
 
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111, projection='polar')
ax.set_ylim(0,2000)
 
data = np.zeros(360)
theta = np.linspace(0,360, num=360)
l,  = ax.plot([],[])
 
 
databuffer = ""
 
def update(i):
    global data, databuffer
    if (ser.inWaiting()>0):
        data_str = ser.read(ser.inWaiting()).decode('ascii')
        databuffer +=  data_str
        if ('A' in databuffer and 'B' in databuffer and 'C' in databuffer):
            databuffer = databuffer.split('A')[1]
            databuffer = databuffer.split('C')[0]
            try:
                (angle, distance) = databuffer.split('B')
                ser.reset_input_buffer()
                data[int(angle)]=int(distance)
            except Exception as e: print(e)
 
    l.set_data(theta, data )
    return l, 
 
 
             
ani = animation.FuncAnimation(fig, update, frames=360, interval=200, blit=True)
plt.show()

Related posts

By 2024, 1/3 of Households Will Be Mobile-only Users

aartashyan

Lenovo M7268 All in One Wireless Laser Printer Review

harinderbajwa

T-Mobile and OPPO Reno 5G Tested New Network In The Netherlands

aartashyan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.