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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_FILTER;
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 FGFilter::FGFilter(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
55 Type = AC_cfg->GetValue("TYPE");
56 Name = AC_cfg->GetValue("NAME");
57 AC_cfg->GetNextConfigLine();
58 dt = fcs->GetState()->Getdt();
60 C1 = C2 = C3 = C4 = C5 = C6 = 0.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 while ((token = AC_cfg->GetValue()) != string("/COMPONENT")) {
71 if (token == "ID") *AC_cfg >> ID;
72 else if (token == "C1") *AC_cfg >> C1;
73 else if (token == "C2") *AC_cfg >> C2;
74 else if (token == "C3") *AC_cfg >> C3;
75 else if (token == "C4") *AC_cfg >> C4;
76 else if (token == "C5") *AC_cfg >> C5;
77 else if (token == "C6") *AC_cfg >> C6;
78 else if (token == "INPUT")
80 token = AC_cfg->GetValue("INPUT");
81 if (token.find("FG_") != token.npos) {
83 InputIdx = fcs->GetState()->GetParameterIndex(token);
84 InputType = itPilotAC;
90 else if (token == "OUTPUT")
93 *AC_cfg >> sOutputIdx;
94 OutputIdx = fcs->GetState()->GetParameterIndex(sOutputIdx);
96 else cerr << "Unknown filter type: " << token << endl;
101 switch (FilterType) {
103 denom = 2.00 + dt*C1;
105 cb = (2.00 - dt*C1) / denom;
108 denom = 2.00*C3 + dt*C4;
109 ca = (2.00*C1 + dt*C2) / denom;
110 cb = (dt*C2 - 2.00*C1) / denom;
111 cc = (2.00*C3 - dt*C2) / denom;
114 denom = 4.0*C3 + 2.0*C5*dt + C6*dt*dt;
115 ca = 4.0*C1 + 2.0*C2*dt + C3*dt*dt / denom;
116 cb = 2.0*C3*dt*dt - 8.0*C1 / denom;
117 cc = 4.0*C1 - 2.0*C2*dt + C3*dt*dt / denom;
118 cd = 2.0*C6*dt*dt - 8.0*C4 / denom;
119 ce = 4.0*C3 - 2.0*C5*dt + C6*dt*dt / denom;
122 denom = 2.00 + dt*C1;
124 cb = (2.00 - dt*C1) / denom;
130 cerr << "Unknown filter type" << endl;
135 cout << " ID: " << ID << endl;
136 cout << " INPUT: " << InputIdx << endl;
137 cout << " C1: " << C1 << endl;
138 cout << " C2: " << C2 << endl;
139 cout << " C3: " << C3 << endl;
140 cout << " C4: " << C4 << endl;
141 cout << " C5: " << C5 << endl;
142 cout << " C6: " << C6 << endl;
143 if (IsOutput) cout << " OUTPUT: " << sOutputIdx << endl;
146 if (debug_lvl & 2) cout << "Instantiated: FGFilter" << endl;
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 FGFilter::~FGFilter()
153 if (debug_lvl & 2) cout << "Destroyed: FGFilter" << endl;
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 bool FGFilter::Run(void)
160 FGFCSComponent::Run(); // call the base class for initialization of Input
164 PreviousOutput1 = PreviousInput1 = Output = Input;
169 switch (FilterType) {
171 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1 * cb;
174 Output = Input * ca + PreviousInput1 * cb + PreviousOutput1 * cc;
177 Output = Input * ca + PreviousInput1 * cb + PreviousInput2 * cc
178 - PreviousOutput1 * cd - PreviousOutput2 * ce;
181 Output = Input * ca - PreviousInput1 * ca + PreviousOutput1 * cb;
184 Output = Input * ca + PreviousInput1 * ca + PreviousOutput1;
190 PreviousOutput2 = PreviousOutput1;
191 PreviousOutput1 = Output;
192 PreviousInput2 = PreviousInput1;
193 PreviousInput1 = Input;
195 if (IsOutput) SetOutput();
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 void FGFilter::Debug(void)
204 //TODO: Add your source code here