1 | #*------------------------------------------------------------------- |
---|
2 | * EMSO Model Library (EML) Copyright (C) 2004 - 2007 ALSOC. |
---|
3 | * |
---|
4 | * This LIBRARY is free software; you can distribute it and/or modify |
---|
5 | * it under the therms of the ALSOC FREE LICENSE as available at |
---|
6 | * http://www.enq.ufrgs.br/alsoc. |
---|
7 | * |
---|
8 | * EMSO Copyright (C) 2004 - 2007 ALSOC, original code |
---|
9 | * from http://www.rps.eng.br Copyright (C) 2002-2004. |
---|
10 | * All rights reserved. |
---|
11 | * |
---|
12 | * EMSO is distributed under the therms of the ALSOC LICENSE as |
---|
13 | * available at http://www.enq.ufrgs.br/alsoc. |
---|
14 | *-------------------------------------------------------------------- |
---|
15 | * Author: Tiago Osório |
---|
16 | * $Id: PIDs.mso 771 2009-06-18 19:28:00Z bicca $ |
---|
17 | *-------------------------------------------------------------------*# |
---|
18 | using "types"; |
---|
19 | |
---|
20 | |
---|
21 | Model PID |
---|
22 | |
---|
23 | ATTRIBUTES |
---|
24 | Pallete = true; |
---|
25 | Icon = "icon/PID"; |
---|
26 | |
---|
27 | PARAMETERS |
---|
28 | |
---|
29 | PID_Select as Switcher (Brief="Type of PID Incremental", Valid=["Ideal","Parallel","Series","Ideal_AWBT","Parallel_AWBT","Series_AWBT","Ideal_AW","Parallel_AW","Series_AW"], Default = "Ideal"); |
---|
30 | Action as Switcher (Brief="Controller action", Valid=["Direct","Reverse"], Default = "Reverse"); |
---|
31 | Mode as Switcher (Brief="Controller mode", Valid=["Automatic","Manual"], Default = "Automatic"); |
---|
32 | Clip as Switcher (Brief="Controller mode", Valid=["Clipped","Unclipped"], Default = "Clipped"); |
---|
33 | |
---|
34 | alpha as positive (Brief="Derivative term filter constant", Default=1); |
---|
35 | beta as positive (Brief="Proportional term setPoint change filter"); |
---|
36 | bias as control_signal (Brief="Previous scaled bias", Default=0.5); |
---|
37 | derivTime as time_sec (Brief="Derivative time constant"); |
---|
38 | intTime as time_sec (Brief="Integral time constant"); |
---|
39 | gain as positive (Brief="Controller gain", Default=0.5); |
---|
40 | gamma as positive (Brief="Derivative term SP change filter"); |
---|
41 | tau as time_sec (Brief="Input filter time constant"); |
---|
42 | tauSet as time_sec (Brief="Input filter time constant"); |
---|
43 | MinInput as control_signal (Default=-1000); |
---|
44 | MaxInput as control_signal (Default=1000); |
---|
45 | MinOutput as control_signal (Default=-1000); |
---|
46 | MaxOutput as control_signal (Default=1000); |
---|
47 | |
---|
48 | VARIABLES |
---|
49 | in Input as control_signal (Protected=true, PosX=0, PosY=0.5); |
---|
50 | out Output as control_signal (Protected=true, PosX=0.54, PosY=1); |
---|
51 | SetPoint as control_signal; |
---|
52 | |
---|
53 | #++++++++++++++++++++ PID Internal Variables ++++++++++++++++++++++++++++++++ |
---|
54 | PID_derivTerm as control_signal (Brief="Derivative term", Hidden =true , Default=0); |
---|
55 | PID_dFilt as control_signal (Brief="Derivative term filtered", Hidden =true ,Default=0.5); |
---|
56 | PID_error as control_signal (Brief="Error definition for proportional term",Hidden =true ); |
---|
57 | PID_errorD as control_signal (Brief="Error definition for derivative term", Hidden =true ); |
---|
58 | PID_errorI as control_signal (Brief="Error definition for integral term", Hidden =true); |
---|
59 | PID_inputFilt as control_signal (Brief="Filtered input", Hidden =true); |
---|
60 | PID_intTerm as control_signal (Brief="Integral term", Hidden =true , Default=0); |
---|
61 | PID_outp as control_signal (Brief="Sum of proportional, integral and derivative terms", Hidden =true ); |
---|
62 | PID_outps as control_signal (Brief="Variable outp scaled between -1 and 1",Hidden =true); |
---|
63 | PID_propTerm as control_signal (Brief="Proportional term", Default=0 , Hidden =true ); |
---|
64 | PID_setPointFilt as control_signal (Brief="Filtered setPoint", Default=0, Hidden =true); |
---|
65 | PID_AWFactor as Real (Brief="Integral term multiplier used in anti-reset windup", Hidden=true); |
---|
66 | PID_action as Real (Protected=true, Hidden=true); |
---|
67 | |
---|
68 | PID_input as control_signal (Brief="Previous scaled input signal", Default=0.5, Hidden=true); |
---|
69 | PID_output as control_signal (Brief="Scaled output signal", Default=0.5, Hidden=true); |
---|
70 | PID_setPoint as control_signal (Brief="Scaled setPoint",Default=0.5, Hidden=true); |
---|
71 | #++++++++++++++++++++++++++++++++++++++++++++++++++++ |
---|
72 | |
---|
73 | EQUATIONS |
---|
74 | |
---|
75 | "Input " |
---|
76 | PID_input*(MaxInput - MinInput) = Input - MinInput; |
---|
77 | |
---|
78 | "Output " |
---|
79 | Output = PID_output*(MaxOutput-MinOutput) +MinOutput; |
---|
80 | |
---|
81 | "Set Point " |
---|
82 | PID_setPoint*(MaxInput - MinInput) = SetPoint - MinInput; |
---|
83 | |
---|
84 | INITIAL |
---|
85 | |
---|
86 | PID_intTerm = 0; |
---|
87 | |
---|
88 | diff(PID_dFilt) = 0/'s'; |
---|
89 | |
---|
90 | diff(PID_inputFilt) = 0/'s'; |
---|
91 | |
---|
92 | diff(PID_setPointFilt) = 0/'s'; |
---|
93 | |
---|
94 | EQUATIONS |
---|
95 | |
---|
96 | if (tau equal 0) then |
---|
97 | "Input first order filter" |
---|
98 | (tau + 1e-3*'s')*diff(PID_inputFilt)= PID_input - PID_inputFilt; |
---|
99 | else |
---|
100 | "Input first order filter" |
---|
101 | tau*diff(PID_inputFilt)= PID_input - PID_inputFilt; |
---|
102 | end |
---|
103 | |
---|
104 | if (tauSet equal 0) then |
---|
105 | "setPoint first order filter" |
---|
106 | (tauSet + 1e-3*'s')*diff(PID_setPointFilt)= PID_setPoint - PID_setPointFilt; |
---|
107 | else |
---|
108 | "setPoint first order filter" |
---|
109 | tauSet*diff(PID_setPointFilt)= PID_setPoint - PID_setPointFilt; |
---|
110 | end |
---|
111 | |
---|
112 | switch Mode |
---|
113 | case "Manual": |
---|
114 | "Error definition for proportional term" |
---|
115 | PID_error = PID_inputFilt*(beta-1.0); |
---|
116 | "Error definition for derivative term" |
---|
117 | PID_errorD= PID_inputFilt*(gamma-1.0); |
---|
118 | "Error definition for integral term" |
---|
119 | PID_errorI= 0; |
---|
120 | case "Automatic": |
---|
121 | "Error definition for proportional term" |
---|
122 | PID_error = beta*PID_setPointFilt - PID_inputFilt; |
---|
123 | "Error definition for derivative term" |
---|
124 | PID_errorD = gamma*PID_setPointFilt - PID_inputFilt; |
---|
125 | "Error definition for integral term" |
---|
126 | PID_errorI = PID_setPointFilt-PID_inputFilt; |
---|
127 | end |
---|
128 | |
---|
129 | "Calculate proportional term" |
---|
130 | PID_propTerm=PID_error; |
---|
131 | |
---|
132 | if (derivTime equal 0) then |
---|
133 | "Derivative term filter" |
---|
134 | alpha*(derivTime + 1e-3*'s')*diff(PID_dFilt) = PID_errorD - PID_dFilt; |
---|
135 | else |
---|
136 | "Derivative term filter" |
---|
137 | alpha*(derivTime)*diff(PID_dFilt) = PID_errorD - PID_dFilt; |
---|
138 | end |
---|
139 | |
---|
140 | "Calculate derivative term" |
---|
141 | PID_derivTerm = derivTime*diff(PID_dFilt); |
---|
142 | |
---|
143 | "Scale outp" |
---|
144 | PID_outps=2*PID_outp-1; |
---|
145 | |
---|
146 | switch Clip |
---|
147 | case "Clipped": |
---|
148 | if abs(PID_outps)>1 then |
---|
149 | "Calculate clipped output when it´s saturated" |
---|
150 | PID_output=(sign(PID_outps)*1+1)/2; |
---|
151 | else |
---|
152 | "Calculate clipped output when it´s not saturated" |
---|
153 | PID_output=PID_outp; |
---|
154 | end |
---|
155 | case "Unclipped": |
---|
156 | "Calculate unclipped output" |
---|
157 | PID_output=PID_outp; |
---|
158 | end |
---|
159 | |
---|
160 | switch Action |
---|
161 | case "Direct": |
---|
162 | PID_action = -1.0; |
---|
163 | case "Reverse": |
---|
164 | PID_action = 1.0; |
---|
165 | end |
---|
166 | |
---|
167 | |
---|
168 | switch PID_Select |
---|
169 | |
---|
170 | case "Ideal_AW": |
---|
171 | |
---|
172 | "Calculate integral term with anti-windup" |
---|
173 | intTime*diff(PID_intTerm) = PID_AWFactor*PID_errorI; |
---|
174 | |
---|
175 | "Sum of proportional, integral and derivative terms" |
---|
176 | PID_outp = bias + PID_action*gain*(PID_propTerm + PID_intTerm + PID_derivTerm); |
---|
177 | |
---|
178 | if abs(PID_outps)>1 and (PID_action*sign(PID_outps)*PID_errorI)>0 then |
---|
179 | "Calculate AWFactor" |
---|
180 | PID_AWFactor=-tanh(sign(PID_outps)*PID_outps*100-102); |
---|
181 | else |
---|
182 | "Calculate AWFactor" |
---|
183 | PID_AWFactor=1; |
---|
184 | end |
---|
185 | |
---|
186 | case "Parallel_AW": |
---|
187 | |
---|
188 | "Calculate integral term with anti-windup" |
---|
189 | intTime*diff(PID_intTerm) = PID_AWFactor*PID_errorI; |
---|
190 | |
---|
191 | "Sum of proportional, integral and derivative terms" |
---|
192 | PID_outp = bias + PID_action*(gain*PID_propTerm + PID_intTerm + PID_derivTerm); |
---|
193 | |
---|
194 | if abs(PID_outps)>1 and (PID_action*sign(PID_outps)*PID_errorI)>0 then |
---|
195 | "Calculate AWFactor" |
---|
196 | PID_AWFactor=-tanh(sign(PID_outps)*PID_outps*100-102); |
---|
197 | else |
---|
198 | "Calculate AWFactor" |
---|
199 | PID_AWFactor=1; |
---|
200 | end |
---|
201 | |
---|
202 | |
---|
203 | case "Series_AW": |
---|
204 | |
---|
205 | "Calculate integral term with anti-windup" |
---|
206 | intTime*diff(PID_intTerm) = PID_AWFactor*PID_errorI; |
---|
207 | |
---|
208 | "Sum of proportional, integral and derivative terms" |
---|
209 | PID_outp = bias + PID_action*(gain*(PID_propTerm + PID_intTerm)*(1 + PID_derivTerm)); |
---|
210 | |
---|
211 | if abs(PID_outps)>1 and (PID_action*sign(PID_outps)*PID_errorI)>0 then |
---|
212 | "Calculate AWFactor" |
---|
213 | PID_AWFactor=-tanh(sign(PID_outps)*PID_outps*100-102); |
---|
214 | else |
---|
215 | "Calculate AWFactor" |
---|
216 | PID_AWFactor=1; |
---|
217 | end |
---|
218 | |
---|
219 | case "Ideal": |
---|
220 | |
---|
221 | "Calculate integral term" |
---|
222 | intTime*diff(PID_intTerm) = PID_errorI; |
---|
223 | |
---|
224 | "Sum of proportional, integral and derivative terms" |
---|
225 | PID_outp = bias + PID_action*gain*(PID_propTerm + PID_intTerm + PID_derivTerm); |
---|
226 | |
---|
227 | "Calculate AWFactor - Not in use in this mode" |
---|
228 | PID_AWFactor=1; |
---|
229 | |
---|
230 | case "Parallel": |
---|
231 | |
---|
232 | "Calculate integral term" |
---|
233 | intTime*diff(PID_intTerm) = PID_errorI; |
---|
234 | |
---|
235 | "Sum of proportional, integral and derivative terms" |
---|
236 | PID_outp = bias + PID_action*(gain*PID_propTerm + PID_intTerm + PID_derivTerm); |
---|
237 | |
---|
238 | "Calculate AWFactor - Not in use in this mode" |
---|
239 | PID_AWFactor=1; |
---|
240 | |
---|
241 | case "Series": |
---|
242 | |
---|
243 | "Calculate integral term" |
---|
244 | intTime*diff(PID_intTerm) = PID_errorI; |
---|
245 | |
---|
246 | "Sum of proportional, integral and derivative terms" |
---|
247 | PID_outp = bias + PID_action*(gain*(PID_propTerm + PID_intTerm)*(1 + PID_derivTerm)); |
---|
248 | |
---|
249 | "Calculate AWFactor - Not in use in this mode" |
---|
250 | PID_AWFactor=1; |
---|
251 | |
---|
252 | case "Ideal_AWBT": |
---|
253 | |
---|
254 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
255 | PID_action*gain*(intTime*diff(PID_intTerm)-PID_errorI) = PID_output-PID_outp; |
---|
256 | |
---|
257 | "Sum of proportional, integral and derivative terms" |
---|
258 | PID_outp = bias + PID_action*gain*(PID_propTerm + PID_intTerm + PID_derivTerm); |
---|
259 | |
---|
260 | "Calculate AWFactor - Not in use in this mode" |
---|
261 | PID_AWFactor=1; |
---|
262 | |
---|
263 | case "Parallel_AWBT": |
---|
264 | |
---|
265 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
266 | PID_action*gain*(intTime*diff(PID_intTerm)-PID_errorI) = PID_output-PID_outp; |
---|
267 | |
---|
268 | "Sum of proportional, integral and derivative terms" |
---|
269 | PID_outp = bias + PID_action*(gain*PID_propTerm + PID_intTerm + PID_derivTerm); |
---|
270 | |
---|
271 | "Calculate AWFactor - Not in use in this mode" |
---|
272 | PID_AWFactor=1; |
---|
273 | |
---|
274 | case "Series_AWBT": |
---|
275 | |
---|
276 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
277 | PID_action*gain*(intTime*diff(PID_intTerm)-PID_errorI) = PID_output-PID_outp; |
---|
278 | |
---|
279 | "Sum of proportional, integral and derivative terms" |
---|
280 | PID_outp = bias + PID_action*(gain*(PID_propTerm + PID_intTerm)*(1 + PID_derivTerm)); |
---|
281 | |
---|
282 | "Calculate AWFactor - Not in use in this mode" |
---|
283 | PID_AWFactor=1; |
---|
284 | |
---|
285 | end |
---|
286 | |
---|
287 | end |
---|
288 | |
---|
289 | Model FirstOrder |
---|
290 | ATTRIBUTES |
---|
291 | Pallete = false; |
---|
292 | Icon = "icon/PIDIncr"; |
---|
293 | |
---|
294 | PARAMETERS |
---|
295 | tau as Real (Brief="Time Constant", Unit = 's', Default=4); |
---|
296 | A as Real (Unit='1/s'); |
---|
297 | B as Real (Unit='1/s'); |
---|
298 | C as Real; |
---|
299 | D as Real(Default=0); |
---|
300 | |
---|
301 | VARIABLES |
---|
302 | x as control_signal(Brief="State"); |
---|
303 | in u as control_signal(Brief="Input signal", PosX=0, PosY=0.5); |
---|
304 | out y as control_signal(Brief="Output signal", PosX=1, PosY=0.5); |
---|
305 | |
---|
306 | EQUATIONS |
---|
307 | diff(x) = A*x + B*u; |
---|
308 | y = C*x + D*u; |
---|
309 | end |
---|
310 | |
---|
311 | Model StepSignal |
---|
312 | ATTRIBUTES |
---|
313 | Pallete = true; |
---|
314 | Icon = "icon/StepSignal"; |
---|
315 | |
---|
316 | PARAMETERS |
---|
317 | StepTime as positive(Unit='s'); |
---|
318 | StartValue as control_signal; |
---|
319 | FinalValue as control_signal; |
---|
320 | |
---|
321 | VARIABLES |
---|
322 | out OutSignal as control_signal(PosX=1, PosY=0.5); |
---|
323 | |
---|
324 | EQUATIONS |
---|
325 | if(time < StepTime) then |
---|
326 | OutSignal = StartValue; |
---|
327 | else |
---|
328 | OutSignal = FinalValue; |
---|
329 | end |
---|
330 | end |
---|
331 | |
---|
332 | Model ConstantSignal |
---|
333 | ATTRIBUTES |
---|
334 | Pallete = true; |
---|
335 | Icon = "icon/ConstSignal"; |
---|
336 | |
---|
337 | PARAMETERS |
---|
338 | Value as control_signal; |
---|
339 | |
---|
340 | VARIABLES |
---|
341 | out OutSignal as control_signal(PosX=1, PosY=0.5); |
---|
342 | |
---|
343 | EQUATIONS |
---|
344 | OutSignal = Value; |
---|
345 | end |
---|