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 306 2007-07-05 01:34:37Z 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 | " |
---|
98 | - Inputs |
---|
99 | * - a scaled processs variable. |
---|
100 | * - a scaled bias. |
---|
101 | * - a scaled setpoint. |
---|
102 | |
---|
103 | - Outputs |
---|
104 | * - a scaled output. |
---|
105 | "; |
---|
106 | |
---|
107 | PARAMETERS |
---|
108 | 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"); |
---|
109 | |
---|
110 | VARIABLES |
---|
111 | Parameters as MParameters; |
---|
112 | Options as MOptions; |
---|
113 | Internal as MInternal_Variables; |
---|
114 | Ports as MPorts; |
---|
115 | AWFactor as Real (Brief="Integral term multiplier used in anti-reset windup"); |
---|
116 | |
---|
117 | INITIAL |
---|
118 | Internal.intTerm = 0; |
---|
119 | diff(Internal.dFilt) = 0/'s'; |
---|
120 | diff(Internal.inputFilt) = 0/'s'; |
---|
121 | diff(Internal.setPointFilt) = 0/'s'; |
---|
122 | |
---|
123 | EQUATIONS |
---|
124 | |
---|
125 | if (Parameters.tau equal 0) then |
---|
126 | "Input first order filter" |
---|
127 | (Parameters.tau + 1e-3*'s')*diff(Internal.inputFilt)= Ports.input - Internal.inputFilt; |
---|
128 | else |
---|
129 | "Input first order filter" |
---|
130 | Parameters.tau*diff(Internal.inputFilt)= Ports.input - Internal.inputFilt; |
---|
131 | end |
---|
132 | |
---|
133 | if (Parameters.tauSet equal 0) then |
---|
134 | "setPoint first order filter" |
---|
135 | (Parameters.tauSet + 1e-3*'s')*diff(Internal.setPointFilt)= Ports.setPoint - Internal.setPointFilt; |
---|
136 | else |
---|
137 | "setPoint first order filter" |
---|
138 | Parameters.tauSet*diff(Internal.setPointFilt)= Ports.setPoint - Internal.setPointFilt; |
---|
139 | end |
---|
140 | |
---|
141 | if Options.autoMan equal 1 then |
---|
142 | "Error definition for proportional term" |
---|
143 | Internal.error = Internal.inputFilt*(Parameters.beta-1.0); |
---|
144 | "Error definition for derivative term" |
---|
145 | Internal.errorD= Internal.inputFilt*(Parameters.gamma-1.0); |
---|
146 | "Error definition for integral term" |
---|
147 | Internal.errorI= 0; |
---|
148 | else |
---|
149 | "Error definition for proportional term" |
---|
150 | Internal.error = Parameters.beta*Internal.setPointFilt - Internal.inputFilt; |
---|
151 | "Error definition for derivative term" |
---|
152 | Internal.errorD = Parameters.gamma*Internal.setPointFilt - Internal.inputFilt; |
---|
153 | "Error definition for integral term" |
---|
154 | Internal.errorI = Internal.setPointFilt-Internal.inputFilt; |
---|
155 | end |
---|
156 | |
---|
157 | "Calculate proportional term" |
---|
158 | Internal.propTerm=Internal.error; |
---|
159 | |
---|
160 | if (Parameters.derivTime equal 0) then |
---|
161 | "Derivative term filter" |
---|
162 | Parameters.alpha*(Parameters.derivTime + 1e-3*'s')*diff(Internal.dFilt) = Internal.errorD - Internal.dFilt; |
---|
163 | else |
---|
164 | "Derivative term filter" |
---|
165 | Parameters.alpha*(Parameters.derivTime)*diff(Internal.dFilt) = Internal.errorD - Internal.dFilt; |
---|
166 | end |
---|
167 | |
---|
168 | "Calculate derivative term" |
---|
169 | Internal.derivTerm = Parameters.derivTime*diff(Internal.dFilt); |
---|
170 | |
---|
171 | "Scale outp" |
---|
172 | Internal.outps=2*Internal.outp-1; |
---|
173 | |
---|
174 | if Options.clip equal 1 then |
---|
175 | if abs(Internal.outps)>1 then |
---|
176 | "Calculate clipped output when it´s saturated" |
---|
177 | Ports.output=(sign(Internal.outps)*1+1)/2; |
---|
178 | else |
---|
179 | "Calculate clipped output when it´s not saturated" |
---|
180 | Ports.output=Internal.outp; |
---|
181 | end |
---|
182 | else |
---|
183 | "Calculate unclipped output" |
---|
184 | Ports.output=Internal.outp; |
---|
185 | end |
---|
186 | |
---|
187 | switch PID_Select |
---|
188 | |
---|
189 | case "Ideal_AW": |
---|
190 | |
---|
191 | "Calculate integral term with anti-windup" |
---|
192 | Parameters.intTime*diff(Internal.intTerm) = AWFactor*Internal.errorI; |
---|
193 | |
---|
194 | "Sum of proportional, integral and derivative terms" |
---|
195 | Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
196 | |
---|
197 | if abs(Internal.outps)>1 and (Options.action*sign(Internal.outps)*Internal.errorI)>0 then |
---|
198 | "Calculate AWFactor" |
---|
199 | AWFactor=-tanh(sign(Internal.outps)*Internal.outps*100-102); |
---|
200 | else |
---|
201 | "Calculate AWFactor" |
---|
202 | AWFactor=1; |
---|
203 | end |
---|
204 | |
---|
205 | case "Parallel_AW": |
---|
206 | |
---|
207 | "Calculate integral term with anti-windup" |
---|
208 | Parameters.intTime*diff(Internal.intTerm) = AWFactor*Internal.errorI; |
---|
209 | |
---|
210 | "Sum of proportional, integral and derivative terms" |
---|
211 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
212 | |
---|
213 | if abs(Internal.outps)>1 and (Options.action*sign(Internal.outps)*Internal.errorI)>0 then |
---|
214 | "Calculate AWFactor" |
---|
215 | AWFactor=-tanh(sign(Internal.outps)*Internal.outps*100-102); |
---|
216 | else |
---|
217 | "Calculate AWFactor" |
---|
218 | AWFactor=1; |
---|
219 | end |
---|
220 | |
---|
221 | |
---|
222 | case "Series_AW": |
---|
223 | |
---|
224 | "Calculate integral term with anti-windup" |
---|
225 | Parameters.intTime*diff(Internal.intTerm) = AWFactor*Internal.errorI; |
---|
226 | |
---|
227 | "Sum of proportional, integral and derivative terms" |
---|
228 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*(Internal.propTerm + Internal.intTerm)*(1 + Internal.derivTerm)); |
---|
229 | |
---|
230 | if abs(Internal.outps)>1 and (Options.action*sign(Internal.outps)*Internal.errorI)>0 then |
---|
231 | "Calculate AWFactor" |
---|
232 | AWFactor=-tanh(sign(Internal.outps)*Internal.outps*100-102); |
---|
233 | else |
---|
234 | "Calculate AWFactor" |
---|
235 | AWFactor=1; |
---|
236 | end |
---|
237 | |
---|
238 | case "Ideal": |
---|
239 | |
---|
240 | "Calculate integral term" |
---|
241 | Parameters.intTime*diff(Internal.intTerm) = Internal.errorI; |
---|
242 | |
---|
243 | "Sum of proportional, integral and derivative terms" |
---|
244 | Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
245 | |
---|
246 | "Calculate AWFactor - Not in use in this mode" |
---|
247 | AWFactor=1; |
---|
248 | |
---|
249 | case "Parallel": |
---|
250 | |
---|
251 | "Calculate integral term" |
---|
252 | Parameters.intTime*diff(Internal.intTerm) = Internal.errorI; |
---|
253 | |
---|
254 | "Sum of proportional, integral and derivative terms" |
---|
255 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
256 | |
---|
257 | "Calculate AWFactor - Not in use in this mode" |
---|
258 | AWFactor=1; |
---|
259 | |
---|
260 | case "Series": |
---|
261 | |
---|
262 | "Calculate integral term" |
---|
263 | Parameters.intTime*diff(Internal.intTerm) = Internal.errorI; |
---|
264 | |
---|
265 | "Sum of proportional, integral and derivative terms" |
---|
266 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*(Internal.propTerm + Internal.intTerm)*(1 + Internal.derivTerm)); |
---|
267 | |
---|
268 | "Calculate AWFactor - Not in use in this mode" |
---|
269 | AWFactor=1; |
---|
270 | |
---|
271 | case "Ideal_AWBT": |
---|
272 | |
---|
273 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
274 | Options.action*Parameters.gain*(Parameters.intTime*diff(Internal.intTerm)-Internal.errorI) = Ports.output-Internal.outp; |
---|
275 | |
---|
276 | "Sum of proportional, integral and derivative terms" |
---|
277 | Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
278 | |
---|
279 | "Calculate AWFactor - Not in use in this mode" |
---|
280 | AWFactor=1; |
---|
281 | |
---|
282 | case "Parallel_AWBT": |
---|
283 | |
---|
284 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
285 | Options.action*Parameters.gain*(Parameters.intTime*diff(Internal.intTerm)-Internal.errorI) = Ports.output-Internal.outp; |
---|
286 | |
---|
287 | "Sum of proportional, integral and derivative terms" |
---|
288 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*Internal.propTerm + Internal.intTerm + Internal.derivTerm); |
---|
289 | |
---|
290 | "Calculate AWFactor - Not in use in this mode" |
---|
291 | AWFactor=1; |
---|
292 | |
---|
293 | case "Series_AWBT": |
---|
294 | |
---|
295 | "Calculate integral term with anti-windup and bumpless transfer" |
---|
296 | Options.action*Parameters.gain*(Parameters.intTime*diff(Internal.intTerm)-Internal.errorI) = Ports.output-Internal.outp; |
---|
297 | |
---|
298 | "Sum of proportional, integral and derivative terms" |
---|
299 | Internal.outp = Parameters.bias + Options.action*(Parameters.gain*(Internal.propTerm + Internal.intTerm)*(1 + Internal.derivTerm)); |
---|
300 | |
---|
301 | "Calculate AWFactor - Not in use in this mode" |
---|
302 | AWFactor=1; |
---|
303 | |
---|
304 | end |
---|
305 | |
---|
306 | end |
---|