Controlling the Sphero using the Leap Motion in Cylon.js

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.

```
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:

```
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!

Advertisements

Published by

Charlie Gerard

Software Developer @ThoughtWorks, I am passionate about programming, Generative Art, Robotics, the Internet of Things and anything related to creative innovations.

4 thoughts on “Controlling the Sphero using the Leap Motion in Cylon.js”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s