Saturday, September 24, 2011

Windows Phone 7: Using the Compass

Want to add a compass to your Windows Phone 7 app? Want to get that AR app started? Most of us are in the same boat--we want to use the compass, motion, gyro, etc. but we can't. We've installed Mango but, without manufacturer-specific drivers, we're out of luck. If you're really anxious to get started anyway, let me share what I did and the lessons learned.

My first attempt was to create a set of "watchers".  I created a CompassVirtualWatcher and a CompassSensorWatcher both derived from an abstract base class, CompassWatcher.  The Sensor watcher implemented the base class functions using the actual Mango Compass API.  The Virtual watcher used a set of values that moved relatively randomly to simulate the compass function.

Having dealt with WP7 sensor APIs before, I knew the events would likely be coming in on a different thread.  Since I implemented the watchers using Observable (Microsoft.Phone.Reactive), it was easy to leverage ObserverOnDispatcher (or so I thought) to interact with the UI during updates.

The idea proved useful.  I was able to add a compass to my "Jack of Tools" app, play with it and see it seem to function.  I even simulated variant accuracy and the need for calibration so my calibration UI would pop up.

Once convinced the app worked, I eyed the CompassSensorWatcher for probably an hour just trying to catch any issues I might not see with the virtual watcher.  This included considering the potential threading issues once again but eventually I was convinced I had done my best.

Of course, the moment it hit real hardware with a compass, it locked up.  Now what?  I knew it must be something about working with the actual hardware but just could not figure out how to test that.  I posted in several places begging for drivers, emulators, ideas, etc. to no benefit.  Hence the reason for making this post.

Finally, an epiphany:  I didn't necessarily need compass hardware to recreate my bug.  Any hardware might be enough.  That is where the FauxCompass came in (my Flux Capacitor caliber idea of the year).  I would create an object derived from SensorBase<T> that implemented compass like functionality using the Accelerometer.

Of course, this was not so simple:

  1. SensorBase has only an internal constructor so there would be no deriving from that.
  2. Compass is sealed so there would be no deriving from it either.
  3. CompassReading has a public constructor but no public property setters so there'd be no implementing anything that returned CompassReadings.
This wasn't a show stopper but it made the implementation of the FauxCompass a little less elegant.  In the end, the signature is just like the Compass but using FauxCompassReading instead.  So, to switch between the regular Compass and the Faux Compass requires changing the code in four spots:  the member declaration, the constructor call, the observable declaration, and observable target's signature.

Once in place, I ran my app, switched to the Compass view, and it locked up.  "Damn it!  Now what?!" I thought.  You're probably already there but it took me a few seconds to really get it--that's exactly what I wanted.  My FauxCompass had repeated the bug that the real compass was producing and in short time.  I used it to fix the Compass bug in minutes.

So what did the FauxCompass do?  It consumed Accelerometer readings and converted them to FauxCompassReadings.  Turning the phone on one axis, produced headings from 0 to 359; turning on another axis, varied the accuracy resulting in a CalibrationNeeded event when it hit 20.

I hope this helps someone else waste less time with their compass app.  Please let me know if it does, if it doesn't, or if you have another solution.

23 comments:

  1. Just played with you tool. Nice app and it will be great to work with. As for the compass, the needle should always point to mag north as you rotate the phone. Maybe join the needle / dial and rotate them both. Add a marker at 12:00 on the dial (relative to the phone) to show the indication. Maybe add an option to show true / mag north and if the user selects true north, then show a needle that deviates from the dial's N indicator.

    Your's is not the first compass app that I have see do this but a counter rotating needle to the actual heading just seems really odd and is not the way that a real compass works.

    -Andy

    ReplyDelete
  2. Andy,

    Thanks for the feedback and, of course, you are right. It's funny b/c, in testing with my simulated values, I never noticed it but, looking at it with real values, it's very clear to me how backward it is.

    Anyway, I did an update a couple days ago to change to the platter-rotation style which seems even easier to read after playing with a couple real compasses. Hopefully that will pass cert. and be available soon.

    Thanks so much for the feedback and for taking the time to post it!
    -Jonathan

    ReplyDelete
  3. Just saw the update. I think it is a major improvement! Works well. The only thing you might add is a indicator / marker at the top of the compass which points to the actual reading. Agreed that it might be redundant since it would live right below the digital reading you have just above the compass.

    I am no UI wiz, but a really cool option would be to superimpose a 1/4 inch bubble at this location showing a magnified version of the readings. Almost combining the horizontal ribbon version you previously had with a small bit of the ribbon cut out inside the bubble. Almost like the day of the month reading on a typical high end analog watch.

    Feel free to email me back andrew / binaryocean.com if you have anything else.

    -Andy

    ReplyDelete
  4. Does this still work today? I have to check but this is way too cool! Technology is wonderful and offers us plenty of choices. However, there's no substitute for learning the basics of using a compass, as my dad used to say when we were kids. I'm glad my father took the time to teach us the basics since this skill is important today. I've been camping for a while and I always have my trusty compass with me, just in case my tech gadgets fail me once. For tips on how to use a compass, here's the link http://backpackingmastery.com/basics/how-to-use-a-compass.html

    ReplyDelete
  5. A number of versions seem to have been somewhat less of a success than others have been,oukitel

    ReplyDelete
  6. Through this, you will gage on how supportive, essential or fun a specific iPhone application is. Without a doubt most applications cost just $ 0.99, however in the event that you download excessively pointless applications and buy them on impulse, you won't understand the vast sum you have collected from obtaining mobile phone applications alone. http://www.thetoolreport.com/cell-phone-spy-app-reviews/

    ReplyDelete
  7. And always look for a cable that is good and sturdy. However, it is important to note that phone cables are not fool proof electronice phone anti theft

    ReplyDelete
  8. After filling all the above columns, hit the SEND MESSAGE button. Recipient will receive your messages within five second bestmobilenumbertracker.com/free-sms

    ReplyDelete
  9. We can all hope that we never drop our phones, run them over, or take them for a swim. Honestly the chances of this happening are greater than you finding $5 dollars on the sidewalk.PCB Tool Expert

    ReplyDelete
  10. Numerous corner stores, newspaper kiosks, and even some supermarkets offer incredible qualities on marked down calling card rates. free-lookup.net

    ReplyDelete
  11. My phone frustrations started a number of years ago after I determined that my native phone bill was way too high. The economy took a serious plunge and I needed to cut back some expenses. http://www.padisoft.org

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. I constantly emailed this site post page to all my friends, because if prefer to read it then my all friends will too.
    cell phone spy software reviews

    ReplyDelete
  14. One of those online journals that has left a critical effect on the hearts of the people.
    best cell phone lookup reviews

    ReplyDelete
  15. Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning telstra global roaming

    ReplyDelete
  16. This is really pretty cool place I like it because it has everything I want more on this blog soon.
    phone spy software

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. henceforth, paying little mind to whether you are searching for Blackberry repair or Nokia repair, any great administration focus will have the capacity to deal with the issue.phone repair Castleford

    ReplyDelete
  20. If the warranty has expired, the cellular phone repair center might charge for repairs.computer repair

    ReplyDelete
  21. When the potential audience find something interesting of value with your app, they will recommend it to their friends so that they can be benefited toogoogle play reviews in slack

    ReplyDelete
  22. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. control parental

    ReplyDelete