Meadow.Foundation.Sensors.Motion.Mpu6050
Mpu6050 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Mpu6050 sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
sensor = new Mpu6050(Device.CreateI2cBus());
// classical .NET events can also be used:
sensor.Updated += (sender, result) =>
{
Resolver.Log.Info($"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)]");
Resolver.Log.Info($"Angular Velocity: [X:{result.New.AngularVelocity3D?.X.DegreesPerSecond:N2}," +
$"Y:{result.New.AngularVelocity3D?.Y.DegreesPerSecond:N2}," +
$"Z:{result.New.AngularVelocity3D?.Z.DegreesPerSecond:N2} (dps)]");
Resolver.Log.Info($"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 => Resolver.Log.Info($"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)
{
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();
Resolver.Log.Info($"Accel: [X:{result.Acceleration3D?.X.MetersPerSecondSquared:N2}," +
$"Y:{result.Acceleration3D?.Y.MetersPerSecondSquared:N2}," +
$"Z:{result.Acceleration3D?.Z.MetersPerSecondSquared:N2} (m/s^2)]");
Resolver.Log.Info($"Angular Accel: [X:{result.AngularVelocity3D?.X.DegreesPerSecond:N2}," +
$"Y:{result.AngularVelocity3D?.Y.DegreesPerSecond:N2}," +
$"Z:{result.AngularVelocity3D?.Z.DegreesPerSecond:N2} (dps)]");
Resolver.Log.Info($"Temp: {result.Temperature?.Celsius:N2}C");
sensor.StartUpdating(TimeSpan.FromMilliseconds(500));
}
Sample project(s) available on GitHub
Class Mpu6050
Represents an MPU6050 Six-Axis (Gyro + Accelerometer) MEMS MotionTracking device
Assembly: Mpu6050.dll
View Source
public class Mpu6050 : ByteCommsSensorBase<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Temperature? Temperature)>, IObservable<IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Temperature? Temperature)>>, ISamplingSensor<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Temperature? Temperature)>, ISensor<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Temperature? Temperature)>, IDisposable, IAccelerometer, ISamplingSensor<Acceleration3D>, ISensor<Acceleration3D>, IGyroscope, ISamplingSensor<AngularVelocity3D>, ISensor<AngularVelocity3D>, ITemperatureSensor, ISamplingSensor<Temperature>, ISensor<Temperature>, ISensor, ISamplingSensor, II2cPeripheral
Inheritance: System.Object
-> Meadow.Foundation.ObservableBase<UNIT>
Implements:
Expand
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.Temperature>>>>
, Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.Temperature>>>
, Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.Temperature>>>
, System.IDisposable
, 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.ITemperatureSensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Temperature>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Temperature>
, Meadow.Peripherals.Sensors.ISensor
, Meadow.Peripherals.Sensors.ISamplingSensor
, Meadow.Hardware.II2cPeripheral
Properties
DefaultI2cAddress
The default I2C address for the peripheral
View Source
public byte DefaultI2cAddress { get; }
Acceleration3D
Acceleration 3D
View Source
public Acceleration3D? Acceleration3D { get; }
AngularVelocity3D
Angular acceleration 3D
View Source
public AngularVelocity3D? AngularVelocity3D { get; }
Temperature
Current Temperature value
View Source
public Temperature? Temperature { get; }
Methods
Wake()
Wake the sensor
View Source
public void Wake()
LoadConfiguration()
Load the sensor configuration
View Source
protected void LoadConfiguration()
RaiseEventsAndNotify(IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Temperature? Temperature)>)
Raise events for subscribers and notify of value changes
View Source
protected override void RaiseEventsAndNotify(IChangeResult<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Temperature? Temperature)> changeResult)
Parameters
Type | Name | Description |
---|---|---|
Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.Temperature>>> | changeResult | The updated sensor data |
ReadSensor()
Reads data from the sensor
View Source
protected override Task<(Acceleration3D? Acceleration3D, AngularVelocity3D? AngularVelocity3D, Temperature? Temperature)> ReadSensor()
Returns
System.Threading.Tasks.Task<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.Temperature>>>
: The latest sensor reading
Implements
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.Temperature>>>>
Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.Temperature>>>
Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.Acceleration3D>,System.Nullable<Meadow.Units.AngularVelocity3D>,System.Nullable<Meadow.Units.Temperature>>>
System.IDisposable
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.ITemperatureSensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Temperature>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Temperature>
Meadow.Peripherals.Sensors.ISensor
Meadow.Peripherals.Sensors.ISamplingSensor
Meadow.Hardware.II2cPeripheral