I’ve always been interested in ways that your PC can reach outside itself and interact with things in the world. That’s what fueled my interest in CNC, as well as things like Lego robotics, and the new sensor and location API.
When I was in Redmond for my first week, I sought out Gavin Gear and Dino Natucci, and chatted about the Sensors and Location platform. I saw some really cool stuff there, some of which I can talk about :)
Gavin also hooked me up with a couple Freescale sensor boards. If you don’t yet have one, they are available for order for a pretty reasonable price. As Windows 7 is still new (at the time of this writing, it has only been released for six days), you won’t find too many sensors out there yet, but this board and the API will allow you to try things out in the meantime. The location API has some companies on board and producing product already. I’ll cover that in a future post.
Sensor and Location Platform
The Windows 7 Sensor and Location platform is something completely new to Windows. It’s a framework helps eliminate the old com (or *gasp* parallel) port programming model that we’ve used for communicating with sensors in the past. It provides standards for drivers as well as a set of APIs for Sensor and Location (specialized and common sensors), and a control panel app to handle access to the sensors.
(image source MSDN)
The Sensor and Location APIs are COM-based APIs usable from native code, scripting languages, and anything which can talk COM. To use them from .NET code, you’ll want to use the friendly interop classes which we’ll cover in a future post.
Install the SDK
Install the Sensor Developer Kit for Windows 7. I simply unpacked the zip into a subfolder of Program Files on my machine.
Install the Sensor Diagnostic Tool. The tool comes in Visual Studio project source format, so I put that in my projects folder. It converted for use with VS2010 just fine.
We’ll cover .NET programming APIs in the next post on this topic.
Don’t have the board, get the Virtual Light Sensor from the Windows SDK.
Install the Drivers
Once you have the SDK downloaded and unpacked somewhere on your machine, open the folder and find SensorKitDriverInstaller.exe
Run that installer, following the directions to connect the board to the computer. Then turn the board on. You should get the two flashing lights. If you do not, you may need to update the firmware.
Configure and Test the Board
For the examples below, I’m using the 32 bit version of Windows 7. The Sensor DK includes drivers for both 32 and 64 bit versions of Windows 7.
Connect the Board
With the board switched “off” connect it to a USB port on your PC. I have a mini-usb plug always connected that I use for my camera, gps, phone, and now this board. I love standard connections. That said, the board will be easier to control if you pick a really cheap USB cable with thin wire and no ferrite toroid.
Update the Firmware
I did not need to update the firmware on the Rev D board I received, it may have been updated in a past life. If you do need to, the directions are included in the README in the SDK. It’s pretty much just a file copy like you would do with any USB stick.
Enable the Board
It took me a couple times before I remembered I had to do this. In control panel, you’ll see the applet for Location and Sensors. You want the “Enable location and other sensors” item
You can also control security on a per-user basis using that applet, but we’ll skip that for this.
Test the Board
Once you go through those steps, you’ll want to test the board to make sure everything is working. The compiled version and C++ source code for the test application are included in the SDK. The read-to-run binaries are in Tools\DiagnosticTool\Binaries
That’s fun for about 2 minutes, about the amount of time it takes you to verify that everything is working. Let’s really test it with something more interesting.
Testing the Board Take 2
Let’s test using Xna.
Once you install the first two, go into VS2008 (the templates don’t install under 2010 unless you manually move them) and create a new XNA racing game starter kit project
Inside the racing game zip file from msdn, you’ll see a Word document with instructions on how to modify the racing game starter kit project for use with the sensor API. You’ll be replacing a few files and recompiling the sample.
Now run it and try out the accelerometer!
Nothing happened, right? Well, the sky responded to the light sensor, but the accelerometer did pretty much nothing.
I ran into the same problem. Luckily, I found a person who had blazed this trail before me: Pietro Brambati. He had a few tweaks to the source code to make the accelerometer work. Scroll down to section 4 (XNA) and you’ll see some code you’ll need to add. FWIW, he also has a great write-up on the rest of the board install process. I encourage you to check it out.
Changes to the Source to Support Motion
Add to CarPhysics.cs Update() method
rotationChange -= effectiveSensitivity *
(Input.AccelerometerAxisX_Force / 3.0f) *
MaxRotationPerSec * moveFactor;
Then to the same method, down in the “Handle Speed” section add
else if (Input.IsAccelerometerConnected)
if (Input.AccelerometerAxisY_Force > 0)
newAccelerationForce += maxAccelerationPerSec;
newAccelerationForce -= maxAccelerationPerSec;
Finally, in the Graphics\BaseGame class add this code right below Sound.Initialize();
Run the App
NOW run it, and it should behave as you would expect.
From the video I recorded (link to be posted shortly) you’ll see I am a horrible driver. I’m thinking I should stick to Pole Position on the C64 :)
Uses for Sensors
Just considering the sensors available on the Freescale board, I can think of a few ways you could use them in your own applications or hardware:
- Adjust application theme (contrast, colors, font size) based on ambient light. For example, if you’re out in the full sun, you may want to bump up the UI size and increase the contrast. This would be great for field applications, especially on tablets. In fact, here’s a great whitepaper on that very topic.
- Accelerometer based games: use your whole laptop/notebook/tablet as a controller, or have an external device you hold, like the video I did along with this posting.
- A friend of mine in DPE suggested controlling music based on the light level in the room. (Presumably if the lights go out, it’s Barry White time <g>)
- I’d be curious to see if you could hang this on your car dash and get larger-scale acceleration readings. Worth trying out, but probably don’t want to tell your SO about this one.
- Accelerometers are crucial in laptops that still use the soon-to-be-outdated spinning platter form or storage. If you can park the heads before the crunch, your data will have a better chance of staying safe.
- Of course, environmental sensors (temperature, , pressure, humidity, etc.) have a ton of uses from IT to museums to portable weather stations.
I’ll have the video walkthrough of this and the test apps up shortly. For grins, here’s the setup I used to record the board:
Dig that high-tech camera stand :)
The XNA game I recorded with a Canon HD camera on a tripod to my right.
Update 11/25/2009 The video is located here.