SOLVED: motor value drifting due to errorGyroI accumulation

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
doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

SOLVED: motor value drifting due to errorGyroI accumulation

Post by doughboy »

how to reproduce

disconnect battery from FC and only connect USB/serial for GUI connection.
connect the gui.
arm the motors.
put throttle say at 70-80%
watch the motor speed, one of the motors will slowly drift away.

in theory, with no input, the motor speed should remain the same.

with deadband defined, error is going to be 0 in deadband zone.
error = (int32_t)rcCommand[axis]*10*8/conf.P8[axis] ; //<==this is always 0.

the problem is introduced in
error -= gyroData[axis];
where gyroData[axis] bounces between -1 and 0 or 0 and -1.
this causes the error value to increase slowly, until it eventually hits the contraint of 16000, at which point one of the 4 motors will be really way off compared to the other three motors.

I added a simple filter to reset error if its value is <3.
error -= gyroData[axis];
if (abs(error)<3) error = 0;

I do not know if this fix is valid or not, but I can leave the throttle on say 80% for 10 minutes and the motor speed for all motors remain about the same.

I'd appreciate it if someone can look into it.

Thanks
Last edited by doughboy on Wed Sep 19, 2012 4:54 pm, edited 1 time in total.

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: bug: motor value drifting due to errorGyroI accumulation

Post by timecop »

Is your RC trimmed exactly to +-1 of MIDRC?

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: bug: motor value drifting due to errorGyroI accumulation

Post by doughboy »

it is not possible. if I set it exactly to 1500, all I need to do is move it up and down and let go and it will land say at 1495, then next time maybe 1500. I see the RX filters +/-3us, but my TX stick usually lands outside of that range. I have defined a deadband of 15, and that takes care of keeping rcCommand[axis] at 0 within +/-15 of 1500. But the gyroData bounding 0 to 1 sometimes 2 is causing the error to accumulate. That's why I added the test if it is <3 to reset it to 0, then nothing accumulates. I figure it is the same filter range for rcData[axis]

my TX is turnigy9x with open9x firmware.

it's quite easy to reproduce.

crashlander
Posts: 506
Joined: Thu May 05, 2011 8:13 am
Location: Slovenia

Re: bug: motor value drifting due to errorGyroI accumulation

Post by crashlander »

@doughboy: see my answer in
viewtopic.php?f=16&t=2457&start=30#p23072

In short you should have good RX-TX system and good gyro to obtain good results (garbage in garbage out).

Regards Andrej


User avatar
shikra
Posts: 783
Joined: Wed Mar 30, 2011 7:58 pm

Re: bug: motor value drifting due to errorGyroI accumulation

Post by shikra »

I saw the same whilst hunting level drift issue.
Less than perfect gyros is cause. Rather than zero, average tends to be a little to one side. And it sums up as you say.
The code is working correctly - it gets data saying it is slightly turning all the time, so it gradually increases/decrease motors to try to compensate.

In flight you should hardly notice, but on the gui - exactly as you say.

I have one bad gyro which will max out in about 15 secs.... unless I'm trying to hover it flies just fine.
I find WMP worst....

I really like the deadband approach - don't know why I didn't think of that..... sometimes the simplest filters are the best...
I wrote a moving average for gyros. It worked well on desk, less so in flight. In the end I replaced gyro instead of trying to fix bad data.

User avatar
shikra
Posts: 783
Joined: Wed Mar 30, 2011 7:58 pm

Re: bug: motor value drifting due to errorGyroI accumulation

Post by shikra »

LOl - looks like I am slowest to type...

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: bug: motor value drifting due to errorGyroI accumulation

Post by doughboy »

Somehow, the gyro value displayed in the GUI are all 0 all the time. Is the GUI filtering the value of gyro data? My gyro is the 6050.

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: bug: motor value drifting due to errorGyroI accumulation

Post by doughboy »

shikra wrote:I saw the same whilst hunting level drift issue.
Less than perfect gyros is cause. Rather than zero, average tends to be a little to one side. And it sums up as you say.
The code is working correctly - it gets data saying it is slightly turning all the time, so it gradually increases/decrease motors to try to compensate.

In flight you should hardly notice, but on the gui - exactly as you say.

I have one bad gyro which will max out in about 15 secs.... unless I'm trying to hover it flies just fine.
I find WMP worst....

I really like the deadband approach - don't know why I didn't think of that..... sometimes the simplest filters are the best...
I wrote a moving average for gyros. It worked well on desk, less so in flight. In the end I replaced gyro instead of trying to fix bad data.

Yes, without using the dead band, the quad was doing a yaw spin, as somehow the software thinks I am holding the yaw off center ever so slightly.

