Electronic – Kp, Ki, Kd for software PID control

cpid controller

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:

int
updatePID(int setPoint, int currentPoint)
{
    float error;
    static float last_error;
    static float integrated_error;

    float pTerm, iTerm, dTerm;

/*  Calculate error and proportional value.*/
    error = setPoint - currentPoint;
    pTerm = Kp * error;

/*  Calculate error and intergral value.*/
    integrated_error += error;
    iTerm = Ki * constrain(integrated_error, LOWER_LIMIT, UPPER_LIMIT);

/*  Calculate deriviative value and reset error.*/
    dTerm = Kd * (error - last_error);
    last_error = error;

/*  Return the PID controlled value.*/
    return constrain(i32K*(pTerm + iTerm + dTerm)), LOWER_LIMIT, UPPER_LIMIT);

}

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.

Set Ki, Kp, and Kd to 0.
Tune Kp until oscillation occurs.
Reduce oscillation and overshoot by tuning Kd.
Tune Ki to increase the speed of the system.

This is my first time answering, hopefully I've done this correctly and helped you a little on the way!