]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGActuator.cpp
Update to sync with JSBSim CVS, small bug fixes.
[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.21 2011/06/30 03:16:10 jentron 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
69   if ( element->FindElement("deadband_width") ) {
70     deadband_width = element->FindElementValueAsNumber("deadband_width");
71   }
72   if ( element->FindElement("hysteresis_width") ) {
73     hysteresis_width = element->FindElementValueAsNumber("hysteresis_width");
74   }
75   if ( element->FindElement("rate_limit") ) {
76     rate_limit = fabs(element->FindElementValueAsNumber("rate_limit"));
77   }
78   if ( element->FindElement("bias") ) {
79     bias = element->FindElementValueAsNumber("bias");
80   }
81   if ( element->FindElement("lag") ) {
82     lag = element->FindElementValueAsNumber("lag");
83     denom = 2.00 + dt*lag;
84     ca = dt*lag / denom;
85     cb = (2.00 - dt*lag) / denom;
86   }
87
88   FGFCSComponent::bind();
89   bind();
90
91   Debug(0);
92 }
93
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 FGActuator::~FGActuator()
97 {
98   Debug(1);
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103 bool FGActuator::Run(void )
104 {
105   Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
106
107   if (fail_zero) Input = 0;
108   if (fail_hardover) Input =  clipmax*sign(Input);
109
110   Output = Input; // Perfect actuator. At this point, if no failures are present
111                   // and no subsequent lag, limiting, etc. is done, the output
112                   // is simply the input. If any further processing is done
113                   // (below) such as lag, rate limiting, hysteresis, etc., then
114                   // the Input will be further processed and the eventual Output
115                   // will be overwritten from this perfect value.
116
117   if (fail_stuck) {
118     Output = PreviousOutput;
119   } else {
120     if (lag != 0.0)              Lag();        // models actuator lag
121     if (rate_limit != 0)         RateLimit();  // limit the actuator rate
122     if (deadband_width != 0.0)   Deadband();
123     if (hysteresis_width != 0.0) Hysteresis();
124     if (bias != 0.0)             Bias();       // models a finite bias
125   }
126
127   PreviousOutput = Output; // previous value needed for "stuck" malfunction
128
129   Clip();
130   if (IsOutput) SetOutput();
131
132   return true;
133 }
134
135 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136
137 void FGActuator::Bias(void)
138 {
139   Output += bias;
140 }
141
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143
144 void FGActuator::Lag(void)
145 {
146   // "Output" on the right side of the "=" is the current frame input
147   // for this Lag filter
148   double input = Output;
149
150   if (!fcs->GetTrimStatus())
151     Output = ca * (input + PreviousLagInput) + PreviousLagOutput * cb;
152
153   PreviousLagInput = input;
154   PreviousLagOutput = Output;
155 }
156
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158
159 void FGActuator::Hysteresis(void)
160 {
161   // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
162   // is - for the purposes of this Hysteresis method - really the input to the
163   // method.
164   double input = Output;
165   
166   if (!fcs->GetTrimStatus()) {
167     if (input > PreviousHystOutput)
168       Output = max(PreviousHystOutput, input-0.5*hysteresis_width);
169     else if (input < PreviousHystOutput)
170       Output = min(PreviousHystOutput, input+0.5*hysteresis_width);
171   }
172
173   PreviousHystOutput = Output;
174 }
175
176 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177
178 void FGActuator::RateLimit(void)
179 {
180   // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
181   // is - for the purposes of this RateLimit method - really the input to the
182   // method.
183   double input = Output;
184   if (!fcs->GetTrimStatus()) {
185     double delta = input - PreviousRateLimOutput;
186     if (fabs(delta) > dt * rate_limit) {
187       double signed_rate_limit = delta > 0.0 ? rate_limit : -rate_limit;
188       Output = PreviousRateLimOutput + signed_rate_limit * dt;
189     }
190   }
191   PreviousRateLimOutput = Output;
192 }
193
194 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195
196 void FGActuator::Deadband(void)
197 {
198   // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
199   // is - for the purposes of this Deadband method - really the input to the
200   // method.
201   double input = Output;
202
203   if (input < -deadband_width/2.0) {
204     Output = (input + deadband_width/2.0);
205   } else if (input > deadband_width/2.0) {
206     Output = (input - deadband_width/2.0);
207   } else {
208     Output = 0.0;
209   }
210 }
211
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213
214 void FGActuator::bind(void)
215 {
216   string tmp = Name;
217   if (Name.find("/") == string::npos) {
218     tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
219   }
220   const string tmp_zero = tmp + "/malfunction/fail_zero";
221   const string tmp_hardover = tmp + "/malfunction/fail_hardover";
222   const string tmp_stuck = tmp + "/malfunction/fail_stuck";
223
224   PropertyManager->Tie( tmp_zero, this, &FGActuator::GetFailZero, &FGActuator::SetFailZero);
225   PropertyManager->Tie( tmp_hardover, this, &FGActuator::GetFailHardover, &FGActuator::SetFailHardover);
226   PropertyManager->Tie( tmp_stuck, this, &FGActuator::GetFailStuck, &FGActuator::SetFailStuck);
227 }
228
229 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 //    The bitmasked value choices are as follows:
231 //    unset: In this case (the default) JSBSim would only print
232 //       out the normally expected messages, essentially echoing
233 //       the config files as they are read. If the environment
234 //       variable is not set, debug_lvl is set to 1 internally
235 //    0: This requests JSBSim not to output any messages
236 //       whatsoever.
237 //    1: This value explicity requests the normal JSBSim
238 //       startup messages
239 //    2: This value asks for a message to be printed out when
240 //       a class is instantiated
241 //    4: When this value is set, a message is displayed when a
242 //       FGModel object executes its Run() method
243 //    8: When this value is set, various runtime state variables
244 //       are printed out periodically
245 //    16: When set various parameters are sanity checked and
246 //       a message is printed out when they go out of bounds
247
248 void FGActuator::Debug(int from)
249 {
250   if (debug_lvl <= 0) return;
251
252   if (debug_lvl & 1) { // Standard console startup message output
253     if (from == 0) { // Constructor
254       if (InputSigns[0] < 0)
255         cout << "      INPUT: -" << InputNames[0] << endl;
256       else
257         cout << "      INPUT: " << InputNames[0] << endl;
258
259       if (IsOutput) {
260         for (unsigned int i=0; i<OutputNodes.size(); i++)
261           cout << "      OUTPUT: " << OutputNodes[i]->getName() << endl;
262       }
263       if (bias != 0.0) cout << "      Bias: " << bias << endl;
264       if (rate_limit != 0) cout << "      Rate limit: " << rate_limit << endl;
265       if (lag != 0) cout << "      Actuator lag: " << lag << endl;
266       if (hysteresis_width != 0) cout << "      Hysteresis width: " << hysteresis_width << endl;
267       if (deadband_width != 0) cout << "      Deadband width: " << deadband_width << endl;
268     }
269   }
270   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
271     if (from == 0) cout << "Instantiated: FGActuator" << endl;
272     if (from == 1) cout << "Destroyed:    FGActuator" << endl;
273   }
274   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
275   }
276   if (debug_lvl & 8 ) { // Runtime state variables
277   }
278   if (debug_lvl & 16) { // Sanity checking
279   }
280   if (debug_lvl & 64) {
281     if (from == 0) { // Constructor
282       cout << IdSrc << endl;
283       cout << IdHdr << endl;
284     }
285   }
286 }
287 }