Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Your PID controller output should be setting the value of the duty cycle directly.

Basically you are going to be controlling the heater settings based on the difference in the actual temperature versus the temperature setpoint.

You will need to adjust the values of the PID parameters to obtain the performance you are looking for.

First, set I and D to zero and put in a value for P, say 2 to start.

Change the setpoint and see what your response is. Increase P and make another setpoint change and see what happens. Eventually you will see the temperature oscillate consistently and never come to any stable value. This value is known as the "ulitmate gain". Pay attention to the frequency of the oscillation as well. Set P equal to half of the ultimate gain.

Start with a value of 1.2(ultimate gain)/(Oscillation Frequency) for I and change the setpoint. Adjust the values of P and I from those values to get to where you want to go, tracking the process and seeing if increasing or decreasing values improves things.

Once you have P and I you can work on D but depending on the process dynamics giving a value for D might make your life worse.

The Ziegler-Nichols method gives you some guidelines for PID values which should get you in the ballpark. From there you can make adjustments to get better performance.

You will have to weigh the options of having overshoot with the amount of time the temperature takes to reach the new setpoint. The faster the temperature adjusts the more overshoot you will have. To have no overshoot will increase that amount of time considerably.

A few suggestions:

  • You seem to be integrating twice. Once inside your TempCtrl_PID function and once outside. Duty_Cycle += . So now your P term is really I.
  • Start with only a P term and keep increasing it until the system becomes unstable. Then back off (e.g. use 1/2 to 1/4 the value where it becomes unstable) and start adding an I term. Start with very low values on the I term and then gradually increase. This process is a way of tuning the loop. Because the system will probably have a pretty long time constant this may be time consuming...
  • You can add some feed-forward as you suggest (expected duty cycle for a given setpoint - map it out by setting the duty cycle and letting the system stabilize.). It doesn't matter if that term isn't perfect since the loop will take out the remaining error. You can also simply add some constant bias to the duty cycle. Keep in mind a constant wouldn't really make any difference as the integrator will take it out. It will only affect a cold start.
  • Make sure you have some sort of fixed time base for this loop. E.g. make an adjustment every 10ms.
  • I would not worry about the D term for now. A PI controller should be good enough for most applications.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top