Have used the basic PID library to run a heater with a Ds18b20 sensor and it seems to work ok.
However I need to incorporate it into a much larger program, eg controlling 4 other heaters / sensors with just simple on/off control etc etc.
Though the heaters are relatively slow devices, not sure how critical the frequency of the returning to the PID routine needs to be ?
Does it have to be a constant loop return time, eg always an even number of ms or seconds and is there a problem if the return time is long , say every 5 or 10 seconds ?
this is not an answer to your question.
since temp control is so slow, and since you do not need I or D in your control, you may be able to use pure P.
to get a feel for your control, you can start a timer when there is little control output change and the temp is very close to setpoint.
see how often it is in that state, or how often it is not.
also, you can make a simple fuzzy logic control that looks at the closeness of the proecess. if more that say, 20% out, make a full step change in output, wait for it to settle. if less than 5% out, make a much smaller change, if less than 1%, make a very small change.
it is sloppy, but since you would be using timers to watch time of settling, you could do lots of other things.
also, a Mini or pro-mini is only about $2. you can use a dedicated processor.
The PID library relies on being called frequently - see here Sample time. It makes it's own decision about when it's time to calculate so you need to call it rather more frequently than whatever your sample time is set to.
All of which means that you need to avoid use of delay in your code and use the blink without delay method to control your other items. You may find it helpful to call the PID routine on every iteration of loop and just pick one of your other items to do on each iteration if they are time consuming.
wildbill:
The PID library relies on being called frequently - see here Sample time.
I'm not sure that that is true. In the link all it says is
Ensure that the PID is called at a regular interval
In a heating project I am working on the performance was much improved when I changed the update interval from 1 second to 5 seconds. But I am not yet finished with my explorations.
It looks to me as though frequent calls are required: the PID library is assuming that the calculation is happening every SampleTime milliseconds. It keeps track of when it last calculated and Once SampleTime is exceeded it's time to do the calc again.
To make sure that the PID's assumption about sample time is actually true, it needs to be able to check frequently whether it's time to calculate again. If it thinks it's calculating every 100 milliseconds, but you only get to it every half a second, the PID calculation will be off.
The "general rules of thumb" are that the PID output calculation should be called at time intervals of 1/10 of the system response time (or faster), and at regular intervals.
For a room heater, one call per minute might be fine.
jremington:
The "general rules of thumb" are that the PID output calculation should be called at time intervals of 1/10 of the system response time (or faster), and at regular intervals.
I wonder if there is any point in calling it more frequently than the input data can change?
Robin2:
I wonder if there is any point in calling it more frequently than the input data can change?
...R
If the input is theoretically a step-input, then that'd be an infinite slope at the theoretical step. They do have rules of thumb about taking samples at a high enough rate to ensure that the control system is able to behave in the way we want. For some systems, sampling at a rate of 5 times the reciprocal of a first order time-constant might not be enough to make a sampled-data system behave like a continuous time version of the system. A sampling rate of 10 times the reciprocal of the time-constant is usually better (ie. a sampling period 10 times smaller than the time-constant). But you're right..... sampling at some relatively fast rate where system performance doesn't get much better than some minimal optimum rate is probably just waste of resources and time etc.
Southpark:
sampling at some relatively fast rate where system performance doesn't get much better than some minimal optimum rate is probably just waste of resources and time etc.
With all due respect, I was not asking (in Reply #6) what sampling rate should be used.
Am I correct to think you are saying two separate things here.
{A} there is no value in updating the PID calcs more often than new input data are collected
{B} the PID calcs should not be done at more often than half the update rate of the sensor
I'm not sure I understand the second one. Do you mean that if I sample a temperature every 6 seconds I should only update the PID calcs every 12 seconds?
And (a separate but related question) if the heating rate is such that the temperature will never increase by 1°C in less than 6 seconds are you saying that I should test the temperature every 3 seconds and call the PID function every 6 seconds? (Of course I am assuming that 1°C steps are sufficient)
Robin2:
With all due respect, I was not asking (in Reply #6) what sampling rate should be used.
You wrote something about input data. I assumed you were talking about data related to system input.... like applied voltage or current. Due respect accepted.
Robin2:
{A} there is no value in updating the PID calcs more often than new input data are collected
Worse than "no value". PID has history (the integral). Feeding it stale data poisons the history (causes wind-up).
All the systems I touched would automatically switch a PID loop to manual and generate an alarm if the input was stale at the time the loop was updated.
{B} the PID calcs should not be done at more often than half the update rate of the sensor
That's what I remember.
Do you mean that if I sample a temperature every 6 seconds I should only update the PID calcs every 12 seconds?
Correct.
And (a separate but related question) if the heating rate is such that the temperature will never increase by 1°C in less than 6 seconds are you saying that I should test the temperature every 3 seconds and call the PID function every 6 seconds? (Of course I am assuming that 1°C steps are sufficient)
"Change" instead of "increase" (controlling temperature is bidirectional).
Robin2:
But I don't understand the value of taking samples at 6 second intervals when I only using every second one.
Excellent question. Took me a while to remember the answer.
It's bedtime so I will leave you with a puzzle (one question) instead of an explanation (probably three short paragraphs)... What assumption are you making about how the input value is acquired?
Simple. When the interval has elapsed read the thermistor with the ADC and convert the value to °C
My practical tests have suggested that it makes no sense to read the temperatures faster than they can change. It works much better with samples at 6 seconds rather than 1 second and I assume that is because there is not sufficient change in the temperature in 1 second even when the heating effect is strong.
jremington:
At some point, you may rediscover facts about sampling and the "system time constant" that have been understood for decades.
If that means that you know something relevant to this that I don't know perhaps you would be kind enough to share it?
For this heating project my sampling time is very constant. But I have another motor control project that works very well and the sample time varies from 4000 to 40000 µsecs depending on the speed the motor is running at.
Yes with the onboard ADC, but I can't see what that has to do with whether I should use every sample, or just every alternate sample.