[bug] MPU6050 DLPF setting has no effect

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
fax8
Posts: 61
Joined: Mon Feb 14, 2011 5:29 pm

[bug] MPU6050 DLPF setting has no effect

Post by fax8 »

Hi everybody,

while debugging a friend (and FreeIMU user) instability problem on his 4copter we discovered after some bloody debugging hours that, with the current MultiWii MPU6050 initialization code, the DLPF settings aren't actually stored in the sensor memory. This means that any DLPF setting is actually lost and never used by the sensor. Affected versions are 2.0 and further developers versions till today svn.

I'm still not sure why this happens, but changing the initialization order of the instructions seems to correctly fix the problem. The idea is to set the 0x1A CONFIG register with the appropriate DLPF setting as last configuration instruction.

Fixed code for MultiWii 2.0 is below. Uncomment the debug2 instruction to see for yourself that the value has been actually stored in sensor (the same debug instruction can be put at the end of the original initialization routine to show that no DLPF setting is stored with the original code).
Patch for today dev available at http://sprunge.us/eaMW

If you wanna test this on 2.0, replace original MPU6050 Gyro_init() function with the following:

Code: Select all

// ************************************************************************************************************
// I2C Gyroscope and Accelerometer MPU6050
// ************************************************************************************************************
#if defined(MPU6050)

void Gyro_init() {
  TWBR = ((16000000L / 400000L) - 16) / 2; // change the I2C clock rate to 400kHz
  i2c_writeReg(MPU6050_ADDRESS, 0x6B, 0x80);             //PWR_MGMT_1    -- DEVICE_RESET 1
  delay(5);
  i2c_writeReg(MPU6050_ADDRESS, 0x19, 0x00);             //SMPLRT_DIV    -- SMPLRT_DIV = 0  Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
 
  i2c_writeReg(MPU6050_ADDRESS, 0x6B, 0x03);             //PWR_MGMT_1    -- SLEEP 0; CYCLE 0; TEMP_DIS 0; CLKSEL 3 (PLL with Z Gyro reference)
  i2c_writeReg(MPU6050_ADDRESS, 0x1B, 0x18);             //GYRO_CONFIG   -- FS_SEL = 3: Full scale set to 2000 deg/sec
  // enable I2C bypass for AUX I2C
  #if defined(MAG)
    i2c_writeReg(MPU6050_ADDRESS, 0x6A, 0x00);             //USER_CTRL     -- DMP_EN=0 ; FIFO_EN=0 ; I2C_MST_EN=0 (I2C bypass mode) ; I2C_IF_DIS=0 ; FIFO_RESET=0 ; I2C_MST_RESET=0 ; SIG_COND_RESET=0
    i2c_writeReg(MPU6050_ADDRESS, 0x37, 0x02);             //INT_PIN_CFG   -- INT_LEVEL=0 ; INT_OPEN=0 ; LATCH_INT_EN=0 ; INT_RD_CLEAR=0 ; FSYNC_INT_LEVEL=0 ; FSYNC_INT_EN=0 ; I2C_BYPASS_EN=1 ; CLKOUT_EN=0
  #endif
 
  i2c_writeReg(MPU6050_ADDRESS, 0x1A, MPU6050_DLPF_CFG); //CONFIG        -- EXT_SYNC_SET 0 (disable input pin for data sync) ; default DLPF_CFG = 0 => ACC bandwidth = 260Hz  GYRO bandwidth = 256Hz)
  //debug2 = i2c_readReg(MPU6050_ADDRESS, 0x1A);
 
  //debug2 = MPU6050_DLPF_CFG;
}



Hope this helps,

Fabio Varesano

p.s.: thanks Sergio for the moral support

Alexinparis
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by Alexinparis »

Hi Fabio,

Thank you for pointing this. It will help a lot of configs to be more stable.

in fact this register setting
i2c_writeReg(MPU6050_ADDRESS, 0x1A, MPU6050_DLPF_CFG);

must be put after this one
i2c_writeReg(MPU6050_ADDRESS, 0x6B, 0x03);


I don't know why since no reset should occur with this line, and there is nothing in the spec to specify orders, but it's a fact ;)


fax8
Posts: 61
Joined: Mon Feb 14, 2011 5:29 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by fax8 »

nice... let us know if the fixed code I posted above seems to work for you Markus..

warthox
Posts: 65
Joined: Sat Jan 29, 2011 10:05 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by warthox »

thanks fabio, will try it this evening.

warthox
Posts: 65
Joined: Sat Jan 29, 2011 10:05 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by warthox »

