Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

PID algorith 3

Status
Not open for further replies.

LHT

Electrical
Jun 5, 2001
1
Dear experts,
I am looking for PID algorith, writing by ASM ( for MSC-51 best)
Can any one help me

Le_Tien@hotmail.com
 
Replies continue below

Recommended for you

le tein
what sort of algorithym ? PID I understand but writing by asm ...msc-51 the best. Is that an instrument device, asssembler or specific machine?
The pid algorithym is straight forward but you'll have to convert it to your device.
all calcs are with respect to error. input -setpoint
out put = gainsection + intergral section + derivative section +offset constant (if any)

1) calc error. also store as error now error and existing value from last cycle as last error
2) calc gain section nowerror * gain value
3) calc intergral section check time interval has elapsed then add error gain section to itself (the prior value)(this is your repeats per time interval)
4) calc the change since last sample then project it over the derivative time setting ( your last error - now error - last error)

If you add all these together then add any offset value, you have a functional pid controller.

You will need to fiddle with the time constants and use some scaling to give the result a look in keeping with a commercial unit but it isn't that hard just "fiddly"

Regards
Don
 
I agree, writing your own PID control algorithm is usually the best solution, and not too difficult to do, as long as your controller (I don't know the MSC-51) gives you the tools (add, subtract, multiply, divide, timer and somewhere to store values from the previous calculation).

You'll need to consider the application. The PID output will need to be set to incremental (eg valve control), or added to / subracted from your previous output (motor speed control), to suit. You also need to think about what your PID control should do if you go into manual control, or stop your controlled device (pump, valve or whatever) from running - should you hold the last value, track what the device does, or set the output to zero - you don't what the integral term to keep on accumulating....

I find it useful to be able to adjust the gain of the intgral term independantly of the gain for the proportional term - helps when commissioning.

Warning:
My experience of commercial PID control algorithms is that they all implement slightly differently. I've come across one commercial controller where the built-in PID algorithm had been rewritten to act on the rate of change of the error rather than the error itself! This introduces a lead in the control function. Although this would be OK for heating control, it introduced some weird instability effects when we tried using it for flow control. - So it's better to know the maths of the PID function (by writing it yourself) than rely on someone else implementing it properly! :)
 
DON1 had the right idea but to restate:

Error = Input - Setpoint (or Setpoint - Input)

ErrorTotal = ErrorTotal + Error (once per integral timeframe)

ErrorNew = Error - LastError (once per derivative timeframe)
LastError = Error (once per derivative timeframe)

P = Error * Pgain
I = ErrorTotal * Igain
D = ErrorNew * Dgain
Output = P + I + D (Limit the output between 0% and 100%)

Also a note on things that I have implemented in developing my own PIDs. If the proportional term is 100%, reset the ErrorTotal that integral is being calculated from. If the PI term is 100%, disable any additions to the ErrorTotal that are positive. Use the same logic for both of these statements when the output is 0%. The idea is to keep the integral from running away during startup of the PID or large step changes in the setpoint. I have never needed derivative.
 
hi all
I'm back again , McAden has probably made my somewhat rushed post look a lot neater and thanks are extended.

Just to highlight a couple of small issues for those who desire to write something like this algorithym.

take the three parts and lets consider them.
P is the straight out gain of error (beta or hfe of your transistor)
Intergral is the lagging component or inductive device in the electronic circuit.
Derivative is the capacitive or leading effect.

for Proportional
to generate the output you take the error as its NOW and multiply it by the gain. For a P only controller that is the output. ( Analogy = lever arm on float connected to valve) controller value is generally called gain or multiplier

for Intergral
you are trying to calculate the area under the error curve.
to generate this output time is the issue. We need to take the error and repeat at the setting time interval. Hence we would use this error NOW and add it back to the output that existed at the last time interval. for a I only control function this would be the output (Analogy = a bellows on same side of that lever arm as the process) controller value is gnerally called repeats per minute or reset rate or minutes per repeat.

for derivative
(the fun one) you are now predicting the future value of the error
take this error, take the last error get the change of these two and extrapolate that over the setting time, this would be your error for the control action. What this is achieving is to apply the output needed for where the error will be when the time period "expires" (Analogy pressing the accelator down before your car gets to the hill so it doesn't slow down) Ever noticed how smart coppers hide just at the bottom of the hill and book you. controller value is generally in minutes.

forward or reverse action.
This is dependent on your process if the error increases as the output increases then multiply the error by -1 when first calculated or at the end of the calcs

Gain in all terms. I personally would discourage the practice of putting independant values for p into the intergral or derivative terms. Yes it will work but you would negate the value of external calculations (eg bode plot) and what you are really doing is changing the values of your (I)repeats / min or (D) minutes "forecast" Just change your tuning values. and tuneing(how do you spell that) is another isssue again.

Bumpless transfer, reset windup, prescaling , output limiting. All these are top features to put into your system but get the pid block down so it appears smooth first then fit what you need around it.

Using derivative. this can be a really handy bit of stuff for those with a light touch. Most processes have a delay - particularly heating and cooling. watch the system carefully and note the lag, now add about 10-20% of the lag time to the derivative control and take about the same off the gain. The process should flatten out a little more or go slightly forward. Now apply the half of the half rule and trim your tuning. I have found in say 25 years that about 30% of control loops have benefitted from a SMALL amount of derivative.

please I am not trying to knock others here but this one of the few areas I have a passion about and love to pass on the little I do know.

Let us know how it goes
Don
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor