Skip to main content

Meadow.Foundation.Grove.Sensors.Motion.ThreeAxisDigitalCompass

3-AxisDigitalCompass
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet Gallery for Meadow.Foundation.Grove.Sensors.Motion.3-AxisDigitalCompass

Code Example

ThreeAxisDigitalCompass sensor;

public override Task Initialize()
{
Console.WriteLine("Initializing ...");

sensor = new ThreeAxisDigitalCompass(Device.CreateI2cBus());

sensor.Updated += (sender, result) => {
Console.WriteLine($"Direction: [X:{result.New.X:N2}," +
$"Y:{result.New.Y:N2}," +
$"Z:{result.New.Z:N2}]");

Console.WriteLine($"Heading: [{Hmc5883.DirectionToHeading(result.New).DecimalDegrees:N2}] degrees");
};

var consumer = Hmc5883.CreateObserver(
handler: result =>
{
Console.WriteLine($"Observer: [x] changed by threshold; " +
$"new [x]: X:{Hmc5883.DirectionToHeading(result.New):N2}, " +
$"old: X:{((result.Old != null) ? Hmc5883.DirectionToHeading(result.Old.Value) : "n/a"):N2} degrees");
},
filter: result => {
if (result.Old is { } old)
{ //c# 8 pattern match syntax. checks for !null and assigns var.
return (Hmc5883.DirectionToHeading(result.New - old) > new Azimuth(5));
}
return false;
});

sensor.Subscribe(consumer);

return Task.CompletedTask;
}

public override Task Run()
{
sensor.StartUpdating(TimeSpan.FromSeconds(1));

return Task.CompletedTask;
}

Sample project(s) available on GitHub

Wiring Example

ThreeAxisDigitalCompassMeadow Pin
GNDGND
VCC3.3V
RXD01
TXD00

Class ThreeAxisDigitalCompass

Represents a 3-Axis Digital Compass Sensor

Assembly: 3-AxisDigitalCompass.dll
View Source
Declaration
public class ThreeAxisDigitalCompass : Hmc5883, IObservable<IChangeResult<Vector>>, ISamplingSensor<Vector>, ISensor<Vector>, ISensor, ISamplingSensor, IDisposable, II2cPeripheral

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<Meadow.Foundation.Spatial.Vector> -> Meadow.Foundation.SamplingSensorBase<Meadow.Foundation.Spatial.Vector> -> Meadow.Foundation.PollingSensorBase<Meadow.Foundation.Spatial.Vector> -> Meadow.Foundation.ByteCommsSensorBase<Meadow.Foundation.Spatial.Vector> -> Meadow.Foundation.Sensors.Motion.Hmc5883

Implements:
System.IObservable<Meadow.IChangeResult<Meadow.Foundation.Spatial.Vector>>, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Foundation.Spatial.Vector>, Meadow.Peripherals.Sensors.ISensor<Meadow.Foundation.Spatial.Vector>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor, System.IDisposable, Meadow.Hardware.II2cPeripheral

Implements

  • System.IObservable<Meadow.IChangeResult<Meadow.Foundation.Spatial.Vector>>
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Foundation.Spatial.Vector>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Foundation.Spatial.Vector>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor
  • System.IDisposable
  • Meadow.Hardware.II2cPeripheral