Sunday, February 04, 2007

Dogceleration Progress

Dogceleration is a project to document the energy and activity of my two Beagles when they playfight.

I started with a three-axis accelerometer board ("WiTilt") with a Bluetooth interface from Sparkfun, along with a small lithium polymer (LiPo) battery about the same size as the board. Both are a bit smaller than a standard business card:

witilt1 witilt2

My goal was to connect it up to PureData (pd) to interactively generate audio in real-time based on the accelerations. I previously have captured data using HyperTerm and played it back later at the Dorkbot DC meeting using the pd file reading object.

The WiTilt talks over Bluetooth as a serial port. I first tried to use the "comport" object in pd, but that was quite flakey (on Windows anyway). Then I tried to use Python with pyext to create a pd "external" in Python. Pyext works OK for pd objects that get messages then send messages, but the threading model for an external that would continuously send data into pd did not work so well.

Instead I used the pd "netreceive" object which uses IP sockets to receive pd messages. A Python helper program runs to intercept the Bluetooth serial port data, I do some string manipulation on it (raw data from WiTilt looks like "X=223 Y=474 Z=678"), and then send it to pd using TCP to a localhost port. This required Python for Windows Extensions as well as pySerial (nothing is ever easy, is it?). So I ended up with the Python program below, note that the message I send to pd needs a semicolon on the end:

import serial
from socket import *
s=socket(AF_INET,SOCK_STREAM)
s.connect(('127.0.0.1',3000))
ser=serial.Serial(3)
ser.baudrate=57600
ser.write("1")
try:
while 1:
l=ser.readline()
l=l.replace(' ','')
ls=l.split("\t")
x=ls[0].partition('=')[2]
y=ls[1].partition('=')[2]
z=ls[2].partition('=')[2]
s.send(x+' '+y+' '+z+';')
except KeyboardInterrupt:
s.close()
ser.close()
print "Interrupted!"

The upside of using netreceive is that the computer running the Bluetooth serial port and the computer running pd could be separate computers, possibly on different continents...

So then came the pd patch, which simply took the three axis data and shot it into three separate oscillators for now:



And here's the video of the system in action. You can see that the board is sensitive to the acceleration of gravity as I tilt it as well as to the acceleration due to physically mov

No comments: