Skip to main content

Meadow.Foundation.Sensors.Motion.Hmc5883

Hmc5883
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet Gallery for Meadow.Foundation.Sensors.Motion.Hmc5883

Code Example

Hmc5883 sensor;

public override Task Initialize()
{
Resolver.Log.Info("Initialize...");

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

// classical .NET events can also be used:
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($"Direction: [X:{result.New.X:N2}," +
$"Y:{result.New.Y:N2}," +
$"Z:{result.New.Z:N2}]");

Resolver.Log.Info($"Heading: [{Hmc5883.DirectionToHeading(result.New).DecimalDegrees:N2}] degrees");
};

// Example that uses an IObservable subscription to only be notified when the filter is satisfied
var consumer = Hmc5883.CreateObserver(
handler: result => Resolver.Log.Info($"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"),
// only notify if there's a greater than 5° of heading change
filter: result =>
{
if (result.Old is { } old)
{
return (Hmc5883.DirectionToHeading(result.New - old) > new Azimuth(5));
}
return false;
});

sensor.Subscribe(consumer);

return Task.CompletedTask;
}

public async override Task Run()
{
var result = await sensor.Read();
Resolver.Log.Info("Initial Readings:");
Resolver.Log.Info($"Direction: [X:{result.X:N2}," +
$"Y:{result.Y:N2}," +
$"Z:{result.Z:N2}]");

Resolver.Log.Info($"Heading: [{Hmc5883.DirectionToHeading(result).DecimalDegrees:N2}] degrees");

sensor.StartUpdating(TimeSpan.FromMilliseconds(1000));
}

Sample project(s) available on GitHub

Class Hmc5883

Driver for the Hmc5883 digital compass

This driver is untested

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

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<UNIT>

Derived:
Meadow.Foundation.Sensors.Motion.Qmc5883

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

Properties

DefaultI2cAddress

The default I2C address for the peripheral

View Source
Declaration
public byte DefaultI2cAddress { get; }

Direction

HMC5883L Direction as a Vector

View Source
Declaration
public Vector? Direction { get; }

Heading

HMC5883L Heading (DEG)

View Source
Declaration
public Azimuth? Heading { get; }

DeviceStatus

HMC5883L Status

View Source
Declaration
public Hmc5883.Statuses DeviceStatus { get; }

Methods

Initialize()

Initialize the sensor

View Source
Declaration
protected virtual void Initialize()

ReadSensor()

Reads data from the sensor

View Source
Declaration
protected override Task<Vector> ReadSensor()
Returns

System.Threading.Tasks.Task<Meadow.Foundation.Spatial.Vector>: The latest sensor reading### DirectionToHeading(Vector) Calculate heading

View Source
Declaration
public static Azimuth DirectionToHeading(Vector direction)
Returns

Meadow.Units.Azimuth: Heading (DEG)

Parameters
TypeNameDescription
Meadow.Foundation.Spatial.VectordirectionHMC5883L Direction

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