PID controller Sample Time

Hey guys,

I am working on a project to control a throttle flap using the PID library. However my control process is very slow so I want to change the flap position only every 60 seconds (maximum movement 10 degrees per loop). I tried SetSampleTime from the library but it doesn't work (I guess it only re-evaluates the PID values every 60 seconds now, but the flap still moves from open to close within 2 seconds). Any ideas?

Thanks, Axo

I guess it only re-evaluates the PID values every 60 seconds now

Exactly.

but the flap still moves from open to close within 2 seconds

That doesn’t seem contrary to requirements.

However my control process is very slow so I want to change the flap position only every 60 seconds (maximum movement 10 degrees per loop).

I guess the most obvious question is why you need PID for this, then.

The second most obvious question might be “Where’s your code?”.

If you want to use PID and you say you then want to change flap position every 60 seconds, what you need to do is firstly, operated the PID loop controller at a set rate, maybe use a timer interrupt, but importantly, you will then need to provide a new setpoint into the PID for the new position every 60 odd seconds for it to alter the flap.

Are you sure you need PID to do what you want, you know what PID is about?

Paul

from the library

PID is one of those simple processes that you can roll your code fairly quickly. So why bother with a "library"?

As to sample time, set up a timer interrupt, and use that to set a flag once your desired time has passed.

some PID code on my blog if you are interested.

Paul

Ok, maybe I should explain what this is about:
I use the throttle flap to control the air supply of my charcoal grill. The flap is meant to open / close depending of the temperature deviation (and gradient of temperature changes, that’s why I think the PID is the right tool). As the temperature will only change slowly when the flap position changes, I want this to happen only once a minute. The main part of the code is:

myPID.Compute();
Input = thermocouple.readCelsius();
if (Input < Setpoint-1){

  • myPID.SetControllerDirection(DIRECT);*
  • pos=pos+Output;*
  • pos=min(pos,180);*
  • myservo.write(pos);*
  • Serial.print(pos);*
    }
    else if (Input > Setpoint +1) {
    myPID.SetControllerDirection(REVERSE);
  • pos= pos-Output;*
  • pos=max(pos,90);*
    myservo.write(pos);
    Serial.print(pos);
    }

How would I implement interrupt to this?

Axel

myPID.Compute();

Input = thermocouple.readCelsius();

Do some computing, then get some input. OK.

"You people get busy coding. I'll go see what their requirements are."

I'm not sure why you want to constrain the way PID operates. The idea is that, if sampled adequately often, the Output value should be the amount that the Input needs to change to achieve the desired Setpoint.

You are computing a new Output value, and then using it, or not, depending on the relationship between Input and Setpoint.

The Output value is a temperature, given that Input and Setpoint are both temperatures, isn't it?

Why you expect a temperature to equate to servo position escapes me.

Just quickly, need to go, look at the libraries section, user contributed and you'll find some there. I use TimerOne, it is simple to use, basically you attach a function to the interrupt. You set the interrupt to operate at a specified rate.

Then in that function, you do you pid stuff, remembering that you will need to have some global variables outside of that function to hold some of the pid calcs.

I don't know why you only want the flap to move once every 60 seconds, am I understanding that part right? Why not allow it to freely operate when it needs, that way the pid will work best. If you stop the output, that is the flap from moving then the pid will do a thing called 'wind up' because it is not seeing a change in the pv, say temperature. Then when you do enable the output it will most likely either bang open or bang closed, no fine control there at all.

You do want fine control don't you, say yes :)

Ok, later all, 12:30am here zzzz... Paul

