Monday, November 16, 2015

Gesture Control via the Myo Armband in Node.js

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.

[caption id="attachment_118975" align="aligncenter" width="1024"]Photo Credit: Thalmic Labs Photo Credit: Thalmic Labs[/caption]

[author_more]

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

[code language="js"]
var Myo = require("myo");
[/code]

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

[code language="js"]
Myo.connect("com.sitepoint.myoarmbandcontroller");
[/code]

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:

[code language="js"]
Myo.on("connected", function(data, timestamp) {
console.log("Myo successfully connected. Data: " + JSON.stringify(data) + ". Timestamp: " + timestamp + ".");
});
[/code]

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

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

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).

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.

Continue reading %Gesture Control via the Myo Armband in Node.js%


by Patrick Catanzariti via SitePoint

No comments:

Post a Comment