Pages

Sunday, 26 April 2020

Virtual Pi Wars 2020 was outstanding!

This year Pi Wars went virtual, and it was a huge success.

Mike Horne did a superb job as compère, and the Robot videos were wonderful: always informative, often amusing and of very high quality.

Over the next few days we'll have a chance to vote. It's going to be a very difficult choice!

The Teams presented creative and individual solutions, but some common themes emerged. Lots of teams made use of simulations. Some used custom software based around standard libraries; others were based on specialised open source frameworks like VTK.

Most Robots were programmed in Python, but several used Pi controllers with peripheral Arduino nanos or teensies programmed in C.

Some teams used ToF (Time of flight) sensors for distance sensing and SLAM (Simultaneous location mapping and mapping). A growing number of entrants used OpenCV or plan to use it next year.

Many Teams share their software and designs. I'm looking forward to exploring several of the projects on GitHub as I look through the videos over the next day or so.

Over 300 people watched the virtual presentation. They  savoured three hours of excellent virus-free entertainment. Thanks to everyone involved, and special thanks to Mike Horne and Tim Richardson for turning that bright idea all those years ago into a wonderful, enduring institution.

Tuesday, 7 April 2020

The micro:bit is talking to the VL53L0X ToF sensors!

This chip is no more :(
This morning I got my Arduino sensor driver talking to a micro:bit using SPI.

The Arduino Uno runs at 5 volts, and the micro:bit works at 3v3, so I used a level shifter to connect the two.

My first attempt failed disastrously.

The level shifter I used was based on the TI TXB0108.
I managed to connect the 5v power and inputs to the 3v3 side, and I fear the magic smoke has left the chip.

It's a shame, as the board was my first attempt at soldering SMD devices. It's been sitting unused in my bits box for a few years, though, so it's not the end of the world.

Fortunately I had an Adafruit level shifter breakout based on the same IC. It's well laid out with very clear pin markings and I managed to connect it correctly.

The replacement worked perfectly and the micro:bit MicroPython code was very straightforward. I've listed it below, and it's with the other code on GitHub.

from microbit import *
import struct
from time import sleep

SENSORS = 2

def spi_read(sensor):
    pin16.write_digital(0)  # Chip select
    ibuffer = struct.pack('<B', sensor)
    spi.write(ibuffer)
    result = spi.read(1)
    pin16.write_digital(1)  # Chip select off
    return result

spi.init(baudrate=100000)
while True:
    for i in [0, 1]:
        print(i, ord(spi_read(i)))
        sleep(0.1)


The technology is working. The next step is to build a compact version for Anastasia. I'll report on progress.

Monday, 6 April 2020

Read distances from up to 8 VL53L0X sensors via SPI

I'v now got my two-Arduino version of a multiiple ToF sensor LiDAR working.

I have only tested it with two sensors; I'll add one more later, and report, but the code should work with more.

I'm a little hesitant about that claim. The Slave Arduino code has little spare memory but I've tried to  reduce requirements to a minimum. I have more VL53L0X sensors on order, and I am hoping I can handle up to eight.

That would allow a robot to get a good sense of the closest obstacles.

The Slave code uses an SPI interrupt routine modified from Nick Gammon's excellent example.

The code that configures and then reads from the ToF sensors is a complete rewrite of Adafruit's example code for dual VL53L0X sensors.

I'm no expert at C programming, so if you can see improvements (or bugs), let me know!

The code is on GitHub, along with some minimal documentation.

The next step will be go get the Arduino slave working with a micro:bit master; then I'll need to work out how to install hardware and software on Anastasia to help her avoid collisions.

Here's the core code for the slave:

const byte MAX_OK = 253; // largest acceptable range / 2
const byte NO_SUCH_SENSOR = 255;
const byte INVALID_READING = 254;
const int SENSORS = 2; // two sensors for now
// addresses we will assign to sensors
byte address[SENSORS];
// pins to shutdown
byte shutdown_pin[SENSORS];
// scaled distances
byte distance[SENSORS];

// create array of sensors
Adafruit_VL53L0X lox[SENSORS];
VL53L0X_RangingMeasurementData_t measure;

void setID() {

  int i;
  for (i=0; i < SENSORS; i++) {
    shutdown_pin[i] = i + 2; // pins go from 2 to SENSORS + 2
    address[i] = (byte) 0x30 + i;
    // addresses go from 0x30 to 0x37
    pinMode(shutdown_pin[i], OUTPUT);
    digitalWrite(shutdown_pin[i], LOW);
  }
  delay(10);
  for (i=0; i < SENSORS; i++) {
    digitalWrite(shutdown_pin[i], HIGH);
    delay(10);
    bool result = lox[i].begin(address[i]);
    if(!result) {
      while(1);  // loop forever if the setup didn't work
    }
  }
  delay(10);
}

void readSensors() {
  for (int i=0; i < SENSORS; i++) {
    float range;
    lox[1].rangingTest(&measure, false);
    if (measure.RangeStatus != 4) {
      range = measure.RangeMilliMeter;
      distance[i] = min(range / 2, MAX_OK);
    } else {
      distance[i] = INVALID_READING;
    }

  }
}

void setup() {
  // turn on SPI in slave mode
  SPCR |= bit (SPE);

  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  setID();
  SPI.attachInterrupt();
}

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;  // grab byte from SPI Data Register
  // return scaled distance or error code
  SPDR = c < SENSORS ? distance[c] : NO_SUCH_SENSOR;
}  // end of interrupt routine SPI_STC_vect


