Skip to main content

Meadow.Foundation.FeatherWings.NineDofImuWing

NineDofImuWing
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet Gallery for Meadow.Foundation.FeatherWings.NineDofImuWing

Code Example

NineDofImuWing nineDofImuWing;

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

var i2cBus = Device.CreateI2cBus(I2cBusSpeed.FastPlus);

nineDofImuWing = new NineDofImuWing(i2cBus);

// Example that uses an IObservable subscription to only be notified when the filter is satisfied
var consumer = NineDofImuWing.CreateObserver(handler: result => HandleResult(this, result),
filter: result => FilterResult(result));

nineDofImuWing.Subscribe(consumer);

// classical .NET events can also be used for all sensors
nineDofImuWing.Updated += HandleResult;

nineDofImuWing.StartUpdating(TimeSpan.FromSeconds(1));

return Task.CompletedTask;
}

bool FilterResult(IChangeResult<(
Acceleration3D? Acceleration3D,
AngularVelocity3D? AngularVelocity3D,
MagneticField3D? MagneticField3D
)> result)
{
return result.New.Acceleration3D.Value.Z > new Acceleration(0.1, Acceleration.UnitType.Gravity);
}

// Example Event Handler for all sensor results
void HandleResult(object sender, IChangeResult<(
Acceleration3D? Acceleration3D,
AngularVelocity3D? AngularVelocity3D,
MagneticField3D? MagneticField3D
)> result)
{
var accel = result.New.Acceleration3D.Value;
var gyro = result.New.AngularVelocity3D.Value;
var mag = result.New.MagneticField3D.Value;

Resolver.Log.Info(
$"Accelerometer (g): X = {accel.X.Gravity:0.##}, Y = {accel.Y.Gravity:0.##}, Z = {accel.Z.Gravity:0.##}; " +
$"Gyroscope (°/s): X = {gyro.X.DegreesPerSecond:0.##}, Y = {gyro.Y.DegreesPerSecond:0.##}, Z = {gyro.Z.DegreesPerSecond:0.##}; " +
$"Magnetometer (gauss) X = {mag.X.Gauss:0.##}, Y = {mag.Y.Gauss:0.##}, Z = {mag.Z.Gauss:0.##}"
);
}

// Example Event Handler for any individual sensors' result
void HandleResult<UNIT>(object sender, IChangeResult<UNIT> result)
where UNIT : struct
{
if (result is IChangeResult<Acceleration3D> accel)
{
Resolver.Log.Info($"Accelerometer (g): X = {accel.New.X.Gravity:0.##}, Y = {accel.New.Y.Gravity:0.##}, Z = {accel.New.Z.Gravity:0.##}");
}
else if (result is IChangeResult<AngularVelocity3D> gyro)
{
Resolver.Log.Info($"Gyroscope (°/s): X = {gyro.New.X.DegreesPerSecond:0.##}, Y = {gyro.New.Y.DegreesPerSecond:0.##}, Z = {gyro.New.Z.DegreesPerSecond:0.##};");
}
else if (result is IChangeResult<MagneticField3D> mag)
{
Resolver.Log.Info($"Magnetometer (gauss) X = {mag.New.X.Gauss:0.##}, Y = {mag.New.Y.Gauss:0.##}, Z = {mag.New.Z.Gauss:0.##}");
}
}

Sample project(s) available on GitHub

Class NineDofImuWing

Represents Adafruit's 9-DOF IMU FeatherWing

Assembly: NineDofImuWing.dll
View Source
Declaration
public class NineDofImuWing : PollingSensorBase<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, MagneticField3D? MagneticField3D)>, IObservable<IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, MagneticField3D? MagneticField3D)>>, ISamplingSensor<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, MagneticField3D? MagneticField3D)>, ISensor<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, MagneticField3D? MagneticField3D)>, IAccelerometer, ISamplingSensor<Acceleration3D>, ISensor<Acceleration3D>, IGyroscope, ISamplingSensor<AngularVelocity3D>, ISensor<AngularVelocity3D>, IMagnetometer, ISamplingSensor<MagneticField3D>, ISensor<MagneticField3D>, ISensor, ISamplingSensor

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>> -> Meadow.Foundation.SamplingSensorBase<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>> -> Meadow.Foundation.PollingSensorBase<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>

Implements:

Expand

System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>>, Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>, Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>, Meadow.Peripherals.Sensors.Motion.IAccelerometer, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Acceleration3D>, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Acceleration3D>, Meadow.Peripherals.Sensors.Motion.IGyroscope, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.AngularVelocity3D>, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.AngularVelocity3D>, Meadow.Peripherals.Sensors.Motion.IMagnetometer, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.MagneticField3D>, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.MagneticField3D>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor

Properties

Lis3mdl

The LIS3MDL Magnetometer

View Source
Declaration
public Lis3mdl Lis3mdl { get; }

Lsm6dsox

The LSM6DSOX Accelerometer/Gyroscope

View Source
Declaration
public Lsm6dsox Lsm6dsox { get; }

UpdateInterval

The sampling interval

View Source
Declaration
public override TimeSpan UpdateInterval { get; }

IsSampling

Is the sensor currently sampling

View Source
Declaration
public bool IsSampling { get; }

Acceleration3D

The current acceleration

View Source
Declaration
public Acceleration3D? Acceleration3D { get; }

AngularVelocity3D

The current angular velocity

View Source
Declaration
public AngularVelocity3D? AngularVelocity3D { get; }

MagneticField3D

The current magnetic field

View Source
Declaration
public MagneticField3D? MagneticField3D { get; }

Methods

StartUpdating(TimeSpan?)

Starts updating the sensor on the updateInterval frequency specified.

This method also starts raising Updated events and notifying IObservable subscribers. Use the updateInterval parameter to specify how often events and notifications are raised/sent.

View Source
Declaration
public override void StartUpdating(TimeSpan? updateInterval = null)
Parameters
TypeNameDescription
System.Nullable<System.TimeSpan>updateIntervalA TimeSpan that specifies how long to
         wait between readings. This value influences how often `*Updated`
events are raised and `IObservable` consumers are notified.
The default is 5 seconds. |

StopUpdating()

Stops sampling the sensor

View Source
Declaration
public override void StopUpdating()

RaiseEventsAndNotify(IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, MagneticField3D? MagneticField3D)>)

Notify observers

View Source
Declaration
protected override void RaiseEventsAndNotify(IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, MagneticField3D? MagneticField3D)> changeResult)
Parameters
TypeNameDescription
Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>changeResultprovides new and old values

ReadSensor()

Read value from sensor

View Source
Declaration
protected override Task<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, MagneticField3D? MagneticField3D)> ReadSensor()
Returns

System.Threading.Tasks.Task<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>: unitized value

Implements

  • System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>>
  • Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>
  • Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.MagneticField3D>>>
  • Meadow.Peripherals.Sensors.Motion.IAccelerometer
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Acceleration3D>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Acceleration3D>
  • Meadow.Peripherals.Sensors.Motion.IGyroscope
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.AngularVelocity3D>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Units.AngularVelocity3D>
  • Meadow.Peripherals.Sensors.Motion.IMagnetometer
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.MagneticField3D>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Units.MagneticField3D>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor