JavaScript Robots

I fear there is a public perception that robots are too "complicated". It is true that some problems are difficult. However, the basics are very accessible. It has never been easier to get started with robotics.

Programming a robot is literally as easy as creating a web-page.

I’ve recently been using the Sphero robot toy as a way to introduce people to robotics. Sphero works well with several technologies: Ubuntu, ROS, rosbridge, sphero_ros. These technologies make it easy to program complex robot behaviors. Because the technologies are standardized, the same programs can be used on other robots, including the PR2.

In the reminder of this article, I will walk through the process of installing these technologies. Once they are all installed, I’ll demonstrate how to program and control Sphero using JavaScript in a web page!

Installation involves six steps:

  1. Get a Sphero Robot
  2. Find a Laptop
  3. Install Ubuntu
  4. Install ROS
  5. Install Sphero_ROS
  6. Install Rosbridge

After installation, we need to:

  1. Reboot
  2. Start Rosbridge
  3. Start Sphero_ROS
  4. Program the robot by creating a Webpage

Get a Sphero Robot

Sphero

You can buy a Sphero for USD79.99 plus shipping. I know that the instructions below do work with the Sphero Original. I have not tested this code for the Sphero 2.0.

Find a Laptop

Sphero communicates with your computer wirelessly using Bluetooth. Fortunately, most laptops sold in the last five years have Bluetooth built-in. To minimize hassle and maximize compatibility, you should ideally use a PC-compatible laptop (i.e., not Apple) from a well-known manufacturer.

Install Ubuntu

Ubuntu

Ubuntu is a free operating system similar to Microsoft Windows. You can install it alongside Windows so that you use both operating systems.

As a precaution, you should first back up any data on your computer!

Then you’ll need to download and install Ubuntu. I recommend downloading Ubuntu 12.04 LTS. You can download an ISO, burn it to a CD-ROM or memory stick and then reboot your computer using the installation media.

Follow the on-screen instructions. If in doubt, use the recommended settings.

Once Ubuntu is installed and running, start up a terminal by clicking the Ubuntu logo in the top left and type “terminal”.

Install ROS

ROS Groovy Galapagos

ROS versions are named in alphabetical order: Box Turtle (B), C Turtle (C), Diamondback (D), Electric Emys (E), Fuerte Turtle (F), Groovy Galapagos (G), Hydro Medusa(H).

Sphero requires ROS Groovy. To install ROS Groovy, you will need to type the following commands into a terminal:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu precise main" > /etc/apt/sources.list.d/ros-latest.list'
wget http://packages.ros.org/ros.key -O - | sudo apt-key add -
sudo apt-get update
sudo apt-get install ros-groovy-ros-desktop-full
echo source /opt/ros/groovy/setup.bash >> .bashrc
source /opt/ros/groovy/setup.bash
sudo rosdep init
rosdep update
rospack profile

If you’d like to understand the individual commands, you can read more information in the ROS installation instructions or in the ROS tutorials.

Install Sphero_ROS

sphero_ros

The sphero_ros package provides ROS nodes for the Sphero. To install, type the following command into a terminal:

sudo apt-get install ros-groovy-sphero-bringup

Install Rosbridge

rosbridge

Rosbridge makes it possible to write ROS clients using JavaScript. To install Rosbridge, type the following command into a terminal:

sudo apt-get install ros-groovy-rosbridge-server

Reboot

Phew! We’ve finished the installation. It would be a good idea to reboot now. Click on the gear icon in the top-right of the screen and then click on “Shutdown…”.

Start Rosbridge

Now we can start Rosbridge. Open a terminal and type the following command:

roslaunch rosbridge_server rosbridge_websocket.launch

Connecting to the Sphero

To connect to the Sphero, first make sure that the Sphero firmware is up-to-date. The easiest way to do this is to install the Sphero app on your Android phone or iPhone and check for updates for your Sphero. Close the app after updating.

Next, you will need to pair the Sphero with your computer. In Ubuntu, click on the Bluetooth logo that appears at the top right of the screen.

Hit the Sphero to “wake it up”. In the Bluetooth setup on your computer, select the Sphero and pair using PIN ‘0000’.

Leave the rosbridge terminal window open. Open up a new terminal window and then type the following command:

roslaunch sphero_bringup sphero.launch

I hope you’ve made it this far. At this point the output from the last command should report that your Sphero has been found and connected.

ROS and Sphero

ROS makes it possible to program a wide variety of robots through a single software interface.

ROS uses a publish/subscribe architecture. This means that all communication occurs by sending (or receiving) messages to named topics.

For example, the sphero subscribes to a topic called /set_color. If you send (publish) a message (of type std_msgs/ColorRGBA) to that topic, the Sphero will change its color. Similarly, the sphero publishes to a topic called /collision. If you subscribe to that topic, the Sphero will send you message (of type sphero_node/SpheroCollision) whenever it collides with another object.

A full list of topics available can be obtained by opening a new terminal and typing:

rostopic list

You can also view the same information in the Sphero_ROS API documentation.

Programming the Robot

Ok. So let’s write some code. In Ubuntu, you can use gEdit to create a HTML file.

First, we need a connection to ROS via Rosbridge:

// Connect to ROS
var ros = new ROSLIB.Ros({
  url : 'ws://localhost:9090'
});

The URL parameter is the address of the WebSocket server of Rosbridge. I have used the default port of 9090.

Next, we need to create a reference to a topic:

// Get a Topic
var collisionTopic = new ROSLIB.Topic({
  ros : ros,
  name : '/collision',
  messageType : 'sphero_node/SpheroCollision'
});

The first parameter refers to the Rosbridge connection we made above. The second parameter is the name of the topic. The third parameter is the ROS message type associated with the topic. You can view the message definition for this message type from an Ubuntu terminal by typing the following command:

rosmsg show sphero_node/SpheroCollision

Now, subscribing to the topic is as simple as registering a callback function in JavaScript:

collisionTopic.subscribe(function(message) {
  console.log('A collision occured');
});

We can publish a message just as easily:

// Publishing a Topic
var colorTopic = new ROSLIB.Topic({
  ros : ros,
  name : '/set_color',
  messageType : 'std_msgs/ColorRGBA'
});

function setGreen() {
  var message = new ROSLIB.Message({
    r: 0.0, // red value
    g: 1.0, // green value
    b: 0.0, // blue value
    a: 1.0  // alpha (transparency) -- not used
  });
  colorTopic.publish(message);
}

To execute this, we need to wrap up our code in a valid HTML document and include references to the Rosbridge JavaScript libraries (eventemitter2.min.js and roslib.min.js).

You can download a working demonstration here.

The demonstration contains two HTML files:

  1. sphero_simple demonstrates the basic publish/subscribe explained above. It makes use of jQuery as well as Brian Grinstead’s Spectrum.js color picker.
  2. sphero_bounce demonstrates how the publish/subscribe architecture can be used to generate reactive behaviors. The code publishes velocity commands every 100ms but reverses direction whenever a collision is encountered. This has the effect of causing the Sphero to bounce around - reversing directions whenever it collides.

I hope you find these examples useful.

It really is simple to program a robot!

Published 9 March 2014 by Benjamin Johnston.