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 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_FILTER;
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 FGFilter::FGFilter(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
54 DynamicFilter = false;
56 C[1] = C[2] = C[3] = C[4] = C[5] = C[6] = 0.0;
57 for (int i=0; i<7; i++) {
58 PropertySign[i] = 1.0;
62 if (Type == "LAG_FILTER") FilterType = eLag ;
63 else if (Type == "LEAD_LAG_FILTER") FilterType = eLeadLag ;
64 else if (Type == "SECOND_ORDER_FILTER") FilterType = eOrder2 ;
65 else if (Type == "WASHOUT_FILTER") FilterType = eWashout ;
66 else if (Type == "INTEGRATOR") FilterType = eIntegrator ;
67 else FilterType = eUnknown ;
69 ReadFilterCoefficients(element, 1);
70 ReadFilterCoefficients(element, 2);
71 ReadFilterCoefficients(element, 3);
72 ReadFilterCoefficients(element, 4);
73 ReadFilterCoefficients(element, 5);
74 ReadFilterCoefficients(element, 6);
76 if (element->FindElement("trigger")) {
77 Trigger = PropertyManager->GetNode(element->FindElementValue("trigger"));
82 CalculateDynamicFilters();
84 FGFCSComponent::bind();
89 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 void FGFilter::ReadFilterCoefficients(Element* element, int index)
101 sprintf(buf, "c%d", index);
102 string coefficient = string(buf);
103 string property_string="";
105 if ( element->FindElement(coefficient) ) {
106 property_string = element->FindElementValue(coefficient);
107 if (!is_number(property_string)) { // property
108 if (property_string[0] == '-') {
109 PropertySign[index] = -1.0;
110 property_string.erase(0,1);
112 PropertySign[index] = 1.0;
114 PropertyNode[index] = PropertyManager->GetNode(property_string);
115 DynamicFilter = true;
117 C[index] = element->FindElementValueAsNumber(coefficient);
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 void FGFilter::CalculateDynamicFilters(void)
128 switch (FilterType) {
130 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
131 denom = 2.00 + dt*C[1];
132 ca = dt*C[1] / denom;
133 cb = (2.00 - dt*C[1]) / denom;
137 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
138 if (PropertyNode[2] != 0L) C[2] = PropertyNode[2]->getDoubleValue()*PropertySign[2];
139 if (PropertyNode[3] != 0L) C[3] = PropertyNode[3]->getDoubleValue()*PropertySign[3];
140 if (PropertyNode[4] != 0L) C[4] = PropertyNode[4]->getDoubleValue()*PropertySign[4];
141 denom = 2.00*C[3] + dt*C[4];
142 ca = (2.00*C[1] + dt*C[2]) / denom;
143 cb = (dt*C[2] - 2.00*C[1]) / denom;
144 cc = (2.00*C[3] - dt*C[4]) / denom;
147 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
148 if (PropertyNode[2] != 0L) C[2] = PropertyNode[2]->getDoubleValue()*PropertySign[2];
149 if (PropertyNode[3] != 0L) C[3] = PropertyNode[3]->getDoubleValue()*PropertySign[3];
150 if (PropertyNode[4] != 0L) C[4] = PropertyNode[4]->getDoubleValue()*PropertySign[4];
151 if (PropertyNode[5] != 0L) C[5] = PropertyNode[5]->getDoubleValue()*PropertySign[5];
152 if (PropertyNode[6] != 0L) C[6] = PropertyNode[6]->getDoubleValue()*PropertySign[6];
153 denom = 4.0*C[4] + 2.0*C[5]*dt + C[6]*dt*dt;
154 ca = (4.0*C[1] + 2.0*C[2]*dt + C[3]*dt*dt) / denom;
155 cb = (2.0*C[3]*dt*dt - 8.0*C[1]) / denom;
156 cc = (4.0*C[1] - 2.0*C[2]*dt + C[3]*dt*dt) / denom;
157 cd = (2.0*C[6]*dt*dt - 8.0*C[4]) / denom;
158 ce = (4.0*C[4] - 2.0*C[5]*dt + C[6]*dt*dt) / denom;
161 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
162 denom = 2.00 + dt*C[1];
164 cb = (2.00 - dt*C[1]) / denom;
167 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
171 cerr << "Unknown filter type" << endl;
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 bool FGFilter::Run(void)
185 PreviousOutput1 = PreviousInput1 = Output = Input;
190 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
192 if (DynamicFilter) CalculateDynamicFilters();
194 switch (FilterType) {
196 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1 * cb;
199 Output = Input * ca + PreviousInput1 * cb + PreviousOutput1 * cc;
202 Output = Input * ca + PreviousInput1 * cb + PreviousInput2 * cc
203 - PreviousOutput1 * cd - PreviousOutput2 * ce;
206 Output = Input * ca - PreviousInput1 * ca + PreviousOutput1 * cb;
210 test = Trigger->getDoubleValue();
211 if (fabs(test) > 0.000001) {
212 Input = PreviousInput1 = PreviousInput2 = 0.0;
215 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1;
223 PreviousOutput2 = PreviousOutput1;
224 PreviousOutput1 = Output;
225 PreviousInput2 = PreviousInput1;
226 PreviousInput1 = Input;
229 if (IsOutput) SetOutput();
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 // The bitmasked value choices are as follows:
236 // unset: In this case (the default) JSBSim would only print
237 // out the normally expected messages, essentially echoing
238 // the config files as they are read. If the environment
239 // variable is not set, debug_lvl is set to 1 internally
240 // 0: This requests JSBSim not to output any messages
242 // 1: This value explicity requests the normal JSBSim
244 // 2: This value asks for a message to be printed out when
245 // a class is instantiated
246 // 4: When this value is set, a message is displayed when a
247 // FGModel object executes its Run() method
248 // 8: When this value is set, various runtime state variables
249 // are printed out periodically
250 // 16: When set various parameters are sanity checked and
251 // a message is printed out when they go out of bounds
253 void FGFilter::Debug(int from)
257 if (debug_lvl <= 0) return;
259 if (debug_lvl & 1) { // Standard console startup message output
260 if (from == 0) { // Constructor
261 cout << " INPUT: " << InputNodes[0]->getName() << endl;
262 switch (FilterType) {
264 if (PropertySign[1] < 0.0) sgn="-";
266 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
267 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
270 if (PropertySign[1] < 0.0) sgn="-";
272 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
273 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
274 if (PropertySign[2] < 0.0) sgn="-";
276 if (PropertyNode[2] == 0L) cout << " C[2]: " << C[2] << endl;
277 else cout << " C[2] is the value of property: " << sgn << PropertyNode[2]->GetName() << endl;
278 if (PropertySign[3] < 0.0) sgn="-";
280 if (PropertyNode[3] == 0L) cout << " C[3]: " << C[3] << endl;
281 else cout << " C[3] is the value of property: " << sgn << PropertyNode[3]->GetName() << endl;
282 if (PropertySign[4] < 0.0) sgn="-";
284 if (PropertyNode[4] == 0L) cout << " C[4]: " << C[4] << endl;
285 else cout << " C[4] is the value of property: " << sgn << PropertyNode[4]->GetName() << endl;
288 if (PropertySign[1] < 0.0) sgn="-";
290 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
291 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
292 if (PropertySign[2] < 0.0) sgn="-";
294 if (PropertyNode[2] == 0L) cout << " C[2]: " << C[2] << endl;
295 else cout << " C[2] is the value of property: " << sgn << PropertyNode[2]->GetName() << endl;
296 if (PropertySign[3] < 0.0) sgn="-";
298 if (PropertyNode[3] == 0L) cout << " C[3]: " << C[3] << endl;
299 else cout << " C[3] is the value of property: " << sgn << PropertyNode[3]->GetName() << endl;
300 if (PropertySign[4] < 0.0) sgn="-";
302 if (PropertyNode[4] == 0L) cout << " C[4]: " << C[4] << endl;
303 else cout << " C[4] is the value of property: " << sgn << PropertyNode[4]->GetName() << endl;
304 if (PropertySign[5] < 0.0) sgn="-";
306 if (PropertyNode[5] == 0L) cout << " C[5]: " << C[5] << endl;
307 else cout << " C[5] is the value of property: " << sgn << PropertyNode[5]->GetName() << endl;
308 if (PropertySign[6] < 0.0) sgn="-";
310 if (PropertyNode[6] == 0L) cout << " C[6]: " << C[6] << endl;
311 else cout << " C[6] is the value of property: " << sgn << PropertyNode[6]->GetName() << endl;
314 if (PropertySign[1] < 0.0) sgn="-";
316 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
317 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
320 if (PropertySign[1] < 0.0) sgn="-";
322 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
323 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
329 for (unsigned int i=0; i<OutputNodes.size(); i++)
330 cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
334 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
335 if (from == 0) cout << "Instantiated: FGFilter" << endl;
336 if (from == 1) cout << "Destroyed: FGFilter" << endl;
338 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
340 if (debug_lvl & 8 ) { // Runtime state variables
342 if (debug_lvl & 16) { // Sanity checking
344 if (debug_lvl & 64) {
345 if (from == 0) { // Constructor
346 cout << IdSrc << endl;
347 cout << IdHdr << endl;