20ms

I’ve just tested this sample I pulled from GitHub; it works.  This isn’t a good thing.  This success sucks!  The only difference between what it’s doing and what I am is all the 20ms sleeps between all reads and writes.  Together that means each read of velocity or distance takes > 60ms – hence the lack of need for sleep in the while loop at the bottom.

But at least I can use it to work out either whether it’s possible to integrate the 20ms into my quadcopter code while I read the IMU in the meantime, or better, get rid of the 20ms breaks altogether.

However, I don’t have a good feeling about achieving either of the above based on what I’ve seen so far.  Only time will tell.

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

  def connect(self, bus):
    try:
      self.bus = smbus.SMBus(bus)
      time.sleep(0.5)
      return 0
    except:
      return -1

  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)

while True:
    velocity = ll.getVelocity()
    distance = ll.getDistance()
    print "Velocity %d; Distance %d" % (velocity, distance)

Leave a Reply

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

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