Playing with Sphero

September 10, 2014

Lately, I’ve acquired a Sphero, a little ball described as “The Ball. Evolved”.

Sphero can be controlled by Bluetooth. SDKs are available for Android and iOS, as well as many applications and games. The ball can also work with Cylon.js, a javascript library for robotics, GoBot (for the Go langage) or Artoo (for Ruby).

Let’s see is a very simple example running with Cylon.js over node.js. In this example we will ask Sphero to roll and to make a u-turn if there is a collision (e.g. a wall). We will also change the color at each collision.

var Cylon = require('cylon');
var run = false;
var direction = 0;
var speed = 100;
Cylon.robot({
  // connection port working on OSX
  connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/tty.Sphero-RPY-AMP-SPP' },
  device: { name: 'sphero', driver: 'sphero' },

  work: function(me) {
    var color = 0x00FF00,
        bitFilter = 0xFFFF00;

    var shuffle = function(){
        direction = Math.floor(Math.random() * 360);
        me.sphero.roll(speed, direction);
        run = true;
    }

    var uTurn = function(){
      direction = (direction + 180) % 360;
      console.log("Direction: " + direction);
      me.sphero.roll(90, direction);
      run = true;
    }
    
    after((1).second(), function() {
      console.log("Setting up Collision Detection...");
      me.sphero.detectCollisions();
      me.sphero.setRGB(color);
      me.sphero.stop();
    });
    
    me.sphero.on('collision', function(data) {
      console.log("Collision:");
      color = color ^ bitFilter;
      console.log("Color: " + (color.toString(16)) + " ");
      me.sphero.setRGB(color);
      uTurn();

    });   

    shuffle();
  }

}).start();

We can improve the script to make the ball run, do a u-turn, change the direction or the speed using the keyboard.

var keypress = require('keypress');
process.stdin.setRawMode(true);
process.stdin.resume();   
keypress(process.stdin);

process.stdin.on('keypress', function (ch, key) {
  //console.log('got "keypress"', key.name);
  if (key.name == 'r') {
    if(!run){
      me.sphero.roll(speed, direction);
      run = true;
    }
    else {
      me.sphero.stop();
      run = false;
    }
  }
  else if (key.name == 'u') {
    uTurn();
  }
  else if (key.name == 's') {
    shuffle();
  }
  //boost
  else if (key.name == 'b') {
    if(speed == 100){
      speed = 200;
    }
    else {
      speed = 100;
    }
    console.log("speed:", speed)
    me.sphero.roll(speed, direction);
  }
  //quit
  else if (key.name == 'q') {
    process.exit(0);
  }
  
});

Finally, we can add a function to ask Sphero to change its drirection automatically.

//auto-change direction
every((5).second(), function() {
  if(run){
    shuffle();
  }
});

In conclusion, I would say that Sphero is a very fun toy (touch controls on smartphone are really amusing). But it is also very good tool to learn programing, in particular for children.

In fact it could be renamed “Turtle graphics. Evolved.” :)

Discussion, links, and tweets

comments powered by Disqus