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

cylonjs-sphero-leap-motion

In my personal time, I love to play around with hardware and robots.

I started in Node.js but recently I discovered Cylon.js and after a quick play around with it, I found it pretty awesome and decided to rewrite my projects using this framework.

As a starting point, I decided to rewrite the project to control the Sphero with the Leap Motion.

You can find the original repo here, but here are a few code snippets:

Screen Shot 2015-01-10 at 3.02.47 pm

Screen Shot 2015-01-10 at 3.04.09 pm

The way it works is pretty straight forward. The Sphero connects via bluetooth and the Leap Motion needs to be plugged in your computer. Once the Sphero is detected, the hand is tracked by the Leap Motion and the direction will be applied to the Sphero.

Feel free to have a better look at the code on Github.

Now, let’s move on to Cylon.js. The first thing I noticed about this framework is the short amount of code necessary to get to the same result. I managed to do pretty much the exact same thing in 68 lines of code!

I guess what makes it easier is that Cylon already has some modules you can install to program for certain devices, like the ones below:

cylon-devices

To start using Cylon, you need to require it and specify which devices you are working with.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var Cylon = require('cylon');

Cylon.robot({
  connections: {
    leapmotion: {adaptor: 'leapmotion'},
    sphero: {adaptor: 'sphero', port: '/dev/rfcomm0'}
  },

  devices: {
    leapmotion: {driver: 'leapmotion', connection: 'leapmotion'},
    sphero: {driver: 'sphero', connection: 'sphero'}
  },

  work: function(f){
  }
}).start();

At the moment, this code is not really doing anything but you can see how to specify which devices you are going to use.

You have to specify a port for the Sphero because it connects to your computer via Bluetooth. To find the port for your own Sphero, run ‘ls /dev/tty.Sphero*’ in your console and replace the port in this code with the result you get.

The rest of the code goes inside the ‘work’ function as below:



1
2
3
4
5
6
7
work: function(my){
  my.leapmotion.on('frame', function(frame){
   if(frame.valid && frame.gestures.length > 0{
     my.sphero.roll(70,0,1);
   }
  }
}

The code above makes the Sphero go forward if the Leap Motion detects any kind of gesture.

For the full code, have a look at my github repo.

I’ll probably write another tutorial soon once I have a chance to rewrite another project but in the meantime let me know if you have any question!