Remarks

3-AxisDigitalCompass
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

ThreeAxisDigitalCompass Meadow Pin
GND GND
VCC 3.3V
RX D01
TX D00
Characteristic Locus
Inheritance object ObservableBase<Vector> SamplingSensorBase<Vector> PollingSensorBase<Vector> ByteCommsSensorBase<Vector> Hmc5883 > ThreeAxisDigitalCompass
Implements IObservable<IChangeResult<Vector>> ISamplingSensor<Vector> ISensor<Vector> IDisposable II2cPeripheral
Inherited Members Hmc5883.Initialize() Hmc5883.RaiseEventsAndNotify(IChangeResult<Vector>) Hmc5883.ReadSensor() Hmc5883.DirectionToHeading(Vector) Hmc5883.DefaultI2cAddress Hmc5883.Direction Hmc5883.Heading Hmc5883.DeviceStatus Hmc5883.DirectionUpdated ByteCommsSensorBase<Vector>.Init(int, int) ByteCommsSensorBase<Vector>.Dispose(bool) ByteCommsSensorBase<Vector>.Dispose() ByteCommsSensorBase<Vector>.BusComms ByteCommsSensorBase<Vector>.ReadBuffer ByteCommsSensorBase<Vector>.WriteBuffer PollingSensorBase<Vector>.StartUpdating(TimeSpan?) PollingSensorBase<Vector>.StopUpdating() SamplingSensorBase<Vector>.samplingLock SamplingSensorBase<Vector>.Read() SamplingSensorBase<Vector>.SamplingTokenSource SamplingSensorBase<Vector>.Conditions SamplingSensorBase<Vector>.IsSampling SamplingSensorBase<Vector>.UpdateInterval SamplingSensorBase<Vector>.Updated ObservableBase<Vector>.NotifyObservers(IChangeResult<Vector>) ObservableBase<Vector>.Subscribe(IObserver<IChangeResult<Vector>>) ObservableBase<Vector>.CreateObserver(Action<IChangeResult<Vector>>, Predicate<IChangeResult<Vector>>) ObservableBase<Vector>.observers object.Equals(object) object.Equals(object, object) object.GetHashCode() object.GetType() object.MemberwiseClone() object.ReferenceEquals(object, object) object.ToString()
Namespace Meadow.Foundation.Grove.Sensors.Motion
Assembly 3-AxisDigitalCompass.dll

Syntax

public class ThreeAxisDigitalCompass : Hmc5883, IObservable<IChangeResult<Vector>>, ISamplingSensor<Vector>, ISensor<Vector>, IDisposable, II2cPeripheral

Constructors

ThreeAxisDigitalCompass(II2cBus, byte, GainLevels, MeasuringModes, DataOutputRates, SampleAmounts, MeasurementConfigurations)

Creates a new ThreeAxisDigitalCompass driver

Declaration
public ThreeAxisDigitalCompass(II2cBus i2cBus, byte address = 30, Hmc5883.GainLevels gain = GainLevels.Gain1090, Hmc5883.MeasuringModes measuringMode = MeasuringModes.Continuous, Hmc5883.DataOutputRates outputRate = DataOutputRates.Rate15, Hmc5883.SampleAmounts samplesAmount = SampleAmounts.One, Hmc5883.MeasurementConfigurations measurementConfig = MeasurementConfigurations.Normal)

Parameters

Type Name Description
II2cBus i2cBus
byte address
Hmc5883.GainLevels gain
Hmc5883.MeasuringModes measuringMode
Hmc5883.DataOutputRates outputRate
Hmc5883.SampleAmounts samplesAmount
Hmc5883.MeasurementConfigurations measurementConfig

Remarks

3-AxisDigitalCompass
Status Status badge: working
Source code GitHub
NuGet package NuGet 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

ThreeAxisDigitalCompass Meadow Pin
GND GND
VCC 3.3V
RX D01
TX D00