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:

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')
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]
                (angle, distance) = databuffer.split('B')
            except Exception as e: print(e)
    l.set_data(theta, data )
    return l, 
ani = animation.FuncAnimation(fig, update, frames=360, interval=200, blit=True)

Leave a Reply