Accelerometer

This chapter introduces the configuration and use of the accelerometer BMA280, as well as the accelerometer component of the BMI160 on the XDK via the accelerometer interface. It shows how to initialize and read the measured sensor data, and it also explains sensor specific presettings.

For more specific information about the BMA280 and the BMI160, please take a look at the corresponding datasheet.

  1. Accelerometer API Overview
  2. Initializing the acceleration sensor
  3. Configuring the Presettings
  4. Reading sensor data
  5. Full Code Example
  6. Appendix
    1. XDK Console Output example

Accelerometer API Overview

It is generally recommended to develop an application based on the highest API level the XDK framework supports, although it is possible to access deeper API levels if this is not enough for the distinct purpose.

Image

Initializing the acceleration sensor

Before we start with the implementation of the accelerometer (BMA280 or BMI160), the sensor handling header file has to be included, which is XdkSensorHandle.h. This header file provides the interfaces for every sensor type, in this case, BCDS_Accelerometer.h. Afterwards the function Accelerometer_init() is used to initialize the sensor with the correct sensor specific handler.

/* Interface for the sensors on the XDK */

#include "XdkSensorHandle.h"

Accelerometer_init(xdkAccelerometers_BMA280_Handle);

The particular sensor specific handler can be found within XdkSensorHandle.h. It contains the sensor specific handler for every sensor type on the XDK.

/************************************************************
*   XdkSensorHandle.h
************************************************************/

/* Sensor Handler for the BMA280 Accelerometer */
extern Accelerometer_HandlePtr_T xdkAccelerometers_BMA280_Handle;

/* Sensor Handler for the BMI160 Accelerometer */
extern Accelerometer_HandlePtr_T xdkAccelerometers_BMI160_Handle;

Configuring the Presettings

This short overview gives an example about how to configure the accelerometer with presettings. It is of course possible to configure the accelerometer without modifying the settings, too.

The functions Accelerometer_setBandwidth() and Accelerometer_setRange() set a specific bandwidth and sensor range for the accelerometer. Please keep in mind that before any configuration can be changed, the accelerometer needs to be initialized first.

Accelerometer_setBandwidth(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_BANDWIDTH_125HZ);

Accelerometer_setRange(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_RANGE_2G);

The table below contains some of the configuration functions of the accelerometer which are available in the interface BCDS_Accelerometer.h.

FunctionEffect
Accelerometer_setBandwidth()Configures the accelerometer with the given bandwidth enumeration.
Accelerometer_getBandwidth()Returns the configured bandwidth of the accelerometer in a passed reference variable.
Accelerometer_setRange()Configures the accelerometer with the given range enumeration.
Accelerometer_getRange()Returns the configured range of the accelerometer in a passed reference variable.
Accelerometer_setMode()Configures the accelerometer with the given power mode enumeration.
Accelerometer_getMode()Returns the configured power mode of the accelerometer in a passed reference variable.
Accelerometer_setSleepDuration()Configures the accelerometer with the given sleep duration.
Accelerometer_getSleepDuration()Returns the configured sleep duration of the accelerometer in a passed reference variable.

A more detailed explanation can be found in the mentioned interface BCDS_Accelerometer.h.

Reading sensor data

This section describes how to read data from the accelerometer. First a storage struct for every axis’ acceleration data needs to be declared. The data is then read by the Accelerometer_readXyzGValue() function and stored in a passed reference of the declared storage struct.

Accelerometer_XyzData_T bma280 = {INT32_C(0), INT32_C(0), INT32_C(0)};

memset(&bma280, 0, sizeof(Accelerometer_XyzData_T));

Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

Accelerometer data can be read by using two different function calls. These functions provide data which differs in its display unit. The first of the two functions provides data that is measured in earth acceleration g. Additionally the physical data is stored in 32 bit integers and therefore measured in milli g. The other function provides the analog sensor data converted into its raw digital representation.

Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

Accelerometer_readXyzLsbValue(xdkAccelerometers_BMA280_Handle,&bma280);

Full Code Example

Note: The full code example is intended for XDK-Workbench versions 3.4.0 and higher.

/*----------------------------------------------------------------------------*/

