This article is Day #27 in a series called 31 Days of Windows 8. Each of the articles in this series will be published for both HTML5/JS and XAML/C#. You can find additional resources, downloads, and source code on our website.
Today, we are taking a look at our final sensor, the Inclinometer. In many ways, it is similar to the Motion class from Windows Phone. Ultimately, it aggregates several of our other values about the X, Y and Z axes to calculate three very important values: Pitch, Yaw, and Roll. If you’ve ever even dabbled in aviation, you’ve heard these values incessantly. They refer to the rotation around those three axis values. Here’s a more visual example:
PITCH |
YAW |
ROLL |
![]() |
![]() |
For more information on the principal axes of aircraft, check out this Wikipedia article.
In our case, however, we’re not talking about aircraft. We’re talking about a Windows 8 device. More specifically, we’re talking about the axial rotation of a Windows 8 device. Thankfully, the same structure that we’ve seen from our other sensors still applies here.
It’s truly as simple as creating a new sensor object, registering an event handler to catch the data that comes from the sensor, and then one method to actually do something with said data. Here’s how my Inclinometer code looks in my MainPage.xaml.cs file:
using System;
using Windows.Devices.Sensors;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace Day27_Inclinometer
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
Inclinometer inclinometer;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
inclinometer = Inclinometer.GetDefault();
if (inclinometer != null)
{
inclinometer.ReadingChanged += inclinometer_ReadingChanged;
Data.Visibility = Visibility.Visible;
}
else
{
NoSensorMessage.Visibility = Visibility.Visible;
}
}
private async void inclinometer_ReadingChanged(Inclinometer sender, InclinometerReadingChangedEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
PitchValue.Text = args.Reading.PitchDegrees.ToString();
RollValue.Text = args.Reading.RollDegrees.ToString();
YawValue.Text = args.Reading.YawDegrees.ToString();
TimeStamp.Text = args.Reading.Timestamp.ToString();
});
}
}
}
As you can see in our example, we receive Pitch, Yaw, and Roll values in degrees, making it very simple to build calculations and animations into our apps using this data. As with all of the sensors in your application, you must make sure that the user’s device contains the sensor before you try to use it. Every Win8 device will be different, and it’s up to you to use the right sensor for the right job.
Summary
Today, we took a quick look at the last available sensor in our series, the Inclinometer. Specifically designed to calculate the Pitch, Yaw, and Roll values of our user’s device, this sensor will be an invaluable tool for anyone building an application that uses rotation-based physics of any kind. Pitch, Yaw, and Roll also become incredibly handy in tracking the position of the user’s device when performing augmented reality applications as well, eliminating what is commonly referred to as “drift".
If you would like to download a working sample to test on your own device, click the icon below:
Tomorrow, we’re going to make the turn to the home stretch, with only 4 articles remaining. We’ll kick the finale of this series off with Push Notifications, followed by some excellent guidance about getting your app to shine. See you then!
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Leave a Reply