Due to variance in the individual motors / frame, you may need to alter the p and d values in the code above. The variable p controls how much the quadrotor will alter motor speeds to correct for an offset from the desired tilt. The variable d controls how much the quadrotor will resist sudden rotation. An incorrect ratio of p to d could cause the quadrotor to be unstable either by being less responsive than desired or by causing oscillations of increasing magnitude. Scaling up both p and d by the same value would increase the magnitude of the current effects.
Our tuning method involved a lot of trial and error. We would support 2 opposite corners of the quadrotor leaving the other 2 free to rotate. Then we would check for stability by first observing if the quadrotor was keeping itself level, then secondly by injecting disturbances to observe if it would return to a level position. If the p and d values permitted the quadrotor to pass both of these tests, we would repeat the procedure and on the other axis. When the quadrotor was stable on both axis, we then were ready to fly!