Sphero Dance Party
My favorite toys allow me to take them apart and put them back together again. Looking at the individual components and how they fit together helps me to learn how they work. It also lets me tinker with the pieces and try to turn them into something new; something that is unique.
As a kid, I could imagine myself taking things from around the house and turning them into a new toy like a machine or a robot. I don’t know if anyone has succeeded at building a robot from paper, glue, yarn and electronic bits. If they have, then my childhood dream has been achieved.
This desire to create a robot is why I purchased a Sphero. The Sphero is a hard plastic ball that contains a small robot that can move, flash lights, and sense movement. It also has a documented developer kit and programmable API . This exposes the internals of the toy to be tinkered with and helps people to control their Sphero with their computer.
Today, we will use Python, bluetooth, and an open source module to connect to your Sphero and have it express itself with dance moves that are all its own.
For this tutorial. you will need the following items:
- TheSphero 2.0 - You can find this online or at your local toy retailer. The retail price is $130, but if you look online for a used Sphero you can find one at a discount.
- A computer with bluetooth - Your computer may already have bluetooth enabled. If your computer does not have bluetooth, you can add it for around $15. The adapter that I am using is small and plugs into my USB port.
- The Python programming language - Absolutely free
Pairing Your Sphero
The first step to creating a dancing robot is to use bluetooth to pair your Sphero with your computer. Once it is paired to your computer, your computer will be able to communicate with your Sphero.
To pair your Sphero with your computer you will need to put your Sphero into pairing mode. You can do this by tapping it until it repeats a series of three colors. Below is a video of what my Sphero looks like in pairing mode.
With your Sphero in bluetooth pairing mode, use your computer to complete the process. This will vary based on your operating system.
Pair your Mac with your Sphero by choosing the bluetooth icon and open bluetooth preferences. When you see the Sphero appear, click pair.
Pair your Windows PC with your Sphero from the Start Menu. Select Devices and Printers and then Add Device. Select your Sphero to add it.
There are a number of tools that you can use in Linux to pair a bluetooth device. I am using one called Blueman. To pair with Blueman, click the Blueman icon in the system tray and select Devices from the menu. Your Sphero should appear in the menu. Right click it and select Pair.
Troubleshooting: Resetting your Sphero
If your computer is having trouble discovering your Sphero, you can try putting the Sphero to sleep and then putting it back into pairing mode. To put it to sleep, return it to its cradle and wait for it to display a burst of colored light. Below is a video of my Sphero going into sleep mode.
Finding the Bluetooth Address
Once you have successfully paired your device to your computer, you will need to look up its bluetooth address. This is how your program will identify the device and help it dance.
The way that you will do this may vary by operating system. If you are finding the address in OSX, you can find it by clicking the bluetooth menulet. It will be listed in the dropdown menu.
In Windows you can find it in your device manager. It will be listed in the properties of the Sphero device.
I am using Linux and I can see the address when I click and list my bluetooth devices from the system tray.
The bluetooth address will be a series of 12 characters that may be separated into pairs of two by colons. It will look something like this: 01:23:A4:56:78:90.
Setting the Stage
We are going to use Python and an open source module called kulka to talk to the Sphero. This module is written in Python and will give us control of the Sphero.
Kulka is freely available and you can add it to your computer with a Python tool called pip. Pip is an easy way to add Python modules to your system. Using pip to install kulka is easy, simply open up your terminal window and type:
pip install kulka
Once pip completes, you should see a message that says that kulka was installed successfully. When you see this message, you can start playing your favorite music because we are ready to make our bot dance party!
To start out, let’s set up our party lighting.
Kulka offers a handful of methods to control the Sphero. With these, we can control the brightness and color of the Sphero with a method called set_rgb().
This method allows you to set the color of the Sphero by specifying three numbers. Each number will range from 0 (off) to 255 (maximum). These numbers are passed to the set_rgb() method. These will specify the brightness of three colors; red, green and blue.
If we call set_rgb() with all zeros the Sphero’s light will turn off. Here are some examples of how this can be used:
bot.set_rgb(0, 0, 0) # turns off the light bot.set_rgb(0, 255, 0) # maximum green bot.set_rgb(255, 255, 255) # bright white light bot.set_rgb(255, 0, 255) # purple
Now we will use this method to set the party mood by cycling through some colors, flashing them on and off with a strobe like effect.
def party(bot): for x in range(255): bot.set_rgb(0, 0, x) bot.set_rgb(0, 0, 0) bot.set_rgb(x, 0, 0) bot.set_rgb(0, 0, 0)
Another method that you can use to control your Sphero is roll(). As you might expect, the roll() method moves your Sphero.
Call the roll() method with two parameters, speed and direction. Like color, the speed parameter accepts values from 0 (stopped) to 255 (maximum). The direction parameter works a little differently; it is a value from 0 to 360. This value represent degrees in a circle around the Sphero.
Using this knowledge we will now teach our bot a basic dance move. This is a dance that steps to the left and then rocks back to the right. We can add a bit of flair by changing the color of the Sphero with each step.
def step_left(bot): bot.roll(255, 0) bot.set_rgb(255, 0, 255) time.sleep(.3) def step_right(bot): bot.roll(255, 180) bot.set_rgb(0, 255, 0) time.sleep(.3)
Notice that we are pausing briefly after each step with time.sleep().
The bot now knows the steps, so it is time to put them together into a dance. Here is a dance function:
def dance(bot): for _ in range(10): step_left(bot) step_right(bot) bot.roll(0, 0)
Ok, we have the lighting and the moves, let’s put it all together with kulka and make our little bot dance!
Putting it all together
Let’s make a python script called "dance_party.py".
Once we have created the file, we will need to import the modules that we will use. These will go at the top of your file.
from future import print_function from kulka import Kulka import time
Next, we will add the functions we created above, and finally at the bottom of the script a *main() *function.
This main() function will be where our code starts running. It will use kulka to open a connection to the Sphero and use that connection to tell the bot to dance() and party().
In the code below, I have used 01:23:A4:56:78:90 to represent the bluetooth address. Please use the address of your own device instead.
def main(): with Kulka(“01:23:A4:56:78:90”) as bot: bot.set_inactivity_timeout(3600) party(bot) dance(bot) if __name__ == '__main__': main()
The final product
Below is the full code listing. You can also find it on github.
from __future__ import print_function from kulka import Kulka import time ADDR = "01:23:A4:56:78:90" FULL_SPEED = 255 def step_left(bot): """Moves the bot a step to the left and turns it purple""" bot.roll(FULL_SPEED, 0) bot.set_rgb(255, 0, 255) time.sleep(.3) def step_right(bot): """Moves the bot a step to the right and turns it green""" bot.roll(FULL_SPEED, 180) bot.set_rgb(0, 255, 0) time.sleep(.3) def dance(bot): """Dance Sphero Dance!""" for _ in range(10): step_left(bot) step_right(bot) bot.roll(0, 0) def party(bot): """Makes the Sphero strobe""" for x in range(255): bot.set_rgb(0, 0, x) bot.set_rgb(0, 0, 0) bot.set_rgb(x, 0, 0) bot.set_rgb(0, 0, 0) def main(): with Kulka(ADDR) as bot: """Tell the Sphero to sleep if it is not used for a while""" bot.set_inactivity_timeout(3600) party(bot) dance(bot) if __name__ == '__main__': main()
It is time to celebrate success with Sphero. Here is a video of Sphero doing his dance.
Make your own Sphero dancing video and tweet us @CodeNewbies.
You have now made your Sphero dance using Python and your computer. But the fun doesn’t stop here. There are many more things that you can do!
Dance Party All Night Long
There are lots of things that you can do with your Sphero and just these two functions. Try changing the program and see what happens.
Here are a few things that you can try to make your Sphero do.
- Modify the colors of the mood lighting
- Add your own robot dance move
- Finish your dance by making your Sphero move in a circle
The possibilities are endless! We would love to know what you come up with, so give it a try and show us what you’ve made. Happy making!