Remarks

Mpu6050
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Motion.Mpu6050

Code Example

Mpu6050 sensor;

public override Task Initialize()
{
    Console.WriteLine("Initialize...");

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

    // classical .NET events can also be used:
    sensor.Updated += (sender, result) => {
        Console.WriteLine($"Accel: [X:{result.New.Acceleration3D?.X.MetersPerSecondSquared:N2}," +
            $"Y:{result.New.Acceleration3D?.Y.MetersPerSecondSquared:N2}," +
            $"Z:{result.New.Acceleration3D?.Z.MetersPerSecondSquared:N2} (m/s^2)]");

        Console.WriteLine($"Angular Velocity: [X:{result.New.AngularVelocity3D?.X.DegreesPerSecond:N2}," +
            $"Y:{result.New.AngularVelocity3D?.Y.DegreesPerSecond:N2}," +
            $"Z:{result.New.AngularVelocity3D?.Z.DegreesPerSecond:N2} (dps)]");

        Console.WriteLine($"Temp: {result.New.Temperature?.Celsius:N2}C");
    };

    // Example that uses an IObservable subscription to only be notified when the filter is satisfied
    var consumer = Mpu6050.CreateObserver(
        handler: result => Console.WriteLine($"Observer: [x] changed by threshold; new [x]: X:{result.New.Acceleration3D?.X:N2}, old: X:{result.Old?.Acceleration3D?.X:N2}"),
        // only notify if there's a greater than 1G change in the Z direction
        filter: result => {
            if (result.Old is { } old) { //c# 8 pattern match syntax. checks for !null and assigns var.
                return ((result.New.Acceleration3D.Value - old.Acceleration3D.Value).Z > new Acceleration(1, AU.Gravity));
            }
            return false;
        });
    sensor.Subscribe(consumer);

    return Task.CompletedTask;
}

public async override Task Run()
{
    //==== one-off read
    var result = await sensor.Read();

    Console.WriteLine($"Accel: [X:{result.Acceleration3D?.X.MetersPerSecondSquared:N2}," +
        $"Y:{result.Acceleration3D?.Y.MetersPerSecondSquared:N2}," +
        $"Z:{result.Acceleration3D?.Z.MetersPerSecondSquared:N2} (m/s^2)]");

    Console.WriteLine($"Angular Accel: [X:{result.AngularVelocity3D?.X.DegreesPerSecond:N2}," +
        $"Y:{result.AngularVelocity3D?.Y.DegreesPerSecond:N2}," +
        $"Z:{result.AngularVelocity3D?.Z.DegreesPerSecond:N2} (dps)]");

    Console.WriteLine($"Temp: {result.Temperature?.Celsius:N2}C");

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

Sample project(s) available on GitHub

Characteristic Locus
Inheritance System.Object ObservableBase<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>> SensorBase<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>> SamplingSensorBase<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>> ByteCommsSensorBase<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>> > Mpu6050
Implements IDisposable IAccelerometer IGyroscope ITemperatureSensor
Inherited Members ByteCommsSensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.Peripheral ByteCommsSensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.ReadBuffer ByteCommsSensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.WriteBuffer ByteCommsSensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.Init(Int32, Int32) ByteCommsSensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.Dispose(Boolean) ByteCommsSensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.Dispose() SamplingSensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.StopUpdating() SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.Updated SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.samplingLock SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.SamplingTokenSource SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.Conditions SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.IsSampling SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.UpdateInterval SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.ReadSensor() SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.Read() ObservableBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.observers ObservableBase<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Motion
Assembly Mpu6050.dll

Syntax

public class Mpu6050 : ByteCommsSensorBase<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Units.Temperature? Temperature)>, IDisposable, IAccelerometer, IGyroscope, ITemperatureSensor

Constructors

Mpu6050(II2cBus, Mpu6050.Addresses)

Declaration
public Mpu6050(II2cBus i2cBus, Mpu6050.Addresses address = default(Mpu6050.Addresses))

Parameters

Type Name Description
II2cBus i2cBus
Mpu6050.Addresses address

Mpu6050(II2cBus, Byte)

Declaration
public Mpu6050(II2cBus i2cBus, byte address)

Parameters

Type Name Description
II2cBus i2cBus
System.Byte address

Properties

Acceleration3D

Acceleration 3D

Declaration
public Acceleration3D? Acceleration3D { get; }

Property Value

Type Description
System.Nullable<Acceleration3D>

AngularVelocity3D

Angualr acceleration 3D

Declaration
public AngularVelocity3D? AngularVelocity3D { get; }

Property Value

Type Description
System.Nullable<AngularVelocity3D>

Temperature

Temperature

Declaration
public Units.Temperature? Temperature { get; }

Property Value

Type Description
System.Nullable<Units.Temperature>

Methods

Initialize(Byte)

Declaration
protected void Initialize(byte address)

Parameters

Type Name Description
System.Byte address

LoadConfiguration()

Declaration
protected void LoadConfiguration()

RaiseEventsAndNotify(IChangeResult<(Nullable<Acceleration3D> Acceleration3D, Nullable<AngularVelocity3D> AngularVelocity3D, Nullable<Units.Temperature> Temperature)>)

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Units.Temperature? Temperature)> changeResult)

Parameters

Type Name Description
IChangeResult<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>> changeResult

ReadSensor()

Declaration
protected override Task<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Units.Temperature? Temperature)> ReadSensor()

Returns

Type Description
Task<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>>

Overrides

Meadow.Foundation.SensorBase<System.ValueTuple<System.Nullable<Acceleration3D>, System.Nullable<AngularVelocity3D>, System.Nullable<Units.Temperature>>>.ReadSensor()

Wake()

Declaration
public void Wake()

Events

Acceleration3DUpdated

Declaration
public event EventHandler<IChangeResult<Acceleration3D>> Acceleration3DUpdated

Event Type

Type Description
EventHandler<IChangeResult<Acceleration3D>>

AngularVelocity3DUpdated

Declaration
public event EventHandler<IChangeResult<AngularVelocity3D>> AngularVelocity3DUpdated

Event Type

Type Description
EventHandler<IChangeResult<AngularVelocity3D>>

TemperatureUpdated

Declaration
public event EventHandler<IChangeResult<Units.Temperature>> TemperatureUpdated

Event Type

Type Description
EventHandler<IChangeResult<Units.Temperature>>