How do I use a PID controller?

thecooltodd picture thecooltodd · Jan 30, 2013 · Viewed 13.9k times · Source

I'm currently working on a temperature controller.

I have a Temperature_PID() function that returns the manipulated variable (which is the sum of the P, I, and D terms) but what do I do with this output?

The temperature is controlled by PWM, so 0% duty cycle = heater off and 100% duty cycle = heater on.

So far I tried

Duty_Cycle += Temperature_PID();
if(Duty_Cycle > 100) Duty_Cycle = 100;
else if(Duty_Cycle < 0) Duty_Cycle = 0;

This didn't work for me because the I term is basically makes this system very unstable. Imagine integrating an area, adding another small data point, and integrating the area again, and summing them. Over and over. That means each data point makes this control scheme exponentially worse.

The other thing I would like to try is

Duty_Cycle = Expected_Duty_Cycle + Temperature_PID();

where Expected_Duty_Cycle is what the temperature should be set to once the controller reaches a stable point and Temperature_PID() is 0. However, this also doesn't work because the Expected_Duty_Cycle would always be changing depending on the conditions of the heater, e.g. different weather.

So my question is what exactly do I do with the output of PID? I don't understand how to assign a duty cycle based on the PID output. Ideally this will stay at 100% duty cycle until the temperature almost reaches the set point and start dropping off to a lower duty cycle. But using my first method (with my I gain set to zero) it only starts lowering the duty cycle after it already overshoots.

This is my first post. Hope I find my answer. Thank you stackoverflow.

EDIT: Here's my PID function.

double TempCtrl_PID(PID_Data *pid)
{
    Thermo_Data tc;
    double error, pTerm, iTerm, dTerm;

    Thermo_Read(CHIP_TC1, &tc);

    pid->last_pv = pid->pv;
    pid->pv = Thermo_Temperature(&tc);
    error = pid->sp - pid->pv;
    if(error/pid->sp < 0.1)
        pid->err_sum += error;

    pTerm = pid->kp * error;
    iTerm = pid->ki * pid->err_sum;
    dTerm = pid->kd * (pid->last_pv - pid->pv);

    return pTerm + iTerm + dTerm;
}

EDIT 2: Never used this before so let me know if the link is broken. https://picasaweb.google.com/113881440334423462633/January302013

Sorry, Excel is crashing on me when I try to rename axes or the title. Note: there isn't a fan in the system yet so I can't cool the heater as fast as I can get it to heat up, so it spends very little time below the set point compared to above. The first picture is a simple on-off controller. The second picture is my PD controller. As you can see, it takes a lot longer for the temperature to decrease because it doesn't subtract before the temperature overshoots, it waits until the temperature overshoots before subtracting from the duty cycle, and does so too slowly. How exactly do I tell my controller to lower the duty cycle before it hits the max temperature?

Answer

Doug Currie picture Doug Currie · Jan 30, 2013

The output of the PID is the duty cycle. You must adjust kp, ki, and kd to put the PID output in the range of the Duty_Cycle, e.g., 0 to 100. It is usually a good idea to explicitly limit the output in the PID function itself.

You should "tune" your PID in simple steps.

  1. Turn off the integral and derivative terms (set ki and kd to zero)

  2. Slowly increase your kp until a 10% step change in the setpoint makes the output oscillate

  3. Reduce kp by 30% or so, which should eliminate the oscillations

  4. Set ki to a fraction of kp and adjust to get your desired tradeoff of overshoot versus time to reach setpoint

  5. Hopefully, you will not need kd, but if you do, make it smaller still