Accelerometer

All MbientLab boards come with an accelerometer, a device which measure acceleration forces e.g. gravity and motion, accessed through the IAccelerometer interface.

Beecause there are different accelerometers used across the boards, the base IAccelerometer interface was designed to be used with any accelerometer thus it can only access the measured acceleration data. Developers building apps for specific MetaWear models should also review the Bosch Accelerometer or MMA8452Q Accelerometer sections.

using MbientLab.MetaWear.Sensor;

IAccelerometer accelerometer= metawear.GetModule<IAccelerometer>();

Global Enable

To retrieve data from the accelerometers, developers will also need to set a global enable bit with the IAccelerometer level Start function. The global Start method must be called last, after all the sensor configuration has been set and async producers enabled. Conversly, to return the accelerometer to standby mode, call Stop.

// enable the sensor
// call last after configuring and enabling other data producers
accelerometer.Start();

// disable the sensor
accelerometer.Stop();

Configuration

It is important that you first set the output data rate and data range before using the accelerometer as these setings are typically global settings that affect the configuration of other acceleromter features. To configure the accelerometer, call Configure with your desired data rate and range. As this is a generic editor interface, only float values are accepted and the actual settings used may not be the same as the values passed in.

// Set sampling frequency to 25Hz & range to +/-4g
// or closest valid values
accelerometer.Configure(odr: 25f, range: 4f);

Console.WriteLine("Actual Odr = " + accelerometer.DataRate);
Console.WriteLine("Actual Range = " + accelerometer.Range);

Acceleration Data

The accelerometers provide access to the raw acceleration data they measure. First, use the Acceleration property to get the IAsyncDataProducer object controlling the acceleration data. Then, setup a data route to handle the data, and finally, start the accelerometer.

Raw acceleration data is represented by the API as a Acceleration type.

await accelerometer.Acceleration.AddRouteAsync(source =>
    source.Stream(data => Console.WriteLine("Acceleration = " + data.Value<Acceleration>()))
);