Example Click PID

Created Date: February 17,2011

Created By: bcarlton

**** This post has been imported from our legacy forum. Information in this post may be outdated and links contained in the post may no longer work.****

This is a Click program (the 'main ' is actually empty) with a PID calculation in the interrupt subroutine. It is called based on a timed interrupt. This is set up according to the PDF referenced in this posting You may want to check the claculation of the error in the first math box. Different types of PID (forward versus reverse acting) will calculate this in the opposite manner.


Garry likes this.
  • adccommunitymod (AutomationDirect)

    Created Date: August 01,2011

    Created by: Dave911

    A couple of typos..

    Here is a correction to this program. I would just like to correct the errors since it might drive a newbie nuts. :)

  • adccommunitymod (AutomationDirect)

    Created Date: September 05,2011

    Created by: keeter1

    I have tried this loop program and it doesn't seem to work. If there are lines in the main program that need to be there please post those also.

    In a flow control application, using a throttling valve, as the process variable and the setpoint reach parity the output goes to zero. I have done PID loops before in several different PLC's and first observation this appeared to be what I needed. I have a 4-20ma generator and when I went to test my loop it did not respond as expected.

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: September 05,2011

    Created by: bcarlton

    You will have to be a little more specific than "it doesn't seem to work ".

    Are you sending the Process Variable to DF1?

    Do you have proportional, integral and derivative gains set?

    What type of numbers do you get in the Control variable?

    All these are functions which are specific to your application. This is only a generalized routine.

    The movement of the Information from you measuring device into the Process Variable and the movement of the value from the Control Variable to your actuation device are up to you.

    Be specific please.

    "it did not respond as expected. "

    Exactly how did it respond?

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: September 05,2011

    Created by: keeter1

    i am also reading thru the PID without a phd that you had the link to.

  • adccommunitymod (AutomationDirect)

    Created Date: September 05,2011

    Created by: keeter1

    i am using df1 as my position input and have it scaled 4-20ma between 0-100, i am guessing that would be between 0-100%. i am not using the derivative term in my calculations. loops that i have done before, siemens plc's, have not needed it. i have a 0-20ma generator that i am using as my input. if i have my setpoint at 50 and my in put at 12ma the signal to my output will go to 0. if i have it either just under or just over it will go to the max in the opposite direction, using a reverse acting loop. i have put in the gain anywhere from .001 to 1 and it doesn't make any difference.

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: March 25,2012

    Created by: kb1gtt

    I'm new to the Click and I'm evaluating it's use for a control system. I found this "PID revised.zip " file and looked it over. I see the key areas of interest, as

    DF1 = process variable input,

    DF2 = set point input,

    DF4 = calculated control output,

    DF6 = I max windup input,

    DF7 = I min windup input,

    DF8 = P gain input,

    DF9 = I gain input,

    DF10 = D gain input

    The time base is set by how often this interrupt is processed. In the downloaded file, in the downloaded file, it's hard coded to once every 100mS. This hard coded can be changed during compile time, however it appears to me like it is hard coded.

    I see this is a parallel algorithm, so changing the gain of P does not effect the gain of I or D. Also a gain of I or D of 0, will simply cancel out those effects.

    Can that 100mS be accessed via a rung? I have used the D06 series PLC many times, I have often allowed OIT adjustments of the PID and time base. Could I allow an adjustable time base via OIT some how?

    I also see that DF20 and DF21 appear to be used somewhere, but I don't know where, or why they might be used. Can someone point me to where they are used? Perhaps they are stray address that should be removed? I'm not sure.

    I do not currently have a Click, however I will likely pickup the C0-00DD2-D in the near future.

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: March 26,2012

    Created by: Dave911

    >>I also see that DF20 and DF21 appear to be used somewhere

    In the Click software, look under Function - CPU Built in I/O setup and look at the analog I/O. They AI's are assigned to use DF20 and 21, the outputs use DF3 and 4.

    Honestly I don't know why you would want to put the PID cycle time on an OIT and the interrupt setting is set via the Click programming software. However you could program it such that even though the interrupt is triggered ever .1 seconds that the PID loop was only executed every other other interupt or every third interrupt etc.

    The PID works ok for simple tasks and you can always add to it to incorporate limits, etc. It is hard to beat the Click PLC on value if it meets your needs.

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: August 03,2013

    Created by: nik.martin

    I know this is an old thread, but pre-sales, I'm trying to see if I can do what I need to with a CLICK PLC - they are very impressive devices for sure. I wrote a PID slightly different but with essentially the same output, the derivative calculation in mine uses PV delta instead of PE delta to avoid Derivative Kick. The problem is with input and output scaling - If I use the RTD module with a PT100, what does the PV look like? I'm storing the output of RTD CH 1-4 in DF1-4, and everything I'm reading says the value stored is the actual temperature as a float, not the 16 bit AD signal. If I feed that value into the PID as the PV, and have a pGain of 2, and iGain of .02, and a dGain of two, with a SP of 350 and a PV of 70, I get 561.6 as the output of the PID - If I'm controlling a discrete output, how do I convert 561 into a duty cycle? What scale is 561 in? It's rather arbitrary, unless I set a lower and upper temperature limit, like 70F for the low end, and 212 for the high end, since I'm heating water. My SP will always be between 105 and 212 F. Since in my model I have the PV at 70F and a SP of 350, I can safely assume that 561 is 100% output, and even may be outside the bounds of the output. I have not set an iMax or iMin value, so 561 is un-bounded.

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: August 03,2013

    Created by: Dave911

    >>First of all, we don't really need to know what you are controlling

    Tinker is right... we need to know what you are controlling.

    Most gas valves are either on or off, unless you have some type of modulating gas valve to vary gas flow and heat input to the water heater. If that is the case (you have an on off valve), then you really need thermostat type control vs a PID loop. That is how most residential, smaller commercial water heaters operate since the gas valve is usually either on or off. Some bigger heaters might have multiple burners and multiple gas valves to vary capacity.. but again we don't know what you really have.

    Dave

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: August 04,2013

    Created by: nik.martin

    First of all, we don't really need to know what you are controling

    Agreed. Fixed in OP

    Since you "wrote a PID slightly different " it would probably help if you either posted your code or at least a detailed description of it. Even small differences in an alogorithm can make a large diference in the output, so it seems to me it would be hard to answer your question about the number you are getting without seeing how you are getting it.

    Since Click IDE doesn't have a handy way of copying ladder code, I 've expressed it here as 1 line = 1 rung:

    Each PID loop is an interrupt routine on a 1 second interrupt, so there aren't any sampling time calculations, since sample time is constant @1sec.

    As a reminder, pGain = 2, iGain = .02, dGain = 2 in this example. Totally arbitrary, but used to make calculations easier to test.

    Calculate PE: SP - PV -> Store in var 'PE '

    Calculate pTerm: PE * pGain -> Store in var 'pTerm '

    Calculate Integral= PE + Integral -> Store in var Integral

    Calculate iTerm = Integral * iGain - Store in var iTerm

    Calculate dTerm = (PV - Derivative) * dGain -> Store in dTerm

    Calculate Derivative: Derivative = PV -> Store in var Derivative

    Calculate PID Control Output: pTerm + iTerm - dTerm -> Store in var PIDOUT

    EDIT:

    Well, I ran your numbers through Dave911's version of the alogorithim, and the first pass gave an output of 565.6, pertty much just your prop gain * the (large) error, since there was only one iteration and a 0.02 integral gain the other terms are pretty much negligable.

    2nd EDIT, Opps, I screwed up my calculation, on the first iteration the Current_derivited is uninitilzed (presumably 0) so the Derivative_term = 2 (Dgain) * (0 - 70) = -140

    Proportional_term (2 * error = 560) + Derivative_term (-140) (ignoringthe small integral_term (small on the first iteration anyway, but will rapidly increse) = 420, nothing like your number, but then the first iteration with uniitialzed varaibles is pretty useless. So, I'm curious how you got your numbers, without limiting, multiple iterations will result in a rapid integral wind up

    Using the function described, I made a spreadsheet using a SP of 350, and initial PV of 70, assuming PV is never 0 (that PV is initialized by the RTD module before PID loop runs), here is the first ten loops as calculated by a spreadsheet:

    SP PV PE pGain Pt I Igain It D dGain Dt PID % OUT Time ON

    350 70.00 280.00 2 560.00 280.00 0.02 5.60 0.00 2.00 0.00 565.60 1.00 30.00

    350 77.17 272.83 2 545.67 272.83 0.02 5.46 7.17 2.00 14.33 536.79 0.95 28.47

    350 84.33 265.67 2 531.33 538.50 0.02 10.77 7.17 2.00 14.33 527.77 0.93 27.99

    350 91.50 258.50 2 517.00 797.00 0.02 15.94 7.17 2.00 14.33 518.60 0.92 27.51

    350 98.67 251.33 2 502.66 1,048.33 0.02 20.97 7.17 2.00 14.33 509.30 0.90 27.01

    350 105.84 244.17 2 488.33 1,292.49 0.02 25.85 7.17 2.00 14.33 499.85 0.88 26.51

    350 113.00 237.00 2 474.00 1,529.49 0.02 30.59 7.17 2.00 14.33 490.25 0.87 26.00

    350 120.17 229.83 2 459.66 1,759.32 0.02 35.19 7.17 2.00 14.33 480.51 0.85 25.49

    350 127.34 222.66 2 445.33 1,981.99 0.02 39.64 7.17 2.00 14.33 470.63 0.83 24.96

    350 134.50 215.50 2 430.99 2,197.49 0.02 43.95 7.17 2.00 14.33 460.61 0.81 24.43

    So, from my earlier post, if I assume that MAX SP/PV of 350, then scaling the output to output/MAXPID will give me a percentage output which can easily be turned into a control output duty cycle, but it will change as the PID gains are tuned, so I may want to dynamically calculate MAXPID by taking max setpoint and use the current gains to set MAXPID

    Am I waaaay off?

    Expand Post
10 of 30