Mobile
Article
By Patrick Catanzariti

Gesture Control via the Myo Armband in Node.js

By Patrick Catanzariti

In a world where we all want to control our gadgets via movements that make us feel like wizards, the Myo armband is a blessing. It brings some very neat gesture control to applications in an impressively simple way. Whilst you can implement the Myo API in iOS and Android, I’m a JavaScript developer at heart and love connecting this sort of thing to Node servers – so in this article we’ll be doing just that! We will be looking at using the Myo npm package to bring this functionality to a Node server.

Photo Credit: Thalmic Labs

Photo Credit: Thalmic Labs

What You’ll Need

  • A Myo Armband
  • Myo Connect – This will need to be installed and running on your computer.
  • Windows or Mac OSX – Myo does not currently seem to have Myo Connect available for Linux.
  • A basic understanding of using Node – Peter Dierx at SitePoint has written a pretty comphrensive guide on starting with npm.

Getting Started

Before going any further, ensure that:

  • You are wearing your Myo armband and it is charged up and ready to go!
  • You’ve got your Myo armband’s Bluetooth adapter plugged into the USB port closest to the arm wearing your Myo armband (a new requirement for later versions of the Myo software).
  • You’ve got Myo Connect running on your PC
  • You remember how to unlock your Myo armband so that it detects your gestures (tap your thumb and middle finger).

If you have all of that sorted, we can begin!

Starting Your Project

Create a new npm enabled project on your computer via the usual npm init (or by making your own package.json file). Install the npm module via npm install myo --save and create a file called index.js (or whatever you’d prefer your Node’s main file to be) for your code. When you want to run the server, run the usual node index.js command.

You could use the same concepts on the front end via Browserify too – however we won’t be covering Browserify in this article.

Start your index.js file with a require statement to bring in the Myo npm module

var Myo = require("myo");

Then, run Myo.connect() with a namespace you come up with (e.g. com.yourCompany.yourProject):

Myo.connect("com.sitepoint.myoarmbandcontroller");

Detecting Myo Connections

In order to detect when a Myo Armband connects, we can use the "connected" event. It provides a data variable and a timestamp variable upon success:

Myo.on("connected", function(data, timestamp) {
  console.log("Myo successfully connected. Data: " + JSON.stringify(data) + ". Timestamp: " + timestamp + ".");
});

The data variable provides a JSON object that looks like so:

{
  "mac_address" : "2b-or-n0-2b-QQ-QQ",
  "myo" : 0,
  "name" : "Stark",
  "timestamp" : "35259097032",
  "type" : "connected",
  "version" : [1,5,1970,2]
}

It provides the MAC address of the Myo, the index of the Myo (if you have more than one), the name you gave that Myo, the timestamp of when it connected (exactly the same as the timestamp variable also available in the callback above), the type of event which triggered this data and the version of Myo Connect you are using.

There is a much better way of retrieving data about your Myo armband from event calls like this one which we will look at later in this article (the version number seems to come through as an array above, it comes through as a version number separated by dots in the other version of the call).

--ADVERTISEMENT--

Detecting Poses

The most common thing every developer wants to use the Myo Armband for is to detect when the user makes certain poses. “Poses” are what Myo calls the specific gestures which Myo understands. These include fist, fingers_spread, wave_in, wave_out and double_tap.

To detect poses, we watch for them using the on() function. Here is an example of detecting fingers spread out:

Myo.on("fingers_spread", function() {
  console.log("Fingers spread!");
});

Detecting all other poses works exactly the same way, just switch out fingers_spread for one of the other events listed earlier (fist, fingers_spread, wave_in, wave_out, double_tap).

Another way you can detect poses is by watching for the generic pose event and then reading the data sent out. The pose event fires on every pose the Myo recognises:

Myo.on("pose", function(pose_name) {
  console.log("Myo pose detected! Pose was: " + pose_name + ".");
});

This method is useful if you’d like to respond to multiple types of poses in the same way:

Myo.on("pose", function(pose_name) {
  switch (pose_name) {
    case "wave_in":
    case "wave_out":
      console.log("You are waving!");
      break;
  }
});

Retrieving Myo Data Within Events

One thing you’ll notice when you start using events, if you continue via the structure of the connected event, is that retrieving the data about the armband doesn’t work. For example, this will not work:

Myo.on("pose", function(data, timestamp) {
  // Note for those copying this code - it is an example of what not to do! Don't copy paste this into your code!
});

That data variable now returns the pose name, as you might recall from the example before. So, what do we do if we want to find out about the Myo armband that caused the event to fire? There is a much better way to retrieve Myo armband data – the this keyword. For example:

Myo.on("fingers_spread", function() {
  console.log("Fingers spread!");
  console.log(this);
});

This will log a whole bunch of details about the Myo Armband that just sent through a fingers_spread event. That data looks like so:

{
  "macAddress": "2b-or-n0-2b-QQ-QQ",
  "name": "Stark",
  "connectIndex": 0,
  "locked": false,
  "connected": true,
  "synced": true,
  "batteryLevel": 0,
  "lastIMU": {
    "orientation": {
      "w": -0.54913330078125,
      "x": -0.4156494140625,
      "y": -0.362060546875,
      "z": -0.6282958984375
    },
    "accelerometer": {
      "x": 0.1279296875,
      "y": 0.8095703125,
      "z": 0.40673828125
    },
    "gyroscope": {
      "x": -3.8125,
      "y": -12.625,
      "z": 10.9375
    }
  },
  "arm": "left",
  "direction": "toward_elbow",
  "warmupState": "warm",
  "orientationOffset": {
    "x": 0,
    "y": 0,
    "z": 0,
    "w": 1
  },
  "events": [],
  "connectVersion": "1.5.1970.2",
  "lastQuant": {
    "w": -0.54913330078125,
    "x": -0.4156494140625,
    "y": -0.362060546875,
    "z": -0.6282958984375
  },
  "lastPose": null
}

Whilst I won’t cover everything that is coming through here in detail, there’s plenty to tinker with! We have the MAC address and name of the armband, whether it is locked, the battery level (though it doesn’t appear to be coming through correctly), last IMU (Inertial Measurement Unit) data (the 9-axis IMU tracks the movement of the armband’s gyroscope, accelerometer and orientation – possibilities are endless with the data this can provide) and more.

Other Possibilities

There are a tonne of more advanced tracking options you can explore with the Myo by using the EMG and IMU data it provides to detect completely custom gestures, this is something we’ll be exploring in a future article!

Conclusion

As you can see, setting up Myo gesture functionality within a Node server is actually nice and simple to do – yet it brings a lot of really fascinating possibilities. Within these callback functions on each gesture, you can run anything that works with Node and npm – connect up your IoT devices, send off some IFTTT triggers, control an Arduino based NodeBot or absolutely anything else!

If you do magically control a Node or JavaScript based app using the Myo armband and these code examples above, please share it in the comments or get in touch with me on Twitter (@thatpatrickguy). I love seeing what developers in the community get up to!

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.