This article is Day #26 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 continues our adventure through the world of sensors, and today we are looking at the Gyrometer. The Gyrometer, or gyroscope, measures rotational velocity. So, while the Accelerometer measures the acceleration around three axes, the Gyrometer measures the velocity of rotation around those axes. Like this:
Now, I’m not suggesting that there’s a little spinning widget inside your Windows 8 device. Generally, mobile devices and tablets use a MEMS gyroscope, which uses vibrations or resonance to determine their data.
The gyroscope is also another one of those sensors that you’re going to get “sometimes.” As you can see in the video below, some devices have one, and some don’t. This is going to be a common theme with any sensor in Windows 8, because it is so terribly backwards compatible.
When Windows Phone 7 came out, every device was new, and there were stringent requirements about the sensors that had to be included in every device. When it comes to the PC world, however, Windows 8 can be installed on practically any computer that was created in the last 5 years. This makes it hard to get a consistent hardware set across devices, but doesn’t mean you shouldn’t support these sensors. Many of the new machines that are now being offered DO include these sensors, which means as time moves forward, a higher percentage of computers will support your efforts.
In addition, if you’re attempting to do any kind of augmented reality in your app (where you look through the device like a window on the real world, with additional data like store ratings or popularity added to the display), the gyrometer will be exceptionally helpful in keeping you oriented.
Getting Data From the Gyrometer
In the past few articles, we’ve seen that getting data from a sensor follows a familiar pattern. We create a new sensor object, we create an event handler, and then we grab the resulting data from the event handler’s method. Today is no different, but I’ve added a little “pizazz” by visualizing the data as well. In my XAML file, I’ve included three Line controls inside a Grid.
<Grid x:Name="ContentPanel" Margin="429,78,12,0">
<Line x:Name="xLine" X1="240" Y1="350" X2="340" Y2="350" Stroke="Red" StrokeThickness="4"></Line>
<Line x:Name="yLine" X1="240" Y1="350" X2="240" Y2="270" Stroke="Yellow" StrokeThickness="4"></Line>
<Line x:Name="zLine" X1="240" Y1="350" X2="190" Y2="400" Stroke="Blue" StrokeThickness="4"></Line>
</Grid>
These lines will extend in three different directions, driven by the Gyrometer data. Here’s the entire contents of 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 Day26_Gyrometer
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
Gyrometer gyrometer;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
gyrometer = Gyrometer.GetDefault();
if (gyrometer != null)
{
gyrometer.ReadingChanged += gyrometer_ReadingChanged;
Data.Visibility = Visibility.Visible;
}
else
{
NoSensorMessage.Visibility = Visibility.Visible;
}
}
async void gyrometer_ReadingChanged(Gyrometer sender, GyrometerReadingChangedEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
XValue.Text = args.Reading.AngularVelocityX.ToString();
YValue.Text = args.Reading.AngularVelocityY.ToString();
ZValue.Text = args.Reading.AngularVelocityZ.ToString();
TimeStamp.Text = args.Reading.Timestamp.ToString();
xLine.X2 = xLine.X1 + args.Reading.AngularVelocityX * 200;
yLine.Y2 = yLine.Y1 - args.Reading.AngularVelocityY * 200;
zLine.X2 = zLine.X1 - args.Reading.AngularVelocityZ * 100;
zLine.Y2 = zLine.Y1 + args.Reading.AngularVelocityZ * 100;
});
}
}
}
.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; }
In this example, you can see that I’m doing some simple math to change the X and Y values of the Line controls, so that they reflect the Gyrometer data that is being captured. In case you’re curious, the reason the zLine has both its X and Y values changed is because it is set at an angle, which requires manipulation of both coordinates of one end of the line.
Ultimately, you’re going to find far more interesting ways to use this data than I have, so I’m looking forward to hearing what you’ve created! Leave a comment on this post when you’ve got something.
If for nothing else, if you’re building a game that might have achievements or badges, I think it’s always cool to add ridiculous awards for hitting a certain number on a sensor. Have some fun with it. It’s certainly easy enough, right?
Summary
Today, we took a look at the Gyrometer (gyroscope) and how we can read the data from this sensor in Windows 8 app development. Another sensor available on Windows 8 devices, it’s going to give you smoother, more accurate data about the movement of the device than the Accelerometer, but they both are excellent tools for the job.
If you would like to download the sample code from this article, click the icon below:
Tomorrow, we’re going to cover the last sensor in this series, the Inclinometer. Much like the Motion class from Windows Phone, this sensor aggregates some of the other data we’ve talked about to generate Pitch, Yaw, and Roll values from our device. See you then!
Leave a Reply