will this code work unedited also for a simple mpu6050 imu without any other sensors?
will the lpf setting from config be used for the setting? so the lpf value i take in config is the lpf value which is stored?

fax8
Posts: 61
Joined: Mon Feb 14, 2011 5:29 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by fax8 »

yes, you can use the code with whichever MPU6050 board you want (also with Drotek :evil: :D ).
yes, if you recompile and upload the code.
yes.

User avatar
mbrak
Posts: 136
Joined: Sat Dec 03, 2011 8:08 pm
Location: Germany, Lemgo

Re: [bug] MPU6050 DLPF setting has no effect

Post by mbrak »

hi

with the new init-code my copter (flyduino mpu6050) is flying a bit better. the high p-values are not flyable. i had to go back to the default values.

what i recognized is that there some glitches. dont know from what source.
yesterday a had none of them. i just changed the init code. my lpf is set to 42hz. the gui shows 3 at debug2. is that right?

br michael

warthox
Posts: 65
Joined: Sat Jan 29, 2011 10:05 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by warthox »

did a short testflight with my flydumini with unbalanced props.
the lpf seems to work now. its flyable pretty good. lpf to 20hz and p to 3.
when lpf is set to 42hz the debug value says 3.
when lpf is set to 20hz the debug value says 4.
will try to do some more tests with the other frames this week.

amourdurisk
Posts: 31
Joined: Wed Nov 02, 2011 8:58 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by amourdurisk »

Hello,
it's been 2 months I noticed that the lpf on the mpu did not work: http://www.multiwii.com/forum/viewtopic.php?f=8&t=1080&start=30#p9589
This is good news to see that FAX8 found a solution.
Thx a lot :)

Matt

2.4g Shaun
Posts: 24
Joined: Sun Sep 18, 2011 5:40 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by 2.4g Shaun »

HiI, is there a current version of a sketch downloadable that includes all the bug fixes?

Also I'm experiencing some wierd behaivour.. The quad flies great but when pitched and rolled aggressively it will suddenly flip.. Very much like when you have auto levelling switched on in wind... It is though it is over correcting. This is with the auto levelling de activated.

Any advice please?

Shaun

fax8
Posts: 61
Joined: Mon Feb 14, 2011 5:29 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by fax8 »

MultiWii_dev_20120504 has the patches from this thread in.

2.4g Shaun
Posts: 24
Joined: Sun Sep 18, 2011 5:40 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by 2.4g Shaun »

fax8 wrote:MultiWii_dev_20120504 has the patches from this thread in.


Thanks..... any ideas why it keeps flipping ????

fax8
Posts: 61
Joined: Mon Feb 14, 2011 5:29 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by fax8 »

Looks like some frames somehow have problems with some kind of resonance/vibrations.. that's usually fixed by using a DLPF introduced in the MPU6050 thanks to the code in this page.

2.4g Shaun
Posts: 24
Joined: Sun Sep 18, 2011 5:40 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by 2.4g Shaun »

fax8 wrote:Looks like some frames somehow have problems with some kind of resonance/vibrations.. that's usually fixed by using a DLPF introduced in the MPU6050 thanks to the code in this page.

Thanks again .. I'll try changing the DLPF settings and post my findings here...

The props are well balanced and there is no evidence of viabration when holding the quad and running the motors up..
The frame is very rigid and it is 350mm motor to motor.

regards,

Shaun

2.4g Shaun
Posts: 24
Joined: Sun Sep 18, 2011 5:40 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by 2.4g Shaun »

Hi, I have upgraded the s/w on my promini as above.
I now have a strange problem, With no ACC the copter appears fine, switch in the ACC and it has what appears to be a reversed effect .i.e hold the copter in hand, start to rotate it in roll and instead of levelling it it tries to increase the rate of rotation as if it is reversed. The 6050 board is one from flyduino.

Any ideas what I have done wrong.. is there something in the sketch of the new version I have missed?

Thanks

User avatar
mgros
Posts: 90
Joined: Thu Jan 20, 2011 12:32 am

Re: [bug] MPU6050 DLPF setting has no effect

Post by mgros »

I have a FreeIMU from Flydiuino with MPU6050 and it fly like a dream.

Have you checked the behavior in GUI? It seems an error in sensor orientation.

fax8
Posts: 61
Joined: Mon Feb 14, 2011 5:29 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by fax8 »

@mgros that's very good to hear!
@2.4g Shaun how did you setup your board in config.h? Did you do any change in sensor orientation in the code? Did you point your sensor board Y axis to the nose of your copter?

