Johnny-Five in Production

Johnny-Five in Production

- 4 mins

I found this writeup that I never published. Oops! Here it is at long last

Photo by Mariana B. on Unsplash

Back in the halcyon days of 2015, I was involved with a project to build a virtual periscope. The brief was that motion of a periscope, sensed via a digital compass, would control a panorama of a cold-war bunker. Said bunker is actually there under your feet, but it’s not the sort of thing you want to be drilling a big hole in to install a real periscope in - you have to lower your equipment down on a bit of rope to get in. It’s certainly not the sort of place you expect to deploy software.

We required a relatively simple way to read from our magnetometer - an HMC5883L if you’re interested - and feed this heading data to our panorama running in the browser to position and interact with the virtual tour.

We chose a Raspberry Pi since it can run a large part of our application and interface with the magnetometer over its GPIO pins. This also opened up the choice of technologies we could use to prototype and trial the idea before committing to a solution.

The decision was neatly narrowed down by a quick glance around the office. We didn’t all have the Ruby or Go experience to use something like Artoo or Gobot, but we all have plenty of JavaScript experience.

I was aware of two robotics libraries for JavaScript: CylonJS, and johnny-five. In the end familiarity won out as I’d used johnny-five more than cylonjs while toying about with my own Raspberry Pi.

As an aside, all of the linked libraries rely on the firmata protocol in order to communicate with the host device. In the case of an arduino a program is flashed to the arduino that bootstraps and runs firmata, accepting instructions over a serial connection. With the Raspberry Pi it uses raspi-io, which uses a firmata-compatible API. It has the downside of requiring a serial connection to run your code.

Getting started with johnny-five is about as straightforward as it gets:

'use strict';

var RasPi = require('raspi-io'),
    five  = require('johnny-five');

var board = new five.Board({io: new RasPi()});

with the board object, we can now make connections over the Raspberry Pi’s GPIO and use asynchronous features of the language to wait for and respond to events from the hardware.

Firstly we need to tell johnny-five to expect a magnetometer to be connected- luckily there is a preset Magnetometer contructor (it’s actually a compass object with some presets for the HMC5888L sensor, see the example code for more details.) The default read rate is 50ms, which can be adjusted up or down depending on the datasheet of the sensor.

board.on('ready', function() {
  var compass = new five.Magnetometer();

  /* ... */
});

Now we have our compass object, which is an EventEmitter, we can listen to a set of events by binding callbacks and respond to them in our application.

The event we want to listen out for is called headingchange, this is fired when the magnetometer reading has changed since the last reading and contains a heading object bound to this. There is also a data event which fires on each read of the heading regardless of all previous. Since we don’t need the data if the reading has not changed, we can safely use headingchange.

compass.on('headingchange', function() {
  // Send the new heading to the waiting browser
  // to tell it to pan the virtual tour appropriately.
});

The best part about the johnny-five code is that it stays out of the way. A colleague with very little johnny-five experience was able to read and work on the code in short order since it introduces no new syntax and keeps the simple event/callback logic that is familiar.

I didn’t find many articles about this so I think it’s safe to say that most johnny-five (and really, most firmata-based) code is being used in a hobbyist or setting. Since we had an unusual environment to run our application and didn’t need to run our hardware in an environment where firmata would be impractical it was the perfect chance to use it in a production setting.

Peter Mellett

Peter Mellett

I put the punctuation in the computer

rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora