]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGActuator.cpp
Clean up header file use of iostream and "using" declarations
[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 (jsb@hal-pc.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 <algorithm>
41
42 #include "FGActuator.h"
43
44 namespace JSBSim {
45
46 static const char *IdSrc = "$Id$";
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   dt = fcs->GetDt();
58
59   // inputs are read from the base class constructor
60
61   PreviousOutput = 0.0;
62   PreviousHystOutput = 0.0;
63   PreviousRateLimOutput = 0.0;
64   PreviousLagInput = PreviousLagOutput = 0.0;
65   bias = lag = hysteresis_width = deadband_width = 0.0;
66   rate_limit = 0.0; // no limit
67   fail_zero = fail_hardover = fail_stuck = false;
68   ca = cb = 0.0;
69
70   if ( element->FindElement("deadband_width") ) {
71     deadband_width = element->FindElementValueAsNumber("deadband_width");
72   }
73   if ( element->FindElement("hysteresis_width") ) {
74     hysteresis_width = element->FindElementValueAsNumber("hysteresis_width");
75   }
76   if ( element->FindElement("rate_limit") ) {
77     rate_limit = element->FindElementValueAsNumber("rate_limit");
78   }
79   if ( element->FindElement("bias") ) {
80     bias = element->FindElementValueAsNumber("bias");
81   }
82   if ( element->FindElement("lag") ) {
83     lag = element->FindElementValueAsNumber("lag");
84     denom = 2.00 + dt*lag;
85     ca = dt*lag / denom;
86     cb = (2.00 - dt*lag) / denom;
87   }
88
89   FGFCSComponent::bind();
90   bind();
91
92   Debug(0);
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96
97 FGActuator::~FGActuator()
98 {
99   Debug(1);
100 }
101
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104 bool FGActuator::Run(void )
105 {
106   dt = fcs->GetDt();
107
108   Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
109   Output = Input; // perfect actuator
110
111   if (fail_zero) Input = 0;
112   if (fail_hardover) Input =  clipmax*fabs(Input)/Input;
113
114   if (lag != 0.0)              Lag();        // models actuator lag
115   if (rate_limit != 0)         RateLimit();  // limit the actuator rate
116   if (deadband_width != 0.0)   Deadband();
117   if (hysteresis_width != 0.0) Hysteresis();
118   if (bias != 0.0)             Bias();       // models a finite bias
119
120   if (fail_stuck) Output = PreviousOutput;
121   PreviousOutput = Output; // previous value needed for "stuck" malfunction
122
123   Clip();
124   if (IsOutput) SetOutput();
125
126   return true;
127 }
128
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
131 void FGActuator::Bias(void)
132 {
133   Output += bias;
134 }
135
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137
138 void FGActuator::Lag(void)
139 {
140   // "Output" on the right side of the "=" is the current frame input
141   // for this Lag filter
142   double input = Output;
143   Output = ca * (input + PreviousLagInput) + PreviousLagOutput * cb;
144   PreviousLagInput = input;
145   PreviousLagOutput = Output;
146 }
147
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150 void FGActuator::Hysteresis(void)
151 {
152   // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
153   // is - for the purposes of this Hysteresis method - really the input to the
154   // method.
155   double input = Output;
156   
157   if (input > PreviousHystOutput) {
158     Output = std::max(PreviousHystOutput, input-0.5*hysteresis_width);
159   } else if (input < PreviousHystOutput) {
160     Output = std::min(PreviousHystOutput, input+0.5*hysteresis_width);
161   }
162
163   PreviousHystOutput = Output;
164 }
165
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167
168 void FGActuator::RateLimit(void)
169 {
170   // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
171   // is - for the purposes of this RateLimit method - really the input to the
172   // method.
173   double input = Output;
174   if (dt > 0.0) {
175     double rate = (input - PreviousRateLimOutput)/dt;
176     if (fabs(rate) > rate_limit) {
177       Output = PreviousRateLimOutput + (rate_limit*fabs(rate)/rate)*dt;
178     }
179   }
180   PreviousRateLimOutput = Output;
181 }
182
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184
185 void FGActuator::Deadband(void)
186 {
187 }
188
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190
191 void FGActuator::bind(void)
192 {
193   string tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
194   const string tmp_zero = tmp + "/malfunction/fail_zero";
195   const string tmp_hardover = tmp + "/malfunction/fail_hardover";
196   const string tmp_stuck = tmp + "/malfunction/fail_stuck";
197
198   PropertyManager->Tie( tmp_zero, this, &FGActuator::GetFailZero, &FGActuator::SetFailZero);
199   PropertyManager->Tie( tmp_hardover, this, &FGActuator::GetFailHardover, &FGActuator::SetFailHardover);
200   PropertyManager->Tie( tmp_stuck, this, &FGActuator::GetFailStuck, &FGActuator::SetFailStuck);
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 //    The bitmasked value choices are as follows:
205 //    unset: In this case (the default) JSBSim would only print
206 //       out the normally expected messages, essentially echoing
207 //       the config files as they are read. If the environment
208 //       variable is not set, debug_lvl is set to 1 internally
209 //    0: This requests JSBSim not to output any messages
210 //       whatsoever.
211 //    1: This value explicity requests the normal JSBSim
212 //       startup messages
213 //    2: This value asks for a message to be printed out when
214 //       a class is instantiated
215 //    4: When this value is set, a message is displayed when a
216 //       FGModel object executes its Run() method
217 //    8: When this value is set, various runtime state variables
218 //       are printed out periodically
219 //    16: When set various parameters are sanity checked and
220 //       a message is printed out when they go out of bounds
221
222 void FGActuator::Debug(int from)
223 {
224   if (debug_lvl <= 0) return;
225
226   if (debug_lvl & 1) { // Standard console startup message output
227     if (from == 0) { // Constructor
228       if (InputSigns[0] < 0)
229         cout << "      INPUT: -" << InputNodes[0]->getName() << endl;
230       else
231         cout << "      INPUT: " << InputNodes[0]->getName() << endl;
232
233       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
234       if (bias != 0.0) cout << "      Bias: " << bias << endl;
235       if (rate_limit != 0) cout << "      Rate limit: " << rate_limit << endl;
236       if (lag != 0) cout << "      Actuator lag: " << lag << endl;
237       if (hysteresis_width != 0) cout << "      Hysteresis width: " << hysteresis_width << endl;
238       if (deadband_width != 0) cout << "      Deadband width: " << deadband_width << endl;
239     }
240   }
241   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
242     if (from == 0) cout << "Instantiated: FGActuator" << endl;
243     if (from == 1) cout << "Destroyed:    FGActuator" << endl;
244   }
245   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
246   }
247   if (debug_lvl & 8 ) { // Runtime state variables
248   }
249   if (debug_lvl & 16) { // Sanity checking
250   }
251   if (debug_lvl & 64) {
252     if (from == 0) { // Constructor
253       cout << IdSrc << endl;
254       cout << IdHdr << endl;
255     }
256   }
257 }
258 }