NineDofImuWing nineDofImuWing;
public override Task Initialize()
{
Resolver.Log.Info("Initializing ...");
var i2cBus = Device.CreateI2cBus(I2cBusSpeed.FastPlus);
nineDofImuWing = new NineDofImuWing(i2cBus);
var consumer = NineDofImuWing.CreateObserver(handler: result => HandleResult(this, result),
filter: result => FilterResult(result));
nineDofImuWing.Subscribe(consumer);
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);
}
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.##}"
);
}
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.##}");
}
}