.. highlight:: cpp Accelerometer ============= All boards come with an accelerometer. The specific accelerometer model varies amongst the boards, howevever the API provides accelerometer agnostic functions in the `accelerometer.h `_ header file that can be safely used with all supported accelerometers. Users can check which accelerometer is on their board at runtime to determine the appropriate accelerometer specific functions they need to use, if necessary. :: #include "metawear/core/module.h" #include "metawear/sensor/accelerometer_bosch.h" #include "metawear/sensor/accelerometer_mma8452q.h" void check_acc_type(MblMwMetaWearBoard* board) { auto acc_type= mbl_mw_metawearboard_lookup_module(board, MBL_MW_MODULE_ACCELEROMETER); switch(acc_type) { case MBL_MW_MODULE_TYPE_NA: // should never reach this case statement printf("No accelerometer on this board\n"); break; case MBL_MW_MODULE_ACC_TYPE_MMA8452Q: printf("Acc Type = MMA8452Q\n"); break; case MBL_MW_MODULE_ACC_TYPE_BMI160: printf("Acc Type = BMI160\n"); break; case MBL_MW_MODULE_ACC_TYPE_BMA255: printf("Acc Type = BMA255\n"); break; default: printf("Unknown type\n"); break; } Configuration ------------- Users can configure the output data rate and the sampling range; these parameters control the sampling rate and the data range and resolution respectively. After configuring the settings, call `mbl_mw_acc_write_acceleration_config `_ to write the configuration to the sensor. :: #include "metawear/sensor/accelerometer.h" void configure_acc(MblMwMetaWearBoard* board) { // Set ODR to 25Hz or closest valid frequency mbl_mw_acc_set_odr(board, 25.f); // Set range to +/-4g or closest valid range mbl_mw_acc_set_range(board, 4.f); // Write the config to the sensor mbl_mw_acc_write_acceleration_config(board); } Acceleration Sampling --------------------- Acceleration sampling measures the current acceleration forces at periodic intervals. To enable acceleration sampling, call `mbl_mw_acc_enable_acceleration_sampling `_ before starting the accelerometer. Linear acceleration is represented with the `MblMwCartesianFloat `_ struct and the values are in units of Gs. The ``x``, ``y``, and ``z`` fields contain the acceleration in that direction. :: #include "metawear/core/datasignal.h" #include "metawear/core/types.h" #include "metawear/sensor/accelerometer.h" void enable_acc_sampling(MblMwMetaWearBoard* board) { auto data_handler = [](const MblMwData* data) -> void { // Cast value to MblMwCartesianFloat* auto acceleration = (MblMwCartesianFloat*) data->value; printf("(%.3f, %.3f, %.3f)g\n", acceleration->x, acceleration->y, acceleration->z); }; auto acc_signal= mbl_mw_acc_get_acceleration_data_signal(board); mbl_mw_datasignal_subscribe(acc_signal, data_handler); mbl_mw_acc_enable_acceleration_sampling(board); mbl_mw_acc_start(board); }