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)
55 dt = fcs->GetState()->Getdt();
58 C1 = C2 = C3 = C4 = C5 = C6 = 0.0;
60 if (Type == "LAG_FILTER") FilterType = eLag ;
61 else if (Type == "LEAD_LAG_FILTER") FilterType = eLeadLag ;
62 else if (Type == "SECOND_ORDER_FILTER") FilterType = eOrder2 ;
63 else if (Type == "WASHOUT_FILTER") FilterType = eWashout ;
64 else if (Type == "INTEGRATOR") FilterType = eIntegrator ;
65 else FilterType = eUnknown ;
67 if (element->FindElement("c1")) C1 = element->FindElementValueAsNumber("c1");
68 if (element->FindElement("c2")) C2 = element->FindElementValueAsNumber("c2");
69 if (element->FindElement("c3")) C3 = element->FindElementValueAsNumber("c3");
70 if (element->FindElement("c4")) C4 = element->FindElementValueAsNumber("c4");
71 if (element->FindElement("c5")) C5 = element->FindElementValueAsNumber("c5");
72 if (element->FindElement("c6")) C6 = element->FindElementValueAsNumber("c6");
73 if (element->FindElement("trigger")) {
74 Trigger = PropertyManager->GetNode(element->FindElementValue("trigger"));
83 cb = (2.00 - dt*C1) / denom;
86 denom = 2.00*C3 + dt*C4;
87 ca = (2.00*C1 + dt*C2) / denom;
88 cb = (dt*C2 - 2.00*C1) / denom;
89 cc = (2.00*C3 - dt*C4) / denom;
92 denom = 4.0*C4 + 2.0*C5*dt + C6*dt*dt;
93 ca = (4.0*C1 + 2.0*C2*dt + C3*dt*dt) / denom;
94 cb = (2.0*C3*dt*dt - 8.0*C1) / denom;
95 cc = (4.0*C1 - 2.0*C2*dt + C3*dt*dt) / denom;
96 cd = (2.0*C6*dt*dt - 8.0*C4) / denom;
97 ce = (4.0*C4 - 2.0*C5*dt + C6*dt*dt) / denom;
100 denom = 2.00 + dt*C1;
102 cb = (2.00 - dt*C1) / denom;
108 cerr << "Unknown filter type" << endl;
111 FGFCSComponent::bind();
116 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 FGFilter::~FGFilter()
123 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 bool FGFilter::Run(void)
131 PreviousOutput1 = PreviousInput1 = Output = Input;
136 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
137 switch (FilterType) {
139 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1 * cb;
142 Output = Input * ca + PreviousInput1 * cb + PreviousOutput1 * cc;
145 Output = Input * ca + PreviousInput1 * cb + PreviousInput2 * cc
146 - PreviousOutput1 * cd - PreviousOutput2 * ce;
149 Output = Input * ca - PreviousInput1 * ca + PreviousOutput1 * cb;
153 test = Trigger->getDoubleValue();
154 if (fabs(test) > 0.000001) {
155 Input = PreviousInput1 = PreviousInput2 = 0.0;
158 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1;
166 PreviousOutput2 = PreviousOutput1;
167 PreviousOutput1 = Output;
168 PreviousInput2 = PreviousInput1;
169 PreviousInput1 = Input;
172 if (IsOutput) SetOutput();
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 // The bitmasked value choices are as follows:
179 // unset: In this case (the default) JSBSim would only print
180 // out the normally expected messages, essentially echoing
181 // the config files as they are read. If the environment
182 // variable is not set, debug_lvl is set to 1 internally
183 // 0: This requests JSBSim not to output any messages
185 // 1: This value explicity requests the normal JSBSim
187 // 2: This value asks for a message to be printed out when
188 // a class is instantiated
189 // 4: When this value is set, a message is displayed when a
190 // FGModel object executes its Run() method
191 // 8: When this value is set, various runtime state variables
192 // are printed out periodically
193 // 16: When set various parameters are sanity checked and
194 // a message is printed out when they go out of bounds
196 void FGFilter::Debug(int from)
198 if (debug_lvl <= 0) return;
200 if (debug_lvl & 1) { // Standard console startup message output
201 if (from == 0) { // Constructor
202 cout << " INPUT: " << InputNodes[0]->getName() << endl;
203 cout << " C1: " << C1 << endl;
204 cout << " C2: " << C2 << endl;
205 cout << " C3: " << C3 << endl;
206 cout << " C4: " << C4 << endl;
207 cout << " C5: " << C5 << endl;
208 cout << " C6: " << C6 << endl;
209 if (IsOutput) cout << " OUTPUT: " << OutputNode->getName() << endl;
212 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
213 if (from == 0) cout << "Instantiated: FGFilter" << endl;
214 if (from == 1) cout << "Destroyed: FGFilter" << endl;
216 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
218 if (debug_lvl & 8 ) { // Runtime state variables
220 if (debug_lvl & 16) { // Sanity checking
222 if (debug_lvl & 64) {
223 if (from == 0) { // Constructor
224 cout << IdSrc << endl;
225 cout << IdHdr << endl;