Device (equipment) gyroscope sensor, Motion API
Introduced
Unique device for Windows Phone 7.5 (SDK 7.1)
Gyro sensor
Motion API (Motion API)
Example
1, demonstrating how to use the gyroscope sensor
Gyroscopedemo.xaml
<phone:phoneapplicationpage x:class= "Demo.Device.GyroscopeDemo" xmlns= "http://schemas.microsoft.com/winfx/20 06/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:phone=" Clr-namespace: Microsoft.phone.controls;assembly=microsoft.phone "Xmlns:shell=" clr-namespace:microsoft.phone.shell;assembly= Microsoft.phone "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "xmlns:mc=" http:// schemas.openxmlformats.org/markup-compatibility/2006 "fontfamily=" {StaticResource PhoneFontFamilyNormal} "FontSiz E= ' {StaticResource phonefontsizenormal} ' foreground= ' {StaticResource Phoneforegroundbrush} ' SupportedOrientations= "Portrait" orientation= "Portrait" mc:ignorable= "D" d:designheight= "768" d:designwidth= "The Shell:SystemTray.IsVis" ible= "True" > <grid x:name= "layoutroot" background= "Transparent" > <stackpanel orientation= "Ve Rtical "> <textblock name=" LblgyroscopesuPported "/> <button name=" btnstart "content=" "Open gyroscope" click= "btnStart_Click"/> <butto
n name= "Btnstop" content= "Close gyroscope" click= "Btnstop_click"/> <textblock "name=" Lblgyroscopestatus <textblock name= "Lbltimebetweenupdates"/> <textblock name= "lblmsg"/> &L T;/stackpanel> </Grid> </phone:PhoneApplicationPage>
GyroscopeDemo.xaml.cs
* * Demonstrates how to use the gyroscope sensor * * gyroscope-for accessing the gyroscope * issupported in the device-whether the device supports the gyroscope * isdatavalid-can be obtained from the gyroscope to the effective number According to * CurrentValue-gyroscope current data, gyroscopereading type * timebetweenupdates-triggers the time interval of the currentvaluechanged event, if the set value is less than Gyroscope the minimum allowable value, the value of this property will be set to the minimum allowable value of gyroscope * Start ()-Open the gyroscope * Stop ()-Close the gyroscope * currentvaluechanged -the event triggered when the data from the gyroscope sensor is changed, and the value of the attribute timebetweenupdates determines the time interval for triggering this event * gyroscopereading-gyroscope sensor data * Rotationrat E-Gets the rotational rate of rotation around the axis of the device (in radians/sec) * DateTimeOffset-the point at which data is obtained from the gyroscope sensor * * * * * Mobile coordinate system: Take the mobile phone position as reference, assuming the mobile phone vertical horizontal plane (vertical ), the screen is on you, then * 1, right and left are X axes, the right side is positive direction, the left side is the negative direction * 2, the upper and lower is the Y axis, the upper side is the positive direction, the lower side is the negative direction * 3, the inside and outside is the Z axis, close to you is the positive direction, far away from you for the negative direction *
Understanding * * using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Net;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media; Using System.Windows.Media.animation;
Using System.Windows.Shapes;
Using Microsoft.Phone.Controls;
Using Microsoft.Devices.Sensors;
Using Microsoft.Xna.Framework; namespace Demo.device {public partial class Gyroscopedemo:phoneapplicationpage {private gyroscope
_gyroscope;
Public Gyroscopedemo () {InitializeComponent (); Determine if the device supports gyroscope if (gyroscope.issupported) {lblgyroscopestatus.text = "This device supports gyroscope
";
else {Lblgyroscopestatus.text = "This device does not support gyroscope";
btnstart.isenabled = false;
btnstop.isenabled = false; }} private void btnStart_Click (object sender, RoutedEventArgs e) {if (_gyros
Cope = = null) {//instantiate gyroscope, register related event _gyroscope = new Gyroscope (); _gyroscope. TimebetweeNupdates = Timespan.frommilliseconds (1); _gyroscope. Currentvaluechanged + = new Eventhandler<sensorreadingeventargs<gyroscopereading>> (_gyroscope_
currentvaluechanged); Lbltimebetweenupdates.text = "Timebetweenupdates set to 1 milliseconds, actually" + _gyroscope.
TimeBetweenUpdates.TotalMilliseconds.ToString () + "millisecond"; try {//Open the gyroscope _gyroscope.
Start ();
Lblgyroscopestatus.text = "Gyroscope opened";
catch (Exception ex) {lblgyroscopestatus.text = "gyroscope open failed"; MessageBox.Show (ex.
ToString ()); }} private void Btnstop_click (object sender, RoutedEventArgs e) {if (_gyrosc Ope!= null) {//close gyroscope _gyroscope.
Stop ();
Lblgyroscopestatus.text = "Gyroscope closed";
}
} void _gyroscope_currentvaluechanged (object sender, Sensorreadingeventargs<gyroscopereading> e) {
Note: This method runs in a background thread, so you need to update the UI note to invoke UI thread Dispatcher.begininvoke (() => UpdateUI (e.sensorreading)); Private DateTimeOffset _lastupdatetime = Datetimeoffset.minvalue; The time of the last access to the gyroscope data private Vector3 _rotationtotal = Vector3.zero;
Cumulative rotation radian of gyroscope axes//update UI private void UpdateUI (Gyroscopereading gyroscopereading) {
The following is used to calculate the cumulative rotational radian if (_lastupdatetime.equals (Datetimeoffset.minvalue)) of the gyroscope axes {
_lastupdatetime = Gyroscopereading.timestamp; else {TimeSpan timesincelastupdate = gyroscopereading.timestamp-_lastupdate
time; Current rotation rate of gyroscope * Calculate the time elapsed for this rate = rotation in this time period _rotationtotal + = Gyroscopereading.rotationrate * (float) (timesin Celastupdate.totalsecoNDS);
_lastupdatetime = Gyroscopereading.timestamp;
} Vector3 rotationrate = Gyroscopereading.rotationrate;
Displays the rotation rate of the gyroscope's current axes (in radians/sec) Lblmsg.text + = "rotationrate.x:" + rotationrate.x.tostring ("0.0");
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = "Rotationrate.y:" + rotationrate.y.tostring ("0.0");
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = "Rotationrate.z:" + rotationrate.z.tostring ("0.0");
Lblmsg.text + = Environment.NewLine; Shows the cumulative rotation angle of the gyroscope axes lblmsg.text + = "rotationtotal.x:" + mathhelper.todegrees (_rotationtotal.x).
ToString ("0.00");
Lblmsg.text + = Environment.NewLine; Lblmsg.text + = "ROTATIONTOTAL.Y:" + mathhelper.todegrees (_rotationtotal.x).
ToString ("0.00");
Lblmsg.text + = Environment.NewLine; Lblmsg.text + = "Rotationtotal.z:" + mathheLper. Todegrees (_rotationtotal.x).
ToString ("0.00"); }
}
}