Jump to content
I2Cdevlib Forums

MPU6050 - getAccelerationX(); hangs during timer interrupt - Please advise


Recommended Posts

Hi all,

 

I have been using Jeff's MPU6050 Arduino library with great results.

However, in my current project, I want to put getAccelerationX() (or getMotion6() for that matter) inside an IRS (ISR(TIMER2_COMPA_vect) specifically) in order to poll the MPU6050 at precise intervals (about 60Hz). But for some reason the code will hang inside the function getAccelerationX().

Is this some kind of interrupt conflict? I would like to know if there is anyway to work around this.

 

For a quick reference, this is the code of getAccelerationX():

 

int16_t MPU6050::getAccelerationX() {
    I2Cdev::readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, 2, buffer);
    return (((int16_t)buffer[0]) << 8) | buffer[1];
}

 

I am using a standalone Arduino UNO, with timer0 untouched, and timer1 already used to control some motors. Will it work if I use an AVR with more timers (3,4,5) and use one of those instead of timer2 above?

Thanks in advance,

Dave

Link to comment
Share on other sites

Hi,

I think you cannot call the function readBytes() from an ISR, at least you shouldnt. The reason for this is because that function includes I2C communications, that take some time and also may conflict with ISR manager for reasons I don't really know.

Just use the timer interrupt to change the value of a flag, and then check that flag from main code and execute the readbytes().

Link to comment
Share on other sites

Hi,

I think you cannot call the function readBytes() from an ISR, at least you shouldnt. The reason for this is because that function includes I2C communications, that take some time and also may conflict with ISR manager for reasons I don't really know.

Thanks a lot for the answer! That makes sense to me, that being the case then it's a big pity for the 6050, since it will limit the applications.

 

Just use the timer interrupt to change the value of a flag, and then check that flag from main code and execute the readbytes().

 

Well for my application which is a balancing robot, I need to know the exact time elapsed between two consecutive readings (hence the original IRS set at 60Hz), so to move the function to the main loop and use something like micros() to keep track of the time, I would just move everything else with it then... Wish I could do the IRS because it would make the code very tidy.

 

Does anyone have any experience using the MPU-6000 (exactly like the MPU-6050 but also comes with SPI interface) inside an IRS? Or do you think it will work?

 

Thanks,

Dave

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...