📜 ⬆️ ⬇️

A few words about the Motion API


Together with the new version of Windows Phone 7.5, widely known under the codename “Mango”, developers were able to use a gyroscope and a compass (or rather, a magnetometer) for their applications, in addition to the GPS and accelerometer. Also became available is a set of API called Motion API, which combines the data from all sensors and produces the processed result, in the form of a position in space and characteristics of movement of the device. The Motion API is the result of Microsoft Research, which can now be used by all developers on Windows Phone.

Notice that the Motion API is available on the device, in addition to the accelerometer, you need at least a compass.

Quite often, you can even come across references to the Motion Sensor, since this API is available in the Microsoft.Devices.Sensors namespace and working with it is similar to working with all other sensors on the device.
Before turning to a simple example of using the Motion API, let's see what data it allows us to obtain.
Attitude, allows us to get the position of the device in space, in a variety of representations:In addition, we can get DeviceAcceleration - linear acceleration of the device; DeviceRotationRate - device rotation speed, Gravity - gravity vector.

As you can see, the Motion API provides all the necessary information to create its own applications of augmented reality or actively use device sensors as data sources in their applications.
')
Let's turn to a simple example. After launching the program, we will simply display the Pitch, Roll, Yaw value in the form of arrows rotation.

To do this, create a new project from the Windows Phone Application template and place the XAML code on the start page, with arrows, also change the project name and the page name. To rotate the arrows, I will use the Projection capabilities — I will rotate the arrow by the appropriate amount. Therefore, in each XAML Polyline object representing an arrow, I will add Projection and name it appropriately.

The resulting XAML code is shown below:
<!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text=" WINDOWS PHONE" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="motion api" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Name="MotionNotPresented" Text="    Motion API" FontSize="22" Visibility="Collapsed"></TextBlock> <Polyline Name="ArrowPitch" HorizontalAlignment="Center" VerticalAlignment="Center" Points="0,0 -10,150 0,140 10,150 0,0" Stroke="Green" StrokeThickness="2" Fill="Green" Margin="-300 -300 0 0"> <Polyline.Projection> <PlaneProjection x:Name="MotionPitchProjection"></PlaneProjection> </Polyline.Projection> </Polyline> <Polyline Name="ArrowRoll" HorizontalAlignment="Center" VerticalAlignment="Center" Points="0,0 -10,150 0,140 10,150 0,0" Stroke="Yellow" StrokeThickness="2" Fill="Yellow" Margin="300 -300 0 0"> <Polyline.Projection> <PlaneProjection x:Name="MotionRollProjection"></PlaneProjection> </Polyline.Projection> </Polyline> <Polyline Name="ArrowYaw" HorizontalAlignment="Center" VerticalAlignment="Center" Points="0,0 -10,150 0,140 10,150 0,0" Stroke="Red" StrokeThickness="2" Fill="Red"> <Polyline.Projection> <PlaneProjection x:Name="MotionYawProjection"></PlaneProjection> </Polyline.Projection> </Polyline> <TextBlock Height="50" HorizontalAlignment="Left" Margin="25,250,0,0" Text="Pitch" VerticalAlignment="Top" FontWeight="Bold" Foreground="Green" FontSize="40" /> <TextBlock FontSize="40" FontWeight="Bold" Foreground="Yellow" Height="50" HorizontalAlignment="Right" Margin="0,250,45,0" Text="Roll" VerticalAlignment="Top" /> <TextBlock FontSize="40" FontWeight="Bold" Foreground="Red" Height="50" HorizontalAlignment="Left" Margin="185,396,0,0" Text="Yaw" VerticalAlignment="Top" /> </Grid> 


For convenience, I have added signatures and a textual indication of unavailability of the Motion API on the device to the arrows.

Now, you need to add a reference (Reference) to Microsoft.Devices.Sensors to the project and add the namespace to the using code block:
 using Microsoft.Devices.Sensors; 


The definition of the accessibility of the API, its initialization and registration of the data change handler will be done in the Loaded event handler:
 Motion motion = null; // Constructor public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { if (Motion.IsSupported) { motion = new Motion(); motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged); motion.Start(); } else MotionNotPresented.Visibility = Visibility.Visible; } 


In the handler motion_CurrentValueChanged I will receive data and perform rotation of the arrows around the Z axis:
 void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e) { Dispatcher.BeginInvoke(() => { MotionPitchProjection.RotationZ = e.SensorReading.Attitude.Pitch * 180 / Math.PI; MotionRollProjection.RotationZ = e.SensorReading.Attitude.Roll * 180 / Math.PI; MotionYawProjection.RotationZ = e.SensorReading.Attitude.Yaw * 180 / Math.PI; }); } 


Now, if you have a device with a compass, and better with a compass and a gyroscope, you can test an example. Unfortunately, since there is no compass or gyro emulation in the emulator, the Motion API is not available in it.

Useful links:
Creating a simple augmented reality application
Windows Phone Development Center on MSDN
Windows Phone SDK 7.1
Forums on development for Windows Phone in Russian

Source: https://habr.com/ru/post/134835/


All Articles