Crashlander, read the original dead band thread. That feature was put here for a reason, instead of telling people to get $1000 transmitter to use on a FC wih a gyro from a Wii.

Sebbi
Posts: 478
Joined: Sun Jul 08, 2012 1:08 am
Location: Germany
Contact:

Re: bug: motor value drifting due to errorGyroI accumulation

Post by Sebbi »

Just a quick reply: I noticed this too, the values drift a lot because the bias (0-level of the gyros) is calibrated on integer values. If MultiWii calibrates the gyros to 512, but in reality it's oscillating between 512 and 513 (and would be 512,5) then the "error" accumulates and the copter will slowly tilt in one direction. That is also an issue with level mode.

Another problem might be that the values from the MPU6050 originally have a far greater range of values but get mapped to 0-1024 to be compatible with other sensors. Combine that with the calibration issue and drifting is almost guaranteed :/

User avatar
shikra
Posts: 783
Joined: Wed Mar 30, 2011 7:58 pm

Re: bug: motor value drifting due to errorGyroI accumulation

Post by shikra »

Another interesting thing - I noticed that under power the gyro average "center value" changes on some of my WMP. i.e calibration/rest center value is different to that when its subject to vibrations.

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

Re: bug: motor value drifting due to errorGyroI accumulation

Post by doughboy »

Sebbi wrote:Just a quick reply: I noticed this too, the values drift a lot because the bias (0-level of the gyros) is calibrated on integer values. If MultiWii calibrates the gyros to 512, but in reality it's oscillating between 512 and 513 (and would be 512,5) then the "error" accumulates and the copter will slowly tilt in one direction. That is also an issue with level mode.

Another problem might be that the values from the MPU6050 originally have a far greater range of values but get mapped to 0-1024 to be compatible with other sensors. Combine that with the calibration issue and drifting is almost guaranteed :/


thanks for the info. that's what I am getitng at, and the purpose of this is to minimize drifting.

I can see from the link Alex provided why it can be viewed as not a bug, but since the 6050 is "officially" added and supported, I think the comment to get a new gyro is not quite right, unless the support for the "cheap" gyros are all removed from multiwii code altogether. I'm not even sure if the error is coming from the 6050 or just calculation error, since the value of gyro roll pitch and yaw on the GUI is showing 0 at all times, and not switching between 1 and 0.

from a usability point of view, I don't see how/why anyone can even think moving the throttle up with the quad statiionary and one motor speed dropping down is normal, specially if the gyro readings on the gui is showing 0. I imagine this topic probably comes up from time to time, hence there is an faq entry. Perhaps it is time to properly address it rather than point to faq every time it comes up. :)

thanks.

Sebbi
Posts: 478
Joined: Sun Jul 08, 2012 1:08 am
Location: Germany
Contact:

Re: bug: motor value drifting due to errorGyroI accumulation

Post by Sebbi »

Two reasons you dont see the fluctuations in the GUI, but you can clearly see them when you log the values directly in the MultiWii software:
1) The value the GUI shows is gyroData which is averaged over multiple cycles. Two readings from the current cyle (gyros are read twice per cycle) mixed with the average of the last cycles two readings (everything in integer arithmetic). GyroData is only used for the normal PID controller, but orientation angles are calculated using the raw gyroADC values (from the second reading of the gyro sensor) which is causing accumulated error when angle/level/horizon mode is active.
2) The cycle time is usually around 3000 µs which equals over 300 Hz, but the GUI updates those values at only 25 Hz.

The other issue is with the normalisation of the ADC values. The MPU6050 full range (gyros) is +-/ 8192, but it gets devided by 4 before it gets used by MultiWii. Same goes for the accelerometer ... MultiWii devides the full range of +/- 8192 by 8 before using the values ... so resolution is lost, assuming the noise isn't that high for these sensors. But from other applications using the MPU6050 I found that it is pretty stable and the gyro is remarkable insenitive to vibrations (compared to the accelerometer) and drifts very little.

This "destruction" of sensor resolution can result in a greater error when integrated over time ... it shouldn't be noticable while flying though. What IS noticable is the level calibration. If that is slightly off the copter will drift and accelerate in that direction. In theory level mode needs a perfect trim that is never achievable in the real world. GPS and/or optical sensors are needed to keep it on position and thus really level ;-)

User avatar
Jonit
Posts: 37
Joined: Sat May 12, 2012 10:12 pm
Location: Slovakia

Re: bug: motor value drifting due to errorGyroI accumulation

Post by Jonit »

