1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 21 February 2006
7 ------------- Copyright (C) 2007 Jon S. Berndt (jsb@hal-pc.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"
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_ACTUATOR;
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 FGActuator::FGActuator(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
57 // inputs are read from the base class constructor
60 PreviousHystOutput = 0.0;
61 PreviousRateLimOutput = 0.0;
62 PreviousLagInput = PreviousLagOutput = 0.0;
63 bias = lag = hysteresis_width = deadband_width = 0.0;
64 rate_limit = 0.0; // no limit
65 fail_zero = fail_hardover = fail_stuck = false;
68 if ( element->FindElement("deadband_width") ) {
69 deadband_width = element->FindElementValueAsNumber("deadband_width");
71 if ( element->FindElement("hysteresis_width") ) {
72 hysteresis_width = element->FindElementValueAsNumber("hysteresis_width");
74 if ( element->FindElement("rate_limit") ) {
75 rate_limit = element->FindElementValueAsNumber("rate_limit");
77 if ( element->FindElement("bias") ) {
78 bias = element->FindElementValueAsNumber("bias");
80 if ( element->FindElement("lag") ) {
81 lag = element->FindElementValueAsNumber("lag");
82 denom = 2.00 + dt*lag;
84 cb = (2.00 - dt*lag) / denom;
87 FGFCSComponent::bind();
93 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 FGActuator::~FGActuator()
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 bool FGActuator::Run(void )
106 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
107 Output = Input; // perfect actuator
109 if (fail_zero) Input = 0;
110 if (fail_hardover) Input = clipmax*fabs(Input)/Input;
112 if (lag != 0.0) Lag(); // models actuator lag
113 if (rate_limit != 0) RateLimit(); // limit the actuator rate
114 if (deadband_width != 0.0) Deadband();
115 if (hysteresis_width != 0.0) Hysteresis();
116 if (bias != 0.0) Bias(); // models a finite bias
118 if (fail_stuck) Output = PreviousOutput;
119 PreviousOutput = Output; // previous value needed for "stuck" malfunction
122 if (IsOutput) SetOutput();
127 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 void FGActuator::Bias(void)
134 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 void FGActuator::Lag(void)
138 // "Output" on the right side of the "=" is the current frame input
139 // for this Lag filter
140 double input = Output;
141 Output = ca * (input + PreviousLagInput) + PreviousLagOutput * cb;
142 PreviousLagInput = input;
143 PreviousLagOutput = Output;
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 void FGActuator::Hysteresis(void)
150 // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
151 // is - for the purposes of this Hysteresis method - really the input to the
153 double input = Output;
155 if (input > PreviousHystOutput) {
156 Output = max(PreviousHystOutput, input-0.5*hysteresis_width);
157 } else if (input < PreviousHystOutput) {
158 Output = min(PreviousHystOutput, input+0.5*hysteresis_width);
161 PreviousHystOutput = Output;
164 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 void FGActuator::RateLimit(void)
168 // Note: this function acts cumulatively on the "Output" parameter. So, "Output"
169 // is - for the purposes of this RateLimit method - really the input to the
171 double input = Output;
173 double rate = (input - PreviousRateLimOutput)/dt;
174 if (fabs(rate) > rate_limit) {
175 Output = PreviousRateLimOutput + (rate_limit*fabs(rate)/rate)*dt;
178 PreviousRateLimOutput = Output;
181 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 void FGActuator::Deadband(void)
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 void FGActuator::bind(void)
191 string tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
192 const string tmp_zero = tmp + "/malfunction/fail_zero";
193 const string tmp_hardover = tmp + "/malfunction/fail_hardover";
194 const string tmp_stuck = tmp + "/malfunction/fail_stuck";
196 PropertyManager->Tie( tmp_zero, this, &FGActuator::GetFailZero, &FGActuator::SetFailZero);
197 PropertyManager->Tie( tmp_hardover, this, &FGActuator::GetFailHardover, &FGActuator::SetFailHardover);
198 PropertyManager->Tie( tmp_stuck, this, &FGActuator::GetFailStuck, &FGActuator::SetFailStuck);
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 // The bitmasked value choices are as follows:
203 // unset: In this case (the default) JSBSim would only print
204 // out the normally expected messages, essentially echoing
205 // the config files as they are read. If the environment
206 // variable is not set, debug_lvl is set to 1 internally
207 // 0: This requests JSBSim not to output any messages
209 // 1: This value explicity requests the normal JSBSim
211 // 2: This value asks for a message to be printed out when
212 // a class is instantiated
213 // 4: When this value is set, a message is displayed when a
214 // FGModel object executes its Run() method
215 // 8: When this value is set, various runtime state variables
216 // are printed out periodically
217 // 16: When set various parameters are sanity checked and
218 // a message is printed out when they go out of bounds
220 void FGActuator::Debug(int from)
222 if (debug_lvl <= 0) return;
224 if (debug_lvl & 1) { // Standard console startup message output
225 if (from == 0) { // Constructor
226 if (InputSigns[0] < 0)
227 cout << " INPUT: -" << InputNodes[0]->getName() << endl;
229 cout << " INPUT: " << InputNodes[0]->getName() << endl;
231 if (IsOutput) cout << " OUTPUT: " << OutputNode->getName() << endl;
232 if (bias != 0.0) cout << " Bias: " << bias << endl;
233 if (rate_limit != 0) cout << " Rate limit: " << rate_limit << endl;
234 if (lag != 0) cout << " Actuator lag: " << lag << endl;
235 if (hysteresis_width != 0) cout << " Hysteresis width: " << hysteresis_width << endl;
236 if (deadband_width != 0) cout << " Deadband width: " << deadband_width << endl;
239 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
240 if (from == 0) cout << "Instantiated: FGActuator" << endl;
241 if (from == 1) cout << "Destroyed: FGActuator" << endl;
243 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
245 if (debug_lvl & 8 ) { // Runtime state variables
247 if (debug_lvl & 16) { // Sanity checking
249 if (debug_lvl & 64) {
250 if (from == 0) { // Constructor
251 cout << IdSrc << endl;
252 cout << IdHdr << endl;