GH-337 Android Low Pass Filter (#354)
* Implemented low pass filter as requested by issue 337 * Added value to sample page. * Updated docs for ApplyLowPassFilter * Enabled ApplyLowPassFilter Switch on Android, disabled on all other platforms * Moved LowPassFilter to shared, removed unnecessary usings. * Updated Docs
This commit is contained in:
Родитель
0a6196442d
Коммит
56bc8dca66
|
@ -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>
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace Samples.ViewModel
|
|||
{
|
||||
bool compass1IsActive;
|
||||
bool compass2IsActive;
|
||||
bool compassApplyLowPassFilter;
|
||||
double compass1;
|
||||
double compass2;
|
||||
int speed1 = 2;
|
||||
|
@ -43,6 +44,16 @@ namespace Samples.ViewModel
|
|||
set => SetProperty(ref compass2IsActive, value);
|
||||
}
|
||||
|
||||
public bool CompassApplyLowPassFilter
|
||||
{
|
||||
get => compassApplyLowPassFilter;
|
||||
set
|
||||
{
|
||||
SetProperty(ref compassApplyLowPassFilter, value);
|
||||
Compass.ApplyLowPassFilter = value;
|
||||
}
|
||||
}
|
||||
|
||||
public double Compass1
|
||||
{
|
||||
get => compass1;
|
||||
|
|
|
@ -54,6 +54,7 @@ namespace Xamarin.Essentials
|
|||
|
||||
class SensorListener : Java.Lang.Object, ISensorEventListener, IDisposable
|
||||
{
|
||||
LowPassFilter filter = new LowPassFilter();
|
||||
float[] lastAccelerometer = new float[3];
|
||||
float[] lastMagnetometer = new float[3];
|
||||
bool lastAccelerometerSet;
|
||||
|
@ -92,6 +93,11 @@ namespace Xamarin.Essentials
|
|||
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);
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Xamarin.Essentials
|
||||
{
|
||||
|
@ -11,6 +10,8 @@ namespace Xamarin.Essentials
|
|||
|
||||
public static bool IsMonitoring { get; private set; }
|
||||
|
||||
public static bool ApplyLowPassFilter { get; set; }
|
||||
|
||||
public static void Start(SensorSpeed sensorSpeed)
|
||||
{
|
||||
if (!IsSupported)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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">
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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">
|
||||
|
|
|
@ -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">
|
||||
|
|
|
@ -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" />
|
||||
|
|
Загрузка…
Ссылка в новой задаче