The Win7 Sensor and Location API Part 1: Introduction and Freescale Board Setup and Testing
Join the DZone community and get the full member experience.Join For Free
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.
- microsoft xna game studio 3.0 (i haven’t tried with 3.1. if you do, comment below and let me know if it works for you)
- microsoft xna racing game starter kit (you’ll want the racinggamewindows-3.0.msi file)
- windows 7 sensors xna racing game
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.
Published at DZone with permission of Pete Brown. See the original article here.
Opinions expressed by DZone contributors are their own.