Monday 8 June 2015

Controlling a Sphero with Astro-Pi

I was introduced to the Sphero by @duck_star at last month's Coder meet-up organised by @CatLamin. I managed to resist ordering one for myself for nearly 24 hours, but eventually succumbed.

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:

print 'error'




Sunday 7 June 2015

Creating images for the Astro-Pi HAT

My CodeClub loved their first session with the Astro-Pis. They particularly enjoyed displaying text on the LED matrix and were keen to create images like the space-invader that comes with the examples.

Obviously you can make these up directly in Python by making a list and loading it by the API, but this can be tricky in terms of colour design and involves lots of squinting.

When we made the TechWillSaveUs DIY gamers last year, they produced a simple Windows App for creating images for the monochrome matrix this device had.

I thought I'd try to knock up a similar application for Raspbian using pygame. There are a few programs for Windows and online that do a similar job, but I wanted something that would run natively on the Pi.


My app allows you to create patterns using the colours selected by the buttons on the righthand side.

You can then export this 8x8 pixel image to a png (filename is image8x8.png), print the corresponding Python list to the console or load it directly onto the Astro-Pi LED matrix.



You can also rotate the image once it is loaded on to the Astro-Pi.

It is a little rough around the edges at the moment although my son's efforts at beta testing seem to have helped my eliminate most of the glaring bugs.

If you want to give it a try, it is here on GitHub.