Charlie Gerard bio photo

Charlie Gerard

Software Developer & Creative technologist, I am passionate about the mix of science, art and technology

Email Twitter Google+ Github

myo-drone-nodjs-tutorial

Following the last tutorial I wrote on how to control the Sphero with the Myo armband, here is another one on how to control the Parrot AR Drone with the Myo using Node.js.

If you want to follow along, you can find the repo on my github.

To begin with, we need to setup the server configuration. After requiring the necessary modules, here is what we need to write:


  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  var env = process.env.NODE_ENV || 'development';
if('development' == env){
  app.set('port', process.env.PORT || 3000);
  app.use(express.static(__dirname + '/public'));
  app.use("/node_modules", express.static(path.join(__dirname, 'node_modules')))
}

server = require('http').createServer(app);

var bayeux = new faye.NodeAdapter({
    mount: '/faye',
    timeout: 50
  });

  bayeux.attach(server);

  client = new faye.Client("http://localhost:" + (app.get("port")) + "/faye", {});

  client.subscribe("/drone/move", function (d) {
    console.log(d);
    console.log("drone move?")
    return drone[d.action](d.speed);
  });

  client.subscribe("/drone/drone", function (d) {
    console.log(d);
    console.log("drone stuff")
      return drone[d.action]();
  });

  server.listen(app.get('port'), function () {
    console.log("Express server listening on port " + app.get("port"));
  })
  

We use express to set up the server and Faye & Bayeux for the communications between the Drone and your app.

We also use the module ‘dronestream’ to be able to stream video from the drone to the browser that is listening on port 3001.

The rest of the code deals with the commands sent from the Myo to the drone. First of all, we need to create an instance of the Myo and then define some commands.


  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  myo = Myo.create();

myo.unlock();

myo.on('fingers_spread', function(){
    takeoff();
});

myo.on('wave_in', function(){
    goLeft();
})

var takeoff = function () {
    flying = true;
    return faye.publish("/drone/drone", {
      action: 'takeoff'
    });
};

var goLeft = function(){
    stopped = false;
    setTimeout(function (){
      return faye.publish("/drone/move", {
        action: 'left'
        // speed: adjustXspeed
      })
    }, timeout);
  };
  

Once the instance of the Myo is created, it can detect certain types of movements and run functions accordingly.

The ‘myo.unlock()’ command should allow the Myo to be unlocked while you use the app. However, I noticed I still needed to execute the unlock gesture ‘double tap’ between each command… Then the ‘faye.publish’ allows to send the commands and make the drone move. You can adjust the action and speed as you like.

This is just a snippet so once again, if you wanna have a look at the rest of the code or if you want to play around with it, everything is available on my Github :)

Hope it helps and enjoy!