Curiouser and curiouser

After more chipping away, I’ve got it down to this:

  • If I define _GLL_ACQ_COMMAND in class scope, the I2C command had no effect.
  • If I define _GLL_ACQ_COMMAND in instance scope, the I2C command is effective.

I use per-class scope for all my other I2C sensor classes, I wonder why it’s different here?  The code below does not work as is.  Remove the # from line 38 and all works perfectly.  Utterly bizarre.

from __future__ import division
import smbus
import time

class Lidar_Lite:

    __GLL_ACQ_COMMAND       = 0x00
    __GLL_STATUS            = 0x01
    __GLL_SIG_COUNT_VAL     = 0x02
    __GLL_ACQ_CONFIG_REG    = 0x04
    __GLL_VELOCITY          = 0x09
    __GLL_PEAK_CORR         = 0x0C
    __GLL_NOISE_PEAK        = 0x0D
    __GLL_FULL_DELAY_HIGH   = 0x8F
    __GLL_FULL_DELAY_LOW    = 0x10
    __GLL_OUTER_LOOP_COUNT  = 0x11
    __GLL_REF_COUNT_VAL     = 0x12
    __GLL_LAST_DELAY_HIGH   = 0x14
    __GLL_LAST_DELAY_LOW    = 0x15
    __GLL_UNIT_ID_HIGH      = 0x16
    __GLL_UNIT_ID_LOW       = 0x17
    __GLL_I2C_ID_HIGHT      = 0x18
    __GLL_I2C_ID_LOW        = 0x19
    __GLL_I2C_SEC_ADDR      = 0x1A
    __GLL_I2C_CONFIG        = 0x1E
    __GLL_ACQ_COMMAND       = 0x40
    __GLL_MEASURE_DELAY     = 0x45
    __GLL_PEAK_BCK          = 0x4C
    __GLL_CORR_DATA         = 0x52
    __GLL_CORR_DATA_SIGN    = 0x53
    __GLL_ACQ_SETTINGS      = 0x5D
    __GLL_POWER_CONTROL     = 0x65

    def __init__(self):
        self.address = 0x62
#        self.__GLL_ACQ_COMMAND = 0x00

        self.bus = smbus.SMBus(1)
        self.bus.write_byte_data(self.address, self.__GLL_OUTER_LOOP_COUNT, 0xFF)
        self.bus.write_byte_data(self.address, self.__GLL_ACQ_COMMAND, 0x04)
    def getDistance(self):
        dist1 = self.bus.read_byte_data(self.address, self.__GLL_FULL_DELAY_HIGH)
        dist2 = self.bus.read_byte_data(self.address, self.__GLL_FULL_DELAY_LOW)
        return ((dist1 << 8) + dist2) / 100

    def getVelocity(self):
        vel = self.bus.read_byte_data(self.address, self.__GLL_VELOCITY)
        return (self.signedInt(vel)) / 100

    def signedInt(self, value):
        return value if value < 128 else (value - 256)

ll = Lidar_Lite()

start_time = time.time()
ii = 0
while ii < 100:
    ii += 1
    velocity = ll.getVelocity()
    distance = ll.getDistance()
    print "Velocity %f; Distance %f" % (velocity, distance)
print "%d reads took %f seconds" % (ii, time.time() - start_time)

P.S. For the moment, I’m just going to accept I must use the per-instance scope rather than wasting more time working out this python oddity. This version has now incorporated into my Quadcopter code.

Leave a Reply

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