1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 21 February 2006
7 ------------- Copyright (C) 2007 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
30 --------------------------------------------------------------------------------
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES, and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGActuator.h"
46 static const char *IdSrc = "$Id: FGActuator.cpp,v 1.20 2011/06/18 17:46:21 bcoconni Exp $";
47 static const char *IdHdr = ID_ACTUATOR;
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 FGActuator::FGActuator(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
58 // inputs are read from the base class constructor
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;
69 if ( element->FindElement("deadband_width") ) {
70 deadband_width = element->FindElementValueAsNumber("deadband_width");
72 if ( element->FindElement("hysteresis_width") ) {
73 hysteresis_width = element->FindElementValueAsNumber("hysteresis_width");
75 if ( element->FindElement("rate_limit") ) {
76 rate_limit = fabs(element->FindElementValueAsNumber("rate_limit"));
78 if ( element->FindElement("bias") ) {
79 bias = element->FindElementValueAsNumber("bias");
81 if ( element->FindElement("lag") ) {
82 lag = element->FindElementValueAsNumber("lag");
83 denom = 2.00 + dt*lag;
85 cb = (2.00 - dt*lag) / denom;
88 FGFCSComponent::bind();
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 FGActuator::~FGActuator()
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 bool FGActuator::Run(void )
105 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
107 if (fail_zero) Input = 0;
108 if (fail_hardover) Input = clipmax*sign(Input);
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.
117 if (lag != 0.0) Lag(); // models actuator lag
118 if (rate_limit != 0) RateLimit(); // limit the actuator rate
119 if (deadband_width != 0.0) Deadband();
120 if (hysteresis_width != 0.0) Hysteresis();
121 if (bias != 0.0) Bias(); // models a finite bias
123 if (fail_stuck) Output = PreviousOutput;
124 PreviousOutput = Output; // previous value needed for "stuck" malfunction
127 if (IsOutput) SetOutput();
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 void FGActuator::Bias(void)
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 void FGActuator::Lag(void)
143 // "Output" on the right side of the "=" is the current frame input
144 // for this Lag filter
145 double input = Output;
147 if (!fcs->GetTrimStatus())
148 Output = ca * (input + PreviousLagInput) + PreviousLagOutput * cb;
150 PreviousLagInput = input;
151 PreviousLagOutput = Output;
154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 void FGActuator::Hysteresis(void)
158 // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
159 // is - for the purposes of this Hysteresis method - really the input to the
161 double input = Output;
163 if (!fcs->GetTrimStatus()) {
164 if (input > PreviousHystOutput)
165 Output = max(PreviousHystOutput, input-0.5*hysteresis_width);
166 else if (input < PreviousHystOutput)
167 Output = min(PreviousHystOutput, input+0.5*hysteresis_width);
170 PreviousHystOutput = Output;
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 void FGActuator::RateLimit(void)
177 // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
178 // is - for the purposes of this RateLimit method - really the input to the
180 double input = Output;
181 if (!fcs->GetTrimStatus()) {
182 double delta = input - PreviousRateLimOutput;
183 if (fabs(delta) > dt * rate_limit) {
184 double signed_rate_limit = delta > 0.0 ? rate_limit : -rate_limit;
185 Output = PreviousRateLimOutput + signed_rate_limit * dt;
188 PreviousRateLimOutput = Output;
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 void FGActuator::Deadband(void)
195 // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
196 // is - for the purposes of this Deadband method - really the input to the
198 double input = Output;
200 if (input < -deadband_width/2.0) {
201 Output = (input + deadband_width/2.0);
202 } else if (input > deadband_width/2.0) {
203 Output = (input - deadband_width/2.0);
209 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 void FGActuator::bind(void)
214 if (Name.find("/") == string::npos) {
215 tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
217 const string tmp_zero = tmp + "/malfunction/fail_zero";
218 const string tmp_hardover = tmp + "/malfunction/fail_hardover";
219 const string tmp_stuck = tmp + "/malfunction/fail_stuck";
221 PropertyManager->Tie( tmp_zero, this, &FGActuator::GetFailZero, &FGActuator::SetFailZero);
222 PropertyManager->Tie( tmp_hardover, this, &FGActuator::GetFailHardover, &FGActuator::SetFailHardover);
223 PropertyManager->Tie( tmp_stuck, this, &FGActuator::GetFailStuck, &FGActuator::SetFailStuck);
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 // The bitmasked value choices are as follows:
228 // unset: In this case (the default) JSBSim would only print
229 // out the normally expected messages, essentially echoing
230 // the config files as they are read. If the environment
231 // variable is not set, debug_lvl is set to 1 internally
232 // 0: This requests JSBSim not to output any messages
234 // 1: This value explicity requests the normal JSBSim
236 // 2: This value asks for a message to be printed out when
237 // a class is instantiated
238 // 4: When this value is set, a message is displayed when a
239 // FGModel object executes its Run() method
240 // 8: When this value is set, various runtime state variables
241 // are printed out periodically
242 // 16: When set various parameters are sanity checked and
243 // a message is printed out when they go out of bounds
245 void FGActuator::Debug(int from)
247 if (debug_lvl <= 0) return;
249 if (debug_lvl & 1) { // Standard console startup message output
250 if (from == 0) { // Constructor
251 if (InputSigns[0] < 0)
252 cout << " INPUT: -" << InputNames[0] << endl;
254 cout << " INPUT: " << InputNames[0] << endl;
257 for (unsigned int i=0; i<OutputNodes.size(); i++)
258 cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
260 if (bias != 0.0) cout << " Bias: " << bias << endl;
261 if (rate_limit != 0) cout << " Rate limit: " << rate_limit << endl;
262 if (lag != 0) cout << " Actuator lag: " << lag << endl;
263 if (hysteresis_width != 0) cout << " Hysteresis width: " << hysteresis_width << endl;
264 if (deadband_width != 0) cout << " Deadband width: " << deadband_width << endl;
267 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
268 if (from == 0) cout << "Instantiated: FGActuator" << endl;
269 if (from == 1) cout << "Destroyed: FGActuator" << endl;
271 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
273 if (debug_lvl & 8 ) { // Runtime state variables
275 if (debug_lvl & 16) { // Sanity checking
277 if (debug_lvl & 64) {
278 if (from == 0) { // Constructor
279 cout << IdSrc << endl;
280 cout << IdHdr << endl;