]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGActuator.cpp
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / FDM / JSBSim / models / flight_control / FGActuator.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGActuator.cpp
4  Author:       Jon Berndt
5  Date started: 21 February 2006
6
7  ------------- Copyright (C) 2007 Jon S. Berndt (jon@jsbsim.org) -------------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17  details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28
29 HISTORY
30 --------------------------------------------------------------------------------
31
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES,  and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGActuator.h"
41
42 using namespace std;
43
44 namespace JSBSim {
45
46 static const char *IdSrc = "$Id: FGActuator.cpp,v 1.23 2012/04/08 15:04:41 jberndt Exp $";
47 static const char *IdHdr = ID_ACTUATOR;
48
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 CLASS IMPLEMENTATION
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52
53
54 FGActuator::FGActuator(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
55 {
56   double denom;
57
58   // inputs are read from the base class constructor
59
60   PreviousOutput = 0.0;
61   PreviousHystOutput = 0.0;
62   PreviousRateLimOutput = 0.0;
63   PreviousLagInput = PreviousLagOutput = 0.0;
64   bias = lag = hysteresis_width = deadband_width = 0.0;
65   rate_limit = 0.0; // no limit
66   fail_zero = fail_hardover = fail_stuck = false;
67   ca = cb = 0.0;
68   initialized = 0;
69   saturated = false;
70
71   if ( element->FindElement("deadband_width") ) {
72     deadband_width = element->FindElementValueAsNumber("deadband_width");
73   }
74   if ( element->FindElement("hysteresis_width") ) {
75     hysteresis_width = element->FindElementValueAsNumber("hysteresis_width");
76   }
77   if ( element->FindElement("rate_limit") ) {
78     rate_limit = fabs(element->FindElementValueAsNumber("rate_limit"));
79   }
80   if ( element->FindElement("bias") ) {
81     bias = element->FindElementValueAsNumber("bias");
82   }
83   if ( element->FindElement("lag") ) {
84     lag = element->FindElementValueAsNumber("lag");
85     denom = 2.00 + dt*lag;
86     ca = dt*lag / denom;
87     cb = (2.00 - dt*lag) / denom;
88   }
89
90   FGFCSComponent::bind();
91   bind();
92
93   Debug(0);
94 }
95
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97
98 FGActuator::~FGActuator()
99 {
100   Debug(1);
101 }
102
103 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104
105 bool FGActuator::Run(void )
106 {
107   Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
108
109   if( fcs->GetTrimStatus() ) initialized = 0;
110
111   if (fail_zero) Input = 0;
112   if (fail_hardover) Input =  clipmax*sign(Input);
113
114   Output = Input; // Perfect actuator. At this point, if no failures are present
115                   // and no subsequent lag, limiting, etc. is done, the output
116                   // is simply the input. If any further processing is done
117                   // (below) such as lag, rate limiting, hysteresis, etc., then
118                   // the Input will be further processed and the eventual Output
119                   // will be overwritten from this perfect value.
120
121   if (fail_stuck) {
122     Output = PreviousOutput;
123   } else {
124     if (lag != 0.0)              Lag();        // models actuator lag
125     if (rate_limit != 0)         RateLimit();  // limit the actuator rate
126     if (deadband_width != 0.0)   Deadband();
127     if (hysteresis_width != 0.0) Hysteresis();
128     if (bias != 0.0)             Bias();       // models a finite bias
129   }
130
131   PreviousOutput = Output; // previous value needed for "stuck" malfunction
132   
133   initialized = 1;
134
135   Clip();
136
137   if (clip) {
138     saturated = false;
139     if (Output >= clipmax) saturated = true;
140     else if (Output <= clipmin) saturated = true;
141   }
142
143   if (IsOutput) SetOutput();
144
145   return true;
146 }
147
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150 void FGActuator::Bias(void)
151 {
152   Output += bias;
153 }
154
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156
157 void FGActuator::Lag(void)
158 {
159   // "Output" on the right side of the "=" is the current frame input
160   // for this Lag filter
161   double input = Output;
162
163   if ( initialized )
164     Output = ca * (input + PreviousLagInput) + PreviousLagOutput * cb;
165
166   PreviousLagInput = input;
167   PreviousLagOutput = Output;
168 }
169
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171
172 void FGActuator::Hysteresis(void)
173 {
174   // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
175   // is - for the purposes of this Hysteresis method - really the input to the
176   // method.
177   double input = Output;
178   
179   if ( initialized ) {
180     if (input > PreviousHystOutput)
181       Output = max(PreviousHystOutput, input-0.5*hysteresis_width);
182     else if (input < PreviousHystOutput)
183       Output = min(PreviousHystOutput, input+0.5*hysteresis_width);
184   }
185
186   PreviousHystOutput = Output;
187 }
188
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190
191 void FGActuator::RateLimit(void)
192 {
193   // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
194   // is - for the purposes of this RateLimit method - really the input to the
195   // method.
196   double input = Output;
197   if ( initialized ) {
198     double delta = input - PreviousRateLimOutput;
199     if (fabs(delta) > dt * rate_limit) {
200       double signed_rate_limit = delta > 0.0 ? rate_limit : -rate_limit;
201       Output = PreviousRateLimOutput + signed_rate_limit * dt;
202     }
203   }
204   PreviousRateLimOutput = Output;
205 }
206
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208
209 void FGActuator::Deadband(void)
210 {
211   // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
212   // is - for the purposes of this Deadband method - really the input to the
213   // method.
214   double input = Output;
215
216   if (input < -deadband_width/2.0) {
217     Output = (input + deadband_width/2.0);
218   } else if (input > deadband_width/2.0) {
219     Output = (input - deadband_width/2.0);
220   } else {
221     Output = 0.0;
222   }
223 }
224
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226
227 void FGActuator::bind(void)
228 {
229   string tmp = Name;
230   if (Name.find("/") == string::npos) {
231     tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
232   }
233   const string tmp_zero = tmp + "/malfunction/fail_zero";
234   const string tmp_hardover = tmp + "/malfunction/fail_hardover";
235   const string tmp_stuck = tmp + "/malfunction/fail_stuck";
236   const string tmp_sat = tmp + "/saturated";
237
238   PropertyManager->Tie( tmp_zero, this, &FGActuator::GetFailZero, &FGActuator::SetFailZero);
239   PropertyManager->Tie( tmp_hardover, this, &FGActuator::GetFailHardover, &FGActuator::SetFailHardover);
240   PropertyManager->Tie( tmp_stuck, this, &FGActuator::GetFailStuck, &FGActuator::SetFailStuck);
241   PropertyManager->Tie( tmp_sat, this, &FGActuator::IsSaturated);
242 }
243
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 //    The bitmasked value choices are as follows:
246 //    unset: In this case (the default) JSBSim would only print
247 //       out the normally expected messages, essentially echoing
248 //       the config files as they are read. If the environment
249 //       variable is not set, debug_lvl is set to 1 internally
250 //    0: This requests JSBSim not to output any messages
251 //       whatsoever.
252 //    1: This value explicity requests the normal JSBSim
253 //       startup messages
254 //    2: This value asks for a message to be printed out when
255 //       a class is instantiated
256 //    4: When this value is set, a message is displayed when a
257 //       FGModel object executes its Run() method
258 //    8: When this value is set, various runtime state variables
259 //       are printed out periodically
260 //    16: When set various parameters are sanity checked and
261 //       a message is printed out when they go out of bounds
262
263 void FGActuator::Debug(int from)
264 {
265   if (debug_lvl <= 0) return;
266
267   if (debug_lvl & 1) { // Standard console startup message output
268     if (from == 0) { // Constructor
269       if (InputSigns[0] < 0)
270         cout << "      INPUT: -" << InputNames[0] << endl;
271       else
272         cout << "      INPUT: " << InputNames[0] << endl;
273
274       if (IsOutput) {
275         for (unsigned int i=0; i<OutputNodes.size(); i++)
276           cout << "      OUTPUT: " << OutputNodes[i]->getName() << endl;
277       }
278       if (bias != 0.0) cout << "      Bias: " << bias << endl;
279       if (rate_limit != 0) cout << "      Rate limit: " << rate_limit << endl;
280       if (lag != 0) cout << "      Actuator lag: " << lag << endl;
281       if (hysteresis_width != 0) cout << "      Hysteresis width: " << hysteresis_width << endl;
282       if (deadband_width != 0) cout << "      Deadband width: " << deadband_width << endl;
283     }
284   }
285   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
286     if (from == 0) cout << "Instantiated: FGActuator" << endl;
287     if (from == 1) cout << "Destroyed:    FGActuator" << endl;
288   }
289   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
290   }
291   if (debug_lvl & 8 ) { // Runtime state variables
292   }
293   if (debug_lvl & 16) { // Sanity checking
294   }
295   if (debug_lvl & 64) {
296     if (from == 0) { // Constructor
297       cout << IdSrc << endl;
298       cout << IdHdr << endl;
299     }
300   }
301 }
302 }