I have slightly different problem, but related to error accumulation... In GUI I see that over time the speed of each motor will differ, but in the air it behave actually pretty stable. My problem is that my Hexa is lacking power under full throttle due to this error. It has no power when fully loaded (camera+FPV). Under full throttle it climbing reaaally slow (let's say 1m/s). When it is under full throttle all motors are showing less power. I have MAXTHROTTLE at 1860, but motors are running only at 1650-1750. On the table, in the GUI i see that 1 of six motors is running at 1860 but others vary from 1450-1750.
I tried to set a deadband zone but it only helps when i am not touching Roll, Pitch and Yaw stick. When i start flying, the error start to accumulating.
Any ideas how to fix that?
(BTW: i have CG right in the middle)

tovrin
Posts: 705
Joined: Tue Sep 20, 2011 4:08 pm

Re: bug: motor value drifting due to errorGyroI accumulation

Post by tovrin »

I would first be more interested in your battery C rating than in the gui performance. 6 motors would require at least a 40C battery.

vpb
Posts: 231
Joined: Mon Jul 23, 2012 4:09 pm

Re: bug: motor value drifting due to errorGyroI accumulation

Post by vpb »

Hi torvin, I think C is not only the important thing in choosing battery. The maximum current of a battery can be calculated = capacity (in A) * C * (safety factor). Safety factor should be 75% (0.75). For example, 3S2200 25C battery will give maximum 2.2*25*0.75 = 41.25A.

Safety factor help battery lives longer, my batteries are all still in good condition after more than a year before I sell them all for the new ones :D

User avatar
Jonit
Posts: 37
Joined: Sat May 12, 2012 10:12 pm
Location: Slovakia

Re: bug: motor value drifting due to errorGyroI accumulation

Post by Jonit »

tovrin wrote:I would first be more interested in your battery C rating than in the gui performance. 6 motors would require at least a 40C battery.

no, battery is not a problem. I have 2x 2200mAh 25c in parallel, that is 25 x 4,4 Ah = 110A continuous. My motors should pull max. 75A at full throttle. The problem is that in GUI i see that motor values are lowered (over time) down to 1450 (on some of the motors), others are at 1750 and one motor is at 1860 as it should be. And that is with full throttle. :?

vpb
Posts: 231
Joined: Mon Jul 23, 2012 4:09 pm

Re: bug: motor value drifting due to errorGyroI accumulation

Post by vpb »

Hi Jonit, how long does it take to reduce to 1450?

User avatar
Jonit
Posts: 37
Joined: Sat May 12, 2012 10:12 pm
Location: Slovakia

Re: bug: motor value drifting due to errorGyroI accumulation

Post by Jonit »

vpb wrote:Hi Jonit, how long does it take to reduce to 1450?

Hi,
on the table it takes 5 seconds to reduce from 1860 to 1750, 15 seconds from 1860 to 1550 and about 1 minute until it drops from 1860 to 1450.

doughboy
Posts: 252
Joined: Tue Sep 04, 2012 7:20 am

SOLVED bug: motor value drifting due to errorGyroI accumulat

Post by doughboy »

Since I never got a satisfactory explanation of the cause of motor speed dropping and the corresponding solution (sebhi explained most quite well though), I studied the code and it turns out the explanation and solution is quite simple.

Motors change speed due to mainly 2 inputs, the sticks and the gyro sensor data.
First, the stick. The default sensitivity is +/-3 us from 1500 (hard coded). This means if your stick reading is beyond 1497 and 1503 range, the software thinks you are controlling the quad and will take that as input and change the motor speed. Since we don't have a perfect transmitter, the solution is to enable deadband. In my case, I set it to 12 and that solves the sticks part of the problem.

Second, the gyro sensor. Readings will have noise. There are two parts to the noise, fluctuations and offset from actual zero. Fluctuations are taken cared of by averaging calculation to smooth out or minimize the fluctuating value. This is already done in the software. you can enable GYRO_SMOOTHING if your gyro fluctuation is still high. Then there is the offset of the zero reading from actual zero. In order to compensate for this, you need to calibrate the gyro, which takes 400 gyro readings at zero (quad is not moving), takes the average, and use the value as offset from zero. Gyro calibration is done by moving the thottle stick to bottom left and pitch to MIN. Once that zero offset value is initialized, then further raw gyro readings are adjusted by subtracting this offset so now you will get a better zero reading. Gyro calibration is automatically done on program reset, but it won't hurt to calibrate it again using the stick combination.

If you do both the deadband and gyro calibration, and adjust the sticks so they are as close to 1000-1500-2000 as possible, then you will get perfect motor speed output and no difting at all.

Perhaps this can be added to the current faq to help others.
Thanks.

vpb
Posts: 231
Joined: Mon Jul 23, 2012 4:09 pm

Re: SOLVED: motor value drifting due to errorGyroI accumulat

Post by vpb »

Nice post & explaination doughboy!

Post Reply