Calculating raw gyro values from MPU9255

Hi!

I have a MPU9255 and I am trying to calculate the gyro angles. I am using this library GitHub - Bill2462/MPU9255-Arduino-Library: Simple arduino library for MPU9255 sensor and with the example rawData I get raw data as labeled and using the dataProssesing example I messure DPS. The sketch I am trying to work out is this one:

/*
Data processing example
This example shows how to process raw readings from the sensors to get acceleration in m/s^2,
angular velocity in dps (degrees per second) and magnetic flux density in T (Tesla).
*/

#include <MPU9255.h>// include MPU9255 library

MPU9255 mpu;

//process raw gyroscope data
//input = raw reading from the sensor, sensor_scale = selected sensor scale
//returns : angular velocity in degrees per second
double process_angular_velocity(int16_t input, scales sensor_scale )
{
  /*
  To get rotation velocity in dps (degrees per second), each reading has to be divided by :
   -> 131   for +- 250  dps scale (default value)
   -> 65.5  for +- 500  dps scale
   -> 32.8  for +- 1000 dps scale
   -> 16.4  for +- 2000 dps scale
  */

  //for +- 250 dps
  if(sensor_scale == scale_250dps)
  {
    return input/131;
  }

  //for +- 500 dps
  if(sensor_scale == scale_500dps)
  {
    return input/65.5;
  }

  //for +- 1000 dps
  if(sensor_scale == scale_1000dps)
  {
    return input/32.8;
  }

  //for +- 2000 dps
  if(sensor_scale == scale_2000dps)
  {
    return input/16.4;
  }

  return 0;
}

void setup() {
  Serial.begin(115200);// initialize Serial port

  if(mpu.init())
  {
  Serial.println("initialization failed");
  }
  else
  {
  Serial.println("initialization successful!");
  }
  
}

void loop() {
  //take readings
  mpu.read_gyro();

  ////process and print gyroscope data////
  //X axis
  Serial.print("      GX: ");
  Serial.print(process_angular_velocity(mpu.gx,scale_250dps));

  //Y axis
  Serial.print("  GY: ");
  Serial.print(process_angular_velocity(mpu.gy,scale_250dps));

  //Z axis
  Serial.print("  GZ: ");
  Serial.print(process_angular_velocity(mpu.gz,scale_250dps));
  Serial.println();
}

How do I mesure the current angle by the DPS? Do I collect all the DPS values in an array and take the avrage? Or some different connection between the current angle and DPS?

Thanks beforehand!
Best regards Max

1 Like

In theory, it's the degrees per second values, multiplied by the number of seconds.

In practice, however...

But what if I rotate it 30 degrees and then make it stay there it would go back to 0 right?
Heres an output of the Serial Monitor:

AX: -0.82  AY: -0.29  AZ: 9.74      GX: 0.00  GY: 0.00  GZ: -2.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: -0.74  AY: -0.39  AZ: 9.84      GX: 0.00  GY: -3.00  GZ: -5.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: -0.45  AY: -0.41  AZ: 9.58      GX: 0.00  GY: -3.00  GZ: 0.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: -0.21  AY: -0.52  AZ: 9.40      GX: 0.00  GY: -14.00  GZ: 2.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: -0.39  AY: -0.15  AZ: 10.37      GX: 4.00  GY: -53.00  GZ: 7.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 2.02  AY: -0.01  AZ: 9.30      GX: 4.00  GY: -83.00  GZ: -4.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 2.96  AY: -0.07  AZ: 8.83      GX: 1.00  GY: -45.00  GZ: -2.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 3.83  AY: -0.32  AZ: 8.04      GX: -1.00  GY: -59.00  GZ: 0.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 5.38  AY: -0.31  AZ: 7.40      GX: 0.00  GY: -95.00  GZ: 0.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 5.45  AY: -0.20  AZ: 8.52      GX: -1.00  GY: -32.00  GZ: 0.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 5.74  AY: -0.65  AZ: 7.96      GX: 1.00  GY: -6.00  GZ: -4.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 6.26  AY: -0.29  AZ: 7.29      GX: -1.00  GY: 9.00  GZ: -7.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 6.39  AY: 0.97  AZ: 2.06      GX: -35.00  GY: 250.00  GZ: 8.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 3.38  AY: -1.28  AZ: 4.51      GX: -8.00  GY: 32.00  GZ: -16.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 0.13  AY: -0.19  AZ: 10.07      GX: -1.00  GY: 0.00  GZ: 0.00      MX: -0.15  MY: -0.15  MZ: -0.15
AX: 0.24  AY: -0.35  AZ: 9.78      GX: 1.00  GY: 1.00  GZ: -3.00

