Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
I noticed that the acceleration data is very erratic. If I do not touch the MPU6050 module the numbers on the display change and the data seems to be very erratic. Any suggestions?
I was already wondering about that - then again: I do not have any experience with devices like the MPU6050. I'd have to guess that the erratic behavior is like a nervous behavior? Meaning: never holding a fixed set of values, always hovering around a certain value?
In that case, not being aware of what kind of numbers we're looking at, I'd only change "w, x, y, z" if the new value is more than a certain percentage or fraction different than the stored values.
So for example, say the value (again: I don't know what kind of values we're looking at) for X = 100, and the returning values hover between 98 and 102 when the MPU6050 is laying still, then I'd only update X is the new value is more than 2 bigger or smaller than the currently stored value. I'd make that "factor" a constant in your code so you can play with different values to see which one offers the best stability.
Something like this:
#define Fluctuation 0.02 // assuming 2%
...
if ( abs(X - newX) > (X * Fluctuation) ) { X = newX; }
I hope I understood your issue correctly and I hope this helps 😊
You got it right. Really interesting idea. I am going to look at that
I really want to solve the problem as to why the data gets crazy. It seems to be a common problem of posts across various sites. But I have not found one that actually solved the problem
I did a little more testing today and think the instability issue is related to how the code resets the FIFO buffer from the MPU6050.
To test I added a line to reset the FIFO after the sketch displays the data on the OLED. I figured it couldn't hurt. The code is mpu.resetFIFO();
Guess what? It is very stable. I left the test sketch running for a minute or so and there was very little variation in the accelerometer values. It also seems to be better at detecting the changing x,y,z accelerometer data.
With that said, I think the issue is derived from the original code from the library example. I don't think it is resetting the FIFO properly. It seems to be good at detecting a FIFO overflow but not resetting the FIFO.
The block of code is as below. Let me know what you think Thanks
// wait for MPU extra packet(s) available while (fifoCount < packetSize) { if (fifoCount < packetSize) { // try to get out of the infinite loop fifoCount = mpu.getFIFOCount(); } }
// get current FIFO count fifoCount = mpu.getFIFOCount(); if(fifoCount < packetSize){ } // check for FIFO overflow else if (fifoCount >= 1024) { // reset so we can continue cleanly mpu.resetFIFO(); // fifoCount = mpu.getFIFOCount(); // will be zero after reset Serial.println(F("FIFO overflow!")); }
// read a packet from FIFO while(fifoCount >= packetSize){ mpu.getFIFOBytes(fifoBuffer, packetSize); // track FIFO count in case there is > 1 packet available fifoCount -= packetSize; }
Awesome and excellent find! 👍 I had not thought of that (probably because I do not have an MPU6050, but then still it would have been a lucky guess on my end haha)!
I see a place for I2C, but sometimes it can be a little challenging to find the culprit when things do not work they way we expect them to work.
Thanks for posting the code and happy to hear you got the issue resolved! I hope someone will find it here and have a good use for it (maybe even myself once I get the chance to play with an MPU6050).
I ended up clearing the buffer after every read. In your code it would be following the line
//read this packet from FIFO buffer
mpu.getFIFOBytes(fifoBuffer,packetSize);
I added
mpu.resetFIFO();
Strange as it maybe, this suggest you should read the bytes first and then reset. A little weird, as I'd expect the reset to do just that to begin with.
In that post you'll find a modification of the mpu.h file (which I usually try to avoid, since updates will overwrite your change and when saving your code somewhere, you'll need to add documentation that you changed the mpu.h file), and a suggestion on how to increase the buffer.
I did find several other posts, but I'm sure you found those as well.
I got the project working although there are a couple of issues and the code needs to be optimized. Since the code is so large I am attaching it as a file.. I am also attaching two sample data files, one for GPS and the other for Accelerometer.
GPS File format is as follows: date, time, latitude, longitude, altitude, speed, number of satellites
Acceleronmeter file format is as follows: date, time, x, y, z, temperature
I would have a few questions, since I do not have a MPU6050.
1) When seeing your code:
//added 8-29-2020 mpu.resetFIFO();
// wait for MPU extra packet(s) available while (fifoCount < packetSize) { if (fifoCount < packetSize) { // try to get out of the infinite loop fifoCount = mpu.getFIFOCount(); } }
I do not see an initial fifoCount read. I do not know if this fixes anything, but I'd be inclined to write it this way, so fifoCount is properly initialized and has a proper value (I hope I didn't miss something in your code where you already did this):
//added 8-29-2020 mpu.resetFIFO();
fifoCount = mpu.getFIFOCount();
// wait for MPU extra packet(s) available while (fifoCount < packetSize) { if (fifoCount < packetSize) { // try to get out of the infinite loop fifoCount = mpu.getFIFOCount(); } }
2) The gaps (I can see them in the data output as well) could be the MPU6050 "freezing up" for a little bit or maybe even completely resetting? Maybe it is an idea to refresh the data less often?
3) The code is reasonably readable, but maybe it is a good idea to clean it up a little to improve readability (use proper and consistent indentation) 😊 (note: I'm not that great at that either when testing code - sometimes I use tools like this one to cleanup and beautify my code - I've attached a "cleaned up" version of your code, which I did with the link mentioned earlier)
I noticed the same thing but the MPU6050 code was copied from the library example. Here is the library DMP6 example with some code added for the ESP32 and OLED
I added the line to set an initial FIFO count as you suggested. It seems to be more stable but I need to do more testing I did a short test and there were no dropouts in the data. Both files from today attached
You do have a point. I do not need to log data multiple times per second. The logs show an average of 5-6 times per second. I think 1 maybe 2 data logs per second would be plenty.
Is there a way to limit the interval the code logging to SD ?
Elementary OS (Freya) Freya, a.k.a. Elementary OS, is my favorite Linux distribution. It looks slick and works great. Highly recommend it.
Animizer Animizer.net provides complete online toolbox for animations (GIF and APNG) - Edit, Assemble, Split or Convert, it's all there.
Security Awareness Tip of the Day Great resource to make you online-life more secure - definitely some great tips here and a recommended read.
Robots.txt Analyzer Very simple and effective tool for online checking of your robots.txt file without having to use Google Webmaster Tools.
WhyNoPadLock Test your website and discover why the green padlock is not appearing when using HTTPS
Ubuntu Ubuntu is probably one of the most used Linux distro's out there. Very well supported and a great Linux distro
Links Page These and more of our favorite links can be found on the Links Page.
New Downloads
ConnectMeNow4-v4.0.18-macOS-x86-64.dmgDate: 2024-04-24 - Size: 3.5 MBVersion 4 of ConnectMeNow - A tool for more convenient mounting of network shares under macOS. This is the Intel version which works on Intel and Apple Silicon Macs.
ConnectMeNow4-v4.0.18-macOS-arm64.dmgDate: 2024-04-24 - Size: 3 MBVersion 4 of ConnectMeNow - A tool for more convenient mounting of network shares under macOS. This is the Apple Silicon version (not suitable for Intel).
MiniWOL2 MacOS (64 bits Apple Silicon)Date: 2023-08-01 - Size: 1.2 MBminiWol is a simple, but effective application to send Wake On LAN to network devices. This is the signed 64 bit MacOS ARM (Apple Silicon) version.
MovieScanner2-2.2.3-Windows-32bit-setup.exeDate: 2023-04-12 - Size: 18.6 MBA small application that uses FFProbe to scan your video files and logs these details in a small database. This is the 32 bit Windows version.
MovieScanner2-2.2.2-Linux-GTK-64bits.tar.gzDate: 2023-04-11 - Size: 29.2 MBA small application that uses FFProbe to scan your video files and logs these details in a small database. This is the 64 bit Linux version for GTK.
MovieScanner2-2.2.2-Linux-QT5-64bits.tar.gzDate: 2023-04-11 - Size: 29.1 MBA small application that uses FFProbe to scan your video files and logs these details in a small database. This is the 64 bit Linux version for QT5.
Downloads Page Find these and more Downloads on the Downloads Page, where you will also find articles references, operating system requirements and categories.
Amazon Ads
Support us by doing your shopping at Amazon.com, either click the link, or click one of the links below …
You can also sponsor us through these Amazon offerings:
Please consider disabling your ad blocker for our website.We rely on these ads to be able to run our website.You can of course support us in other ways (see Support Us on the left).