Gyrometer
Represents a device that measures the rate of rotation around the device's X, Y, and Z axes.
Overview
The Gyrometer is a sensor that provides data about the rotational velocity of a device. This data is crucial for applications that involve motion detection, augmented reality, gaming, and user interface interactions that respond to device orientation changes.
Note: Ensure your application has the necessary capabilities declared in its manifest file to access sensor data. For the Gyrometer, this is typically the
Sensors capability.
Properties
ReadingType- Gets the type of sensor reading. For Gyrometer, this is typically
GyrometerReadingType.Absolute. MinimumReportInterval- Gets the minimum report interval supported by the sensor. This value is in milliseconds.
ReportInterval- Gets or sets the report interval for the sensor. This value is in milliseconds and determines how frequently the sensor generates readings.
Methods
Start()- Starts the reporting of sensor data. This method should be called when you want to begin receiving readings.
Stop()- Stops the reporting of sensor data. This method should be called when you no longer need to receive readings to conserve power.
Events
ReadingChanged- Occurs when the sensor reports a new reading. You can attach an event handler to this to process the latest sensor data.
GyrometerReading Class
The GyrometerReading class contains the actual sensor data for a specific point in time.
Properties
Timestamp- Gets the timestamp of the reading.
AngularVelocityX- Gets the angular velocity around the X-axis in radians per second.
AngularVelocityY- Gets the angular velocity around the Y-axis in radians per second.
AngularVelocityZ- Gets the angular velocity around the Z-axis in radians per second.
Properties- Gets any additional sensor-specific properties.
Usage Example (C#)
Here's a basic example of how to use the Gyrometer to display angular velocity data:
C# Example
using System;
using Windows.Devices.Sensors;
using Windows.UI.Core;
public sealed partial class MainPage : Page
{
private Gyrometer _gyrometer;
private DataWriter _dataWriter; // Assume DataWriter is initialized elsewhere
public MainPage()
{
this.InitializeComponent();
InitializeGyrometer();
}
private async void InitializeGyrometer()
{
_gyrometer = await Gyrometer.GetDefaultAsync();
if (_gyrometer != null)
{
// Set the report interval to a reasonable value, e.g., 16ms for ~60fps
_gyrometer.ReportInterval = 16;
_gyrometer.ReadingChanged += Gyrometer_ReadingChanged;
}
else
{
// Handle the case where the gyrometer is not available
// e.g., display a message to the user.
}
}
private async void Gyrometer_ReadingChanged(Gyrometer sender, GyrometerReadingChangedEventArgs args)
{
// Update UI on the UI thread
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
GyrometerReading reading = args.Reading;
// Update UI elements with reading.AngularVelocityX, reading.AngularVelocityY, reading.AngularVelocityZ
// For example:
// X_TextBlock.Text = $"X: {reading.AngularVelocityX:F3} rad/s";
// Y_TextBlock.Text = $"Y: {reading.AngularVelocityY:F3} rad/s";
// Z_TextBlock.Text = $"Z: {reading.AngularVelocityZ:F3} rad/s";
});
}
// Ensure you stop listening when the page is unloaded
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (_gyrometer != null)
{
_gyrometer.ReadingChanged -= Gyrometer_ReadingChanged;
_gyrometer = null;
}
base.OnNavigatedFrom(e);
}
}
Tip: For battery-sensitive applications, consider setting a larger
ReportInterval or stopping the gyrometer when it's not actively needed.