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)
{
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>, IObservable<IChangeResult<Vector>>, ISamplingSensor<Vector>, ISensor<Vector>, IDisposable, II2cPeripheral
Constructors
Hmc5883(II2cBus, byte, GainLevels, MeasuringModes, DataOutputRates, SampleAmounts, MeasurementConfigurations)
Create a new Hmc5883 object
Declaration
public Hmc5883(II2cBus i2cBus, byte address = 30, Hmc5883.GainLevels gain = GainLevels.Gain1090, Hmc5883.MeasuringModes measuringMode = MeasuringModes.Continuous, Hmc5883.DataOutputRates outputRate = DataOutputRates.Rate15, Hmc5883.SampleAmounts sampleAmount = SampleAmounts.One, Hmc5883.MeasurementConfigurations measurementConfig = MeasurementConfigurations.Normal)
Parameters
Type | Name | Description |
---|---|---|
II2cBus | i2cBus | The I2C bus |
byte | address | The I2C address |
Hmc5883.GainLevels | gain | Gain |
Hmc5883.MeasuringModes | measuringMode | Measuring mode |
Hmc5883.DataOutputRates | outputRate | Output rate |
Hmc5883.SampleAmounts | sampleAmount | Sample amount |
Hmc5883.MeasurementConfigurations | measurementConfig | Measurement configuration |
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)
{
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));
}
Properties
DefaultI2cAddress
The default I2C address for the peripheral
Declaration
public byte DefaultI2cAddress { get; }
Property Value
Type | Description |
---|---|
byte |
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)
{
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));
}
DeviceStatus
HMC5883L Status
Declaration
public Hmc5883.Statuses DeviceStatus { get; }
Property Value
Type | Description |
---|---|
Hmc5883.Statuses |
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)
{
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));
}
Direction
HMC5883L Direction as a Vector
Declaration
public Vector? Direction { get; }
Property Value
Type | Description |
---|---|
Vector? |
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)
{
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));
}
Heading
HMC5883L Heading (DEG)
Declaration
public Azimuth? Heading { get; }
Property Value
Type | Description |
---|---|
Azimuth? |
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)
{
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));
}
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) |
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)
{
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));
}
Initialize()
Initialize the sensor
Declaration
protected virtual void Initialize()
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)
{
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));
}
RaiseEventsAndNotify(IChangeResult<Vector>)
Raise events for subscribers and notify of value changes
Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Vector> changeResult)
Parameters
Type | Name | Description |
---|---|---|
IChangeResult<Vector> | changeResult | The updated sensor data |
Overrides
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)
{
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));
}
ReadSensor()
Reads data from the sensor
Declaration
protected override Task<Vector> ReadSensor()
Returns
Type | Description |
---|---|
Task<Vector> | The latest sensor reading |
Overrides
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)
{
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));
}
Events
DirectionUpdated
Event to be raised when the compass changes
Declaration
public event EventHandler<IChangeResult<Vector>> DirectionUpdated
Event Type
Type | Description |
---|---|
EventHandler<IChangeResult<Vector>> |
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)
{
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));
}