Remarks
Hmc5883 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
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) { //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 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
Syntax
public class Hmc5883 : ByteCommsSensorBase<Vector>, ISamplingSensor<Vector>, ISamplingSensor<Vector>, IDisposable
Constructors
Hmc5883(II2cBus, Byte, Hmc5883.GainLevels, Hmc5883.MeasuringModes, Hmc5883.DataOutputRates, Hmc5883.SampleAmounts, Hmc5883.MeasurementConfigurations)
Create a new Hmc5883 object
Declaration
public Hmc5883(II2cBus i2cBus, byte address = null, Hmc5883.GainLevels gain = default(Hmc5883.GainLevels), Hmc5883.MeasuringModes measuringMode = default(Hmc5883.MeasuringModes), Hmc5883.DataOutputRates outputRate = default(Hmc5883.DataOutputRates), Hmc5883.SampleAmounts samplesAmount = default(Hmc5883.SampleAmounts), Hmc5883.MeasurementConfigurations measurementConfig = default(Hmc5883.MeasurementConfigurations))
Parameters
Type | Name | Description |
---|---|---|
II2cBus | i2cBus | The I2C bus |
System.Byte | address | The I2C address |
Hmc5883.GainLevels | gain | Gain |
Hmc5883.MeasuringModes | measuringMode | Measuring mode |
Hmc5883.DataOutputRates | outputRate | Output rate |
Hmc5883.SampleAmounts | samplesAmount | Samples amount |
Hmc5883.MeasurementConfigurations | measurementConfig | Measurement configuration |
Properties
DeviceStatus
HMC5883L Status
Declaration
public Hmc5883.Statuses DeviceStatus { get; }
Property Value
Type | Description |
---|---|
Hmc5883.Statuses |
Direction
HMC5883L Direction as a Vector
Declaration
public Vector? Direction { get; }
Property Value
Type | Description |
---|---|
System.Nullable<Vector> |
Heading
HMC5883L Heading (DEG)
Declaration
public Azimuth? Heading { get; }
Property Value
Type | Description |
---|---|
System.Nullable<Azimuth> |
Methods
DirectionToHeading(Vector)
Calculate heading
Declaration
public static Azimuth DirectionToHeading(Vector direction)
Parameters
Type | Name | Description |
---|---|---|
Vector | direction | HMC5883L Direction |
Returns
Type | Description |
---|---|
Azimuth | Heading (DEG) |
Initialize()
Initalize the sensor
Declaration
protected virtual void Initialize()
RaiseEventsAndNotify(IChangeResult<Vector>)
Raise events for subcribers and notify of value changes
Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Vector> changeResult)
Parameters
Type | Name | Description |
---|---|---|
IChangeResult<Vector> | changeResult | The updated sensor data |
ReadSensor()
Reads data from the sensor
Declaration
protected override Task<Vector> ReadSensor()
Returns
Type | Description |
---|---|
Task<Vector> | The latest sensor reading |
Overrides
Meadow.Foundation.SamplingSensorBase<Meadow.Foundation.Spatial.Vector>.ReadSensor()
Events
DirectionUpdated
Event to be raised when the compass changes
Declaration
public event EventHandler<IChangeResult<Vector>> DirectionUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Vector>> |