@@ -0,0 +1,2740 @@
+#  OpenMV M7 I2C interface with Garmin Lidar Lite V3 - By: Grant Phillips - Sun Apr 8 2018%0A%0A%0A#  Returns a basic distance reading from the lidar in cm for the target point and prints to console%0A#  Uses default lidar settings. For more advanced settings, see the I2C commands in the manual:%0A#  https://static.garmin.com/pumac/LIDAR_Lite_v3_Operation_Manual_and_Technical_Specifications.pdf%0A%0A#  I2C Control of LIDAR Lite V3%0A#  1. Write 0x04 to register 0x00%0A#  2. Read register 0x01. Repeat until bit 0 (LSB) goes low.%0A#  3. Read two bytes from 0x8f (high byte 0x0f then low byte 0x10) to obtain 16 bit measurement in cm%0A%0A#  HARDWARE CONNECTIONS:%0A#  Connect the lidar SCL line (green) to I2C 2 SCL on openMV (Pin 4)%0A#  Connect the lidar SDA line (blue) to I2C 2 SDA on openMV (pin 5)%0A#  680uF filter capacitor in parallel with the lidar%0A#  10k pullup resistors on the SCL and SDA lines to +5Vdc%0A%0A%0Aimport pyb%0Afrom pyb import I2C%0A%0A%0AlidarReady = bytearray(%5B0xff%5D)          #  holds the returned data for ready check%0AlidarReadyCheck = bytes(%5B1%5D)            #  to compare bit 0 of lidarReady%0A%0AstartBuf = bytearray(%5B0x00,0x04%5D)       #  step 1 address and data%0AreadyBuf = bytearray(%5B0x01%5D)            #  step 2 address for readiness check%0AdistBuf = bytearray(%5B0x8f%5D)             #  step 3 address for distance reading%0Adistance = -1                           #  variable for distance reading%0A%0A#  I2C setup%0ALidar=I2C(2,I2C.MASTER)                 #  initialise I2C 2 bus in master mode%0A%0A%0Awhile(True):%0A    distance = -1                       #  reset to -1 so we know when we get a real reading%0A%0A    try:                                #  handles errors thrown up if we have an I2C error%0A    #  Step 1 Write 0x04 to register 0x00%0A        Lidar.send(startBuf,0x62)       #  this is making it read (laser visible)%0A%0A    #  Step 2 Read register 0x01 and wait for bit 0 to go low%0A        while (lidarReady%5B0%5D & readyBuf%5B0%5D):%0A            Lidar.send(readyBuf,0x62)%0A            lidarReady=Lidar.recv(1,0x62)%0A            pyb.delay(50)               #  This seems to help reduce errors on the I2C bus%0A        lidarReady=bytearray(%5B0xff%5D)    #  reset the ready check data for next reading%0A%0A    #  Step 3 Read the distance measurement from 0x8f (0x0f and 0x10)%0A        Lidar.send(distBuf,0x62)%0A        dist=Lidar.recv(2,0x62)%0A        distance=dist%5B0%5D%0A        distance%3C%3C=8                    #  move 2 bytes into a 16 bit int%0A        distance%7C=dist%5B1%5D%0A        pyb.delay(100)                  #  allow time between readings, can go faster but more errors%0A%0A    except OSError:                     #  reninitialise i2c bus if error%0A        Lidar.init(I2C.MASTER)%0A        print(%22error, reinitialising%22)%0A%0A    if distance %3E -1:%0A        print(%22Distance:%22, distance, %22cm%22)%0A

Add user example