source: mso/eml/controllers/PIDs.mso @ 1

Last change on this file since 1 was 1, checked in by Rafael de Pelegrini Soares, 16 years ago

Initial import of the library

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.1 KB
Line 
1#*-------------------------------------------------------------------
2* Model of PIDs
3*--------------------------------------------------------------------
4*  - Inputs
5*       - a scaled processs variable
6*       - a scaled bias
7*       - a scaled setpoint
8*
9*  - Outputs
10*   - a scaled output
11*
12*  - Assumptions
13*
14*
15*--------------------------------------------------------------------
16* Author: Tiago Osório
17* $Id: PIDs.mso 1 2006-06-20 17:33:53Z rafael $
18*-------------------------------------------------------------------*#
19using "types";
20
21Model MParameters
22
23        VARIABLES
24       
25        alpha      as positive (Brief="Derivative term filter constant", Default=1);
26        beta       as positive (Brief="Proportional term setPoint change filter");
27        bias       as control_signal (Brief="Previous scaled bias", Default=0.5);
28        derivTime  as time_sec (Brief="Derivative time constant");
29        intTime    as time_sec (Brief="Integral time constant");
30        gain       as positive (Brief="Controller gain", Default=0.5);
31        gamma      as positive (Brief="Derivative term SP change filter");
32        tau        as time_sec (Brief="Input filter time constant");
33        tauSet     as time_sec (Brief="Input filter time constant");
34
35end
36
37Model MOptions 
38
39        VARIABLES       
40       
41        action     as Real     (Brief="Controller action: (-1) Direct,(1) Reverse", Default=-1);
42    autoMan    as Real     (Brief="Controller option: (0) Automatic, (1) Manual", Default=0);   
43        clip       as Real     (Brief="Controller option: (1) output clipped, (0) output unclipped", Default=1);
44
45end
46
47Model MPorts
48
49        VARIABLES
50       
51        input      as control_signal (Brief="Previous scaled input signal", Default=0.5);
52        output     as control_signal (Brief="Scaled output signal", Default=0.5);
53        setPoint   as control_signal (Brief="Scaled setPoint",Default=0.5);
54
55end
56
57Model MInternal_Variables
58       
59        VARIABLES
60
61        derivTerm  as control_signal (Brief="Derivative term", Default=0);
62        dFilt      as control_signal (Brief="Derivative term filtered", Default=0.5);
63        error      as control_signal (Brief="Error definition for proportional term");
64        errorD     as control_signal (Brief="Error definition for derivative term");
65        errorI     as control_signal (Brief="Error definition for integral term");
66        inputFilt  as control_signal (Brief="Filtered input");
67        intTerm    as control_signal (Brief="Integral term", Default=0);
68        outp       as control_signal (Brief="Sum of proportional, integral and derivative terms");
69        outps      as control_signal (Brief="Variable outp scaled between -1 and 1");
70        propTerm   as control_signal (Brief="Proportional term", Default=0);
71        setPointFilt  as control_signal (Brief="Filtered setPoint", Default=0);
72
73end
74
75Model PID_basic
76
77        VARIABLES
78        Parameters         as MParameters;
79        Options            as MOptions;
80        Internal           as MInternal_Variables;
81        Ports              as MPorts;
82       
83        EQUATIONS
84
85        if (Parameters.tau equal 0) then
86                "Input first order filter"
87                (Parameters.tau + 1e-3*"s")*diff(Internal.inputFilt)= Ports.input - Internal.inputFilt;
88        else
89                "Input first order filter"
90                Parameters.tau*diff(Internal.inputFilt)= Ports.input - Internal.inputFilt;     
91        end
92
93        if (Parameters.tauSet equal 0) then
94                "setPoint first order filter"
95                (Parameters.tauSet + 1e-3*"s")*diff(Internal.setPointFilt)= Ports.setPoint - Internal.setPointFilt;
96        else
97                "setPoint first order filter"
98                Parameters.tauSet*diff(Internal.setPointFilt)= Ports.setPoint - Internal.setPointFilt;
99        end
100       
101        if Options.autoMan equal 1 then
102                "Error definition for proportional term"
103                Internal.error = Internal.inputFilt*(Parameters.beta-1.0);
104                "Error definition for derivative term"
105                Internal.errorD= Internal.inputFilt*(Parameters.gamma-1.0);
106                "Error definition for integral term"           
107                Internal.errorI= 0;
108        else
109                "Error definition for proportional term"                       
110                Internal.error = Parameters.beta*Internal.setPointFilt - Internal.inputFilt;
111                "Error definition for derivative term"
112                Internal.errorD = Parameters.gamma*Internal.setPointFilt - Internal.inputFilt;
113                "Error definition for integral term"
114                Internal.errorI = Internal.setPointFilt-Internal.inputFilt;
115        end
116       
117        "Calculate proportional term"
118        Internal.propTerm=Internal.error;
119       
120        if (Parameters.derivTime equal 0) then
121                "Derivative term filter"       
122                Parameters.alpha*(Parameters.derivTime + 1e-3*"s")*diff(Internal.dFilt) = Internal.errorD - Internal.dFilt;
123        else
124                "Derivative term filter"       
125                Parameters.alpha*(Parameters.derivTime)*diff(Internal.dFilt) = Internal.errorD - Internal.dFilt;
126        end
127
128        "Calculate derivative term"
129        Internal.derivTerm = Parameters.derivTime*diff(Internal.dFilt);
130       
131        "Scale outp"
132        Internal.outps=2*Internal.outp-1;
133       
134        if Options.clip equal 1 then
135                if abs(Internal.outps)>1 then
136                        "Calculate clipped output when it´s saturated"
137                        Ports.output=(sign(Internal.outps)*1+1)/2;
138                else
139                        "Calculate clipped output when it´s not saturated"
140                        Ports.output=Internal.outp;
141                end
142        else
143                "Calculate unclipped output"
144                Ports.output=Internal.outp;
145        end
146
147        INITIAL
148        Internal.intTerm = 0;
149        diff(Internal.dFilt) = 0*"1/s";
150        diff(Internal.inputFilt) = 0*"1/s";
151        diff(Internal.setPointFilt) = 0*"1/s";
152end
153
154
155Model PID_Ideal_AW as PID_basic
156       
157        VARIABLES
158        AWFactor     as Real     (Brief="Integral term multiplier used in anti-reset windup");
159
160        EQUATIONS
161       
162        "Calculate integral term with anti-windup"
163        Parameters.intTime*diff(Internal.intTerm) = AWFactor*Internal.errorI;
164       
165        "Sum of proportional, integral and derivative terms"
166        Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm);
167
168        if abs(Internal.outps)>1 and (Options.action*sign(Internal.outps)*Internal.errorI)>0 then
169                "Calculate AWFactor"
170                AWFactor=-tanh(sign(Internal.outps)*Internal.outps*100-102);
171        else
172                "Calculate AWFactor"
173                AWFactor=1;
174        end
175end
176
177Model PID_Parallel_AW as PID_basic
178       
179        VARIABLES
180        AWFactor     as Real     (Brief="Integral term multiplier used in anti-reset windup");
181
182        EQUATIONS
183       
184        "Calculate integral term with anti-windup"
185        Parameters.intTime*diff(Internal.intTerm) = AWFactor*Internal.errorI;
186       
187        "Sum of proportional, integral and derivative terms"
188        Internal.outp = Parameters.bias + Options.action*(Parameters.gain*Internal.propTerm + Internal.intTerm + Internal.derivTerm);
189
190        if abs(Internal.outps)>1 and (Options.action*sign(Internal.outps)*Internal.errorI)>0 then
191                "Calculate AWFactor"
192                AWFactor=-tanh(sign(Internal.outps)*Internal.outps*100-102);
193        else
194                "Calculate AWFactor"
195                AWFactor=1;
196        end
197end
198
199Model PID_Series_AW as PID_basic
200       
201        VARIABLES
202        AWFactor     as Real     (Brief="Integral term multiplier used in anti-reset windup");
203       
204        EQUATIONS
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)*(1 + 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
219end
220
221Model PID_Ideal as PID_basic
222       
223        EQUATIONS
224       
225        "Calculate integral term"       
226        Parameters.intTime*diff(Internal.intTerm) = Internal.errorI;
227       
228        "Sum of proportional, integral and derivative terms"   
229        Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm);
230
231end
232
233Model PID_Parallel as PID_basic
234       
235        EQUATIONS
236       
237        "Calculate integral term"       
238        Parameters.intTime*diff(Internal.intTerm) = Internal.errorI;
239       
240        "Sum of proportional, integral and derivative terms"   
241        Internal.outp = Parameters.bias + Options.action*(Parameters.gain*Internal.propTerm + Internal.intTerm + Internal.derivTerm);
242
243end
244
245Model PID_Series as PID_basic
246       
247        EQUATIONS
248       
249        "Calculate integral term"       
250        Parameters.intTime*diff(Internal.intTerm) = Internal.errorI;
251       
252        "Sum of proportional, integral and derivative terms"
253        Internal.outp = Parameters.bias + Options.action*(Parameters.gain*(Internal.propTerm + Internal.intTerm)*(1 + Internal.derivTerm));
254
255end
256
257Model PID_Ideal_AWBT as PID_basic
258       
259        EQUATIONS
260       
261        "Calculate integral term with anti-windup and bumpless transfer"       
262        Options.action*Parameters.gain*(Parameters.intTime*diff(Internal.intTerm)-Internal.errorI) = Ports.output-Internal.outp;       
263       
264        "Sum of proportional, integral and derivative terms"   
265        Internal.outp = Parameters.bias + Options.action*Parameters.gain*(Internal.propTerm + Internal.intTerm + Internal.derivTerm);
266
267end
268
269Model PID_Parallel_AWBT as PID_basic
270       
271        EQUATIONS
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
279end
280
281Model PID_Series_AWBT as PID_basic
282       
283        EQUATIONS
284       
285        "Calculate integral term with anti-windup and bumpless transfer"
286        Options.action*Parameters.gain*(Parameters.intTime*diff(Internal.intTerm)-Internal.errorI) = Ports.output-Internal.outp;       
287       
288        "Sum of proportional, integral and derivative terms"
289        Internal.outp = Parameters.bias + Options.action*(Parameters.gain*(Internal.propTerm + Internal.intTerm)*(1 + Internal.derivTerm));
290
291end
Note: See TracBrowser for help on using the repository browser.