VL53L1X ToF distance sensor - adapting to micropython

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
User avatar
pythoncoder
Posts: 4792
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: VL53L1X ToF distance sensor - adapting to micropython

Post by pythoncoder » Sat Oct 03, 2020 4:24 pm

Thanks, that is useful information. That's a substantial archive ;)

AAMOI when you wrote your driver did you manage to track down a programming manual?
Peter Hinch

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

Re: VL53L1X ToF distance sensor - adapting to micropython

Post by deshipu » Sat Oct 03, 2020 11:39 pm

No, it's based on the Pololu Arduino library, if I remember correctly.

rodrigolange
Posts: 1
Joined: Fri Oct 30, 2020 2:50 pm

Re: VL53L1X ToF distance sensor - adapting to micropython

Post by rodrigolange » Fri Oct 30, 2020 3:21 pm

For those with some difficults regarding how to actually get the .py file from the bitbucket archive, my small contribution:

1 - download the repository from https://bitbucket-archive.softwareherit ... 53l0x.html and extract it somewhere. You will end with folder with a .hg subfolder

2 - in a linux machine install mercurial (in Ubuntu: sudo apt install mercurial) (you may use Windows, but I only tested in Linux)

Using a terminal, do the following:

3 - create a temporary folder (for instance, "temp" :-) ) and access it (cd temp)
4 - run the command "hg init ." (this will create a new mercurial repo in the local folder - dont forget the dot)

5- copy the .hg file from the folder created in step 1 to the new temp folder created in step 3
6 - form inside the temp folder, start a mercurial webserver with the command "hg serve".
7 - It will give you an error regarding some requirements (revlog-compression-zstd sparserevlog!). Its ok.
8 - edit the file .hg/requires and remove the entries related to the previous step (revlog-compression-zstd and sparserevlog)
9 - run "hg serve" again. It shoud give you a url (for instance, localhost:8000). Open that url with an web browser. You should see the repository

- Click in browse. The VL5310x.py is should be there :-)

Hope it helps and sorry my english :-)

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

Re: VL53L1X ToF distance sensor - adapting to micropython

Post by deshipu » Fri Oct 30, 2020 7:15 pm

You can just run "hg update" in the directory to which you unpacked the ".hg" directory.

User avatar
JonHylands
Posts: 66
Joined: Sun Dec 29, 2013 1:33 am

Re: VL53L1X ToF distance sensor - adapting to micropython

Post by JonHylands » Fri Nov 27, 2020 9:59 pm

So I'm working on Roz, my bioloid quad walker, and I've built a new head with five VL53L1X sensors in it.

It turns out the OpenMV project has a perfectly functional VL53L1X library:

https://github.com/openmv/openmv/blob/m ... vl53l1x.py

That works for a single sensor. I added a new method to allow the user to change the id of the current sensor:

Code: Select all

    def change_id(self, new_id):
        self.writeReg(0x0001, new_id & 0x7F)
        pyb.delay(50)
        self.address = new_id        
You need a lot of pauses in the right places to initialize 5 of the sensors, but it works, and I have it running.

dukeduck
Posts: 19
Joined: Thu Aug 29, 2019 2:06 pm

Re: VL53L1X ToF distance sensor - adapting to micropython

Post by dukeduck » Fri Jan 01, 2021 2:55 pm

Thanks for the link! I was looking for this driver for quite a while.

Also, would you provide an example for your change_ide method, say how to correctly initialize 5 sensors? What's the type of the variable new_id?

Thanks again.
JonHylands wrote:
Fri Nov 27, 2020 9:59 pm
So I'm working on Roz, my bioloid quad walker, and I've built a new head with five VL53L1X sensors in it.

It turns out the OpenMV project has a perfectly functional VL53L1X library:

https://github.com/openmv/openmv/blob/m ... vl53l1x.py

That works for a single sensor. I added a new method to allow the user to change the id of the current sensor:

Code: Select all

    def change_id(self, new_id):
        self.writeReg(0x0001, new_id & 0x7F)
        pyb.delay(50)
        self.address = new_id        
You need a lot of pauses in the right places to initialize 5 of the sensors, but it works, and I have it running.

User avatar
JonHylands
Posts: 66
Joined: Sun Dec 29, 2013 1:33 am

Re: VL53L1X ToF distance sensor - adapting to micropython

Post by JonHylands » Fri Jan 01, 2021 3:10 pm

Okay, here's my code, which works pretty much 100% of the time. Note that the delays are absolutely required, but the times may need optimization. The values that I use work, but you might be able to get away with shorter delays. You will of course want to change out the shutdown pin definitions with the pins you have them hooked up to. Note also I assume that self.i2c is already set with something like this:

