Hi vortix2950,

Congratulations on getting your quadcopter flying in rate mode by the way.

1. When you create two inner PID loops, how fast do each one has to be? right now my rate controller runs once every 4ms but i dont know how fast should the Angle Pid run.

As it's possible to sample the gyroscope and accelerometer at the same rate, (up to 1000 times per second using the MPU6050 with filtering), just run both the outer angle and inner rate PID loops as fast as your microcontroller allows. 4ms is about right for rate mode using an Uno/Nano/Pro Micro with floating point PIDs.

2. Should the angle PID has its own separate PID gains or should both the rate loop and the angle loop use the same terms?

The angle PIDs for roll and pitch should have their own gain values. I only use the P gain term for these outer control loops. For a PID controller using angle in degrees, I set the P term to a value of 3.0.

3.to be clear, the corrections that the angle loop produces should be used as a setpoint and create an error for the rate loop to correct, right?

Yes that is correct. The key point is that a PID control loops' output doesn't have an units as such, it's just a magnitude. In this case, the outputs of the outer angle PIDs control the inner rate PIDs.

Angle Estimation using the Complementary FilterAs you mention you need two outer PID control loops for roll and pitch. In auto-level mode the pilot input for the roll and pitch channels are converted to angle, say ±45° instead and act as the "setpoint". The "input" to these control loops is the estimated roll and pitch angle. The output from these outer control loops is used to drive the corresponding inner rate loop. The yaw axis remains unchanged from rate mode and as before is driven by the pilot "setpoint" and gyrscope "input" in degrees per second using a single rate PID control loop.

Angle estimation opens up a how whole new world of mathematics, sensor fusion and filters techniques, such a Direction Cosine Matrices (DCM), Quaternions and Kalman filtering. Fortunately however, it's possible to produce a good angle estimation using some simpler techniques.

It's possible to calculate the roll and pitch angle by integrating the gyroscope output, but while gyroscopes are stable over the short term, they tend to drift over the long term. It's also possible calculate the roll and pitch angle by employing an accelerometer to measure gravity and using a bit of trigonometry, but while the accelerometer is noisy over the short term, it does however remain stable over the long term. In short both sensors are able to measure angle, but their measurement characteristics are somewhat different.

The solution is to combine or fuse the sensors using a complementary filter, to take advantage of their beneficial characteristics. The filter estimates the angle by summing the previous angle with a percentage of the integrated gyroscope and accelerometer outputs:

angle estimation = previous angle + 0.999 * gyroscope output * dt + 0.001 * accelerometer output

It's worth reading these articles to get a feel for how it works:

http://www.geekmomprojects.com/gyroscopes-and-accelerometers-on-a-chip/http://scolton.blogspot.com/2012/08/kk20-firmware-mod-unleash-complementary.htmlAlso, there's the additional complication of yaw coupling. In reality roll, pitch and yaw are not independent variables. DCM and quaternion mathematics with advanced filtering accounts for this, but simply using roll, pitch and yaw Euler angles with a complementary filter does not. For example imagine your quadcopter is hovering level, if you pitch up the nose by 45° and then yaw to the left 90°, you're now actually in the roll position at 45°. The issue is that during the yaw motion the gyroscope cannot sense the change in roll and pitch. The accelerometer does sense this change, but as complementary filter is only taking a tiny 0.1% (0.001) fraction of its output, it takes too long for the filter to react. Therefore it's necessary to cross couple roll and pitch motion when yawing, this is detailed in the article here:

http://scolton.blogspot.com/2012/09/fun-with-complementary-filter-multiwii.html