I am currently working on my own flight controller for fixed wing aircrafts. I am currently trying to implement roll stabilisation. I'm using an Arduino nano and the MPU6050 for this. For my flight controller I need the angles of the MPU6050 and the aircraft. For this I use the library "MPU6050_light". If I hold the sensor in my hand and rotate it around the roll or pitch axis, I get precise and correct values.
However, I have noticed one thing: If I only move the sensor horizontally, for example, without changing the angle of the sensor, the calculated angle still changes. I am not quite sure if this phenomenon is really related to my actual problem.
Because after a few seconds of flying my aircraft, the MPU6050 seems to produce completely wrong values. I can tell by the fact that the aircraft turns 180 degrees out of nowhere, for example, or goes into a dive without warning. I consider it impossible that this can be traced back to a bug in my code. Because on the ground, the angle calculation works perfectly. The steering surfaces move exactly as they should in order to compensate for a banked aircraft.
To solve the problem, I have already tried different libraries, but all of them seem to have the above mentioned phenomenon with the horizontal movement of the sensor, which is why I imagine that they cause the same nosedives to the plane as the library "MPU6050_light".
Could it basically be because the MPU6050 cannot calculate angles while the sensor is moving? Or could the accelerations make the angle calculation impossible?
No, but the calculation you do in your code might be incorrect if there are accelerations other than gravitation. I would expect the values from the sensor to be correct.
May I turn that question around ?
Why would it produce correct values ?
The genuine MPU-6050 is no longer made, it is outdated. The genuine MPU-6050 was noisy and sometimes the module had the wrong capacitors which caused even more noise. So the genuine MPU-6050 is not very good, but if you bought your MPU-6050 on Ebay/Amazon/AliExpress, then it is a counterfeit. They sold you "something".
This would be possible. The MPU6050 was included with an Arduino box at the time. There were various sensors in this box. I think the quality was not very good.
But why were the values in the "resting state" (i.e. rotation of the MPU6050 but without moving around) too good?
What would be a suitable supplier for such sensors, if not Amazon?
Here is the link to my kit from back then, where the sensor was included:
That is the problem. That library grossly oversimplifies the problem, uses many crude and/or incorrect approximations and is (possibly) useful only for level, stable flight.
The AHRS code used in the library referenced in post #5 actually works, but do NOT rely on Euler angles for anything other than approximately level flight.
For attitude correction in extreme conditions, you must use the quaternion representation.
But why were the values in the "resting state" (i.e. rotation of the MPU6050 but without moving around) too good?
Not sure what you mean by "too good", but the acceleration values represent the direction of "down" only when gravity is the sole external acceleration.
As I said, the sensor must be correctly aligned, as I see the correct deflections on the control surfaces at rest. So if the aircraft is not moving, then it reacts completely correctly to tilts or other situations.
It is also noticeable that the aircraft flies normally for the first few seconds and only makes these extreme manoeuvres after 3-5 seconds.
Don't you think it's time to show us the code you base this statement on?
I assume it's your code that isn't acting correctly. And yes, if you use an inappropriate library it's also your code.
I will do that if it is absolutely necessary, however the code is spread all over my project. Therefore, it would save me a lot of time if we could solve the problem without my code. If that is not the case, I will make it available later.
As I said, on the ground, without external acceleration / movement, everything works wonderfully. And I don't know what changes in my code when the aircraft is in the air, except for a misbehaviour of the MPU6050.
I would like to ask you one more question: Does it make any sense at all to have the angle of the MPU6050 "calculated" over so many different stages. Or should I rather use the raw data of the gyroscope for stabilisation in general? So is it at all possible to reliably provide the angle of the aircraft with the MPU6050?
So far it has only been said that it is not possible with this library, but is there a library at all that makes this possible reliably?
What answer did I miss? You repeated that the "MPU6050_light" library is not suitable.
Are you talking about releasing my code? I don't see any value in that at the moment. Even if there was a bug in my code, the MPU6050 would prevent it from working correctly anyway. Well, we've already agreed that the library I've been using to determine the angle of the moving plane won't work. So this problem needs to be fixed first. And you don't need my code for that. Or am I wrong?
" consider that the longer your program, the harder it is for anyone to understand it and help you. For this reason, consider writing a short program that illustrates the problem and post or link to that instead. You are more likely to get help with a short, easy to understand program than with a long, complicated one. For more about this see How to create a Minimal, Reproducible Example .
I know what you mean. But what I find difficult with this problem is the reproduction. It's super difficult for me to reproduce this error because I have to fly an airplane to see the error. As I said, the error does not occur on the ground, but only in the air.
This is the code I'm trying to use to determine the angles of the MPU6050 (using the old MPU6050_light library):
#include "Wire.h"
#include <MPU6050_light.h>
MPU6050 mpu(Wire);
unsigned long timer = 0;
void setup() {
Serial.begin(115200);
Wire.begin();
byte status = mpu.begin();
Serial.print(F("MPU6050 status: "));
Serial.println(status);
while(status!=0){ } // stop everything if could not connect to MPU6050
Serial.println(F("Calculating offsets, do not move MPU6050"));
delay(1000);
mpu.calcOffsets(); // gyro and accelero
Serial.println("Done!\n");
}
void loop() {
mpu.update();
if((millis()-timer)>10){ // print data every 10ms
Serial.print("X : ");
Serial.print(mpu.getAngleX());
Serial.print("\tY : ");
Serial.print(mpu.getAngleY());
Serial.print("\tZ : ");
Serial.println(mpu.getAngleZ());
timer = millis();
}
}
I can't imagine that this will help solve the problem as it is simply the demo program of this library. But that's where the error appears to be.
Is that what you mean or did I misunderstand you?
But in the end it boils down to the same question again: In the current state and with the current library, the in-flight angle determination does not work.
What do I have to change / how do I have to use the mpu6050 to get the right angle data while flying
To check if my MPU6050 is possibly broken: Could one of you test the program and tell me if the angles also change for you if you don't turn / rotate the MPU6050 but just move it horizontally e.g. on the table?