PaulS: ``` myPID.Compute();

Input = thermocouple.readCelsius();



Do some computing, then get some input. OK.

Ok, maybe I should review this sequence...

Please correct me if I am wrong, but as far as I know the output of the PID does not have to be the same unit as the input - in my case for example the Kp would be "degrees (of the servo) / degrees Celsius (deviation)", the resulting output is then a position change of the servo

rockwallaby: I don't know why you only want the flap to move once every 60 seconds, am I understanding that part right? Why not allow it to freely operate when it needs, that way the pid will work best. If you stop the output, that is the flap from moving then the pid will do a thing called 'wind up' because it is not seeing a change in the pv, say temperature. Then when you do enable the output it will most likely either bang open or bang closed, no fine control there at all.

You do want fine control don't you, say yes :)

Ok, later all, 12:30am here zzzz... Paul

That's a good point - so that would mean I don't change the sample time but choose the kp, Ki and Kd small enough to achieve slower changes, right?

Thanks for all your quick responses - and: yes, I want fine control...

Axo

Please correct me if I am wrong, but as far as I know the output of the PID does not have to be the same unit as the input - in my case for example the Kp would be "degrees (of the servo) / degrees Celsius (deviation)", the resulting output is then a position change of the servo

I guess I missed where you did that.

The Output value is a temperature, given that Input and Setpoint are both temperatures, isn't it?

No, the output represents a unitless value that represents an error term to be applied to whatever is used to try and manipulate the process being controlled to try and force the process variable (the input) closer to the setpoint value. At our refinery most all the P&ID loops had unit values for the inputs and setpoints, but outputs were always scaled at 0-100% and would typically represent the value to be sent to a control valve where 0% might mean fully closed control valve and 100% would mean fully opened. For a temperature control loop the output might be manipulating a steam flow control valve or possibly an electrically powered heating element, so again the output is best expressed as a simple value 0-100% of output control range. Any other units of measurement makes no sense for the output term. Lefty

Maybe my description was confusing (you will have realized I am not a native speaker...). Anyway, the output in my case is a [u]change[/u] of the flap position. I don't want it to be the flap position itself beccause I assume that the flap's position will not result in the same temperature every time (it will rather depend on how much charcoal is glowing, ambient temperature, "meat-load"...). So, high deviations are to result in higher (or faster) position changes (or 100% of the maximum allowed change) while slight deviations will cause only small corrections.

Anyway, I will change the tunings to smaller values, leave the sample time untouched and see what happens...

Axo

If you want to ensure that the flap is only moved slowly, then you could achieve that by increasing the authority of the derivative feedback element of the PID.

choose the kp, Ki and Kd small enough to achieve slower changes, right?

Shorter sampling time equates to higher Kp / Ki but lower Kd.

I think your original idea makes a lot more sense. For a slow moving system like yours, it makes more sense to have longer sampling time. If I were you, I would implement your own PID algorithm and being done with that.

Folks no offence but I think you may be confusing the process requirement with the control loop.

PID is designed to control some external variable, be that a flow, level, position, whatever ...

Feedback from the thing you are controlling is generally referred to as the PV Process Variable. The output which manipulates whatever is being controlled is called the MV Manipulated Variable The PID loop is always attempting to get the PV to match the set-point ... The difference between the two is the error term and drives the controller gain directly. The integral term is about time and has a characteristic period, the average error within that period contributes to the MV. The Derivative term is also about, more specifically rate of change, it to contributes the MV.

They are required when the PV is likely to change without an MV change. If the PV is always directly proportional to the MV, like an RC Servo for example, you don't need a PID loop. If you are driving a valve with the RC servo, controlling a fluid flow, then the flow is the PV and a PID loop is almost obligatory.

The PID loop its self should be set up so that it can accurately control its target with an acceptable level of oscillation, settling time and dead band, whilst remaining as responsive and accurate as possible.

Once you have a loop set up and your system, overall, can respond quickly and accurately you can then do whatever is required.

You have a requirement to change the position of a flap slowly and also not to move it at all for specific periods. This should be done by ramping or fixing the set-point not by making the PID loop slow.

If you take this approach it will work in all circumstances other than you asking something to change faster than is physically possible.

Al

The difference between the two is the error term and drives the controller gain directly.

The gains are usually constant and independent of the errors.

The Derivative term is also about, more specifically rate of change, ...

That depends on your particular implementation. There are PID controllers where that derivative term can be either predictive or higher order derivatives (2nd order is fairly common for example).

Hi all, Ah, one cup of café down.

Let's not start a PID war here, but the description Dyslexical is very good and I think the OP may need to do some research himself to better understand what PID is rather than us flinging all kinds of implementations and our own interpretations of PID, and there are a few, I know. He will only get more confused.

It appears the OP knows what he wants to achieve.

Paul

Dyslexicbloke: This should be done by ramping or fixing the set-point not by making the PID loop slow.

I'd agree with "This can be done ...", but I don't agree with your assertion that this is the only correct approach.

The PID algorithm is capable of controlling the dynamic behaviour of a linear system i.e. controlling the acceleration and deceleration as well as the position. You can take control of that outside the PID if you want, but it's not wrong to use the PID to do it.

Sorry I don't see your point or rational! Perhaps I am missing the point because all the loops I have dealt with to date are lightweight or something.

If you increase the derivative influence to the point where a few degrees of movement takes 10 mins you are simply hog tying the whole system. You may have a ramp but you cant control its duration or rate, well not without effort and complexity. If you fiddle with it enough you might find settings that get the system to where you want in approximately the time you want but it isn't going to stay there because it cant adjust quickly enough. When some external influence causes a positional error it will take a long time to correct the error.

I don't understand why you would want to make things harder, less accurate and less flexible all for the want of a counter. But that's just me....