Information

* basic PID routine to get output value
*/
int getPIDoutput(int setPoint, int actualValue, int maxValue, int minValue){
static float sumE = 0;
static int16_t error, previousError = 0;
float outputValue;
static int pidAvg[4] = {0,0,0,0};
static int pidAvgIndex = 0;
// reset sumE when actualValue exceed setPoint by 5
static int noWaitCycles = 0;
if(actualValue > setPoint + 5){
++noWaitCycles;
if(noWaitCycles >= 30){
sumE = 100;
noWaitCycles = 0;
}
}
else{
noWaitCycles = 0;
}
// PID implementation
error = setPoint - actualValue;
sumE += (float) error * dT;
outputValue = Kp*error + Ki*sumE + Kd*(error - previousError)/dT;
previousError = error;
// restrict output PID value into range between minValue and maxValue
if(outputValue > maxValue)
outputValue = maxValue;
else if(outputValue < minValue)
outputValue = minValue;
// store n output values for averaging
pidAvg[pidAvgIndex] = outputValue;
++pidAvgIndex;
if(pidAvgIndex >= 4)
pidAvgIndex = 0;