The magnetometer doesnt work for some reason I belive. But I twisted it in the middle witch you can see here. But f I twist it and stop at 30 degrees the DPS will be 0 and the degree will be 0 right?

I'm going to guess a rotation about the Y axis.
You're missing timestamps.

Yes, the rotation values should return to zero when at rest.

It was the Y axis, GY. I ran it again with timestamps. This is ofcourse a very small part of the output. Sins theres "a lot" of data:

13:29:13.372 -> AX: 0.66  AY: -0.37  AZ: 9.73      GX: -1.00  GY: -3.00  GZ: -3.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.372 -> AX: 0.59  AY: -0.35  AZ: 9.75      GX: -1.00  GY: -4.00  GZ: -3.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.372 -> AX: 0.66  AY: -0.32  AZ: 9.91      GX: -2.00  GY: -6.00  GZ: -1.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.372 -> AX: 0.74  AY: -0.19  AZ: 9.91      GX: 0.00  GY: -5.00  GZ: 1.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.372 -> AX: 0.53  AY: -0.11  AZ: 10.25      GX: -2.00  GY: -13.00  GZ: 4.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.419 -> AX: 0.57  AY: -0.23  AZ: 10.07      GX: -2.00  GY: -19.00  GZ: 6.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.419 -> AX: 0.57  AY: -0.30  AZ: 9.99      GX: -2.00  GY: -27.00  GZ: 4.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.419 -> AX: 0.73  AY: -0.28  AZ: 9.67      GX: -4.00  GY: -23.00  GZ: 7.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.419 -> AX: 0.87  AY: -0.37  AZ: 9.57      GX: -1.00  GY: -22.00  GZ: 3.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.419 -> AX: 0.85  AY: -0.48  AZ: 9.71      GX: -2.00  GY: -19.00  GZ: 4.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.419 -> AX: 0.90  AY: -0.51  AZ: 9.64      GX: -4.00  GY: -15.00  GZ: 3.00      MX: -0.15  MY: -0.15  MZ: -0.15
13:29:13.466 -> AX: 0.80  AY: -0.45  AZ: 9.81      GX: -2.00  GY: -14.00  GZ: 4.00      MX: -0.15  MY: -0.15  MZ: -0.15

But if the value is 0 at rest it will give a "not true" value. It will say that the current angle is 0?

The word is RATE.

Rate Gyro.

A rate gyro is a type of gyroscope, which rather than indicating direction, indicates the rate of change of angle with time. If a gyro has only one gimbal ring, with consequently only one plane of freedom, it can be adapted for use as a rate gyro to measure a rate of angular movement.

Is it the time between every measurement or time from start? The derivative between each measurement ?

interesting. Do you belive you can still calculate the current angle with it?

Calculate angle using gyro data? Gyro data is not required for an angle measurement.

Have you tried using the words "arduino calculate imu angle formula" in an internet search?

Yes I have. I can only find information about the MPU9250, but theres nothing with the MPU9255. On my module it is labeled MPU 9250 / 6050 but my research on the internet says that it is a MPU9250 for that reason.

There is plenty of information on the web for the MPU-9255.

Data sheet here: https://stanford.edu/class/ee267/misc/MPU-9255-Datasheet.pdf

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.