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 354 2007-08-30 17:17:16Z arge $ |
---|
17 | *-------------------------------------------------------------------*# |
---|
18 | using "types"; |
---|
19 | |
---|
20 | Model MParameters |
---|
21 | |
---|
22 | ATTRIBUTES |
---|
23 | Pallete = false; |
---|
24 | Brief = "Model of Parameters to be used with PIDs."; |
---|
25 | |
---|
26 | VARIABLES |
---|
27 | |
---|
28 | alpha as positive (Brief="Derivative term filter constant", Default=1); |
---|
29 | beta as positive (Brief="Proportional term setPoint change filter"); |
---|
30 | bias as control_signal (Brief="Previous scaled bias", Default=0.5); |
---|
31 | derivTime as time_sec (Brief="Derivative time constant"); |
---|
32 | intTime as time_sec (Brief="Integral time constant"); |
---|
33 | gain as positive (Brief="Controller gain", Default=0.5); |
---|
34 | gamma as positive (Brief="Derivative term SP change filter"); |
---|
35 | tau as time_sec (Brief="Input filter time constant"); |
---|
36 | tauSet as time_sec (Brief="Input filter time constant"); |
---|
37 | |
---|
38 | end |
---|
39 | |
---|
40 | Model MOptions |
---|
41 | |
---|
42 | ATTRIBUTES |
---|
43 | Pallete = false; |
---|
44 | Brief = "Model of Options to be used with PIDs."; |
---|
45 | |
---|
46 | VARIABLES |
---|
47 | |
---|
48 | action as Real (Brief="Controller action: (-1) Direct,(1) Reverse", Default=-1); |
---|
49 | autoMan as Real (Brief="Controller option: (0) Automatic, (1) Manual", Default=0); |
---|
50 | clip as Real (Brief="Controller option: (1) output clipped, (0) output unclipped", Default=1); |
---|
51 | |
---|
52 | end |
---|
53 | |
---|
54 | Model MPorts |
---|
55 | |
---|
56 | ATTRIBUTES |
---|
57 | Pallete = false; |
---|
58 | Brief = "Model of Ports to be used with PIDs."; |
---|
59 | |
---|
60 | VARIABLES |
---|
61 | |
---|
62 | input as control_signal (Brief="Previous scaled input signal", Default=0.5); |
---|
63 | output as control_signal (Brief="Scaled output signal", Default=0.5); |
---|
64 | setPoint as control_signal (Brief="Scaled setPoint",Default=0.5); |
---|
65 | |
---|
66 | end |
---|
67 | |
---|
68 | Model MInternal_Variables |
---|
69 | |
---|
70 | ATTRIBUTES |
---|
71 | Pallete = false; |
---|
72 | Brief = "Model of Internal Variables to be used with PIDs."; |
---|
73 | |
---|
74 | VARIABLES |
---|
75 | |
---|
76 | derivTerm as control_signal (Brief="Derivative term", Default=0); |
---|
77 | dFilt as control_signal (Brief="Derivative term filtered", Default=0.5); |
---|
78 | error as control_signal (Brief="Error definition for proportional term"); |
---|
79 | errorD as control_signal (Brief="Error definition for derivative term"); |
---|
80 | errorI as control_signal (Brief="Error definition for integral term"); |
---|
81 | inputFilt as control_signal (Brief="Filtered input"); |
---|
82 | intTerm as control_signal (Brief="Integral term", Default=0); |
---|
83 | outp as control_signal (Brief="Sum of proportional, integral and derivative terms"); |
---|
84 | outps as control_signal (Brief="Variable outp scaled between -1 and 1"); |
---|
85 | propTerm as control_signal (Brief="Proportional term", Default=0); |
---|
86 | setPointFilt as control_signal (Brief="Filtered setPoint", Default=0); |
---|
87 | |
---|
88 | end |
---|
89 | |
---|
90 | Model PID |
---|
91 | |
---|
92 | ATTRIBUTES |
---|
93 | Pallete = true; |
---|
94 | Icon = "icon/PID"; |
---|
95 | Brief = "Model of PIDs."; |
---|
96 | Info = |
---|
97 | "== Inputs == |
---|
98 | * scaled processs variable. |
---|
99 | * scaled bias. |
---|
100 | * scaled setpoint. |
---|
101 | |
---|
102 | == Outputs == |
---|
103 | * scaled output. |
---|
104 | "; |
---|
105 | |
---|
106 | PARAMETERS |
---|
107 | PID_Select as Switcher (Brief="Type of PID", Valid=["Ideal","Parallel","Series","Ideal_AWBT","Parallel_AWBT","Series_AWBT","Ideal_AW","Parallel_AW","Series_AW"], Default = "Ideal"); |
---|
108 | |
---|
109 | VARIABLES |
---|
110 | Parameters as MParameters; |
---|
111 | Options as MOptions; |
---|
112 | Internal as MInternal_Variables; |
---|
113 | Ports as MPorts; |
---|
114 | AWFactor as Real (Brief="Integral term multiplier used in anti-reset windup"); |
---|
115 | |
---|
116 | INITIAL |
---|
117 | Internal.intTerm = 0; |
---|
118 | diff(Internal.dFilt) = 0/'s'; |
---|
119 | diff(Internal.inputFilt) = 0/'s'; |
---|
120 | diff(Internal.setPointFilt) = 0/'s'; |
---|
121 | |
---|
122 | EQUATIONS |
---|
123 | |
---|
124 | if (Parameters.tau equal 0) then |
---|
125 | "Input first order filter" |
---|
126 | (Parameters.tau + 1e-3*'s')*diff(Internal.inputFilt)= Ports.input - Internal.inputFilt; |
---|
127 | else |
---|
128 | "Input first order filter" |
---|
129 | Parameters.tau*diff(Internal.inputFilt)= Ports.input - Internal.inputFilt; |
---|
130 | end |
---|
131 | |
---|
132 | if (Parameters.tauSet equal 0) then |
---|
133 | "setPoint first order filter" |
---|
134 | (Parameters.tauSet + 1e-3*'s')*diff(Internal.setPointFilt)= Ports.setPoint - Internal.setPointFilt; |
---|
135 | else |
---|
136 | "setPoint first order filter" |
---|
137 | Parameters.tauSet*diff(Internal.setPointFilt)= Ports.setPoint - Internal.setPointFilt; |
---|
138 | end |
---|
139 | |
---|
140 | if Options.autoMan equal 1 then |
---|
141 | "Error definition for proportional term" |
---|
142 | Internal.error = Internal.inputFilt*(Parameters.beta-1.0); |
---|
143 | "Error definition for derivative term" |
---|
144 | Internal.errorD= Internal.inputFilt*(Parameters.gamma-1.0); |
---|
145 | "Error definition for integral term" |
---|
146 | Internal.errorI= 0; |
---|
147 | else |
---|
148 | "Error definition for proportional term" |
---|
149 | Internal.error = Parameters.beta*Internal.setPointFilt - Internal.inputFilt; |
---|
150 | "Error definition for derivative term" |
---|
151 | Internal.errorD = Parameters.gamma*Internal.setPointFilt - Internal.inputFilt; |
---|
152 | "Error definition for integral term" |
---|
153 | Internal.errorI = Internal.setPointFilt-Internal.inputFilt; |
---|
154 | end |
---|
155 | |
---|
156 | "Calculate proportional term" |
---|
157 | Internal.propTerm=Internal.error; |
---|
158 | |
---|
159 | if (Parameters.derivTime equal 0) then |
---|
160 | "Derivative term filter" |
---|
161 | Parameters.alpha*(Parameters.derivTime + 1e-3*'s')*diff(Internal.dFilt) = Internal.errorD - Internal.dFilt; |
---|
162 | else |
---|
163 | "Derivative term filter" |
---|
164 | Parameters.alpha*(Parameters.derivTime)*diff(Internal.dFilt) = Internal.errorD - Internal.dFilt; |
---|
165 | end |
---|
166 | |
---|
167 | "Calculate derivative term" |
---|
168 | Internal.derivTerm = Parameters.derivTime*diff(Internal.dFilt); |
---|
169 | |
---|
170 | "Scale outp" |
---|
171 | Internal.outps=2*Internal.outp-1; |
---|
172 | |
---|
173 | if Options.clip equal 1 then |
---|
174 | if abs(Internal.outps)>1 then |
---|
175 | "Calculate clipped output when it´s saturated" |
---|
176 | Ports.output=(sign(Internal.outps)*1+1)/2; |
---|
177 | else |
---|
178 | "Calculate clipped output when it´s not saturated" |
---|
179 | Ports.output=Internal.outp; |
---|
180 | end |
---|
181 | else |
---|
182 | "Calculate unclipped output" |
---|
183 | Ports.output=Internal.outp; |
---|
184 | end |
---|
185 | |
---|
186 | switch PID_Select |
---|
187 | |
---|
188 | case "Ideal_AW": |
---|
189 | |
---|
190 | "Calculate integral term with anti-windup" |
---|
191 | Parameters.intTime*diff(Internal.intTerm) = AWFactor*Internal.errorI; |
---|
192 | |
---|
193 | "Sum of proportional, integral and derivative terms" |
---|
194 | Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
195 | |
---|
196 | if abs(Internal.outps)>1 and (Options.action*sign(Internal.outps)*Internal.errorI)>0 then |
---|
197 | "Calculate AWFactor" |
---|
198 | AWFactor=-tanh(sign(Internal.outps)*Internal.outps*100-102); |
---|
199 | else |
---|
200 | "Calculate AWFactor" |
---|
201 | AWFactor=1; |
---|
202 | end |
---|
203 | |
---|
204 | case "Parallel_AW": |
---|
205 | |
---|
206 | "Calculate integral term with anti-windup" |
---|
207 | Parameters.intTime*diff(Internal.intTerm) = AWFactor*Internal.errorI; |
---|
208 | |
---|
209 | "Sum of proportional, integral and derivative terms" |
---|
210 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
211 | |
---|
212 | if abs(Internal.outps)>1 and (Options.action*sign(Internal.outps)*Internal.errorI)>0 then |
---|
213 | "Calculate AWFactor" |
---|
214 | AWFactor=-tanh(sign(Internal.outps)*Internal.outps*100-102); |
---|
215 | else |
---|
216 | "Calculate AWFactor" |
---|
217 | AWFactor=1; |
---|
218 | end |
---|
219 | |
---|
220 | |
---|
221 | case "Series_AW": |
---|
222 | |
---|
223 | "Calculate integral term with anti-windup" |
---|
224 | Parameters.intTime*diff(Internal.intTerm) = AWFactor*Internal.errorI; |
---|
225 | |
---|
226 | "Sum of proportional, integral and derivative terms" |
---|
227 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*(Internal.propTerm + Internal.intTerm)*(1 + Internal.derivTerm)); |
---|
228 | |
---|
229 | if abs(Internal.outps)>1 and (Options.action*sign(Internal.outps)*Internal.errorI)>0 then |
---|
230 | "Calculate AWFactor" |
---|
231 | AWFactor=-tanh(sign(Internal.outps)*Internal.outps*100-102); |
---|
232 | else |
---|
233 | "Calculate AWFactor" |
---|
234 | AWFactor=1; |
---|
235 | end |
---|
236 | |
---|
237 | case "Ideal": |
---|
238 | |
---|
239 | "Calculate integral term" |
---|
240 | Parameters.intTime*diff(Internal.intTerm) = Internal.errorI; |
---|
241 | |
---|
242 | "Sum of proportional, integral and derivative terms" |
---|
243 | Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
244 | |
---|
245 | "Calculate AWFactor - Not in use in this mode" |
---|
246 | AWFactor=1; |
---|
247 | |
---|
248 | case "Parallel": |
---|
249 | |
---|
250 | "Calculate integral term" |
---|
251 | Parameters.intTime*diff(Internal.intTerm) = Internal.errorI; |
---|
252 | |
---|
253 | "Sum of proportional, integral and derivative terms" |
---|
254 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
255 | |
---|
256 | "Calculate AWFactor - Not in use in this mode" |
---|
257 | AWFactor=1; |
---|
258 | |
---|
259 | case "Series": |
---|
260 | |
---|
261 | "Calculate integral term" |
---|
262 | Parameters.intTime*diff(Internal.intTerm) = Internal.errorI; |
---|
263 | |
---|
264 | "Sum of proportional, integral and derivative terms" |
---|
265 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*(Internal.propTerm + Internal.intTerm)*(1 + Internal.derivTerm)); |
---|
266 | |
---|
267 | "Calculate AWFactor - Not in use in this mode" |
---|
268 | AWFactor=1; |
---|
269 | |
---|
270 | case "Ideal_AWBT": |
---|
271 | |
---|
272 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
273 | Options.action*Parameters.gain*(Parameters.intTime*diff(Internal.intTerm)-Internal.errorI) = Ports.output-Internal.outp; |
---|
274 | |
---|
275 | "Sum of proportional, integral and derivative terms" |
---|
276 | Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
277 | |
---|
278 | "Calculate AWFactor - Not in use in this mode" |
---|
279 | AWFactor=1; |
---|
280 | |
---|
281 | case "Parallel_AWBT": |
---|
282 | |
---|
283 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
284 | Options.action*Parameters.gain*(Parameters.intTime*diff(Internal.intTerm)-Internal.errorI) = Ports.output-Internal.outp; |
---|
285 | |
---|
286 | "Sum of proportional, integral and derivative terms" |
---|
287 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
288 | |
---|
289 | "Calculate AWFactor - Not in use in this mode" |
---|
290 | AWFactor=1; |
---|
291 | |
---|
292 | case "Series_AWBT": |
---|
293 | |
---|
294 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
295 | Options.action*Parameters.gain*(Parameters.intTime*diff(Internal.intTerm)-Internal.errorI) = Ports.output-Internal.outp; |
---|
296 | |
---|
297 | "Sum of proportional, integral and derivative terms" |
---|
298 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*(Internal.propTerm + Internal.intTerm)*(1 + Internal.derivTerm)); |
---|
299 | |
---|
300 | "Calculate AWFactor - Not in use in this mode" |
---|
301 | AWFactor=1; |
---|
302 | |
---|
303 | end |
---|
304 | |
---|
305 | end |
---|