Thursday, 12 October 2017

The ClusterHat goes Stereo!

Yesterday I described my first experiments with the Raspberry Pi ClusterHat.

I've had a play done a bit more serious research since then and I now have two Pi cam2s connected to two of the Pi zeros. Now I can take stereo photos!

The first pair of images aren't of great artistic merit, but they prove things work.

Morten and I plan to build on this using APL to convolve the two images in order to determine the distance from the cameras of each pixel in the scene. Once the ClusterHat and PiCams are mounted on a mobile base we hope we'll have a mobile robot with stereo depth perception.

Follow progress on @rareblog and @dyalogapl!

Wednesday, 11 October 2017

Clusterfun with the Raspbery Pi Clusterhat

Want to build your own supercomputer at home?

You can take a step in that direction with a Raspberry Pi 3, four Pi zeros and a ClusterHat.

The ClusterHat allows you to connect four pi Zeros to a Raspberry Pi controller and network them all together. You've now got 8 cores and 2 Gb of memory to play with - not quite up to Cray computing standards, but enough to explore some modern clustering software.

Avoid these snags!

I've just got my cluster up and running, with four ssh sessions from the controller to the four zeros. The instructions on the ClusterHat website are pretty clear, but I did hit a few minor snags before I got everything working smoothly.

Make sure you have a 2.5 Amp power supply.

The first is entirely my own fault, but don't make the same mistake! I used an old-style power supply which was rated at 2 amps. When I started the fourth zero, the power supply became overlaoded and Bad Things Happened. The controller crashed and corrupted its SD card, so I had to re-flash the image.

I switched to a 2.5 Amp supply, as recommended for the Pi 3, and now everything is fine.

Enable ssh on each image

You'll need to create an empty file called ssh in the boot partition of each SD card in order to be able to log in to each Pi zero once the cluster is running.

If you're running Linux, you can just

touch /media//boot/ssh 

or whatever the path  is on the machine you're using to prepare the SD cards. Remember to do this for each of the images! If you forget, the zeros will reject attempts to log in via ssh once they have started up, and you will have no safe way of closing them down.

Be wary when you upgrade the software

It's good practice to upgrade the installed software on your Pi after installing a new image. If you do so, take care not to overwrite the custom configuration for dnsmasq that's on the special cluter images.

Here's what to watch out for when you run sudo apt-get upgrade:

apt-get may well find a newer version of the dnsmasq program which is needed to network the Pi zeros to the controlling Pi. Since dnsmasq comes with a default configuration file, you will be asked if you want to retain or overwrite the custom configuration which the ClusterHat needs for its configration. Accept the default answer, which will not overwrite the changes and leave the required custom configuration in place.

Parallel Computing made simple

My particular interest is in running Dyalog APL on all five computers in the cluster. APL is a great language for exploratory programming, and it's also capable of high-performance computing. Morten Kromberg and I recently did some quick informal benchmarks and found that APL was up to five times faster than Python's numpy for some matrix operations.

Since I am actively researching Neural Networks, and I build Robots controlled by the Raspberry Pi, I'm interested in anything that can squeeze more performance out of the platform.

Dyalog APL supports two different forms of parallel processing.The first needs no programmer effort; if the APL run-time engine detects that you are carrying out operations on large arrays, it will seamlessly and automatically share the work across multiple processor cores.

The second uses a language feature called Isolates. It allows you to farm off work to a different process, which may be running in a different processor. You can see a fun demonstration of Isolates on Morten Kromber's blog.

Over the next few days Morten and I will be exploring the ClusterHat as a way of sharing computational workload using Isolates. Follow @rareblog and @dyalogapl to see our progress.

Saturday, 7 October 2017

Programming with MicroPython by Nicholas Tollervey

I've just finished reading the final version of Programming with MicroPython by Nicholas Tollervey.

I love MicroPython - it helps me to develop working code faster, and it's fun to use!

MicroPython has been around for a while. I first encountered it when I met the original author, Damien George, at an Open Technology seminar at Cambridge.

More recently, I've been exploring MicroPython on the BBC micro:bit, and in the last few days I've been working on a doorbell extender project (more details soon!) using the Adafruit Feather Huzzah ESP8266, a small, low-cost board with WiFi capability. 

All these and more are covered in Tollervey's book.


Programming with MicroPython: contents

The book starts with a forward by Damien George, and continues with an introduction to the language. MicroPython is an almost complete implementation of the popular Python Language. It comes with its own set of libraries which allow you to access the features of the micro-controller it's running on.

(The documentation lists a few missing language features; for example, you can't use MetaClasses. The omissions won't bother the majority of developers, and they certainly won't prevent you from creating fun, interesting and useful projects.)

Next comes one of my favourite passages. Tollervey describes the excitement he and many others felt when the original BBC Micro arrived back in the 1980s.

MicroPython can generate the same excitement, but it runs on hardware that is more capable and costs a fraction of the price of the original BBC Micro.


MicroPython Hardware

The next four chapters cover four of the hardware platforms on which MicroPython runs. Each chapter has an overview of the hardware and its capabilities, and then gives detailed instructions for setting up a development environment.


The PyBoard

Tollervey starts with the PyBoard. The PyBoard is the original platform created by Damien George.

It launched after a very successful kickstarter campaign, and is now widely available.

The image is of the original version, which has been replaced by version 1.1


The micro:bit

The next platform is the BBC micro:bit. It's the BBC's re-invention of their 1980s microcomputer, which captured the imagination of Tollervey, Eben Upton (of Raspberry Pi fame) and a whole generation of British Schoolchildren.

The micro:bit is doing the same thing for schoolchildren world-wide. It's enjoyed great success in the UK but there are flourishing communities in countries as far apart as Croatia and Sri Lanka.

The micro:bit is small, fun and inexpensive, and it has a variety of programming environments available. There are several reasons to choose MicroPython and the Mu editor:
  • The Python language was created with educational use in mind. It's easy to learn and has a large, world-wide supportive community
  • The Mu editor is simple to use, and you can deploy your program almost instantly. Other environments involve a fiddly code/compile/copy process that can slow down development. It's easy to make an error - the last thing you want when you're learning a new skill.
  • Mu gives you easy access to the Python REPL, so you can type code in and see the results immediately.
Tollervey describes how to set up and use the Mu editor and gives a sample program. He also gives a link to the online documentation and tutorial.

You can also get a free workbook which covers most of the micro:bit MicroPython capabilities. Disclosure - I wrote it :)


