Recently I’ve doing a little work with Windows Phone 7. One aspect that interests me a lot is the integration of the Accelerometer. An accelerometer is a instrument that allows you to measure the forces acting on the telephone, including the force of gravity. This information will allow you to know what the orientation of the phone is and if the is moving or not. However there are a few challenges in interpreting is information. Firstly, the telephone’s accelerometer, when activated, will generate 50 events per second, so you will need to write code capable of analysing this volume of data. Secondly, and perhaps more importantly, the only information you are given in this event is a 3D vector that represents the forces acting on the phone along each axis, so interpreting the meaning of this information is not always easy.

The below diagram, originally from the WP7 blog, gives you some idea of the information you will receive via the accelerometer:

The help me understand the meaning of all this stream of data I wanted to be able to record and analyse this data on my PC, so I could visualize the data with graphs and test detection functions without having to deploy them to the phone. There’s no application cable of doing this so I had to hand roll my own. I have an unlocked WP7 phone, so I wrote a small WP7 client that is capable of forwarding this data to a WCF service. This WCF service could be hosted anywhere, but to enable easy analysis of the data I chose to host it F# interactive, to allow me direct access to the data received. I’m making this tool freely available to other WP7 devs via my github.com repo.

The client must be able to connect to my local PC so it can receive the data. There are several ways this could be archived but I chose to host a WCF service on my local PC and connect the WP7 to it via the USB cable. When the phone is connected to a PC via a USB cable it sees the PC as if it was connected to it via a TCP/IP network, which means the phone and PC can easily talk to each other via a WCF service. It would also be possible for the phone to send this data to a WCF service on the internet or via WIFI  if they were both connected to the same WIFI network, but the USB cable enables easy and rapid communication between the phone and PC. The only problem with this approach is the phone must know the name/address of the PC, as there is no way for it to discover it automatically. In the current version of the application, you just hard code the PC’s name (it’s for developers, right?), but in future versions I may offer the possibility of entering it via the UI of the WPF client.

The applications architecture looks something like this:

So once the app is deployed on a telephone, activated and connected to a PC we can receive the data in F# interactive. I’ll cover the details of the implementation in another post, but basically this means I have a property which stores all vectors that the phone has sent and event that is raised each time we receive a new vector (actually we batch up the vectors before sending, so we have two events one which is raised when a packet of vectors is received and one that is raised for each vector).

This means that I can write code like this, to grab some of the data from the phone and store in in the identifier “pointsToChart”:

  1. // create a local reference to the chart
  2. let points = ReceiveDataService.Instance.AllPoints
  3.  
  4. // slit the points down by thier position in the list
  5. let between start length =
  6.     points
  7.     |> Seq.skip start
  8.     |> Seq.take length
  9.     |> Seq.toList
  10.  
  11. let noOfPoints = Seq.length points
  12. // get some points to analyse
  13. let start = 11000
  14. let length = 600
  15. let pointsToChart = between start length