07/11/2009

The End or the Beginning - Flex Accelerometer


AstroJAM Effectively stopped Jamming at the end of our time at university in Leeds. All the Jammers have gone our separate ways and effectively the collective which is JAM is no more. However... gone but not forgotten.

For me, JAM was an intense crash course in alternative appropriation of web technologies which I still refer to today.

I'm currently working on a project to get an Accelerometer to control a flash (Flex AIR) application. I feel that JAM is the best place to showcase this as, if we could have done this at the time... we would have.

So here we go, this is my tutorial for the integration of a Serial Accelerometer into FLEX.

The starting point for this project was completly coincitental. I found a serial (RS-232 (Old school COM port) ) Accelerometer from SparkFun in a shoe box in the office - something which the boss had bought for R+D but hadn't got round to yet.
For those of you who don't know what an accelerometer there's is a bunch of information on Wikipedia
However, if you can't be bothered reading that that know this - there are three of them in your Wii remote working together to sense movement. Essentially, they sense acceleration (change in movement).
My vision was that I would try and develop software to enable me to read its output which I could then use to control some kind of flash visualisation/game/experiment/whatever; so I asker the boss if I could take the device home with me along with a USB to Serial converter - my laptop like many today don't have a serial port so I've used a USB/Serial port adapter to turn one of my available USB ports into a Serial (RS-232) Port. Such devices can be picket up for a few pounds (or $ or Euros) - see http://www.google.co.uk/products?q=usb+serial+port+converter&scoring=p



So I had a functional USB serial device and an a Serial Accelerometer in my hands... What next!?

Firstly, a few searches on Google highlighted a problem - Neither Flash, Flex or even AIR support Serial Port communications. There simply is no support for this in current version and no mention of it in forthcoming versions of the platform; another solution is required.

Then I came across this - http://freshmeat.net/projects/serproxy/
Serproxy is a Serial Proxy (TCP/IP) server. It allows you to a serial device via a serial port through a TCP Socket. (EH What..!? SerProxy acts as a web server listening for incoming connections (on specific ports). When a connection is established SerProxy connects the appropriate socket directly to the COM port specified in the config).

The flash.net.Socket class has been available to developers for *yonks*! (AIR 1.0, Flash Player 9 to be exact). This allows you to read an write binary data to a Socket.... see where this is going? :)

'We can use a flash Socket to connect to SerProxy which will allow us to send and receive binary data to an attached Serial device'... woop!

In reality is doen't quite work. SerProxy won't talk nicely to the Acceleromener I have. As the device is not powered by an external source it relies on the DTR pin to be held high as its power source (its powered by the host machine). I couldn't get SerProxy to do this - the source is provided but I don't know C++ well enough to do anything with it. So I wrote my own version of SerProxy - TCPSerialProxy.

For the record, you can achieve similar Serial proxying results using Apache MINA. I wrote my own version in C# so three reasons, firstly I wanted to experiment, secondly, I didn't want to install MINA and thirdly, I've never written a windows service before and relished the chance to do so (sad I know...).

So to recap on the ramblings so far: The Serial Accelerometer has been connected to a free USB port using a USB/Serial adapter. The final software will be based on the Adobe Flash AIR platform so in order to do this I've written a SerialProxy in C# .NET 3.5 to act as a bridge.

My Serial Proxy "TCPSerialProxy" can be downloaded here

All source is provided.

Configure '.\TCPSPConsole\bin\Debug\TCPSPConsole.exe.config'
Run '.\TCPSPConsole\bin\Debug\TCPSPConsole.exe'

Sorry I can't provide a more detailed manual for this fortware at the moment - it keeps changing. This version is stable. I'll be building it into a windows service shortly.

So, on to the Flex app.

At the moment the software which is written is some debug software which demonstrates the implementation. I've not got JAMMY with it yet.

The app demonstrates how to decode/interpret the data stream from the accelerometer. The way its configured at the moment the accelerometer reads packets of data like this:

X= 0.900 Y=-0.700 Z= 0.500\n\r

I've also included a papervision visualisation (cube) which rotates on its XYZ axese base on the output from the accelerometer.

I've also applies some motion smoothing and a motionthreshold in order to render more stable motion.

Full Source is available here.

I plan on making something more of this in the future. I'll post more later on too.

Taraa for now!