I am designing a software PID control. Logic is almost final but I wonder how do I decide the value of \$k_p\$, \$k_i\$ and \$k_d\$. Also, I need to determine the max and min value for the Pterm Item and Dterm. How do I do that?. Also, I am trying to implement the inverse control. Also, am I going in the right way in designing the software PID? Also, if integral time and \$k_i\$ both are same?
The code I wrote so far is given below.
Calculate_Error();
P_Term = Percnt_Error;
P_Term = KP * P_Term;
if(P_Term >= PMAX)
{
P_Term = PMAX;
}
else if(P_Term <= PMIN)
{
P_Term = PMIN;
}
// Integral calculation
if(Integraltime==1) // Take integration at every 1s
{
Integraltime = 0;
Error_Accum = Error_Accum + Percnt_Error; // Error Accumulation over time
if(Error_Accum >= MaxAccumError)
{
Error_Accum = MaxAccumError;
}
if(Error_Accum <= -MinAccumError)
{
Error_Accum = -MinAccumError;
}
I_Term = (Error_Accum)*KI;
if(I_Term >= IMAX)
{
I_Term = IMAX;
}
else if(I_Term <= IMIN)
{
I_Term = IMIN;
}
}
Best Answer
All credit for this is from the (now unfortunately dead) Balancing Robots for Dummies thread on the Arduino forum.
The PID controller is as follows:
This version of 'software PID control' implements the limit on the intergral value (similar to your code) using the constrain function from the Arduino library. Added benefit of this code is that it allows you to choose what from controller you want by changing the return value. E.g. a PD controller would remove the
iTerm
from the return value.As far as tuning goes, it is similar to what Illegal Immigrant said.
This is my first time answering, hopefully I've done this correctly and helped you a little on the way!