Gyroscope

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 gyroscope 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 gyroscope sensors BMI160 and BMG160 used by the XDK, please read the corresponding BMI160 datasheet or the BMG160 datasheet.

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

Gyroscope API Overview

Mita provides the possibility to use the gyroscope 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 gyroscope 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 Gyroscope Resources

The standard alias gyroscope refers to GyroscopeSensorFusion. The alias can not be changed, but the XDK gyroscopes can be used without SensorFusion using the respective handles:

  • Gyroscope_BMG160
  • Gyroscope_BMI160

As such, whenever gyroscope is used in this guide, then gyroscope can be substituted with one of the listed handles.

Reading Gyroscope Data

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

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

every 100 milliseconds {
    var x = gyroscope.x_axis.read();
    var y = gyroscope.y_axis.read();
    var z = gyroscope.z_axis.read();
  println(`x: ${x} y: ${y} z: ${z}  [deg/s]`);
}

Configuring the Gyroscope Sensor

This section provides a brief overview of the possible configuration for the gyroscope sensor. For more information on specific settings, please refer to the BMI160 datasheet or the BMG160 datasheet.

BMG160

The BMG160 can be configured as follows:

setup Gyroscope_BMG160 {
  range = Range_250s;
  bandwidth = Bw_12Hz;
}

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

  • Bw_12Hz, sets bandwidth to LowPass 12 HZ
  • Bw_23Hz, sets bandwidth to LowPass 23 HZ
  • Bw_32Hz, sets bandwidth to LowPass 32 HZ
  • Bw_47Hz, sets bandwidth to LowPass 47 HZ
  • Bw_64Hz, sets bandwidth to LowPass 64 HZ
  • Bw_116Hz, sets bandwidth to LowPass 116 HZ
  • Bw_230Hz, sets bandwidth to LowPass 230 HZ
  • Bw_523Hz, sets bandwidth to LowPass 523 HZ

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

  • Range_125s, sets to 0.0625 deg/s resolution in 125 deg/s range
  • Range_250s, sets to 0.125 deg/s resolution in 250 deg/s range
  • Range_500s, sets to 0.25 deg/s resolution in 500 deg/s range
  • Range_1000s, sets to 0.5 deg/s resolution in 1000 deg/s range
  • Range_2000s, sets to 1 deg/s resolution in 2000 deg/s range

BMI160

The BMI160 can be configured as follows:

setup Gyroscope_BMI160 {
  range = Range_250s;
  bandwidth = BW_10_7Hz;
}

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

  • BW_10_7Hz, sets bandwidth to LowPass 10.7 HZ
  • BW_20_8Hz, sets bandwidth to LowPass 20.8 HZ
  • BW_39_9Hz, sets bandwidth to LowPass 39.9 HZ
  • BW_74_6Hz, sets bandwidth to LowPass 74.6 HZ
  • BW_136_6Hz, sets bandwidth to LowPass 136.6 HZ
  • BW_254_6Hz, sets bandwidth to LowPass 254.6 HZ
  • BW_523_9Hz, sets bandwidth to LowPass 523.9 HZ
  • BW_890Hz, sets bandwidth to LowPass 890 HZ

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

  • Range_125s, sets to 0.0625 deg/s resolution in 125 deg/s range
  • Range_250s, sets to 0.125 deg/s resolution in 250 deg/s range
  • Range_500s, sets to 0.25 deg/s resolution in 500 deg/s range
  • Range_1000s, sets to 0.5 deg/s resolution in 1000 deg/s range
  • Range_2000s, sets to 1 deg/s resolution in 2000 deg/s range

Full Code Example

package main;
import platforms.xdk110;

setup Gyroscope_BMI160 {
  range = Range_250s;
  bandwidth = BW_10_7Hz;
}

every 100 milliseconds {
    var x = gyroscope.x_axis.read();
    var y = gyroscope.y_axis.read();
    var z = gyroscope.z_axis.read();
    println(`x: ${x} y: ${y} z: ${z}  [deg/s]`);
}

Appendix

XDK Console Output example

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

Image