Without overstepping, I would like to offer a few suggestions. First, set the filter window size. For example: #define WINDOW_SIZE 15 float pitchBuffer[WINDOW_SIZE] = {0}; float rollBuffer[WINDOW_SIZE] = {0}; float azimuthBuffer[WINDOW_SIZE] = {0}; int bufferIndex = 0; // It would be appropriate to keep the WINDOW_SIZE between 5 and 20. Then, add a moving average filter. For example: void applyMovingAverage(float &pitch, float &roll, float &azimuth) { pitchBuffer[bufferIndex] = pitch; rollBuffer[bufferIndex] = roll; azimuthBuffer[bufferIndex] = azimuth; bufferIndex = (bufferIndex + 1) % WINDOW_SIZE; pitch = 0; roll = 0; azimuth = 0; for (int i = 0; i < WINDOW_SIZE; i++) { pitch += pitchBuffer[i]; roll += rollBuffer[i]; azimuth += azimuthBuffer[i]; } pitch /= WINDOW_SIZE; roll /= WINDOW_SIZE; azimuth /= WINDOW_SIZE; } add loop: azimuth = atan2(correctedY, correctedX); azimuth += magneticDeclination; // Normalize azimuth to the 0-360 range if (azimuth < 0) azimuth += 2 * PI; if (azimuth > 2 * PI) azimuth -= 2 * PI; azimuth = azimuth * 180.0 / PI; // Convert to degrees // Apply moving average filter to pitch, roll, and azimuth applyMovingAverage(pitch, roll, azimuth); This way, you will be less affected by vibrations and quick direction changes. Please let me know the results if you try it. Best regards.
Wow this could not have come at a better time. I’m struggling with an MPU9250 for a project due on Friday. Can’t get the magnetometer to function properly to Measure the rotated angle. Thanks!
The MPU9250 has a similar structure, but the register addresses is different. I also had hard time in reading magnetometer reading from MPU9250. Tried multiple MPU9250, none of them.worked.
@@HowtoElectronicsoh dear this does not bode well.. possible to do sensor fusion only using the 6050? I just need it to give a reference point for a digital compass.
I got my MPU9250 to work ultimately, ended up using an I2C scanner to find the IMU, then TwoWire to run it in parallel to my oled screen. I used hideakitai's examples to connect and calibrate it first, then managed to work. One thing I discovered, ESP32-S3 has an issue with pin reassignment in V3 of the Board Manager. I had to downgrade to 2.0.14 to get it to work.
@HowtoElectronics you are partially correct.... Register addresses and set mode values aren't the Same..... And Thank you so so much... I really needed this video... For a long time, I was expecting someone to use the Kalman filter to fuse the accelerometer gyroscope and magnetometer sensor data... I couldn't see anyone doing it... So thank you again... And a small request (I know it's not small), If you can make code for an extended kalman filter to Fuse the mpu 6050 data and qmc 5883 data... It would be very useful to me and others as well. I was trying to make a model for EKF for a long time ... But I ended up failing...
Thank you for your detailed feedback and kind words! Regarding the Extended Kalman Filter (EKF) to fuse MPU6050 and QMC5883 data, I understand its importance. I will definitely give it a try, and if I can successfully implement it, I will post a video explaining the process step-by-step. Stay tuned, and I appreciate your patience!
Without overstepping, I would like to offer a few suggestions. First, set the filter window size. For example:
#define WINDOW_SIZE 15
float pitchBuffer[WINDOW_SIZE] = {0};
float rollBuffer[WINDOW_SIZE] = {0};
float azimuthBuffer[WINDOW_SIZE] = {0};
int bufferIndex = 0;
// It would be appropriate to keep the WINDOW_SIZE between 5 and 20.
Then, add a moving average filter. For example:
void applyMovingAverage(float &pitch, float &roll, float &azimuth) {
pitchBuffer[bufferIndex] = pitch;
rollBuffer[bufferIndex] = roll;
azimuthBuffer[bufferIndex] = azimuth;
bufferIndex = (bufferIndex + 1) % WINDOW_SIZE;
pitch = 0;
roll = 0;
azimuth = 0;
for (int i = 0; i < WINDOW_SIZE; i++) {
pitch += pitchBuffer[i];
roll += rollBuffer[i];
azimuth += azimuthBuffer[i];
}
pitch /= WINDOW_SIZE;
roll /= WINDOW_SIZE;
azimuth /= WINDOW_SIZE;
}
add loop:
azimuth = atan2(correctedY, correctedX);
azimuth += magneticDeclination;
// Normalize azimuth to the 0-360 range
if (azimuth < 0) azimuth += 2 * PI;
if (azimuth > 2 * PI) azimuth -= 2 * PI;
azimuth = azimuth * 180.0 / PI; // Convert to degrees
// Apply moving average filter to pitch, roll, and azimuth
applyMovingAverage(pitch, roll, azimuth);
This way, you will be less affected by vibrations and quick direction changes. Please let me know the results if you try it. Best regards.
It's actually a small wonder that such things can be built into such a small component and that it is robust against vibrations, shocks etc.
Wow this could not have come at a better time. I’m struggling with an MPU9250 for a project due on Friday. Can’t get the magnetometer to function properly to
Measure the rotated angle. Thanks!
The MPU9250 has a similar structure, but the register addresses is different. I also had hard time in reading magnetometer reading from MPU9250. Tried multiple MPU9250, none of them.worked.
@@HowtoElectronicsoh dear this does not bode well.. possible to do sensor fusion only using the 6050? I just need it to give a reference point for a digital compass.
I am able to read the angle but it does not update in the void loop.
I got my MPU9250 to work ultimately, ended up using an I2C scanner to find the IMU, then TwoWire to run it in parallel to my oled screen. I used hideakitai's examples to connect and calibrate it first, then managed to work. One thing I discovered, ESP32-S3 has an issue with pin reassignment in V3 of the Board Manager. I had to downgrade to 2.0.14 to get it to work.
Great man, you discovered so many new things. And I to find out you got it working. Congratulations
Good job , Thanks a lot.
Glad to hear it was helpful!
Hey, how complex would it be to integrate (RTK) GPS into this, for a fusion of position and orientation?
very insightfull, make a tutorial to measure position using any imu sensor please
It's a good idea. I will make one soon.
@HowtoElectronics thank you dear
What is best for tvoc and eco2
ENS160 OR SGP30
how much drift after 30 minutes?
Kalman Filter is used to normalize the reading and remove drift.
@HowtoElectronics I will try, again your way, thank you!
Make sure pins are not magnetic or else it might disturb the magnetometer
Nowadays it's hard to get hmc5883.... So please make a video about fusing the sensor of mpu 6050 and qmc5883 sensor... Asap
QMC5883 and HMC5883 both are same. You can work with any of these....
@HowtoElectronics you are partially correct.... Register addresses and set mode values aren't the Same.....
And Thank you so so much... I really needed this video... For a long time, I was expecting someone to use the Kalman filter to fuse the accelerometer gyroscope and magnetometer sensor data... I couldn't see anyone doing it... So thank you again...
And a small request (I know it's not small),
If you can make code for an extended kalman filter to Fuse the mpu 6050 data and qmc 5883 data... It would be very useful to me and others as well. I was trying to make a model for EKF for a long time ... But I ended up failing...
Thank you for your detailed feedback and kind words!
Regarding the Extended Kalman Filter (EKF) to fuse MPU6050 and QMC5883 data, I understand its importance. I will definitely give it a try, and if I can successfully implement it, I will post a video explaining the process step-by-step. Stay tuned, and I appreciate your patience!
How to provision esp8266/32 using custom mobile phone