Circuit diagram

3Dsimo Kit
Device: 3Dsimo Kit
Rev. 1.00 Page 12/18
/*
* PID variables and constants for tuning
*/
float Kp= 15 , Ki= 1 , Kd= 1.0 , dT = 0.1 , Hz= 10 ;
/*
* 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 ;