7 steps to add multiple ToF LiDAR sensors to a micro:bit
I'm happy with the solution I came up with to attach multiple Time-of-Flight (ToF) sensors to a Raspberry Pi or CircuitPython board.
Now I want to add one or more ToF sensors to Anastasia, my Python-powered micro:bit robot.
Anastasia needs at least one distance sensor to stop her running into things.
Since she can travel backwards as fast as she goes forwards, she really needs two sensors: one looking forwards, and one looking behind.
I've been looking for a simple solution but I have hit a snag.
I've found several Python drivers for the VL53L0X chip I'm using, but none will run on the micro:bit.
Some are Python wrappers for C code; others are just too big to fit on the micro:bit.
I finally came up with an approach which looks as if it may work.
The goal is to use a board with an Arduino-ish clone, programmed in C, driving the sensors and returning results via SPI.
I've broken the project plan into seven steps which will build up into a complete solution.
Step 4 needs a bit more thought. It's tempting to return floating-point values that show the measured distances, but there's a potential snag. The SPI save code is interrupt-driven, and I'd need to make sure that the code does not try to return a multi-byte value while that value is changing.
Rule one of concurrent programming is to avoid it if you can! Concurrent code is often hard to write, hard to read, and hard to test.
What I'll do is allow an SPI request that specifies which sensor to read. I'll return a single byte of data which will be a scaled distance measurement. If a valid distance can't be read I will return a value of zero.
I will be working on this over the next day or two and will report progress as I go.
I forgot to mention that the code I'm using/developing is on GitHub.
Now I want to add one or more ToF sensors to Anastasia, my Python-powered micro:bit robot.
Anastasia needs at least one distance sensor to stop her running into things.
Since she can travel backwards as fast as she goes forwards, she really needs two sensors: one looking forwards, and one looking behind.
I've been looking for a simple solution but I have hit a snag.
I've found several Python drivers for the VL53L0X chip I'm using, but none will run on the micro:bit.
Some are Python wrappers for C code; others are just too big to fit on the micro:bit.
I finally came up with an approach which looks as if it may work.
The goal is to use a board with an Arduino-ish clone, programmed in C, driving the sensors and returning results via SPI.
Seven steps to ToF success
I've broken the project plan into seven steps which will build up into a complete solution.
- Arduino driving one sensor
- Arduino with more than one sensor
- Arduino(s) with one as SPI slave
- Arduino with more than one sensor that's interrupt-friendly
- Arduino with more than one sensor as SPI slave
- Arduino with level shifter and micro:bit
- 8MHz 3V3 Arduino and micro:bit
Step 4 needs a bit more thought. It's tempting to return floating-point values that show the measured distances, but there's a potential snag. The SPI save code is interrupt-driven, and I'd need to make sure that the code does not try to return a multi-byte value while that value is changing.
Concurrency rules
Rule one of concurrent programming is to avoid it if you can! Concurrent code is often hard to write, hard to read, and hard to test.
What I'll do is allow an SPI request that specifies which sensor to read. I'll return a single byte of data which will be a scaled distance measurement. If a valid distance can't be read I will return a value of zero.
I will be working on this over the next day or two and will report progress as I go.
Update
I forgot to mention that the code I'm using/developing is on GitHub.
Comments
Post a Comment