MMA8452Q Accelerometer

The MMA8452Q accelerometer class is an extension of the Accelerometer class that interacts specifically with the MMA8452Q chip, the accelerometer on the MetaWear R board. This sensor supports: axis sampling, movement detection, tap detection, orientation detection, and shake detection.

Acceleration Sampling

Axis sampling measures the acceleration data on the XYZ axes. You use a SamplingConfigEditor to set the measurement range and enable high pass filtering. Setting the output data rate is done by calling setOutputDataRate.

Available data sources are the acceleration from the XYZ axes and from the individual axis. Three axis data can be interpreted as a CartesianFloat (G’s) or a CartesianShort (milli G’s) whereas single axis data can be interpreted as a float (G) or short/integer/long (milli G).

When processing the 3 axis data, you are restricted to only data filters, minus the comparison and sample filters, and the rms and rss transformers. In order to access the full library of data processors, they must be chained after an rms or rss transformer. On firmware v1.1.2 and later, you can use the math processor on acceleration data. Each axis will be treated as separate inputs to the operation defined.

import com.mbientlab.metawear.module.Mma8452qAccelerometer.FullScaleRange;
import com.mbientlab.metawear.module.Mma8452qAccelerometer.OutputDataRate;

Mma8452qAccelerometer mma8452qModule= mwBoard.getModule(Mma8452qAccelerometer.class);
// Enable high pass filtering with the highest cutoff freq
// Set the measurement range to +/-8g
mma8452qModule.configureAxisSampling().enableHighPassFilter((byte) 0)
    .setFullScaleRange(FullScaleRange.FSR_8G)
    .commit();
// Set the output data rate to 100Hz
mma8452qModule.setOutputDataRate(OutputDataRate.ODR_100_HZ);

// enable axis sampling
mma8452qModule.enableAxisSampling();

// Switch the accelerometer to active mode
mma8452qModule.start();

High Frequency Streams

Firmware v1.2.3+ supports high frequency streaming of accelerometer data, see the High Frequency Streams section for more details.

Movement Detection

Movement detection uses the chips pre-programmed algorithms to detect motion or free fall movement. You use a MovementConfigEditor to configure the movement detection, setting thresholds and detection axes. Motion and free fall data are lumped together in one source as they are mutually exclusive on this chip. The movement data source is selected from the fromMovement method and data can be interpreted as a MovementData object.

import com.mbientlab.metawear.module.Mma8452qAccelerometer.Axis;
import com.mbientlab.metawear.module.Mma8452qAccelerometer.MovementType;

Mma8452qAccelerometer mma8452qModule= mwBoard.getModule(Mma8452qAccelerometer.class);
// Use configureFreeFallDetection to configure free fall detection
// configure motion detection to monitor the Z axis
mma8452qModule.configureMotionDetection().setAxes(Axis.Z)
    .commit();
// Use MovementType.FREE_FALL to enable free fall detection
// enable motion detection
mma8452qModule.enableMovementDetection(MovementType.MOTION);

// Switch the accelerometer to active mode
mma8452qModule.start();
import com.mbientlab.metawear.module.Mma8452qAccelerometer.MovementData;

mwBoard.getModule(Mma8452qAccelerometer.class).routeData()
    .fromMovement().stream("movement_stream")
.commit().onComplete(new CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
        result.subscribe("movement_stream", new MessageHandler() {
            @Override
            public void process(Message msg) {
                Log.i("MainActivity", msg.getData(MovementData.class).toString());
            }
        });
    }
});

Tap Detection

Tap detection uses the chips pre-programmed algorithms to detect single or double taps. You use a TapConfigEditor to configure tap detection. The tap data source is selected from the fromTap method and data can be interpreted as a TapData object.

import com.mbientlab.metawear.module.Mma8452qAccelerometer.Axis;
import com.mbientlab.metawear.module.Mma8452qAccelerometer.TapType

Mma8452qAccelerometer mma8452qModule= mwBoard.getModule(Mma8452qAccelerometer.class);
// configure tap detection to monitor the Z axis
mma8452qModule.configureTapDetection().setAxis(Axis.Z)
    .commit();
// enable single and double tap detection
mma8452qModule.enableTapDetection(TapType.DOUBLE, TapType.SINGLE);

// Switch the accelerometer to active mode
mma8452qModule.start();
import com.mbientlab.metawear.module.Mma8452qAccelerometer.TapData;

mwBoard.getModule(Mma8452qAccelerometer.class).routeData()
    .fromTap().stream("tap_stream").commit()
    .onComplete(new CompletionHandler<RouteManager>() {
        @Override
        public void success(RouteManager result) {
            result.subscribe("tap_stream", new MessageHandler() {
                @Override
                public void process(Message msg) {
                    TapData tapData= msg.getData(TapData.class);
                    switch (tapData.type()) {
                        case SINGLE:
                            Log.i("MainActivity", "single tap");
                            break;
                        case DOUBLE:
                            Log.i("MainActivity", "double tap");
                            break;
                    }
                }
            });
        }
    });

Orientation Detection

Orientation detection uses the chips pre-programmed algorithms to detect orientation changes. You use a OrientationConfigEditor to configure orientation detection. The orientation data source is selected from the fromOrientation method and data can be interpreted as a Orientation enum entry.

Mma8452qAccelerometer mma8452qModule= mwBoard.getModule(Mma8452qAccelerometer.class);
// enable orientation detection
mma8452qModule.enableOrientationDetection();

// Switch the accelerometer to active mode
mma8452qModule.start();
import com.mbientlab.metawear.module.Mma8452qAccelerometer.Orientation;

mwBoard.getModule(Mma8452qAccelerometer.class).routeData()
    .fromOrientation().stream("orientation_stream")
.commit().onComplete(new CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
        result.subscribe("orientation_stream", new MessageHandler() {
            @Override
            public void process(Message msg) {
                Log.i("MainActivity", msg.getData(Orientation.class).toString());
            }
        });
    }
});

Shake Detection

Shake detection uses the chips pre-programmed algorithms to detect shakes. You use a ShakeConfigEditor to configure shake detection. The shake data source is selected from the fromShake method and data can be interpreted as a MovementData object.

import com.mbientlab.metawear.module.Mma8452qAccelerometer.Axis;

Mma8452qAccelerometer mma8452qModule= mwBoard.getModule(Mma8452qAccelerometer.class);
// configure shake detection to monitor the X axis
mma8452qModule.configureShakeDetection().setAxis(Axis.X)
    .commit();
mma8452qModule.enableShakeDetection();

// Switch the accelerometer to active mode
mma8452qModule.start();
import com.mbientlab.metawear.module.Mma8452qAccelerometer.MovementData;

mwBoard.getModule(Mma8452qAccelerometer.class).routeData()
    .fromShake().stream("shake_stream")
.commit().onComplete(new CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
        result.subscribe("shake_stream", new MessageHandler() {
            @Override
            public void process(Message msg) {
                Log.i("MainActivity", msg.getData(MovementData.class).toString());
            }
        });
    }
});