Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

GH-337 Android Low Pass Filter #354

Merged
merged 9 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Samples/Samples/View/CompassPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<StackLayout>
<Label Text="Monitor compass for changes." FontAttributes="Bold" Margin="12" />

<Switch IsToggled="{Binding CompassApplyLowPassFilter}" >
<Switch.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean" Android="True" Default="False"/>
</Switch.IsVisible>
</Switch>

<ScrollView>
<Grid Padding="12,0,12,12">
<Grid.RowDefinitions>
Expand Down
11 changes: 11 additions & 0 deletions Samples/Samples/ViewModel/CompassViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CompassViewModel : BaseViewModel
{
bool compass1IsActive;
bool compass2IsActive;
bool compassApplyLowPassFilter;
double compass1;
double compass2;
int speed1 = 2;
Expand Down Expand Up @@ -43,6 +44,16 @@ public bool Compass2IsActive
set => SetProperty(ref compass2IsActive, value);
}

public bool CompassApplyLowPassFilter
{
get => compassApplyLowPassFilter;
set
{
SetProperty(ref compassApplyLowPassFilter, value);
Compass.ApplyLowPassFilter = value;
}
}

public double Compass1
{
get => compass1;
Expand Down
6 changes: 6 additions & 0 deletions Xamarin.Essentials/Compass/Compass.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ internal static void PlatformStop()

class SensorListener : Java.Lang.Object, ISensorEventListener, IDisposable
{
LowPassFilter filter = new LowPassFilter();
float[] lastAccelerometer = new float[3];
float[] lastMagnetometer = new float[3];
bool lastAccelerometerSet;
Expand Down Expand Up @@ -92,6 +93,11 @@ void ISensorEventListener.OnSensorChanged(SensorEvent e)
SensorManager.GetRotationMatrix(r, null, lastAccelerometer, lastMagnetometer);
SensorManager.GetOrientation(r, orientation);
var azimuthInRadians = orientation[0];
if (Compass.ApplyLowPassFilter)
{
filter.Add(azimuthInRadians);
azimuthInRadians = filter.Average();
}
var azimuthInDegress = (Java.Lang.Math.ToDegrees(azimuthInRadians) + 360.0) % 360.0;

var data = new CompassData(azimuthInDegress);
Expand Down
3 changes: 2 additions & 1 deletion Xamarin.Essentials/Compass/Compass.shared.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;

namespace Xamarin.Essentials
{
Expand All @@ -11,6 +10,8 @@ public static partial class Compass

public static bool IsMonitoring { get; private set; }

public static bool ApplyLowPassFilter { get; set; }

public static void Start(SensorSpeed sensorSpeed)
{
if (!IsSupported)
Expand Down
39 changes: 39 additions & 0 deletions Xamarin.Essentials/Compass/LowPassFilter.shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;

namespace Xamarin.Essentials
{
class LowPassFilter
{
const int length = 10;

float sin;
float cos;
Queue<float> history = new Queue<float>(length);

internal void Add(float radians)
{
sin += (float)Math.Sin(radians);

cos += (float)Math.Cos(radians);

history.Enqueue(radians);

if (history.Count > length)
{
var old = history.Dequeue();

sin -= (float)Math.Sin(old);

cos -= (float)Math.Cos(old);
}
}

internal float Average()
{
var size = history.Count;

return (float)Math.Atan2(sin / size, cos / size);
}
}
}
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-android.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Member Id="E:Xamarin.Essentials.Compass.ReadingChanged" />
<Member Id="M:Xamarin.Essentials.Compass.Start(Xamarin.Essentials.SensorSpeed)" />
<Member Id="M:Xamarin.Essentials.Compass.Stop" />
<Member Id="P:Xamarin.Essentials.Compass.ApplyLowPassFilter" />
<Member Id="P:Xamarin.Essentials.Compass.IsMonitoring" />
</Type>
<Type Name="Xamarin.Essentials.CompassChangedEventArgs" Id="T:Xamarin.Essentials.CompassChangedEventArgs">
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-ios.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Member Id="E:Xamarin.Essentials.Compass.ReadingChanged" />
<Member Id="M:Xamarin.Essentials.Compass.Start(Xamarin.Essentials.SensorSpeed)" />
<Member Id="M:Xamarin.Essentials.Compass.Stop" />
<Member Id="P:Xamarin.Essentials.Compass.ApplyLowPassFilter" />
<Member Id="P:Xamarin.Essentials.Compass.IsMonitoring" />
<Member Id="P:Xamarin.Essentials.Compass.ShouldDisplayHeadingCalibration" />
</Type>
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-uwp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Member Id="E:Xamarin.Essentials.Compass.ReadingChanged" />
<Member Id="M:Xamarin.Essentials.Compass.Start(Xamarin.Essentials.SensorSpeed)" />
<Member Id="M:Xamarin.Essentials.Compass.Stop" />
<Member Id="P:Xamarin.Essentials.Compass.ApplyLowPassFilter" />
<Member Id="P:Xamarin.Essentials.Compass.IsMonitoring" />
</Type>
<Type Name="Xamarin.Essentials.CompassChangedEventArgs" Id="T:Xamarin.Essentials.CompassChangedEventArgs">
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Member Id="E:Xamarin.Essentials.Compass.ReadingChanged" />
<Member Id="M:Xamarin.Essentials.Compass.Start(Xamarin.Essentials.SensorSpeed)" />
<Member Id="M:Xamarin.Essentials.Compass.Stop" />
<Member Id="P:Xamarin.Essentials.Compass.ApplyLowPassFilter" />
<Member Id="P:Xamarin.Essentials.Compass.IsMonitoring" />
</Type>
<Type Name="Xamarin.Essentials.CompassChangedEventArgs" Id="T:Xamarin.Essentials.CompassChangedEventArgs">
Expand Down
20 changes: 20 additions & 0 deletions docs/en/Xamarin.Essentials/Compass.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@
</remarks>
</Docs>
<Members>
<Member MemberName="ApplyLowPassFilter">
<MemberSignature Language="C#" Value="public static bool ApplyLowPassFilter { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property bool ApplyLowPassFilter" />
<MemberSignature Language="DocId" Value="P:Xamarin.Essentials.Compass.ApplyLowPassFilter" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Docs>
<summary>Applies a moving average filter on the Android implementation. Unused on other platforms</summary>
<value>If a low pass filter is applied</value>
<remarks>
<para></para>
</remarks>
</Docs>
</Member>
<Member MemberName="IsMonitoring">
<MemberSignature Language="C#" Value="public static bool IsMonitoring { get; }" />
<MemberSignature Language="ILAsm" Value=".property bool IsMonitoring" />
Expand Down