Control the Sphero with the Leap Motion using Node.js

leap_sphero_node

A few months ago, I wrote a tutorial on how to control the Sphero with the Leap Motion using Cylon.js but I actually forgot to write one about using Node.js (which is what I started with).

If you’d like to go straight to the source code, you can find it on my github.

When I started working on this project, I used the Spheron module but I think something is not working with it anymore so I changed to the official sphero.js library.

If I start by explaining the setup of the app in Node.js, here is a snippet of my app.js file with comments to explain what everything does:

// require the relevant modules
var express = require('express'),
    path = require('path');

//create web server
var app = express();

//require the custom module
var test = require('./my_modules/sphero');
test();

// Everything in public will be accessible from '/'
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'views')));

app.all('*', function(req, res){
  res.sendfile('views/index.html');
});

app.listen(3001);
console.log("server running on port 3001");

As you can see, this is pretty straight forward. The most interesting part of the code is inside my custom module (index.js file).

Snippet:

module.exports = function() {

  var Leap = require('leapjs');
  var sphero = require("sphero");

  // Set this to the device Sphero connects as on your computer.
  var device = sphero("/dev/tty.Sphero-RBR-AMP-SPP");

  var safeMode = true;

  var controlSphero = function(spheroBall) {

      var controller = Leap.loop({frameEventName:'deviceFrame',    enableGestures:true});

      controller.on('frame', function(frame) {
          if(frame.hands[0]){
            var g = frame.hands[0];
            handleSwipe(g);
          }
      });

      var handleSwipe = function(g) {
            var previousFrame = controller.frame(1);
            var movement = g.translation(previousFrame);
            var direction = '?';

            if(movement[0] > 4){
              direction = 'RIGHT'
            } else if(movement[0] < -4){  
              direction = 'LEFT'             
            }             

            if(movement[1] > 4){
              direction = 'UP'
            } else if(movement[1] < -4){               
              direction = 'DOWN'             
            }             

            if(movement[2] > 4){
              direction = 'REVERSE'
            } else if(movement[2] < -4){
              direction = 'FORWARD'
            }

          switch (direction) {
            case 'LEFT':
              console.log('left')
              spheroBall.roll(70, 270, 1);
              break;
            case 'RIGHT':
              spheroBall.heading = 90;
              spheroBall.roll(70, 90, 1);
              break;
          }
      	console.log('Direction: %s', direction);
      }

        controller.connect();
      };

  // Stops the Sphero from rolling.
  var stopSphero = function(spheroBall) {
      spheroBall.roll(0,spheroBall.heading||0,0);
  };

  device.connect(function() {
      console.log('connected to Sphero');
      controlSphero(device);
  });

};

You start by requiring the modules you need, so here, ‘leapjs’ and ‘sphero’.

You then need to create an instance of the Sphero by using the reference of the ball on your computer.

To find that, turn your Sphero on as well as the bluetooth on your computer, connect to the Sphero, and once this is done, execute this command in your terminal: ‘ls /dev/tty.Sphero*’. The reference of the Sphero should appear and you just need to copy it in the code (it will probably be the same as mine).

Then, create an instance of the Leap Motion controller and start slowly by just tracking movement over it. Once you can track your hand, here is how you track the direction of your movement:

controller.frame(1);

tracks the first frame of your movement.

g.translation(previousFrame);

gets you the difference between your first frame and the current frame, so you can see which coordinate is the most impacted by your movement and therefore get the direction.

These coordinates are in the format “x,y,z” so horizontal axis, vertical axis and depth.

Once you got that working, it’s time to hook up the Sphero and the Leap Motion together.

To make the ball roll, you simply need to call “.roll()” on the sphero, like this:

spheroBall.roll(70, 270, 1);

The attributes passed in are the following:

sphero.roll(speed, heading, state, option)

To finish, all you need to do is call the “device.connect” function to connect with your Sphero.

And that’s it!

Once again, don’t hesitate to fork the repo if you’d like to play with it yourself and improve it 🙂

Also, feel free to let me know if any of this is not working!

Have fun!

Advertisements

The future of interaction is touch-free with ultrasound tech

Elliptic_Labs

More and more devices seem to use gesture tracking to allow touch-free interaction such as the Leap Motion and Kinect but also the recent and innovative onecue (see related article).

However, Elliptic Labs have introduced a new tool called the “Multi Layer Interaction” (MLI). It is not a new device but rather a new technology that can be implemented in already existing devices like your smartphone.

Tracking the position and distance of your hand, the device can ‘wake up’ when it senses movement, and execute other commands depending on the distance threshold the hand reaches. It can then improve the user’s interaction and experience with their smartphone.

On the technical side, it uses a minute transducer and a small ultrasound speaker to detect the movements around and it can be easily adapted to computers, smartphones, tablets and wearables. It can also use the built-in microphone to echo-locate the user’s hand so most manufacturers simply need to add the ultrasound speakers and the software to their device.

One of the most exciting thing is that it can detect the location of a hand with a 180 degrees interaction space with a maximum range of 50cm while using a small amount of power.

Watch the video below to learn more:

If you’re interested in playing around with it, you can get a dev kit on the website, Elliptic Labs.