06 Jul
Posted by kdutta in Development, HTC, Windows Mobile
When I first got my HTC Touch Diamond a while ago, one of the first things I tried to do was reverse engineer the Sensor API found in HTCSensorSDK.dll. However, anyone who has tried to reverse engineer DLL arguments knows how tedious and painful it can be to create a dummy DLL to intercept valid arguments, parse through assembly, and inspect random memory pointers. Luckily, I did discover a registry key: HKEY_LOCAL_MACHINE\Software\HTC\HTCSensor\GSensor\EventChanged which let me figure out what the general orientation of the device was; and that was good enough for what I was trying to do.
However Scott, from scottandmichelle.net, successfully reverse engineered the HTCSensorSDK.dll. This allows developers to use the g-sensor that is available on the device. Very impressive work on the part of Scott!
Anyhow, I spent a portion of today writing a managed wrapper for HTC’s Sensor API. You can download it here. The code also includes a sample Teeter-esque type application which allows you to roll a ball around the screen.
The managed API exposes the HTCGSensor class which allows you to hook to query the state of the g-sensor on the device. It exposes the following methods, properties, and events:
GetGVector
Returns a vector that desribes the direction of gravity/acceleration in relation to the device screen.
The vector returned will have a length measured in the unit meters per second square.
Ideally the when the device is in a motionless state, the vector would be of length 9.8 (the gravitational constant). However, the sensor is not extremely accurate, so this almost never the case.
Orientation
Retrieves the current orientation of the device, returning one of the following enums: Landscape, ReverseLandscape, Portrait, ReversePortrait, FaceDown, FaceUp.
OrientationChanged
This event fires whenever the device’s orientation changes.
Enjoy, and let me know if you find any bugs!
Koushik Dutta
Software Engineer
www.koushikdutta.com
45 Responses
Jeroen Brosens
08|Jul|2008 1Hi Koushik,
Looks interesting, your Managed wrapper! Good work!
About the vector data; does this 9.8 have anything to do with ‘g’ (gravitational acceleration, being 9.8 m/s^2) or am I totally wrong here?
Next to that, I was browsing your code quickly but is there any chance that you can provide a non-ambiguous illustration of the coordinate system with the axes that you describe in relation to the device?
Thanks in advance,
– Jeroen
kdutta
08|Jul|2008 2HTC actually reports their sensor data back in the 3 axis with values between approximately -1000 and 1000. However, to make this more API device independent and give the numbers real world meaning, I converted those values into meters/second^2. So yes, in a stand still state, the device would return a vector of length 9.8, which is the gravitational constant.
I have also updated the blog post to further explain the coordinate system in relation to the device’s orientation.
I would also encourage you to inspect the sample application. Also you could create one of your own that displays the vector values as you twist and turn the device. It’s difficult to explain it via words and diagrams, but it all makes perfect sense when you play with it.
Daniele
09|Jul|2008 3Hi,
i’ve tried to use your code and I receive an error when execute this row
Dim htcGV As HTCGSensor = New HTCGSensor()
the error is:
InvalidProgramException
Can you help me?
kdutta
09|Jul|2008 4I updated the code to explictly pass the the HTC Sensor type as a uint. I doubt that was the problem though. It works fine on my phone still, obviously.
Let me know if the change works for you. I also emailed you on the address you provided in your comment; feel free to email me back with the results of the new code.
Svein Skogen
09|Jul|2008 5Wouldn’t it, using this sensor, be possible to add (albeit low-precision) inertia-navigation to augment the gps sensor?
The reason I’m asking is this:
GPS systems are infamous for their (lack of) capabilities in tunnels and garages. Using this three-axis-accelerometer, we should be able to detect speed changes, and to a certain amount turning, while we are missing the gps signals. If so, the gps functionality of this cellphone might suddenly rival the capabilities of many gps-units sold for car navigation today. And even better: It might give GPS-unit manufacturers a hint that such an “extra” chip may be a good idea to add to their gear.
Regards,
Svein Skogen
AJ
17|Jul|2008 6I’m thinking of an app that shows a keyboard if you tilt the device backwards, hides it when tilted forward.
The keyboard itself will be quite large but tilting left and right shows either the left or the right side.
kdutta
18|Jul|2008 7That’s a pretty good idea, but I don’t know if you can programatically show the SIP (using SipShowIM) in a process outside of your own. That would be something to look into.
Un SDK per l’accelerometro del HTC Touch Diamond « P@ssioneMobile
27|Jul|2008 8[...] si è adoperato per fare un reverse engineering dell’API presente HTCSensorSDK.dll. Enteprisemobile ha quindi realizzato un wrapper per la classe, esponendola e rendendola utilizzabile. Nel post è presente anche una guida [...]
wmpoweruser.com » Blog Archive » Sensory Overload - asteroids game for the HTC Diamond using G-sensor
10|Aug|2008 9[...] version of the venerable Asteroids game utilizes the G-Sensor, Nav Sensor, and the 3D hardware capabilities of the the HTC Touch Diamond and looks simply [...]
Klaus Post
09|Sep|2008 10@Svein Skogen: You would need to know the orientation of the phone in relation to the car movement. You can of course estimate that based on the gravity, but moving the phone inside would make any readings meaningless for navigation.
borshika
10|Sep|2008 11very good! I already tested your application and It works good!!,, fantastic!
But how can i manage the tilt sensors’s sampling rate?
In your code, I changed timer interval 1/1000 seconds and I could see output sensor value’s number are 7~8 /1(sec)
I want to encrease the values maybe 12~64/1(sec)..Can you give me an advice??
tbc
17|Sep|2008 12Hi Koushik!
I came across your page and found that playing with this G sensor thing could be fun. I’m just learning how to program in C#, so I’m not ready to do anything with your code. Yet.
Just a few questions:
- is this sensor able to give the actual G force as well? I’m asking this because I am gliding and in a plane sometimes you experience quite high Gs. Well, this seems to be a good tool to measure those G forces.
I would be really interested what forces apply when you do a takeoff or a corkscrew.
Another usage is what the dynolicous application does: acceleartion of cars.
Is it possible to simply create a log file every 1/10th of a second for example that has 3 columns for the 3 G-force axis?
Ludovic
23|Sep|2008 13My objective is to use GRemote – GMouse (like Logitech MX Air), on my HTC X7500 Adantage, with WM6.
http://forum.xda-developers.com/showthread.php?t=427097
I have the following error: Can’t find PInvoke DLL ‘HTCSensorSDK’.
Please note that THC, X7500 have GSensor. Maybe not the correct version …
May I ask you please to help me ? Thanks.
Just few notes below:
=====
NOTE 1
=====
- http://s-k-tools.com/index.html?m_util.html#gsen
The HTC Advantage X7510 is a significant update to the X7500, featuring solid-state memory and a haptic keyboard. Unfortunately, for unknown reasons, HTC saw fit to remove one of the great features of the original Advantage – automatic screen rotation using the accelerometer.
Now fix what HTC broke with the SK-Tools GSEN, which allows automatic screen rotation on the HTC Advantage X7510.
Download the app from SK-Tools here.
- http://s-k-tools.com/index.html?m_util.html#gsen
Installation: fail for me on HTC7500, WM6
But I have already a function that do screen rotation.
=====
NOTE 2
=====
BubbleLevel measure the slope of a surface using the ‘bubble level’ concept.
http://handheld.softpedia.com/get/Business/Engineering/BubbleLevel-66185.shtml
Installation: fail for me on HTC7500, WM6
Error: HTCSensorSDK
=====
NOTE 3
=====
For anyone interested, the sensor is a Kionix KXSD9 and they’ve put up some handy technical documents about it on their website: http://www.kionix.com/sensors/application-notes.html
http://msmobiles.com/news.php/7502.html
=====
NOTE 4
=====
HTCSensor.dll, from: (there is two versions)
http://www.basic4ppc.com/forum/questions-help-needed/2652-using-diamonds-accelerometer.html
http://forum.xda-developers.com/archive/index.php/t-410694.html
If you could help me … thank you.
Stephen
04|Nov|2008 14What program (s) do I need to open the managed API stuff. I assumed VS2008 would work but no go for me.
thanks
Patrik B
05|Dec|2008 15@@Stephen
Do you have Windows Mobile 6 Pro SDK?
George C
31|Jan|2009 16Being an avid cyclist I have Co Pilot live loaded on my HTC Touch Pro. Now for the next step, to create a g-sensor app so I can gauge the gradient on those gut wrenching climbs, thanks for the great info it will be an excellent starting point.
I say: “Hello mobile World!” - der Ende des Internets
01|Feb|2009 17[...] fand ich Berichte, dass bereits findige Entwickler, die aus der HTCSensorSDK.dll die entsprechende API über Reverse [...]
Michael L
21|Feb|2009 18Hello, first of all: Many thanks for for your work developing such a nice piece of software.
But, do you know whether it is possibble to access Diamonds GPS receiver via C#.
And is it possible to access phone functions like sending an SMS via C# as well?
Regards,
Michael
Lars Persson
23|Feb|2009 19I also get: Can’t find PInvoke DLL ‘HTCSensorSDK’, please help.
I have downloaded HTCsensor.dll and put it in the bin-folder but that didn’t help.
csaintamant
23|Feb|2009 20@ Michael L: yes, you can access phone functions (for making calls) and PIM functions (for sending SMS) in C# using the .NET Compact Framework. Check out the following APIs on MSDN:
Microsoft.WindowsMobile.Telephony.Phone
Microsoft.WindowsMobile.PocketOutlook.SmsMessage
glen b
02|Mar|2009 21where can I download HTCSensorSDK.dl, ive been searching for days. can u help
Chris Saint-Amant
03|Mar|2009 22@glen b: HTCSensorSDK.dll should be on your HTC Touch Diamond device.
Esben Rasmussen
10|Mar|2009 23U have used your wrapper with great success. But recently i have flashed my HTC Touch Diamond with the newest rom and somehow the Regkey event is not thrown anymore. The API works fine (i can retrieve the tilt from the SDK). But the event fired from the registry is not fired anymore. Have anybody experienced something like that too?
Lars Persson
10|Mar|2009 24Please help! As I wrote in post 19:
I also get: Can’t find PInvoke DLL ‘HTCSensorSDK’, please help.
I have downloaded HTCsensor.dll and put it in the bin-folder but that didn’t help.
Anyone?
Alessio
17|Mar|2009 25Hi, I’m a university student, I work with this application and I try to save the accelerations on the 3 directions, from the mobile phone, for make a program that draw a point on the map and let you see where you walked. My question is if the x, y, z value are a really accelerations or maybe a speed value because I have some problem when I draw these.
Regards,
Alessio
Maarten Edgar
27|Apr|2009 26Amazing work, Koushik (and Scott)! I hope to become proficient enough to be able to do this myself in the future
But for now I’ve just started learning C#. And, of course, I have questions
I’ve downloaded the code, and I’ve been looking through it all. I have a HTC machine (HD).
Am I correct in thinking that what I have to do to get only gsensor info working in my project is to add the code in
HTCNativeMethods.cs,
HTCGSensor.cs and
IGSensor.cs
as classes to my project and then use/call what those classes provide?
Or am I missing a class I need (GSensorBase and PollingSensor seem like usefull add ons but which I might program on my own, and NativeMethods.cs seems like, well, a native method which I’m better off using HTCNativemethods for because I’m using a HTC machine)?
Anyway, thanks for the work! As you can see, loads of people are enjoying and building using what you’ve made
@Alessio: x, y ,z seem to me to really be acceleration values. You know this because gravity is an acceleration of 9,8 meters per second per second. You need to derive speed from acceleration (and possibly also location from speed) if you want to draw things on a map, is my guess.
Rene
30|Apr|2009 27On a HTC Diamond2 it works perfectly except for the orientationChanged event (which I’m personally searching for)
Am i correct in thinking that the returned value of screenorientation is not set?
Rene
30|Apr|2009 28Found it.
The actual RegistryState value to check in a Diamond2 is EventChangeEx. EventChange seems to be only used in case of manual Screenrotations.
Luc
08|May|2009 29Thank you so much! I finally got this to work after installing new versions of Visual Studio (2008) and Windows Mobile SDK (6). The 5GB of downloads was worth it though, my GPS program now displays turning and braking G-Force. AWESOME, you rock.
Michael L
11|May|2009 30@Luc:
Can you kindly explain to me how to access GPS functions programatically?
Phani
12|Jun|2009 31Hi:
When we try to run the application using VS2008, the HTC runs the ClientShutdown.exe!!! Is there any changes to security setting that I need to do.
Please help! We are pretty new to this.
Thanks
Phani
Esben Rasmusen
17|Jun|2009 32ToRene.
I have tried til use the EventChangeEx instead of the EventChange event, but with no luck. The event dont seem to raise.
Can it be enabled or disabld in WM6? In the regdb?
Note im using the HTC Touch Diamond (not the second model), but with the newest firmware.
Rene
17|Jun|2009 33Esben:
I guess EventChange might work in your case.
What I did, to find out the key EventChangeEx was used, was to open a mobile registry editor to my mobile on a pc and watch for a key that changes when I flip the mobile
You could try that as well.
kalpesh
17|Jun|2009 34i have htc p3000 without sensor
is it possible to install sensor in my htc mobile
raver99
25|Jun|2009 35Please tell me, is there a posibility to measure distances with the sensor SDK , library ?
Thanx
Mark
11|Jul|2009 36Koush,
Was wondering if there was a reason for not having an AngleZ in the code, I only wish to know so that I can negate the gravitation pull from the results and have just the acceleration in my results.
Also, I seem to be coming across a maximum acceleration of roughly 23-24, is this a limitation of the sensor itself?
Thanks again for a great wrapper, keep up the good work
-Mark
skywalkerian
25|Jul|2009 37I have been looking at this API and trying to figure how to measure angles but cannot find a lead. Any help pls.
Mohammad Hefny
18|Aug|2009 38Great Code….
I tried to make the reading more smooth and ball behavior is similar to the Teeter game.
I found that queuing the readings and taking average of last 3 readings improves the behavior.
Thank you for your effort.. you did a great job.
Johan van der Merwe
16|Sep|2009 39Great piece of code. I know this post probably isnt the right one to ask, but I’m currently using a htc Tytn II, which does not have a tilt sensor. I suppose the only way to determine whether the screen has changed is to monitor the slide event of the keyboard, but I dont know what Im doing wrong, because I cant find anything substantial on the web on how to do it in code. Any suggestions or URL’s would be appreciated.
Thanks in advance.
Esben
17|Sep|2009 40You should be able to use the idea of this code. Unfortunetly i dont have the code lying around, but here is the idea.
Download the Sensor SDK sourcecode. If you open it, somwhere you will find where the author is hooking up to a event from the registry.
Search your registry after the event/key that changes when the keyboard slides. The write another dll in .NET where you hook up to this event in the registry and throw a new C# event. The you have a simple library that will thorw an event whenever the keyboard slides.
Background:
Nomatter if it is the accelerometer or the keyboard that slides, the OS (in this case Windows Mobile) will set a value in the registry. In your case probably either in the keyboard node or in the screen node. Find the value your self, im not sure where it is. (But its there..)
In c# its possible to access the registry and hook up to a key. Taht will kame .NET catch the event that the OS throws and make it possible to do what ever you want in C#.
And the Sensor SDK does exactly this. It works in two ways. 1: It polls the current oposition from the registry and then calculates the vector – forget about that part.
But the Orientation-property is calculated by hooking up to the landscape, faceup, facedown etc in Windows Mobile. And thats the idea you need to go for.
I strongly advise you (for good SW architecture sake) to put it in a assenbly and make that assembly throw an event. In taht way you can reuse the assembly in other projects AND maybe others can gain from you hard work.
Happy coding.
Feel free to get back to me.
Etch-A-Sketch Emulator with the HTC Fuze Grav Sensor « Cubicle Ninja
15|Oct|2009 41[...] search for “htc fuze sensor api.” On the first patch of results I found a link to a blog post on Enterprise Mobile that appeared to be exactly what I needed (their information was greatly [...]
abaire
29|Oct|2009 42Not sure if it’s interesting to anybody or not, but through inspection using an HTC Touch Pro 2, I believe the “Unknown2″ field in the HTC sensor data struct is an overall orientation indicator. I assume this is used to simplify the development of apps like the built in speakerphone that turns on when the device is placed face-down
I’ve only tested on a single device, so it’d be interesting to see if it’s consistent on all handsets, but these are the values I found:
With the phone standing up on the right edge of the screen (the side opposite the hardware volume buttons) – 0
With the phone standing up on the left edge of the screen (the side with the hardware volume buttons) – 1
With the phone standing up on the bottom edge of the screen (the side with the USB connector) – 2
With the phone standing up on the top edge of the screen (the side with the power button) – 3
With the phone lying down on its face – 4
With the phone lying down on its back (with the screen side up) – 5
typedef enum ORIENTATION {
O_RIGHTEDGE = 0,
O_LEFTEDGE = 1,
O_VERTICAL_BOTTOMDOWN = 2,
O_VERTICAL_TOPDOWN = 3,
O_FLAT_FACEDOWN = 4,
O_FLAT_FACEUP = 5
} ORIENTATION;
Erric
30|Dec|2009 43Hello.
I have one HTC-HD2.
My question:
How to make (choice in a list or other) so that all the applications of HTC HD2 can pass from the portrait mode to the landscape mode by simple rotation of phone (for example: particularly important function for the “file explorer” to improve the reading of it.
I address my best wishes for 2010 to you.
Erric
ghk
02|Feb|2010 44I cannot believe HTC is still not supporting g-sensor with HD2 and nothing changed with WM6.5..
I was thinking HD2 would be the real WM alternative to iphone.
I think I will just give up any hope for WM. This is just fooling people with g-sensor installed on their phones and not providing any applications, while iphone nicely works with g-sensor and have a LOT of applications.
Shinky77
11|Feb|2010 45I spend more time with build application for HTC-Gensor and want to say thanks to autor. In my program I use HTCSensorGetDataOutput function from HTCSensorSDK.dll. I think this is best way came back real variable for aplication. Function return TiltX,Y,Z for all axes from -1000 to 1000.
Inclinometer77:
http://www.youtube.com/watch?v=0RXuzw6vMvo
or
http://www.handango.com/catalog/ProductDetails.jsp?storeId=2218&productId=265715
http://mobil.pocketland.de/53736/Inclinometer77.html
Leave a reply
Search
Archives
Categories
Device Info
Windows Mobile Blogs
Copyright © 2008 - Enterprise Mobile
Proudly powered by WordPress - InSense 1.0 Theme by Design Disease.