Adafruit Circuit Playground Express

The next hardware platform is the recently updated Adafruit Circuit Playground Express. (There is an earlier version which uses a different processor that doesn't support MicroPython).

This board is overloaded with goodies:
  • 10 x mini NeoPixels, each one can display any color
  • 1 x Motion sensor (LIS3DH triple-axis accelerometer with tap detection, free-fall detection)
  • 1 x Temperature sensor (thermistor)
  • 1 x Light sensor (phototransistor). Can also act as a color sensor and pulse sensor.
  • 1 x Sound sensor (MEMS microphone)
  • 1 x Mini speaker with class D amplifier (7.5mm magnetic speaker/buzzer)
  • 2 x Push buttons, labeled A and B
  • 1 x Slide switch
  • Infrared receiver and transmitter - can receive and transmit any remote control codes, as well as send messages between Circuit Playground Expresses. Can also act as a proximity sensor.
  • 8 x alligator-clip friendly input/output pins
  • Includes I2C, UART, 8 pins that can do analog inputs, multiple PWM output
  • 7 pads can act as capacitive touch inputs and the 1 remaining is a true analog output
  • Green "ON" LED so you know it's powered
  • Red "#13" LED for basic blinking
  • Reset button
  • ATSAMD21 ARM Cortex M0 Processor, running at 3.3V and 48MHz
  • 2 MB of SPI Flash storage, used primarily with CircuitPython to store code and libraries.
  • MicroUSB port for programming and debugging
  • USB port can act like serial port, keyboard, mouse, joystick or MIDI!
 Best of all, you can control this with MicroPython! The book tells you how, and introduces CircuitPython - Adafruit's fork of MicroPython, designed to give you a consistent API across all the Adafruit MicroPython platforms.

The fourth chapter on hardware covers two powerful processors from Shanghai-based manufacturer, Espressif Systems; the ESP8266 and ESP32.


ESP8266 / ESP32

The ESP8266 has generated a huge amount of interest in the maker community. It has a processor that's powerful enough to run MicroPython, it has a low-power mode, and it has on-board WiFi. That's a perfect set of features for low-cost IoT (Internet of Things) projects.

Adafruit has a Feather Huzzah ESP8266 board for around $17. (You can get similar boards from China for as little as $1). The feather means that there is a family of add-on Adafruit wings - compatible boards like motor controllers, GPS modules and OLED display drivers.

Best of all, the ESP8266 has an easy-to-use browser-based development environment called WebREPL. The book has very clear instructions telling you how to set it up; I had mine going in just over three minutes! (It may take you a little longer - you are probably less impetuous than me.)

The ESP32 is even more powerful but the software is currently less mature. Tollervey guides you through the steps you'll need to take in order to set up a development environment.


Thinking Embedded

The next chapter has great advice about how to approach the design, specification and development of embedded projects by focusing on user needs and usefulness.

It's followed by a series of chapters which cover the main ingredients of an embedded system:

  • Visual Feedback
  • Input and Sensing
  • GPIO
  • Networking
  • Sound
 Code examples are sprinkled throughout the text.


I love building robots, and this book covers two: the DIY TrundleBot and the Bit:Bot from 4Tronics. There are complete, detailed MicroPython programs for both, and plenty of suggestions for experiments of your own.

Tollervey is a keen exponent of good Pythonic coding, so the next chapter should be no surprise.

Idiomatic MicroPython

The chapter on Idiomatic MicroPython is recommended reading for Python novices and veterans alike. It describes a set of qualities sought after by the most experienced Python developers, and explains some of the particular challenges of programming in a resource-constrained environment.

Next Steps

The final chapter encourages readers to participate in the Python community, and lists additional resources.

It ends with great advice:

get stuck in and remember...

Hack it,
Less is more,
Keep it simple,
Small is beautiful,
Be brave! Break things! Learn and have fun!
Express yourself with MicroPython.
Happy hacking! :-)

—The Zen of MicroPython

About the Author

Tollervey is well-qualified to write about MicroPython. He's the author of the excellent, Open Source, minimalist Mu IDE, and was the original instigator of the effort to port it to the micro:bit. He's a Python Software Foundation Fellow, and has recently been recognised by Google for his contributions to the Open Source community.

An excellent book

I strongly recommend this book. If you're a novice, keen to explore the fun and power of programming micro-controllers with MicroPython, this forms an excellent introduction. If you're a grizzled veteran like me you can still expect to find new and useful material in every chapter.

Availability - and a chance to meet the author!

The book is available to pre-order on Amazon. It's scheduled for release on 21st October in the US and 31st October in the UK.

If you're in or near London on the evening of 6th November, there's a chance to meet the author. Nicholas Tollervey is going to speak at the London MicroPython Meetup at the Barclays Eagle Labs in Notting Hill Gate. You can find details here.