Converting the VL53L01X Arduino Library

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
mjs513
Posts: 7
Joined: Wed May 10, 2017 12:01 am

Converting the VL53L01X Arduino Library

Post by mjs513 » Wed May 10, 2017 12:16 am

Hi,

First time poster here and real newbie to micropython/python. I just picked up a OpenMV board that uses micropython but want to use a VL53L0X time of flight sensor. I made a good start but now trying to under stand i2c calls using machine.ic2. I get what needs to be supplied but what I don't get is what is the format and how to do it. In Arduino/c++ world I know just by passing the hex addresses and whatever data but I am lost at this point. There are other issues but I think I can work those once this is resolved. Here is what I am struggling with and what gets passed (format, etc):

def _set_reg8(self, reg_address, value):
#data = ustruct.pack('>HB', address, value)
self.i2c.writeto_mem(self._address, reg_address, value)

def _get_reg8(self, address, reg_address):
data = self.i2c.readfrom_mem(self._address, reg_address, 1)
return data

def _set_reg16(self, reg_address, value):
dst[0] = (value >> 8) & 0xFF ## value high byte
dst[1] = ( value & 0xFF) ## value low byte
#self.i2c.writeto_mem(self._address, reg_address, value, *, addrsize = 16 )
test = 1

def _get_reg16(self, reg_address):
data = bytearray[2]
self.i2c.readfrom_into(self._address, reg_address, data)
value = data[1] << 8 ## value high byte
value = value | data[0] ## value low byte
return value

def _set_reg32(self, reg_address, value):
data[3] = (value >> 24) & 0xFF ## value highest byte
data[2] = (value >> 16) & 0xFF
data[1] = (value >> 8) & 0xFF
data[0] = (value >> 8) & 0xFF ## value lowest byte
self.i2c.writeto_mem(self._address, reg_address, data)
return


def _get_reg32(self, reg_address):
data = bytearrar(4)
self.i2c.readfrom_into(self._address, reg_address, data)
value = data[3] << 24 ## value highest byte
value = value | data[2] << 16
value = value | data[1] << 8
value = value | data[0] ## value lowest byte
return value

# Read an arbitrary number of bytes from the sensor, starting at the given
# register, into the given array
def _readMulti(reg_address, count):
dst = bytearray(count)
self.i2c.readfrom_mem_into(self._address, reg_address, dst, count)
return dst

# Write an arbitrary number of bytes from the given array to the sensor,
# starting at the given register
def _writeMulti(reg_address, dst, count):
self.i2c.writeto_mem(self._address, reg_address, dst)

User avatar
deshipu
Posts: 1332
Joined: Thu May 28, 2015 5:54 pm

Re: Converting the VL53L01X Arduino Library

Post by deshipu » Wed May 10, 2017 9:07 am

So what is the problem exactly? How can we help?

mjs513
Posts: 7
Joined: Wed May 10, 2017 12:01 am

Re: Converting the VL53L01X Arduino Library

Post by mjs513 » Wed May 10, 2017 11:03 am

Question is will the constructs I used work if I am passing hex values to the functions. When reading the documentation and some examples I was getting confused on whether I had to convert them to a binary representation. For instance, would I have to change 0x29 to 41 (decimal) equivalent first and vice versa?

Thanks
Mike

User avatar
deshipu
Posts: 1332
Joined: Thu May 28, 2015 5:54 pm

Re: Converting the VL53L01X Arduino Library

Post by deshipu » Wed May 10, 2017 2:46 pm

Whether you write 0x10, 16, 0b00001111 or anything else, to the computer it looks exactly the same -- it's just some integer value in the memory. So you can use one or the other.

However, b"\x10" is a different, as it's a byte-string of length one containing the value 16 -- not an integer.

mjs513
Posts: 7
Joined: Wed May 10, 2017 12:01 am

Re: Converting the VL53L01X Arduino Library

Post by mjs513 » Wed May 10, 2017 3:19 pm

Ok thanks for the clarification. It was getting confusing reading some of the stuff out there. It looked like in what I saw they converted to ints first the passed the reg values.

For the returned values in order to perform operations I need to convert from byte string to int. Doing a little research I saw a function int.from_bytes but it does not appear to be implemented in micropython? Either that or the OpenMv implementation doesn't support it yet. I then came across a issue on GitHub that gave me a function that I can use:

def from_bytes_big(b):
n = 0
for x in b:
n <<= 8
n |= x
return n

Think this may work, yes?

Thanks
Mike

User avatar
deshipu
Posts: 1332
Joined: Thu May 28, 2015 5:54 pm

Re: Converting the VL53L01X Arduino Library

Post by deshipu » Wed May 10, 2017 5:28 pm

Just use the ustruct module, that's what it is for -- converting strings of bytes to different types and vice versa. I saw you commented out all the uses of it in that code, which is pretty much why it doesn't work.

mjs513
Posts: 7
Joined: Wed May 10, 2017 12:01 am

Re: Converting the VL53L01X Arduino Library

Post by mjs513 » Wed May 10, 2017 6:07 pm

To be honest, because it shows my ignorance, couldn't figure out what it was doing. Told you I have no idea about python. What got me is the format string portion of the struct. If I look the way its done here, I get it:

Code: Select all

values = (1, 'ab', 2.7)
s = struct.Struct('I 2s f')
packed_data = s.pack(*values)
The way its done in the docs, I am lost on how to implement. If you have a good site to explain it I will take it.


User avatar
deshipu
Posts: 1332
Joined: Thu May 28, 2015 5:54 pm

Re: Converting the VL53L01X Arduino Library

Post by deshipu » Sat May 13, 2017 9:14 pm

I sat down today and finished a first perliminary version of the driver for the VL53L0X that I have been working on for some time now already. This is mostly based on the Pololu's Arduino library.

The code is here: https://bitbucket.org/thesheep/micropyt ... vl53l0x.py

I'm publishing it, because I managed to get some readings from the sensor with it. However, the readings I got are wrong. My guess is that the timeout budget settings, which I have completely skipped in my code, are actually important, and needed to get proper measurements in mm. I will have to work on that part of the code some more to implement them, though.

Having said that, the readings I'm getting seem to be directly proportional to the distance, so this may be still useful to someone.

mjs513
Posts: 7
Joined: Wed May 10, 2017 12:01 am

Re: Converting the VL53L01X Arduino Library

Post by mjs513 » Sat May 13, 2017 9:41 pm

Hi deshipu,
Not sure if you saw the post over on the OpenMV forum, but I did manage to get same library converted. I did do the budget settings and seem to be getting fairly descent readings. Its is comparable to what I get out of the Arduino sketch. Here is the link. http://forums.openmv.io/viewtopic.php?f=6&t=211#p1769 Unfortunately the OpenMV board is the only one I have that has micropython on it. So if you want to give it a try, have fun. Compared to your version mine is a mess :)

Mike

Post Reply