1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 ------------- Copyright (C) 2000 -------------
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU 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 General Public License for more
19 You should have received a copy of the GNU 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 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_FILTER;
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 FGFilter::FGFilter(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
58 Type = AC_cfg->GetValue("TYPE");
59 Name = AC_cfg->GetValue("NAME");
60 AC_cfg->GetNextConfigLine();
61 dt = fcs->GetState()->Getdt();
64 C1 = C2 = C3 = C4 = C5 = C6 = 0.0;
66 if (Type == "LAG_FILTER") FilterType = eLag ;
67 else if (Type == "LEAD_LAG_FILTER") FilterType = eLeadLag ;
68 else if (Type == "SECOND_ORDER_FILTER") FilterType = eOrder2 ;
69 else if (Type == "WASHOUT_FILTER") FilterType = eWashout ;
70 else if (Type == "INTEGRATOR") FilterType = eIntegrator ;
71 else FilterType = eUnknown ;
73 while ((token = AC_cfg->GetValue()) != string("/COMPONENT")) {
75 if (token == "C1") *AC_cfg >> C1;
76 else if (token == "C2") *AC_cfg >> C2;
77 else if (token == "C3") *AC_cfg >> C3;
78 else if (token == "C4") *AC_cfg >> C4;
79 else if (token == "C5") *AC_cfg >> C5;
80 else if (token == "C6") *AC_cfg >> C6;
81 else if (token == "TRIGGER")
83 token = AC_cfg->GetValue("TRIGGER");
85 Trigger = resolveSymbol(token);
87 else if (token == "INPUT")
89 token = AC_cfg->GetValue("INPUT");
90 if( InputNodes.size() > 0 ) {
91 cerr << "Filters can only accept one input" << endl;
94 InputNodes.push_back( resolveSymbol(token) );
97 else if (token == "OUTPUT")
100 *AC_cfg >> sOutputIdx;
101 OutputNode = PropertyManager->GetNode( sOutputIdx );
103 else cerr << "Unknown filter type: " << token << endl;
108 switch (FilterType) {
110 denom = 2.00 + dt*C1;
112 cb = (2.00 - dt*C1) / denom;
115 denom = 2.00*C3 + dt*C4;
116 ca = (2.00*C1 + dt*C2) / denom;
117 cb = (dt*C2 - 2.00*C1) / denom;
118 cc = (2.00*C3 - dt*C2) / denom;
121 denom = 4.0*C4 + 2.0*C5*dt + C6*dt*dt;
122 ca = (4.0*C1 + 2.0*C2*dt + C3*dt*dt) / denom;
123 cb = (2.0*C3*dt*dt - 8.0*C1) / denom;
124 cc = (4.0*C1 - 2.0*C2*dt + C3*dt*dt) / denom;
125 cd = (2.0*C6*dt*dt - 8.0*C4) / denom;
126 ce = (4.0*C4 - 2.0*C5*dt + C6*dt*dt) / denom;
129 denom = 2.00 + dt*C1;
131 cb = (2.00 - dt*C1) / denom;
137 cerr << "Unknown filter type" << endl;
140 FGFCSComponent::bind();
145 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 FGFilter::~FGFilter()
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 bool FGFilter::Run(void)
158 FGFCSComponent::Run(); // call the base class for initialization of Input
162 PreviousOutput1 = PreviousInput1 = Output = Input;
165 } else if (Trigger != 0) {
166 test = Trigger->getIntValue();
168 Output = PreviousOutput1 = PreviousOutput2 = 0.0;
169 Input = PreviousInput1 = PreviousInput2 = 0.0;
171 Output = PreviousOutput1 = PreviousOutput2 = 0.0;
175 Input = InputNodes[0]->getDoubleValue();
176 switch (FilterType) {
178 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1 * cb;
181 Output = Input * ca + PreviousInput1 * cb + PreviousOutput1 * cc;
184 Output = Input * ca + PreviousInput1 * cb + PreviousInput2 * cc
185 - PreviousOutput1 * cd - PreviousOutput2 * ce;
188 Output = Input * ca - PreviousInput1 * ca + PreviousOutput1 * cb;
191 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1;
199 PreviousOutput2 = PreviousOutput1;
200 PreviousOutput1 = Output;
201 PreviousInput2 = PreviousInput1;
202 PreviousInput1 = Input;
204 if (IsOutput) SetOutput();
209 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 // The bitmasked value choices are as follows:
211 // unset: In this case (the default) JSBSim would only print
212 // out the normally expected messages, essentially echoing
213 // the config files as they are read. If the environment
214 // variable is not set, debug_lvl is set to 1 internally
215 // 0: This requests JSBSim not to output any messages
217 // 1: This value explicity requests the normal JSBSim
219 // 2: This value asks for a message to be printed out when
220 // a class is instantiated
221 // 4: When this value is set, a message is displayed when a
222 // FGModel object executes its Run() method
223 // 8: When this value is set, various runtime state variables
224 // are printed out periodically
225 // 16: When set various parameters are sanity checked and
226 // a message is printed out when they go out of bounds
228 void FGFilter::Debug(int from)
230 if (debug_lvl <= 0) return;
232 if (debug_lvl & 1) { // Standard console startup message output
233 if (from == 0) { // Constructor
234 cout << " INPUT: " << InputNodes[0]->getName() << endl;
235 cout << " C1: " << C1 << endl;
236 cout << " C2: " << C2 << endl;
237 cout << " C3: " << C3 << endl;
238 cout << " C4: " << C4 << endl;
239 cout << " C5: " << C5 << endl;
240 cout << " C6: " << C6 << endl;
241 if (IsOutput) cout << " OUTPUT: " << OutputNode->getName() << endl;
244 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
245 if (from == 0) cout << "Instantiated: FGFilter" << endl;
246 if (from == 1) cout << "Destroyed: FGFilter" << endl;
248 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
250 if (debug_lvl & 8 ) { // Runtime state variables
252 if (debug_lvl & 16) { // Sanity checking
254 if (debug_lvl & 64) {
255 if (from == 0) { // Constructor
256 cout << IdSrc << endl;
257 cout << IdHdr << endl;