Quadcopter stabilization algorithm

Sorry that I haven't had a chance to look at your actual code - it's been a busy couple of days. Since you say it is achieving a more-or-less stable height and not oscillating or drifting, I assume that you either have implemented the I & D components, or don't need them.

I am probably teaching Granny to suck eggs (I hope that expression translates to your culture!) but here are my thoughts:

If you are working at the limits of the resolution of your barometer then I suggest you design the algorithm to hover at the transition between two values, rather than sitting in the middle of a value (iyswim). So, rather than saying the target height is 'X' and the height error is (current height - X), you say that the target height is the boundary between X and X+1 (X + 0.5, in other words). When you are at that height, you should see relatively frequent transitions in the baro readings between X and X+1 and the proportion of time at X and X+1 should be roughly equal. By averaging multiple readings of a signal with noise present you can get better resolution than the sensor's nominal resolution for a single sample. I suspect you are already doing something of the sort, but the algorithm needs to be designed to find and stay at the boundary between adjacent sensor values for this to work well.

If you have a vertical accelerometer then you can use this to interpolate between the barometer readings, and the barometer can be used to compensate for accelerometer drift and cumulative integration errors. I have a motorsport data logger (I didn't make it!) which provides a resolution down to a few inches using the combination of GPS and an accelerometer; you should be able to obtain far better resolution if your barometer alone gives you resolution down to a few feet. The way I'd approach this is similar to the algorithm used for NTP - periodically compare the position implied by the accelerometer against the position implied by the barometer. If there is a consistent drift between them then skew the accelerometer accel/speed/position integration formulae to reduce that drift to zero. I'm sure you'll see the potential for a PID algorithm to control the drift. :slight_smile: