That sample code in the previous post works fine with the time.sleep(0.02) just commented out throughout. I took 1000 readings in 2.4s while moving a copy of the MagPi in front of the LiDAR-Lite. I got a variety of values as expected. 2.4ms per sample is completely acceptable sampling period for integrating with the quadcopter main processing loop.
So the next step was to set up the auto-repeat so that there’s no need for the writes prior to each read. That worked too, producing 10,000 in 14 seconds or 1.4ms per samples.
So the next step was to increase the I2C baudrate from 100kbps to 400kbps as required for 1kHz sampling of the MPU-9250. 10,000 reads then took only 4.4s or 0.44ms per sample. I can’t ask for more!
Next step is to read the MPU-9250 at the same time and check they don’t clash. They shouldn’t, but this is another rumour I need to negate / depose. I’ll post separately on that as it needs a non-trivial code tweak. For now, here’s the updated code with all time.sleep()s commented out, and the addition of the 3 extra write commands in the connect() phase to kick off the automatic sampling.
import smbus import time class Lidar_Lite(): def __init__(self): self.address = 0x62 self.distWriteReg = 0x00 self.distWriteVal = 0x04 self.distReadReg1 = 0x8f self.distReadReg2 = 0x10 self.velWriteReg = 0x04 self.velWriteVal = 0x08 self.velReadReg = 0x09 self.outerLoopReg = 0x11 self.outerLoopVal = 0xFF def connect(self, bus): try: self.bus = smbus.SMBus(bus) # time.sleep(0.5) except: return -1 self.writeAndWait(self.outerLoopReg, self.outerLoopVal) self.writeAndWait(self.distWriteReg, self.distWriteVal) self.writeAndWait(self.distWriteReg, self.distWriteVal) return 0 def writeAndWait(self, register, value): self.bus.write_byte_data(self.address, register, value); # time.sleep(0.02) def readAndWait(self, register): res = self.bus.read_byte_data(self.address, register) # time.sleep(0.02) return res def getDistance(self): # self.writeAndWait(self.distWriteReg, self.distWriteVal) dist1 = self.readAndWait(self.distReadReg1) dist2 = self.readAndWait(self.distReadReg2) return (dist1 << 8) + dist2 def getVelocity(self): # self.writeAndWait(self.distWriteReg, self.distWriteVal) # self.writeAndWait(self.velWriteReg, self.velWriteVal) vel = self.readAndWait(self.velReadReg) return self.signedInt(vel) def signedInt(self, value): if value > 127: return (256-value) * (-1) else: return value ll = Lidar_Lite() ll.connect(1) start_time = time.time() ii = 0 while ii <= 10000: ii += 1 velocity = ll.getVelocity() distance = ll.getDistance() print "Velocity %d; Distance %d" % (velocity, distance) print "%d reads took %f seconds" % (ii, time.time() - start_time)