void loop() {
  readSensors();
  delay(100); // read all sensors every 1/10 second
}

Saturday, 4 April 2020

SPI access to multiple ToF sensors is working

I've been ploughing through the steps towards my goal of connecting multiple distance sensors to Anastasia's micro:bit.

I now have a pair of Arduinos connected via SPI, with the slave reporting distances from two VL53L0X sensors.

The wiring is a mess and the code needs cleaning up.

But it's working, and replacing the Arduino master by a micro:bit should be pretty straightforward.

I'll report on progress.

The code is on GitHub.

Friday, 3 April 2020

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.

Seven steps to ToF success


I've broken the project plan into seven steps which will build up into a complete solution.

  1. Arduino driving one sensor
  2. Arduino with more than one sensor
  3. Arduino(s) with one as SPI slave
  4. Arduino with more than one sensor that's interrupt-friendly
  5. Arduino with more than one sensor as SPI slave
  6. Arduino with level shifter and micro:bit
  7. 8MHz 3V3 Arduino and micro:bit
So far I've got, and run, sample code for the first three steps. The code so far has all come from reliable sources on the web. The core driver code and code for multiple sensors are Adafruit Arduino libraries for the VL53L0X. The SPI slave code comes from Nick Gammon's excellent site.

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.


Wednesday, 1 April 2020

Muliplexing I2C with the Adafruit TCA9548A multiplexer

I want to attach multiple VL53L0X Time-of-flight sensors to a single I2C bus. It looks as if that should be easy, but it's not.

One solution involves disabling the sensors and then enabling them one at  a time to change their address. It's workable, but fiddly.

If you only have two sensors you could use different I2C buses, but I eventually want to run 8 sensors, so that won't work.

The TCA9548A I2C multiplexer


There is another approach that is easy to implement and works out of the box.

The TCA9548A is an I2C device that acts a little like a telephone exchange.

You can use it to route I2C transactions to one of eight separate I2C busses. That allows you to have eight devices with the same I2C address, one on each bus.

I did a first experiment using an Adafruit CircuitPython m4 feather.

That worked well, and I've just confirmed that it works just as well with a Raspberry Pi. Since I'm using Adafruit's CircuitPython libraries, the same code runs on the Adafruit m4 and the Pi.

As you can see from the image above, the Pi I used was a veteran Pi model 2.

Kudos to the team at Raspberry Pi for maintaining software compatibility, and to Adafruit for making CircuitPython so portable.