I just re-read your original post, which I had read incorrectly the first time. I took it to be that you had implemented a PID loop, and then were using K to fudge the output of the PID loop into a valid PWM value. But now I think your K calculations ARE the PID loop.
bask185:
first I read the analog voltage with the ADC, then I calculate the difference, delta, between the SetPoint and the output voltage, and last the PWM gets incremented with delta devided by a factor K, which increases the output voltage.
This sounds like you are making a basic implementation of the P term of the PID loop: the output is proportional to the input. This is the main adjusting factor: the larger the error, the bigger the adjustment.
theoratically the output voltage will only "aproach" the SetPoint Voltage.
That's what the I term is for: small errors are integrated over time to bring the output in line with the set point, to counter the tendency for the P term to asymptotically approach the set point but never reach it. A small error over a long time will tend to tweak the output value.
However It will take a significant time for the boost converter to reach the SetPoint voltage, so when the frequentie is to high, It will overshoot due to pwm being incremented to often. To prevent an overshoot I could either lower the frequentie and/or increase the factor K for better dampening.
This is where you need the D term, to gracefully handle a large step function change in the error term. (Think of the startup case as the error instantly going from zero to full scale.) The derivative portion gives a large but short adjustment to the output to quickly get it in range. (Think of driving your car: when at speed, you make small adjustments to the accelerator position to maintain speed, but when pulling away from a stop sign you give it much more gas until you get going, else it would take a long time to get up to speed.)
Am I on the right track here? This PID controlling is new for me. I am going to need it for my 2 wheel balance car in the future
and perhaps also for my quadcopter.
I think you're heading in the right direction using a PID loop, especially for your follow-on projects. But it looks like you need to spend more effort on the D term, because your existing improvements to adjusting K don't seem to be working for you. You will likely also need an I term, especially for the balancing robot and copter applications.
It looks like you nay gave taken the PID loop concepts, simplified them into your code, and then started tweaking your code to account for the observed behavior. A better first step might be to take an existing full PID loop implementation, and just tweak the tuning parameters (which can be more art than science.) don't try to optimize the code by leaving out parts you don't think you need, that may only cause problems when tuning. For example, if you don't need the integral portion, just set I to zero, rather
han removing that code. It makes it easier to reactivate that portion if it turns out to be needed in the future. With a properly implemented PID loop, you should be able to make a wide range of adjustments by just changing the P, I, and D factors, with no changes to your code. When faced with a situation like this, I store the tuning factors in EEPROM and use a simple command line interpreter that let's me watch the behavior of the loop (seeing the individual P, I, and D contributions to the output value) and lets me change the P, I, and D factors in EEPROM so I can play with different values without loading new code each time. Such a facility may be overkill for your power supply project, but will probably come in very handy when it comes time to tune the loop for your balancing robot and copter (which will likely be much more difficult to tune.)
I would recommend making a generic PID loop that will work in all three cases: the only code that should need to change is determining the error and controlling the output (for your power supply, that means reading the analog input and controlling your switcher frequency.) All other customization should be handled by adjusting P, I, and D.
I'm going to go out on a limb and guess that Dave misread your post the same way I did. That's why we both said to not adjust K according to the error or output state, and why our comments probably made no sense to you. Hopefully this clears things up some?