Unsurprisingly, it proved popular with my boys so I hadn't found as much time to mess around with programming it as I'd hoped.
Until this week, when I suddenly thought how neat it would be if you could control it using the accelerometer on the Astro-Pi.
Here is the code. You'll need a Bluetooth dongle for the Pi and you'll have to pair and connect to the Sphero before running the program. I found the easiest way to do this initially is through the Bluetooth GUI.
You'll need to install the following packages first:
apt-get install bluetooth bluez-utils blueman
Once you've done that, run startx and then use the Bluetooth manager to connect to the Sphero.
The PIN for pairing with the Sphero is 1234. Once that has been accepted, you can connect using the 'setup' option.
You should then see a message saying that the Sphero has been connected as /dev/rfcomm0. If for some reason you see a different location, change the appropriate line in the Python code.
Once you've established a working connection, you can then set things up to work form the command line: edit /etc/bluetooth/rfcomm.conf and uncomment the various settings. Replace the MAC address for the 'device' parameter with the right one for your Sphero.
Then to connect from the command line, run:
bluetooth-agent 1234 &
sudo rfcomm connect rfcomm0
Now your Sphereo should be a glowing a solid light blue colour. Controlling is fairly intuitive. Tilt the Astro-Pi away from you and the Sphero will move away, tilt it back to reverse direction (pitch axis). Tilt to the left and right (roll axis) to move accordingly. In the clip below, the Sphero also changes colour, corresponding to whether its moving forward/backwards (green) or left and right (blue).
Here's the Python code. The astro_pi and sphero modules do all the heavy lifting.
import RPi.GPIO as GPIO
from sphero import core
from astro_pi import AstroPi
s = core.Sphero("/dev/rfcomm0")
print "Connecting to Sphero..."
ap = AstroPi()
r = [255,0,0]
e = [0,0,0]
w = [255,255,255]
g = [0,255,0]
b = [0,0,255]
stop = [
arrow = [
speed = 0x88
mot = 'SSSS'
x, y, z = ap.get_accelerometer_raw().values()
x = round(x, 0)
y = round(y, 0)
if x == -1 and abs(y) == 0 and mot != 'LLLL': # left?
mot = 'LLLL'
elif x == 1 and abs(y) == 0 and mot != 'RRRR': # right?
mot = 'RRRR'
elif y == -1 and mot != 'FFFF': # back?
mot = 'FFFF'
elif y == 1 and mot != 'BBBB': # fwd?
mot = 'BBBB'
elif abs(x) == 0 and abs(y) == 0:
mot = 'SSSS'