Introduction



Home Screen :

Name of Experiment : Compass or Magnetometer

Exp No : SSN2

Background : Student should have a basic knowledge of C#.

Summary : Compass or magnetometer sensor used to determine the angle by which the device is rotated relative to the Earth’s magnetic north pole. It senses orientation relative to the Earth's magnetic field using the Hall Effect. This sensor is mainly use for auto rotation of digital map depending upon your physical orientation.

Learning Objective : Using this experiment student will get to know about the inbuilt Compass sensor in mobile and its capability to use in their own application as per the requirement. This experiment helps him/her to understand the proper working of the Compass and he/she is able to get the real time values generated by Compass.

Target

Target Platform : This experiment is tested on Nokia Lumia 800.

*Note : Windows Phone Emulator does not support the Compass simulation.

Procedure

*Note : Compass is not supported by Windows Phone Emulator.

SourceCode

Source Code : Comments :
MainPage.xaml.cs

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<br />
<!--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="ExperimentTitle" Text="ExpNo:SSN2" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="ApplicationTitle" Text="Sensors" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="magnetometer" 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">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="status:"/>
<TextBlock x:Name="txtStatus" Text=" " FontWeight="Bold" Foreground="Bisque" TextAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Compass Orienatation:"/>
<TextBlock x:Name="txtOrientation" Text=" " Foreground="Aquamarine">
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Magnetic Heading:"/>
<TextBlock x:Name="txtMagnetic" Text="" Foreground="Blue">
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Accuracy Level:"/>
<TextBlock x:Name="txtAccuracy" Text=" " Foreground="Chocolate">
</TextBlock><br />
</StackPanel>
<Grid Margin="20" Height="220" Name="mygrid">

<Line x:Name="magneticLine" X1="220" Y1="100" X2="220" Y2="0" Stroke="Yellow" StrokeThickness="4" >
</Line>
<Line x:Name="trueLine" X1="220" Y1="100" X2="220" Y2="0" Stroke="Red" StrokeThickness="4">
</Line>
</Grid>
<StackPanel Orientation="Horizontal">
<Button x:Name="bStart" Content="Start" BorderBrush="Chocolate" Width="200" Click="bStart_Click"/>
<Button x:Name="bStop" Content="Stop" BorderBrush="Chocolate" Click="bStop_Click" Width="200" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="bReset" Content="Reset" BorderBrush="Chocolate" Click="bReset_Click"/>
</StackPanel>
</StackPanel>
<br />
</Grid>
</Grid>


MainPage.xaml.cs

using System;

using System.Windows;

using Microsoft.Phone.Controls;

using Microsoft.Xna.Framework;

using Microsoft.Devices.Sensors;

using System.Windows.Threading;

namespace Sensors_Compass

{

public partial class Main2 :

PhoneApplicationPage

{

Compass myCompass;
DispatcherTimer timer;
double magneticHeading;
double accuracyHeading;
double trueHeading;
bool isDataValid=false;
Vector3 rawMagnetometerReading;
public Main2()
{
InitializeComponent();

if (!Compass.IsSupported
{
txtStatus.Text = "Compass is not supported by the Device";
}
else {
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(60);
timer.Tick+=new EventHandler(timer_Tick);
}
}
private void timer_Tick(object sender, EventArgs e) {
if (isDataValid) {
txtStatus.Text = "receiving data from compass";
}
txtOrientation.Text = ""; txtMagnetic.Text =magneticHeading.ToString("0.0");
txtAccuracy.Text = accuracyHeading.ToString("0.0"); ;
double centerX = mygrid.ActualWidth / 2.0;
double centerY = mygrid.ActualHeight / 2.0;
magneticLine.X2 = centerX - centerY * Math.Sin(MathHelper.ToRadians((float)magneticHeading));
magneticLine.Y2 = centerX - centerY * Math.Sin(MathHelper.ToRadians((float)magneticHeading));
trueLine.X2 = centerX - centerY * Math.Sin(MathHelper.ToRadians((float)trueHeading));
trueLine.Y2 = centerX - centerY * Math.Sin(MathHelper.ToRadians((float)trueHeading));

}
private void bStart_Click(object sender, RoutedEventArgs e)
{
if (myCompass == null) {
myCompass = new Compass();
myCompass.TimeBetweenUpdates =
TimeSpan.FromMilliseconds(30);
myCompass.CurrentValueChanged+=new EventHandler<SensorReadingEventArgs <CompassReading>>(myCompass_CurrentValueChanged);<br />
}
try {
txtStatus.Text="Starting Compass";
myCompass.Start();
timer.Start();}
catch(InvalidOperationException){
txtStatus.Text = "Unable to start compass.";
}

}
private void myCompass_CurrentValueChanged(object sender,
SensorReadingEventArgs<CompassReading> e) { magneticHeading = e.SensorReading.MagneticHeading;
accuracyHeading = e.SensorReading.HeadingAccuracy; trueHeading = e.SensorReading.TrueHeading;
rawMagnetometerReading = e.SensorReading.MagnetometerReading;
}
private void bStop_Click(object sender, RoutedEventArgs e)
{
if (myCompass != null && myCompass.IsDataValid)
{
myCompass.Stop();
timer.Stop();
txtStatus.Text = "Compass Stopped";
}
}

private void bReset_Click(object sender, RoutedEventArgs e)
{
txtAccuracy.Text = string.Empty;
txtMagnetic.Text = string.Empty;
txtStatus.Text = string.Empty;
magneticLine.X1 = 220; magneticLine.X2 = 220; magneticLine.Y1 = 100;
magneticLine.Y2 = 0;
trueLine.X1 = 220;
trueLine.X2 = 220;
trueLine.Y1 = 100;
trueLine.Y2 = 0;

}
}
}









Name= "ExperimentTitle" Text= "Exp No.SSN2"





Name= "ApplicationTitle"
Text= "Sensors"







Name= "PageTitle" Text= "Magnetometer"

Snapshots




Observation

Observation : It is observed by the developer that Compass or Magnetometer, can be used in variety of application. This experiment shows the way, how smartly we can introduce the Compass services in other areas.

Video Link

For Windows Phone

Download

Right Click Here And Select Save Target For Entire Download :

Magnetometer