General

Decoding MetaWear APIs Using BLE To Read Gyro Sensor

Pradeep V R

08 Mar 2017

Decoding MetaWear APIs Using BLE To Read Gyro Sensor

In the previous article we understood how to interact with MetaWear hardware by reading pulse sensor data; now let’s see how we can read data from gyroscope of the hardware. Like pulse sensor, we can read data from gyro in two ways with MetaWear APIs and without MetaWear APIs. Let’s understand both the ways with step by step procedure.

  • Using MetaWear APIs

Below is the step by step procedure to read gyro data using MetaWear API

1. Set output data rate and angular range    

Gyro gyroModule = mwBoard.getModule(Gyro.class);
 
 gyroModule.setOutputDataRate(25f);
 
 gyroModule.setAngularRateRange(125f);
 
 

2. Set the handlers to configure gyro and manage stream

AsyncOperation routeManagerResult = gyroModule.routeData()
 
   .fromAxes()
 
   .process(new Time(Time.OutputMode.ABSOLUTE, 1000))
 
   .stream(“gyro_stream”)
 
   .commit();
 
   routeManagerResult.onComplete(dataStreamManager);
 
 

3. Subscribe for the gyro data and parse the response ​

protected final AsyncOperation.CompletionHandler dataStreamManager = new AsyncOperation.CompletionHandler() {
 
   @Override
 
   public void success(RouteManager result) {
 
    result.subscribe(streamKey, new RouteManager.MessageHandler() {
 
   @Override
 
   public void process(Message message) {
 
  final CartesianFloat spin = message.getData(CartesianFloat.class);
 
  float xAxis = spin.x();
 
  float yAxis = spin.y();
 
  float zAxis = spin.z();
 
   }
 
    });
 
   }
 
 };
 
 
 
 

4. Disable gyro sensor and stop reading data from the hardware

gyroModule.stop();
  • Without using MetaWear APIs

1. Set output data rate

Input – 13032600, where 13 is gyro module, 03 is config opcode, 26 is output data rate (38 hz), 00 is angular data range.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

 characteristic uuid: 326a9001-85cb-9195-d9dd-464cfbbae75a

 write type: 1

    2. Set angular data range  

Input – 13032604, where 13 is gyro module, 03 is config opcode, 26 is output data rate (38 hz), 04 is index of angular data range from the array { 2000f, 1000f, 500f, 250f, 125f }.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

  characteristic uuid: 326a9001-85cb-9195-d9dd-464cfbbae75a

  write type: 1

 3. Enable data output  

Input – 130501, where 13 is gyro module, 05 is data opcode, 01 is flag to enable data output.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

  characteristic uuid: 326a9001-85cb-9195-d9dd-464cfbbae75a

  write type: 1

4. Enable data interrupt

Input – 13020100, where 13 is gyro module, 02 is data interrupt enable opcode, 0100 is flag to enable data interrupt.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

  characteristic uuid: 326a9001-85cb-9195-d9dd-464cfbbae75a

  write type: 1

     5. Enable Power

Input – 130101, where 13 is gyro module, 01 is power mode opcode, 01 is flag to enable power mode.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

  characteristic uuid: 326a9001-85cb-9195-d9dd-464cfbbae75a

  write type: 1

6. Receive gyro values – After executing above 5 steps sequentially onCharacteristicsChanged will be called which contains gyro sensor data.​

        Output – 1305B3FE7FFF6F00, 13 is gyro module, 05 is data opcode, next 2 bytes (B3FE) is the x-axis value. Since data is in little endian format hex value is FEB3, equivalent short value is -333; next 2 bytes (7FFF) is the y-axis value. Since data is in little endian format hex value is FF7F, equivalent short value is -129; next 2 bytes (6F00) is the z-axis value. Since data is in little endian format hex value is 006F, equivale short value is 28416.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

  characteristic uuid: 326a9006-85cb-9195-d9dd-464cfbbae75a

     7. Stop receiving gyro values – To stop the gyro sensor, we should execute last 3 steps (which we did to enable gyro) in the reverse order. So first step in this process is to disable power.​

        Input – 130100, where 13 is gyro module, 01 is power mode opcode, 00 is flag to disable power mode.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

  characteristic uuid: 326a9001-85cb-9195-d9dd-464cfbbae75a

  write type: 1

 8. Disable data interrupt​

        Input – 13020000, where 13 is gyro module, 02 is data interrupt enable opcode, 0000 is flag to disable data interrupt.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

  characteristic uuid: 326a9001-85cb-9195-d9dd-464cfbbae75a

  write type: 1

     9. Disable data output

        Input – 130500, where 13 is gyro module, 05 is data opcode, 00 is flag to disable data output.

service uuid: 326a9000-85cb-9195-d9dd-464cfbbae75a

  characteristic uuid: 326a9001-85cb-9195-d9dd-464cfbbae75a

  write type: 1