Welcome to another Myo Unleashed. This time we've got Scott Tolksdorf, the author of myo.js. Scott is actually one of our awesome web developers at Thalmic Labs, but he decided that building our sweet websites wasn't enough and made this killer JavaScript framework in his spare time.

Did you know you can embed Myo control directly into any website with myo.js, without the need for any extra software or plugins? That's how the Myo Diagnostics Page was built. I'm Scott Tolksdorf, and I wrote the myo.js JavaScript framework. Today I'm going to give you a quick run through on how to get started so you can Myo-enable your own webpages.

Setup

The initial setup is pretty easy:

  1. Grab myo.js and include it into your webpage.
  2. Done.

It also plays nicely with Node.js. If you want to play around with myo.js and don't have a webserver set up (or are already on board with building something using Node) Here's what you need to do:

  1. Install node.js
  2. Open up a command line (on Windows, there is a special "Node.js Command Prompt" you can launch
  3. Run npm install myo

Now you are good to go. We are going to assume you are using node for the rest of this guide, but like I said you can also connect everything up with a real webpage.

How to Myo

Let's make a basic script. In Node, you will first need to require myo:

    var Myo = require('myo');  

You'll probably want to make sure that Myo Connect is actually running:

Myo.onError = function () {  
        console.log("Woah, couldn't connect to Myo Connect");
}

Myo.js is all about events. Every time a Myo armband is connected, every time a user does a pose, every time you get bit of orientation data, it's an event. We handle that through the Myo.on(eventName, callback) function. Just register the callback you want to be fired in response to a given event. So we might do something like this:

Myo.on('fist', function(){  
   console.log('Fist!');
   this.vibrate();
});

Nothing will actually happen until you call Myo.connect(), however. That's what actually opens the websocket and starts talking to Myo Connect. I recommend you do all your setup and connect up your events before you call Myo.connect(). You could call it earlier, but there's a chance you'll hit a race condition.

Anyway, our fist code above will actually register that function to be called whenever ANY Myo armband connected to the system detects a fist. If you want to listen for a specific Myo, the best place is in the connected event (called in response to a specific Myo armband connecting, not as a direct result of calling Myo.connect()). That would something like this:

var myMyo;

Myo.on('connected', function(){  
    myMyo = this;
    addEvents(myMyo);
});

var addEvents = function(myo){  
    myo.on('fist', function(){
        console.log('fist for ', this.macAddress);
    })
}

Capital M Myo will let you deal with global events. Lowercase m myo generally means a specific one. Under the covers, Myo.js will automatically create a myo instance for every paired Myo armband on your system.

Note that in the above code myMyo will get overwritten every time a Myo armband connects, so if you care about accessing it outside of the event handlers you register, make sure you stick it in an array or something.

Anyway, that's how you register for events, and once you know how to that, you can do everything. For example, IMU data is just another set of events. Some rudimentary punch detection using the x axis of the accelerometer might look something like this:

var punchTime = 0;  
myMyo.on('imu', function(data) {  
       var time = (new Date()).getTime();
       if (punchTime < time - 1000 && data.accelerometer.x < -1.0) {
              console.log("PUNCH!");
              punchTime = time;
       }
})

EMG data is also basically the same. You just have to turn it on first. Best place to do that is in connected (unless you only need it in certain situations), because the EMG streaming state will get turned off every time the Myo connects.

myMyo.on('connected', function(){  
    myMyo.streamEMG(true);
});
myMyo.on('emg', function(data){  
    console.log(data);
});

You can get a list of all the events you can register for, and their arguments, here.

That's pretty much it. There are more helper functions in there, and you can of course do typical Myo things like vibration and controlling the lock state. That's all in the README though. I plan on adding more examples in the near future, as well. Just don't forget to call Myo.connect()!

There is one other thing. I have a bit of... "experimental" work, that you might be interested in. This goes above and beyond the typical SDK, and.. it's a work in progress. Check it out here, but there are gems like determining whether the arm is busy, tap detection, and directional waves.

Let me know in the comments if you make something cool! And this project is open source, so if you find a bug or something, feel free to submit a pull request!

Newsletter

Enter your email address and get all latest content delivered to your inbox every now and then.