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 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
41 changes: 41 additions & 0 deletions Xamarin.Essentials/Compass/Compass.android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Android.Hardware;
using Android.Runtime;

Expand Down Expand Up @@ -52,8 +53,43 @@ internal static void PlatformStop()
}
}

class LowPassFilter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that since this is generic it could be moved over to the .shared.cs file perhaps.

{
const int length = 10;

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

public 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);
}
}

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

return (float)Math.Atan2(sin / size, cos / size);
}
}

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 +128,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
2 changes: 2 additions & 0 deletions Xamarin.Essentials/Compass/Compass.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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
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