Raspberry Pi Lidar Visualization notes

Xiaomi lidar sensor pinout

I’ve tried matplotlib which has a very convenient polar coordinate mode, 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()

6 thoughts on “Raspberry Pi Lidar Visualization notes”

  1. hello, very cool project! I’m currently trying to get a Xiaomi scanner on the Pi to work. How exactly did you do the wiring with the Pi? should the engine turn when connecting the 5V or do I have to do it myself?
    Thanks for your Help…

      1. many thanks for the help. a question still should the laser led light up after I have connected 5V? if I connect my sensor to 5v, nothing happens.

        1. The laser is controlled by the module internally. When the head is rotated (manually, with an unregulated DC motor or with the constant-speed PWM thingy) it automatically takes measurements and sends data

  2. Oh yes, the laser diode is on after external motor start. Now I’ll check it out if data come out. If I connect GND and RX from the Raspberry with the sensor to test should Go?

Leave a Reply

Your email address will not be published. Required fields are marked *