Control the Sphero with the Leap Motion using Node.js

Charlie Gerard
3 min readAug 8, 2015

--

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!

Originally published at charliegerard.wordpress.com on May 29, 2015.

--

--

Charlie Gerard

Software developer, creative coder and tinkerer of things.