Accelerometer

Note: The code snippets in this article are intended for Mita version 1a1ed7881e2220895ec60ce4f657972538c43195 and XDK-Workbench versions 3.4.0 and higher.

This article introduces the configuration and use of the accelerometer sensor in Mita. The article’s focus is on the software and API provided by the platform. For more specific information about the hardware itself, specifically the accelerometer sensor BMA280 used by the XDK, please read the corresponding BMA280 datasheet.

  1. Accelerometer API Overview
  2. Available Accelerometer Resources
  3. Reading Accelerometer Data
  4. Configuring the Accelerometer Sensor
    1. BMA280
  5. Full Code Example
  6. Appendix
    1. XDK Console Output example

Accelerometer API Overview

Mita provides the possibility to use the accelerometer sensor resources without setting them up beforehand. If the configuration and implementation provided by the Mita implementation are insufficient for a certain purpose, it is recommended to make adapting changes in the generated C code.

The accelerometer implementation in Mita is based on the Platform Sensor library, which is included in the XDK SDK. It contains modules for a range of sensors and virtual sensors. The image below illustrates the sensor stack.

Image

Available Accelerometer Resources

The alias accelerometer refers to the BMA280 sensor. The alias can not be changed, but the XDK accelerometer can be used with the respective handle:

  • Accelerometer_BMA280

Reading Accelerometer Data

Reading the accelerometer data is fairly simple, as it does not require any kind of initialization beforehand.

The following code will read the three axes of the accelerometer sensor and print the values to the console every 100 milliseconds.

every 100 milliseconds {
    var x = accelerometer.x_axis.read();
    var y = accelerometer.y_axis.read();
    var z = accelerometer.z_axis.read();
    println(`Accelerometer output: \n x: ${x}mg\n y: ${y}mg\n z: ${z}mg`);
}

Configuring the Accelerometer Sensor

This section provides a brief overview of the possible settings for the accelerometer sensor. For more information on specific settings, please refer to the BMA280 datasheet.

BMA280

The BMA280 can be configured as follows:

setup accelerometer {
    bandwidth = BW_500Hz;
  range = Range_8G;     // 8G range conveniently does not require a conversion into mg
}

The configuration-item bandwidth can be set to one of the following values:

  • BW_7_81Hz, sets bandwidth to LowPass 7.81 HZ
  • BW_15_63Hz, sets bandwidth to LowPass 15.63 HZ
  • BW_31_25Hz, sets bandwidth to LowPass 31.25 HZ
  • BW_62_50Hz, sets bandwidth to LowPass 62.50 HZ
  • BW_125Hz, sets bandwidth to LowPass 125 HZ
  • BW_250Hz, sets bandwidth to LowPass 250 HZ
  • BW_500Hz, sets bandwidth to LowPass 500 HZ
  • BW_1000Hz, sets bandwidth to LowPass 1000 HZ

The configuration-item range can be set to one of the following values, otherwise defaults to 2G mode:

  • Range_2G, sets range to +/- 2G mode
  • Range_4G, sets range to +/- 4G mode
  • Range_8G, sets range to +/- 8G mode
  • Range_16G, sets range to +/- 16G mode

Full Code Example

package main;
import platforms.xdk110;

setup accelerometer {
    bandwidth = BW_500Hz;
    range = Range_8G;       // 8G range convienently does not require a conversion into mg
}

every 100 milliseconds {
    var x = accelerometer.x_axis.read();
    var y = accelerometer.y_axis.read();
    var z = accelerometer.z_axis.read();
    println(`Accelerometer output: \n x: ${x}mg\n y: ${y}mg\n z: ${z}mg`);
}

Appendix

XDK Console Output example

The following console log is an example output of the full code example above.

Image