MPU6050 stops working

hi there

I have a problem with a code that I wrote with MPU6050. I'm gonna share it here so there might be someone with the same problem:

my piece of code is really big! so I'll just try to explain it here:

in setup() I just configure the MPU6050.

in the loop(), I let it measure the angle.

the thing is that in loop(), I also have different functions and interrupts going on and as soon as one of them wants to be executed, MPU6050 measurements corrupts, here is why I think it happens:

in the MPU6050, I should keep my last measuring of time and angle, in order to measure the angle for the next time. as in the formula:

angle(this_time)=(this_time - last_time)*rate_of_angle +angle(last_time)

the thing is when the code goes to do another process, time will pass and (this_time) becomes really large when it goes to MPU6050 processing again.

so what I see is a huge jump in angle measurements!

how can I fix it? is MPU6050 too unreliable in big codes???

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless.

the thing is when the code goes to do another process, time will pass and (this_time) becomes really large when it goes to MPU6050 processing again. so what I see is a huge jump in angle measurements!

Is this a surprise to you? The code is wrong.

As jremington points out, you have figured out what the problem is.

To fix your problem, you need to learn how to write non-blocking code, that is, code that will enable you to read the MPU6050 while other tasks are processing. Search the internet on how to multi-task the arduino and you will find plenty of examples and ways to do this.

Two ways I can think of to handle this would be to use timer interrupts or FreeRTOS. Timer interrupts would be the easiest way, but the ISR (interrupt service routine) needs to be kept short & simple. Think - just read the value from the MPU6050, nothing more. FreeRTOS is a Real Time Operating System that allows you to define tasks and priorities for those tasks. It's a bit more complex than timer interrupts to use, but I didn't think it was too hard to use.

You haven't mentioned what you are doing, or what kind of arduino you are using. If you are using an UNO, go with timer interrupts. If your using a better arduino, or you need to do complex operations after getting the MPU6050 data, then go with FreeRTOS.

Just my thoughts,
Randy

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.