/* --------------------------------------------------------------------------- |
 * INCLUDES & DEFINES ******************************************************** |
 * -------------------------------------------------------------------------- */

#include "XdkAppInfo.h"
#undef BCDS_MODULE_ID  // [i] Module ID define before including Basics package
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER
#include "AppController.h"
#include <stdio.h>
#include "BCDS_CmdProcessor.h"
#include "FreeRTOS.h"
#include "XdkSensorHandle.h"
#include "timers.h"

/* --------------------------------------------------------------------------- |
 * HANDLES ******************************************************************* |
 * -------------------------------------------------------------------------- */

static CmdProcessor_T * AppCmdProcessor;
xTimerHandle accelerometerHandle = NULL;

/* --------------------------------------------------------------------------- |
 * EXECUTING FUNCTIONS ******************************************************* |
 * -------------------------------------------------------------------------- */

 static void readAccelerometer(xTimerHandle xTimer)
 {
     (void) xTimer;

     Retcode_T returnValue = RETCODE_FAILURE;

     /* read and print BMA280 accelerometer data */

     Accelerometer_XyzData_T bma280 = {INT32_C(0), INT32_C(0), INT32_C(0)};
     memset(&bma280, 0, sizeof(CalibratedAccel_XyzMps2Data_T));

     returnValue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

     if (RETCODE_OK == returnValue) {
         printf("BMA280 Acceleration Data - M/S2 %f m/s2 %f m/s2 %f m/s2\n\r",
             (float) bma280.xAxisData, (float) bma280.yAxisData, (float) bma280.zAxisData);
     }
 }

static void initAccelerometer(void)
{
    Retcode_T returnValue = RETCODE_FAILURE;
    Retcode_T returnBandwidthValue = RETCODE_FAILURE;
    Retcode_T returnRangeValue = RETCODE_FAILURE;

    /* initialize accelerometer */

    returnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);

    if ( RETCODE_OK != returnValue) {
        printf("BMA280 Accelerometer initialization failed\n\r");
    }

    returnBandwidthValue = Accelerometer_setBandwidth(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_BANDWIDTH_125HZ);

    if (RETCODE_OK != returnBandwidthValue) {
        printf("Configuring bandwidth failed \n\r");
    }
    returnRangeValue = Accelerometer_setRange(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_RANGE_2G);

    if (RETCODE_OK != returnRangeValue) {
        printf("Configuring range failed \n\r");
    }
}

/* --------------------------------------------------------------------------- |
 * BOOTING- AND SETUP FUNCTIONS ********************************************** |
 * -------------------------------------------------------------------------- */

 static void AppControllerEnable(void * param1, uint32_t param2)
 {
     BCDS_UNUSED(param1);
     BCDS_UNUSED(param2);

     uint32_t timerBlockTime = UINT32_MAX;

     xTimerStart(accelerometerHandle,timerBlockTime);
 }

 static void AppControllerSetup(void * param1, uint32_t param2)
 {
     BCDS_UNUSED(param1);
     BCDS_UNUSED(param2);
     Retcode_T retcode = RETCODE_OK;

     uint32_t oneSecondDelay = UINT32_C(1000);
     uint32_t timerAutoReloadOn = UINT32_C(1);

     initAccelerometer();

     /* Creation and start of the timer task */
     accelerometerHandle = xTimerCreate((const char *) "readAcclerometer", oneSecondDelay,timerAutoReloadOn, NULL, readAccelerometer);

     retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
     if (RETCODE_OK != retcode)
     {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0);

        /* To provide LED indication for the user */
     }
 }

 void AppController_Init(void * cmdProcessorHandle, uint32_t param2)
 {
     BCDS_UNUSED(param2);

     Retcode_T retcode = RETCODE_OK;

     if (cmdProcessorHandle == NULL)
     {
         printf("AppController_Init : Command processor handle is NULL \r\n");
         retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);
     }
     else
     {
         AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;
         retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));
     }

     if (RETCODE_OK != retcode)
     {
         Retcode_RaiseError(retcode);
         assert(0); /* To provide LED indication for the user */
     }
 }
/*----------------------------------------------------------------------------*/

Appendix

XDK Console Output example

The following console log is an example output of the code that has been implemented in the Sensors Accelerometer example guide:

Image