2.4g Shaun
Posts: 24
Joined: Sun Sep 18, 2011 5:40 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by 2.4g Shaun »

@fax8 thanks... I used the 6050 generic sensor set up as below.

/*************************** independent sensors ********************************/
//leave it commented if you already checked a specific board above
/* I2C gyroscope */
//#define ITG3200
//#define L3G4200D
#define MPU6050 //combo + ACC

Nothing else was changed re the sensor set up.. I would have assumed the code is correct without the need to change ACC orientation... Board is correctly oriented Y forwards. The only difference is I used the independent sensor tab above , previously I used a modified freeimu one with the baro and mag "// " out.

I assumed this was OK, but on checking the GUI ( thanks mgos) the sensor directions are all wrong now .. as follows

Pitch and Roll have moved 90 degrees clockwise.. i.e. X is now forwards . YAW is 180 degrees out i.e the trace goes DOWN on rotating the board clockwise.... ACC Z axis is correct..... This is wierd.....

It leads me to believe that the code for this sensor is all wrong in the sketch.... I can't be the only person who has had this ... I must be doing something wrong......

Shaun

fax8
Posts: 61
Joined: Mon Feb 14, 2011 5:29 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by fax8 »

Try this. Put the following code at the bottom of def.h

#if defined(MPU6050)
#define ACC_ORIENTATION(X, Y, Z) {accADC[ROLL] = -X; accADC[PITCH] = -Y; accADC[YAW] = Z;}
#define GYRO_ORIENTATION(X, Y, Z) {gyroADC[ROLL] = Y; gyroADC[PITCH] = -X; gyroADC[YAW] = -Z;}
#endif

Then in config.h uncomment only the #define MPU6050 line (as you already have).

2.4g Shaun
Posts: 24
Joined: Sun Sep 18, 2011 5:40 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by 2.4g Shaun »

Thanks fax8.. I'll try that later today and post back...

Shaun

2.4g Shaun
Posts: 24
Joined: Sun Sep 18, 2011 5:40 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by 2.4g Shaun »

Fax8.. you are a star !! many thanks ....problem sorted..
I'm assuming this is an error in the latest software release?

Regards,

Shaun

fax8
Posts: 61
Joined: Mon Feb 14, 2011 5:29 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by fax8 »

@Shaun thanks for testing. Yes, that's a bug.

@Alex I think you already understood the problem.. but I can provide a proper patch if needed.. just let me know.

LeoLeo
Posts: 12
Joined: Wed May 29, 2013 9:34 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by LeoLeo »

Hello,

I've got a problem that led me to this thread. Sorry it's such an old one, but I have the exact same problem described here in MultiWii 2.2
Last week I bought a MPU6050 bare sensor breakout to replace the original WMP on my old copter (was still on MultiWii 1.7 I think - now updated to 2.2). The copter flies very well in heading hold mode. I calibrated the acc in my livingroom. While flying outside I decided to test acc mode. While hovering on one place, everything seemed to be fine. But then I started to fly around in a big circle. The copter started to wobble around like hell and crashed badly :-(
After fixing the copter I just decided to not use acc mode ;-)
Today I installed a camera gimbal on my copter. After wasting the whole day trying to get it working properly, I noticed the horizon in the config GUI moves weird. It seems acc roll and pitch are rotated by 90°
It seems this issue has been discussed in this thread nearly a year ago - the bug is still there in 2.2
After reading I also checked what the OP in the first post suggested and I can see in my code that this sensor initialization is also not the last instruction as suggested in the first post.
But that doesn't seem to cause any problem for me - or could my copter maybe fly even better than it does now? Or is it maybe a side-effect of my problem? :)
Is it correct to assume the sensor orientation of the MPU6050 is false in the code? Since gyro and acc are fixed (in one single chip) the orientation of these sensors should be the same in all boards using this sensor, shouldn't it?
Ok, now please tell me if I missed something or understood terribly wrong. I just can't believe anybody else had this problem in the past year.

Regards,
Leo

User avatar
linuxslate
Posts: 91
Joined: Mon May 13, 2013 3:55 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by linuxslate »

By coincidance, I have arrived at this same page on the same date.

I can verify that the atificial horizon in MWConf is not 90 deg off.

However, you may be right that something is still wrong with the MPU6050 code.

I noticed that my Z axis seems more sensitive than X or Y.

Calibration works, and with the Quad flat on the table, I get X= very close to 0, Y= very close to 0 and Z = very close to 512.

But Z also seems to be much more sensitive to prop vibrations than X or Y, which is counter-intuitive. I would think Z axis vibrations would be affected the least by off-balance props.