Code: Select all

from machine import I2C
self.i2c = I2C(HEAD_I2C_BUS)
In my case, the HEAD_I2C_BUS is 1, since I am using I2C_1.

Note also that it is really critical to pull all five sensor shutdown pins low at the beginning - if you do a soft-reboot of your MIcroPython processor, and don't power cycle the sensors, you will try to initialize them from whatever state they are in, including their new addresses, so this routine wouldn't work. When you shut them down with the shutdown lines, you basically power cycle them, and when you start each one up, it comes up in a fresh state with the default address.

Code: Select all

HEAD_SHUTDOWN_LEFT = pyb.Pin.cpu.C3
HEAD_SHUTDOWN_FRONT_LEFT = pyb.Pin.cpu.B9
HEAD_SHUTDOWN_FRONT = pyb.Pin.cpu.B8
HEAD_SHUTDOWN_FRONT_RIGHT = pyb.Pin.cpu.C6
HEAD_SHUTDOWN_RIGHT = pyb.Pin.cpu.C7

HEAD_RANGE_DEFAULT_ADDRESS = 0x29
HEAD_RANGE_FRONT_ADDRESS = 0x30
HEAD_RANGE_FRONT_LEFT_ADDRESS = 0x31
HEAD_RANGE_FRONT_RIGHT_ADDRESS = 0x32
HEAD_RANGE_LEFT_ADDRESS = 0x33
HEAD_RANGE_RIGHT_ADDRESS = 0x34


  def initializeRangeSensors(self):
    shutdown_left_pin = pyb.Pin(HEAD_SHUTDOWN_LEFT, pyb.Pin.OUT_PP)
    shutdown_front_left_pin = pyb.Pin(HEAD_SHUTDOWN_FRONT_LEFT, pyb.Pin.OUT_PP)
    shutdown_front_pin = pyb.Pin(HEAD_SHUTDOWN_FRONT, pyb.Pin.OUT_PP)
    shutdown_front_right_pin = pyb.Pin(HEAD_SHUTDOWN_FRONT_RIGHT, pyb.Pin.OUT_PP)
    shutdown_right_pin = pyb.Pin(HEAD_SHUTDOWN_RIGHT, pyb.Pin.OUT_PP)
    # Shut down all the sensors
    shutdown_left_pin.value(0)
    shutdown_front_left_pin.value(0)
    shutdown_front_right_pin.value(0)
    shutdown_right_pin.value(0)
    shutdown_front_pin.value(0)
    pyb.delay(250)

    shutdown_front_pin.value(1)
    pyb.delay(250)
    self.front_lidar = VL53L1X(self.i2c, address=HEAD_RANGE_DEFAULT_ADDRESS)
    pyb.delay(100)
    self.front_lidar.change_id(HEAD_RANGE_FRONT_ADDRESS)
    pyb.delay(100)
    shutdown_front_left_pin.value(1)
    pyb.delay(100)
    self.front_left_lidar = VL53L1X(self.i2c, address=HEAD_RANGE_DEFAULT_ADDRESS)
    pyb.delay(100)
    self.front_left_lidar.change_id(HEAD_RANGE_FRONT_LEFT_ADDRESS)
    pyb.delay(100)
    shutdown_front_right_pin.value(1)
    pyb.delay(100)
    self.front_right_lidar = VL53L1X(self.i2c, address=HEAD_RANGE_DEFAULT_ADDRESS)
    pyb.delay(100)
    self.front_right_lidar.change_id(HEAD_RANGE_FRONT_RIGHT_ADDRESS)
    pyb.delay(100)
    shutdown_left_pin.value(1)
    pyb.delay(100)
    self.left_lidar = VL53L1X(self.i2c, address=HEAD_RANGE_DEFAULT_ADDRESS)
    pyb.delay(100)
    self.left_lidar.change_id(HEAD_RANGE_LEFT_ADDRESS)
    pyb.delay(100)
    shutdown_right_pin.value(1)
    pyb.delay(100)
    self.right_lidar = VL53L1X(self.i2c, address=HEAD_RANGE_DEFAULT_ADDRESS)
    pyb.delay(100)
    self.right_lidar.change_id(HEAD_RANGE_RIGHT_ADDRESS)
    pyb.delay(100)
Let me know if this works for you.

dukeduck
Posts: 19
Joined: Thu Aug 29, 2019 2:06 pm

Re: VL53L1X ToF distance sensor - adapting to micropython

Post by dukeduck » Sun Jan 03, 2021 7:27 am

Thanks a lot. I ordered a few of them, and will test when I get them.

Post Reply