Bosch Accelerometer¶
With the exception of our first ever board, the MetaWear R, all other MbientLab boards use a Bosch accelerometer, represeted by the
IAccelerometerBosch interface.
This interface is an extension of the IAccelerometer
interface providing methods and classes specific to Bosch accelerometers.
Currently, we support both the BMI160 and BMA255 sensors and they share much of their functionality with only a few differences enumerated in their respective interfaces.
using MbientLab.MetaWear.Sensor;
using MbientLab.MetaWear.Sensor.AccelerometerBosch;
IAccelerometerBosch accBosch = metawear.GetModule<IAccelerometerBosch>();
Low/High Detection¶
Low/High-g detection is an algorithm on the sensor that detects when the measured acceleration falls below a lower bound (in freefall) or rises above an upper bound (high-g motion). This algorithm is accessed via the LowAndHighG property.
Before enabling the algorithm, first configure the parameters by calling ILowAndHighGDataProducer
’s
Configure method. This function configures both low and high-g detection.
For low-g detection, set EnableLowG
to true, and configure the threshold, hysteresis, and acceleration criteria if desired.
// enable low-g detection, use sum criteria,
// detect when sum < 0.333g
accBosch.LowAndHighG.Configure(enableLowG: true, lowThreshold: 0.333f, mode: LowGMode.Sum);
Unlike low-g detection, High-g detection can be disabled at a per axis level so developers need to explicitly set which axes to monitor. Developers can also optionally configure the duration, threshold, and hyesteresis of the high-g detection algorithm.
// enable high-g detection on z-axis,
// detect when acc > 2g
accBosch.LowAndHighG.Configure(enableHighGz: true, highThreshold: 2f);
After configuring the algorithm, add a route for the low/high-g responses. Data is interpreted as a LowHighG type.
await accBosch.LowAndHighG.AddRouteAsync(source =>
source.Stream(data => Console.WriteLine(data.Value<LowHighG>()))
);
accBosch.LowAndHighG.Start();
accBosch.Start();
Flat Detection¶
Flat detection checks when the sensor enters or leaves a horizontal position i.e. laying on a table. Data is interpretted as a boolean where true signifies the sensor is laying horizontally.
await accBosch.Flat.AddRouteAsync(source =>
source.Stream(data => Console.WriteLine("Flat? " + data.Value<bool>()))
);
accBosch.Flat.Start();
accBosch.Start();
Orientation Detection¶
The orientation detector alerts you when the sensor’s orientation changes between portrait/landscape and front/back. Data is represented as a
SensorOrientation
enum.
using MbientLab.MetaWear.Data;
await accelerometer.Orientation.AddRouteAsync(source =>
source.Stream(data => Console.WriteLine("Orientation = " + data.Value<SensorOrientation>()))
);
accBosch.Orientation.Start();
accBosch.Start();
Tap Detection¶
The tap detection algorithm checks if the difference in acceleration exceeds a threshold. To detect double tap, a second tap must be registered within
the quiet delay but before the double tap window ends. The shock duration is a period of time where the direction of the first tap is locked; the quiet
delay starts after the shock duration ends. Use ITapDataProducer
’s
Configure
to set the aforementioned parameters and to select which tap types to detect.
Data from the tap detection algorithm is represented as a Tap type.
using MbientLab.MetaWear.Data;
// enable single tap detection
accBosch.Tap.Configure(threshold: 2f, shock: TapShockTime._50ms);
await accelerometer.Tap.AddRouteAsync(source => source.Stream(data => {
var tap = data.Value<Tap>();
if (tap.Type.HasValue) {
switch (tap.Type.Value) {
case TapType.Single:
Console.WriteLine("Single tap!");
break;
case TapType.Double:
Console.WriteLine("Double tap!");
break;
}
}
}));
accBosch.Tap.Start();
accBosch.Start();
Motion Detection¶
The motion detection algorithms on the Bosch chips use the difference in consecutive acceleration data samples to determine different types of motion.
Motion | Description |
---|---|
Any | Difference exceeds threshold for N consecutive samples |
No | Difference doesn’t exceed the threshold for a period of time |
Slow | Same as any motion but axis and direction information is not retained |
The different motion detection algorithms are accessed with the Motion property and only one algorithm can be active at any time.
// configure no motion detection
// difference < 0.1g for 10 seconds before interrupt is fired
accBosch.Motion.ConfigureNo(duration: 10000, threshold: 0.1f);
accBosch.Motion.AddRouteAsync(source =>
source.Stream(data => Console.WriteLine("No motion detected"))
);
accBosch.Motion.Start();
accBosch.Start()