So I looked at the code in 2.2, and it seems that there are several things that they are not doing, but the suggested code in the OP shows.

For example the 2.2 MPU6050 Gyro_init does not seem to write 0x03 to 0x6A - USER_CTRL (Accel init writes 0b0010000, which is 0x20, unless I have my endians wrong, or something.)

scrat
Posts: 925
Joined: Mon Oct 15, 2012 9:47 am
Location: Slovenia

Re: [bug] MPU6050 DLPF setting has no effect

Post by scrat »

Gyro scale was corrected in dev version 1428.

LeoLeo
Posts: 12
Joined: Wed May 29, 2013 9:34 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by LeoLeo »

Is it possible that the MPU6050 sensor outputs different sensor orientation if there is something soldered differently on the board? I have a cheap breakout board from dx.com

I can't find the part of code which defines the sensor orientation if only "#define MPU6050" is used in config.h, therefor I just added the lines to def.h fax8 suggested.
After some trial & error I now have the following, working setup:

#define ACC_ORIENTATION(X, Y, Z) {accADC[ROLL] = Y; accADC[PITCH] = -X; accADC[YAW] = Z;}
#define GYRO_ORIENTATION(X, Y, Z) {gyroADC[ROLL] = X; gyroADC[PITCH] = Y; gyroADC[YAW] = -Z;}

compared to other sensor boards, it seems I have mounted my sensor by 180° rotated.

User avatar
linuxslate
Posts: 91
Joined: Mon May 13, 2013 3:55 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by linuxslate »

Gyro scale was corrected in dev version 1428.


Thank you again, scrat.

I am going to have to put your name on my Quad since you keep helping me.

I will look at 1428 and see if there is something I need to back port.

User avatar
linuxslate
Posts: 91
Joined: Mon May 13, 2013 3:55 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by linuxslate »

OK.... I did a little digging.

(From 2.2 tag in svn, Interesting that it is not the same as in the 2.2 on the downloads page)(In IMU.ino)

#define GYRO_SCALE ((2380 * PI)/((32767.0f / 4.0f ) * 180.0f * 1000000.0f))

Doing the math, the scale factor is: 5.070809181e−9

Up to 1420: (This is the same values as in the "release" 2.2)
(Code changed a bit, but still in IMU.ino)(value changed a small amount)
#define GYRO_SCALE ((2279 * PI)/((32767.0f / 4.0f ) * 180.0f * 1000000.0f))

Doing the math, the scale factor is: 4.85561938e−9


1428: Now in def.h for MPU6050
#define GYRO_SCALE (4 / 16.4 * PI / 180.0 / 1000000.0)
Doing the math, the scale factor is now: 4.256897019e−9

So there is a difference in the scaling factor. (Looks like they cleaned up the math a bit too.)

It is also correct to have a different GYRO_SCALE for each part, so moving it to def.h was a good move.

So I am going to change my IMU.ino to be as follows.

(Starting at line 108)

Code: Select all

#if defined(MPU6050)
  #define GYRO_SCALE (4 / 16.4 * PI / 180.0 / 1000000.0)  // <--- Backported from 1428.  Only correct for MPU6050
#else
  #define GYRO_SCALE ((2279 * PI)/((32767.0f / 4.0f ) * 180.0f * 1000000.0f)) //(ITG3200 and MPU6050) <---No, not MPU6050
  // +-2000/sec deg scale
  // for WMP, empirical value should be #define GYRO_SCALE (1.0f/200e6f)
  // !!!!should be adjusted to the rad/sec and be part defined in each gyro sensor
#endif


I'll let you know if I see any difference.

scrat
Posts: 925
Joined: Mon Oct 15, 2012 9:47 am
Location: Slovenia

Re: [bug] MPU6050 DLPF setting has no effect

Post by scrat »

Please do. Thanks.

User avatar
linuxslate
Posts: 91
Joined: Mon May 13, 2013 3:55 pm

Re: [bug] MPU6050 DLPF setting has no effect

Post by linuxslate »

Perhaps someone more stable than I am should try this and report back.

I mean both in terms of the aircraft, and of being careful in testing.

I believe I loaded the same settings as before, but I have been trying to get this thing stable, and I changed too many things at the same time.

I've also had 2 fairly bad crashes, and thus new props.

I've also been playing with gyro filters, which would tend to produce the same results, so I have to go back and make sure I try it with the same gyro filter settings the same.

Through the noise and chaos, I would say that the above sw mod made my quad more docile. i.e. less aerobatic. It seems to take /slightly/ more control movement to produce the same result as before.

Post Reply