bolderflight / invensense-imu

Arduino and CMake library for communicating with the InvenSense MPU-6500, MPU-9250 and MPU-9255 nine-axis IMUs.
MIT License
498 stars 211 forks source link

Accelerometer and Gyroscope readings are reversed. #39

Closed nickgoza closed 5 years ago

nickgoza commented 5 years ago

Hello,

Thank you all for the information on this topic.

I have modified the advanced I2C to add a filter to stabilize the data and wrote test code to graph each axis (one program per axis per reading) in real time for testing. I've found that my Accelerometer readings are reading the Gyro (it is still displaying with the Accelerometer units) and my Gyro is reading acceleration in radians.

My functions are correct (I did not put the wrong one in). I am using an Arduino Uno with a sparkfun MPU-9250. I believe this has something to do with the header file, but I am not experienced enough to identify what I would need to interchange. Can I get some help on this?

Let me know what I can provide to be helpful.

Thank you, Nick

nickgoza commented 5 years ago

As an afterthought, it might be helpful to mention this applies to the X,Y, and Z axis on both

flybrianfly commented 5 years ago

Hi Nick,

Can you attach your code?

Thanks, Brian

nickgoza commented 5 years ago

Here is my code to test the X axis on the Gyro. This one is showing accelerometer results, as in the value increases when I put the unit into motion and goes back to zero when I stop regardless of its rotational position. It does this without my filter added as well.

The accelerometer is the exact same code except with the function for getting accelerometer data. It works well with the magnetometer and the filter give a nice smooth curve with the filter added.

Forgive me if this doesn't show up right I am new to Github, but here is my code.

`#include "MPU9250.h"

// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68 MPU9250 IMU(Wire,0x68); int status;

void setup() { Serial.begin(115200); while(!Serial) {}

// start communication with IMU status = IMU.begin(); if (status < 0) { Serial.println("IMU initialization unsuccessful"); Serial.println("Check IMU wiring or try cycling power"); Serial.print("Status: "); Serial.println(status); while(1) {} } // setting the accelerometer full scale range to +/-8G IMU.setAccelRange(MPU9250::ACCEL_RANGE_8G); // setting the gyroscope full scale range to +/-500 deg/s IMU.setGyroRange(MPU9250::GYRO_RANGE_1000DPS); // setting DLPF bandwidth to 20 Hz IMU.setDlpfBandwidth(MPU9250::DLPF_BANDWIDTH_20HZ); // setting SRD to 19 for a 50 Hz update rate IMU.setSrd(19); }

//Constants for RAF const int RunningAverageCount=16; float RunningAverageBuffer[RunningAverageCount]; int NextRunningAverage;

void loop() { // read the sensor IMU.readSensor();

//Running Average Filter-------------------------------------------------------------------------- float rawGyroX = IMU.getGyroX_rads();

RunningAverageBuffer[NextRunningAverage++] = rawGyroX; if (NextRunningAverage >= RunningAverageCount) { NextRunningAverage=0; } float RunningAverageGyroX = 0; for(int i=0; i<RunningAverageCount; ++i) { RunningAverageGyroX += RunningAverageBuffer[i]; } RunningAverageGyroX /= RunningAverageCount; //----------------------------------------------------------------------------------------------

//Print Data Serial.print(RunningAverageGyroX,6); Serial.print("\n"); delay(50); }`

flybrianfly commented 5 years ago

Hi Nick, A gyro measures rotational rate (i.e. deg/s or rad/s), not rotational position (i.e. deg or rad). So, I would expect the gyro to give a value when it is in motion and 0 when it is at rest. I was able to compile and run your code on a Teensy and the _IMU.getGyroXrads() method is returning the gyro value. I can tell that it is the gyro value and not the accelerometer, because if I leave the X axis pointed up or down it is returning 0 still (no motion) rather than +/- 9.8 (apparent acceleration due to gravity).

In order to estimate rotational position, you'll need something like a complimentary, kalman, or extended kalman filter.

Brian

nickgoza commented 5 years ago

Wow, everything makes sense now...I misunderstood what the sensors did...haha. I am new to this. Thank you for your help and your code has been very helpful to me!

Nick