I've been trying to get my head around the best method of controlling the throttle channel on my KK tricopter to get it to stick to a set altitude (anywhere between 0.5 to 8m) using just my XL sonar I pulled from my APM based quad and an Arduino Uno board.
To start with, I coded the sketch to use channel 5 to turn the alt hold code on or off. When off, the source throttle PWM value is just passed to the throttle out as is. When on, the current altitude and input throttle PWM are captured and used as a reference. When the height drops below the captured height the heigh difference is added to the captured PWM (hover PWM) to push the tricopter back up. If the measured hight is higher than the captured reference height the height difference is subtracted from the hover PWM value to allow the tricopter to come back down.
This is obviously a very simplistic approach to trying to control the throttle to make it stick to a set height. In practice this just results in an ever increasing bounce up and down as soon as I flick the switch on channel 5.
I did try a measured pulse up to start it coming back up and then a measured throttle drop as soon as the hight was obtained but as soon as wind was brought into the equation this just resulted in the tricopter drifting away from the desired height constantly.
The frustration of all this is getting the better of me so now I am looking into what PID calculations are and how they could apply to this requirement.
I have also ordered a 3 axis accelerometer to see if detecting the fall/rise motion as well as the height will increase the accuracy.
I know this is a large request, but has anyone got some code lying around which specifically uses PID algorithms to calculate the required output throttle given the following input values -
1) hover PWM (the PWM captured when the mode is changed to alt hold)
2) desired height (cm captured when initially set to alt hold)
3) current height (cm)
4) optional z acceleration
I did have a look at the code for arducopter but there seemed to be a number of calls that were not applicable to the Uno, just the Mega.
Any simplified examples of PID code for height control would be handy right now.