Help needed with Integration of MPU values.

Hi everyone :slight_smile: ,

Before I begin I am very new to Arduino and programming in general. This is for a university project and have never completed anything like this before.

I have managed to connect an MPU 6050, Arduino Nano and Hc-05 together and can see the X,Y,Z acceleration in the serial monitor. Here is my current code to do this:

#include “I2Cdev.h” // include the I2Cdev library
#include “MPU6050.h” // include the accelerometer library
MPU6050 accelgyro; // set device to MPU6050
int16_t ax, ay, az, gx, gy, gz; // define accel as ax,ay,az
int baselineX = 0;
int baselineY = 0;
int baselineZ = 0;

void setup() {

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

Wire.begin(); // join I2C bus
Serial.begin(9600); // initialize serial communication
accelgyro.initialize(); // initialize the accelerometer
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
baselineX = ax;
baselineY = ay;
baselineZ = az;
}

void loop() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // read measurements from device
Serial.print((gx-baselineX));
//Serial.println((ax-baselineX)*0.05);
Serial.print("\t");
Serial.print(gx);
Serial.print("\t");
Serial.print(gy);
Serial.print("\t");
Serial.print(gz);

Serial.print("\t");
Serial.print("\t");
Serial.print((gy-baselineY));
//Serial.println((ax-baselineX)*0.05);

Serial.print("\t");
Serial.print("\t");
Serial.println((gz-baselineZ));
//Serial.println((ax-baselineX)*0.05);
delay(200);
}

I now want to integrate these values twice to determine velocity and displacement of the values ( I understand the issues of the Errors increasing by doing this but that doesn’t matter for this project). I was wondering if anyone knew of any code or could help explain some code that would let me do this?

I appreciate that there are lots of threads on this topic already however with my lack of knowledge I cannot figure out how to apply them to my code :confused:. Im sorry if i have made any mistakes already. Thank you for any help you can give.

Thanks!

Do you understand what is necessary to integrate the acceleration values to get velocity? Forget about integrating velocity to get displacement, for the moment.

You need a minimum of two sets of values at two known times. You currently have one set of values and no known time.

That won’t work, because consumer grade sensors are too noisy and inaccurate.

Read more about why it won’t work here: Using Accelerometers to Estimate Position and Velocity |

Please use code tags when posting code ("</>" button).

PaulS:
Do you understand what is necessary to integrate the acceleration values to get velocity? Forget about integrating velocity to get displacement, for the moment.

You need a minimum of two sets of values at two known times. You currently have one set of values and no known time.

I have a brief understanding of integration from highschool but not much more.

I had thought that the "delay(200)" would have logged the time intervals? Would I be able to use the previous serial monitor data data point as the initial values and the current serial monitor value as the second value?

Am I really far off?

Please use code tags when posting code ("</>" button).
[/quote]

Im sorry I was trying to figure out how to do that.

I was trying to figure out how to do that.

Explained in the "How to use this forum" post, at the head of every list of forum posts.

I had thought that the "delay(200)" would have logged the time intervals?

If the function were properly named, stickYourHeadInTheSandForAWhileDoingNothing(), would you still think that?

Would I be able to use the previous serial monitor data data point as the initial values and the current serial monitor value as the second value?

Well, sure. If you can figure out how to read from the write-only field. No one else has.

The ONLY data that shows up in the Serial Monitor app is data your program wrote there. If you want to reuse that data, you must remember it, somehow. That would be what variables are for.

PaulS:
If the function were properly named, stickYourHeadInTheSandForAWhileDoingNothing(), would you still think that?

Yeah Probably.

Am I really far off?

Yes.

For a "university project" most people would assume that some basic understanding and preparation is required.

It appears that you are lacking in that regard.