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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41 #include "input_output/FGXMLElement.h"
42 #include "input_output/FGPropertyManager.h"
51 static const char *IdSrc = "$Id: FGFilter.cpp,v 1.15 2009/10/24 22:59:30 jberndt Exp $";
52 static const char *IdHdr = ID_FILTER;
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 FGFilter::FGFilter(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
61 DynamicFilter = false;
63 C[1] = C[2] = C[3] = C[4] = C[5] = C[6] = 0.0;
64 for (int i=1; i<7; i++) {
65 PropertySign[i] = 1.0;
67 ReadFilterCoefficients(element, i);
70 if (Type == "LAG_FILTER") FilterType = eLag ;
71 else if (Type == "LEAD_LAG_FILTER") FilterType = eLeadLag ;
72 else if (Type == "SECOND_ORDER_FILTER") FilterType = eOrder2 ;
73 else if (Type == "WASHOUT_FILTER") FilterType = eWashout ;
74 else if (Type == "INTEGRATOR") FilterType = eIntegrator ;
75 else FilterType = eUnknown ;
77 if (element->FindElement("trigger")) {
78 Trigger = PropertyManager->GetNode(element->FindElementValue("trigger"));
83 CalculateDynamicFilters();
85 FGFCSComponent::bind();
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 void FGFilter::ReadFilterCoefficients(Element* element, int index)
101 // index is known to be 1-7.
102 // A stringstream would be overkill, but also trying to avoid sprintf
103 string coefficient = "c0";
104 coefficient[1] += index;
106 if ( element->FindElement(coefficient) ) {
107 string property_string = element->FindElementValue(coefficient);
108 if (!is_number(property_string)) { // property
109 if (property_string[0] == '-') {
110 PropertySign[index] = -1.0;
111 property_string.erase(0,1);
113 PropertySign[index] = 1.0;
115 PropertyNode[index] = PropertyManager->GetNode(property_string);
116 DynamicFilter = true;
118 C[index] = element->FindElementValueAsNumber(coefficient);
123 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 void FGFilter::CalculateDynamicFilters(void)
129 switch (FilterType) {
131 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
132 denom = 2.00 + dt*C[1];
133 ca = dt*C[1] / denom;
134 cb = (2.00 - dt*C[1]) / denom;
138 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
139 if (PropertyNode[2] != 0L) C[2] = PropertyNode[2]->getDoubleValue()*PropertySign[2];
140 if (PropertyNode[3] != 0L) C[3] = PropertyNode[3]->getDoubleValue()*PropertySign[3];
141 if (PropertyNode[4] != 0L) C[4] = PropertyNode[4]->getDoubleValue()*PropertySign[4];
142 denom = 2.00*C[3] + dt*C[4];
143 ca = (2.00*C[1] + dt*C[2]) / denom;
144 cb = (dt*C[2] - 2.00*C[1]) / denom;
145 cc = (2.00*C[3] - dt*C[4]) / denom;
148 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
149 if (PropertyNode[2] != 0L) C[2] = PropertyNode[2]->getDoubleValue()*PropertySign[2];
150 if (PropertyNode[3] != 0L) C[3] = PropertyNode[3]->getDoubleValue()*PropertySign[3];
151 if (PropertyNode[4] != 0L) C[4] = PropertyNode[4]->getDoubleValue()*PropertySign[4];
152 if (PropertyNode[5] != 0L) C[5] = PropertyNode[5]->getDoubleValue()*PropertySign[5];
153 if (PropertyNode[6] != 0L) C[6] = PropertyNode[6]->getDoubleValue()*PropertySign[6];
154 denom = 4.0*C[4] + 2.0*C[5]*dt + C[6]*dt*dt;
155 ca = (4.0*C[1] + 2.0*C[2]*dt + C[3]*dt*dt) / denom;
156 cb = (2.0*C[3]*dt*dt - 8.0*C[1]) / denom;
157 cc = (4.0*C[1] - 2.0*C[2]*dt + C[3]*dt*dt) / denom;
158 cd = (2.0*C[6]*dt*dt - 8.0*C[4]) / denom;
159 ce = (4.0*C[4] - 2.0*C[5]*dt + C[6]*dt*dt) / denom;
162 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
163 denom = 2.00 + dt*C[1];
165 cb = (2.00 - dt*C[1]) / denom;
168 if (PropertyNode[1] != 0L) C[1] = PropertyNode[1]->getDoubleValue()*PropertySign[1];
172 cerr << "Unknown filter type" << endl;
178 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 bool FGFilter::Run(void)
186 PreviousOutput1 = PreviousInput1 = Output = Input;
191 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
193 if (DynamicFilter) CalculateDynamicFilters();
195 switch (FilterType) {
197 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1 * cb;
200 Output = Input * ca + PreviousInput1 * cb + PreviousOutput1 * cc;
203 Output = Input * ca + PreviousInput1 * cb + PreviousInput2 * cc
204 - PreviousOutput1 * cd - PreviousOutput2 * ce;
207 Output = Input * ca - PreviousInput1 * ca + PreviousOutput1 * cb;
211 test = Trigger->getDoubleValue();
212 if (fabs(test) > 0.000001) {
213 Input = PreviousInput1 = PreviousInput2 = 0.0;
216 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1;
224 PreviousOutput2 = PreviousOutput1;
225 PreviousOutput1 = Output;
226 PreviousInput2 = PreviousInput1;
227 PreviousInput1 = Input;
230 if (IsOutput) SetOutput();
235 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 // The bitmasked value choices are as follows:
237 // unset: In this case (the default) JSBSim would only print
238 // out the normally expected messages, essentially echoing
239 // the config files as they are read. If the environment
240 // variable is not set, debug_lvl is set to 1 internally
241 // 0: This requests JSBSim not to output any messages
243 // 1: This value explicity requests the normal JSBSim
245 // 2: This value asks for a message to be printed out when
246 // a class is instantiated
247 // 4: When this value is set, a message is displayed when a
248 // FGModel object executes its Run() method
249 // 8: When this value is set, various runtime state variables
250 // are printed out periodically
251 // 16: When set various parameters are sanity checked and
252 // a message is printed out when they go out of bounds
254 void FGFilter::Debug(int from)
258 if (debug_lvl <= 0) return;
260 if (debug_lvl & 1) { // Standard console startup message output
261 if (from == 0) { // Constructor
262 cout << " INPUT: " << InputNodes[0]->getName() << endl;
263 switch (FilterType) {
265 if (PropertySign[1] < 0.0) sgn="-";
267 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
268 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
271 if (PropertySign[1] < 0.0) sgn="-";
273 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
274 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
275 if (PropertySign[2] < 0.0) sgn="-";
277 if (PropertyNode[2] == 0L) cout << " C[2]: " << C[2] << endl;
278 else cout << " C[2] is the value of property: " << sgn << PropertyNode[2]->GetName() << endl;
279 if (PropertySign[3] < 0.0) sgn="-";
281 if (PropertyNode[3] == 0L) cout << " C[3]: " << C[3] << endl;
282 else cout << " C[3] is the value of property: " << sgn << PropertyNode[3]->GetName() << endl;
283 if (PropertySign[4] < 0.0) sgn="-";
285 if (PropertyNode[4] == 0L) cout << " C[4]: " << C[4] << endl;
286 else cout << " C[4] is the value of property: " << sgn << PropertyNode[4]->GetName() << endl;
289 if (PropertySign[1] < 0.0) sgn="-";
291 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
292 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
293 if (PropertySign[2] < 0.0) sgn="-";
295 if (PropertyNode[2] == 0L) cout << " C[2]: " << C[2] << endl;
296 else cout << " C[2] is the value of property: " << sgn << PropertyNode[2]->GetName() << endl;
297 if (PropertySign[3] < 0.0) sgn="-";
299 if (PropertyNode[3] == 0L) cout << " C[3]: " << C[3] << endl;
300 else cout << " C[3] is the value of property: " << sgn << PropertyNode[3]->GetName() << endl;
301 if (PropertySign[4] < 0.0) sgn="-";
303 if (PropertyNode[4] == 0L) cout << " C[4]: " << C[4] << endl;
304 else cout << " C[4] is the value of property: " << sgn << PropertyNode[4]->GetName() << endl;
305 if (PropertySign[5] < 0.0) sgn="-";
307 if (PropertyNode[5] == 0L) cout << " C[5]: " << C[5] << endl;
308 else cout << " C[5] is the value of property: " << sgn << PropertyNode[5]->GetName() << endl;
309 if (PropertySign[6] < 0.0) sgn="-";
311 if (PropertyNode[6] == 0L) cout << " C[6]: " << C[6] << endl;
312 else cout << " C[6] is the value of property: " << sgn << PropertyNode[6]->GetName() << endl;
315 if (PropertySign[1] < 0.0) sgn="-";
317 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
318 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
321 if (PropertySign[1] < 0.0) sgn="-";
323 if (PropertyNode[1] == 0L) cout << " C[1]: " << C[1] << endl;
324 else cout << " C[1] is the value of property: " << sgn << PropertyNode[1]->GetName() << endl;
330 for (unsigned int i=0; i<OutputNodes.size(); i++)
331 cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
335 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
336 if (from == 0) cout << "Instantiated: FGFilter" << endl;
337 if (from == 1) cout << "Destroyed: FGFilter" << endl;
339 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
341 if (debug_lvl & 8 ) { // Runtime state variables
343 if (debug_lvl & 16) { // Sanity checking
345 if (debug_lvl & 64) {
346 if (from == 0) { // Constructor
347 cout << IdSrc << endl;
348 cout << IdHdr << endl;