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
import time
import socket
import sys
from sphero import core
from astro_pi import AstroPi
s = core.Sphero("/dev/rfcomm0")
print "Connecting to Sphero..."
s.connect()
ap = AstroPi()
r = [255,0,0]
e = [0,0,0]
w = [255,255,255]
g = [0,255,0]
b = [0,0,255]
stop = [
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
]
def displayArrow(c,rot):
arrow = [
e,e,e,c,c,e,e,e,
e,e,c,c,c,c,e,e,
e,c,e,c,c,e,c,e,
c,e,e,c,c,e,e,c,
e,e,e,c,c,e,e,e,
e,e,e,c,c,e,e,e,
e,e,e,c,c,e,e,e,
e,e,e,c,c,e,e,e
]
ap.set_rotation(rot)
ap.set_pixels(arrow)
speed = 0x88
try:
mot = 'SSSS'
while True:
x, y, z = ap.get_accelerometer_raw().values()
x = round(x, 0)
y = round(y, 0)
print x,y,z
if x == -1 and abs(y) == 0 and mot != 'LLLL': # left?
print 'left'
s.stop()
displayArrow(b,0)
s.roll(speed,90)
mot = 'LLLL'
elif x == 1 and abs(y) == 0 and mot != 'RRRR': # right?
print 'right'
s.stop()
displayArrow(b,180)
s.roll(speed,270)
mot = 'RRRR'
elif y == -1 and mot != 'FFFF': # back?
print 'fwd'
s.stop()
displayArrow(g,270)
s.roll(speed,1)
mot = 'FFFF'
elif y == 1 and mot != 'BBBB': # fwd?
print 'back'
s.stop()
displayArrow(g,90)
s.roll(speed,180)
mot = 'BBBB'
elif abs(x) == 0 and abs(y) == 0:
print 'stop'
ap.set_rotation(90)
ap.set_pixels(stop)
s.stop()
mot = 'SSSS'
time.sleep(0.1)
except:
That's so cool, now I want a Sphero, need to dig out my Pi too. Must try one day.
ReplyDeleteLooks more fun than using a phone, for the Sphero
Very Cool Device in present generation
ReplyDeletehttps://youtu.be/wUC9XI1JABE
watch & subscribe