]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGFilter.cpp
Tidy up the autoconf/automake configuration a bit.
[flightgear.git] / src / FDM / JSBSim / filtersjb / FGFilter.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGFilter.cpp
4  Author:       Jon S. Berndt
5  Date started: 11/2000
6  
7  ------------- Copyright (C) 2000 -------------
8
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
12  version.
13
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
17  details.
18
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.
22
23  Further information about the GNU General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28
29 HISTORY
30 --------------------------------------------------------------------------------
31
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES,  and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGFilter.h"
41
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_FILTER;
44
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 CLASS IMPLEMENTATION
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48
49 FGFilter::FGFilter(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
50                                                        AC_cfg(AC_cfg)
51 {
52   string token;
53   double denom;
54   string sOutputIdx;
55
56   Type = AC_cfg->GetValue("TYPE");
57   Name = AC_cfg->GetValue("NAME");
58   AC_cfg->GetNextConfigLine();
59   dt = fcs->GetState()->Getdt();
60
61   C1 = C2 = C3 = C4 = C5 = C6 = 0.0;
62
63   if      (Type == "LAG_FILTER")          FilterType = eLag        ;
64   else if (Type == "LEAD_LAG_FILTER")     FilterType = eLeadLag    ;
65   else if (Type == "SECOND_ORDER_FILTER") FilterType = eOrder2     ;
66   else if (Type == "WASHOUT_FILTER")      FilterType = eWashout    ;
67   else if (Type == "INTEGRATOR")          FilterType = eIntegrator ;
68   else                                    FilterType = eUnknown    ;
69
70   while ((token = AC_cfg->GetValue()) != string("/COMPONENT")) {
71     *AC_cfg >> token;
72     if      (token == "ID")     *AC_cfg >> ID;
73     else if (token == "C1")     *AC_cfg >> C1;
74     else if (token == "C2")     *AC_cfg >> C2;
75     else if (token == "C3")     *AC_cfg >> C3;
76     else if (token == "C4")     *AC_cfg >> C4;
77     else if (token == "C5")     *AC_cfg >> C5;
78     else if (token == "C6")     *AC_cfg >> C6;
79     else if (token == "INPUT")
80     {
81       token = AC_cfg->GetValue("INPUT");
82       if (token.find("FG_") != token.npos) {
83         *AC_cfg >> token;
84         InputNode = PropertyManager->GetNode( 
85                     fcs->GetState()->GetPropertyName(token) );
86         InputType = itPilotAC;
87       } else {
88         *AC_cfg >> InputIdx;
89         InputType = itFCS;
90       }
91     }
92     else if (token == "OUTPUT")
93     {
94       IsOutput = true;
95       *AC_cfg >> sOutputIdx;
96       OutputNode = PropertyManager->GetNode( 
97                      fcs->GetState()->GetPropertyName(sOutputIdx) );
98     }
99     else cerr << "Unknown filter type: " << token << endl;
100   }
101
102   Initialize = true;
103
104   switch (FilterType) {
105     case eLag:
106       denom = 2.00 + dt*C1;
107       ca = dt*C1 / denom;
108       cb = (2.00 - dt*C1) / denom;
109       break;
110     case eLeadLag:
111       denom = 2.00*C3 + dt*C4;
112       ca = (2.00*C1 + dt*C2) / denom;
113       cb = (dt*C2 - 2.00*C1) / denom;
114       cc = (2.00*C3 - dt*C2) / denom;
115       break;
116     case eOrder2:
117       denom = 4.0*C3 + 2.0*C5*dt + C6*dt*dt;
118       ca = 4.0*C1 + 2.0*C2*dt + C3*dt*dt / denom;
119       cb = 2.0*C3*dt*dt - 8.0*C1 / denom;
120       cc = 4.0*C1 - 2.0*C2*dt + C3*dt*dt / denom;
121       cd = 2.0*C6*dt*dt - 8.0*C4 / denom;
122       ce = 4.0*C3 - 2.0*C5*dt + C6*dt*dt / denom;
123       break;
124     case eWashout:
125       denom = 2.00 + dt*C1;
126       ca = 2.00 / denom;
127       cb = (2.00 - dt*C1) / denom;
128       break;
129     case eIntegrator:
130       ca = dt*C1 / 2.00;
131       break;
132     case eUnknown:
133       cerr << "Unknown filter type" << endl;
134     break;
135   }
136
137   Debug(0);
138 }
139
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141
142 FGFilter::~FGFilter()
143 {
144   Debug(1);
145 }
146
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148
149 bool FGFilter::Run(void)
150 {
151   FGFCSComponent::Run(); // call the base class for initialization of Input
152
153   if (Initialize) {
154
155     PreviousOutput1 = PreviousInput1 = Output = Input;
156     Initialize = false;
157
158   } else {
159
160     switch (FilterType) {
161       case eLag:
162         Output = Input * ca + PreviousInput1 * ca + PreviousOutput1 * cb;
163         break;
164       case eLeadLag:
165         Output = Input * ca + PreviousInput1 * cb + PreviousOutput1 * cc;
166         break;
167       case eOrder2:
168         Output = Input * ca + PreviousInput1 * cb + PreviousInput2 * cc
169                                   - PreviousOutput1 * cd - PreviousOutput2 * ce;
170         break;
171       case eWashout:
172         Output = Input * ca - PreviousInput1 * ca + PreviousOutput1 * cb;
173         break;
174       case eIntegrator:
175         Output = Input * ca + PreviousInput1 * ca + PreviousOutput1;
176         break;
177       case eUnknown:
178         break;
179     }
180
181   }
182
183   PreviousOutput2 = PreviousOutput1;
184   PreviousOutput1 = Output;
185   PreviousInput2  = PreviousInput1;
186   PreviousInput1  = Input;
187
188   if (IsOutput) SetOutput();
189
190   return true;
191 }
192
193 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 //    The bitmasked value choices are as follows:
195 //    unset: In this case (the default) JSBSim would only print
196 //       out the normally expected messages, essentially echoing
197 //       the config files as they are read. If the environment
198 //       variable is not set, debug_lvl is set to 1 internally
199 //    0: This requests JSBSim not to output any messages
200 //       whatsoever.
201 //    1: This value explicity requests the normal JSBSim
202 //       startup messages
203 //    2: This value asks for a message to be printed out when
204 //       a class is instantiated
205 //    4: When this value is set, a message is displayed when a
206 //       FGModel object executes its Run() method
207 //    8: When this value is set, various runtime state variables
208 //       are printed out periodically
209 //    16: When set various parameters are sanity checked and
210 //       a message is printed out when they go out of bounds
211
212 void FGFilter::Debug(int from)
213 {
214   if (debug_lvl <= 0) return;
215
216   if (debug_lvl & 1) { // Standard console startup message output
217     if (from == 0) { // Constructor
218       cout << "      ID: " << ID << endl;
219       switch(InputType) {
220       case itPilotAC:
221         cout << "      INPUT: " << InputNode->getName() << endl;
222         break;
223       case itFCS:
224         cout << "      INPUT: FCS Component " << InputIdx << " (" << 
225                                         fcs->GetComponentName(InputIdx) << ")" << endl;
226         break;
227       case itAP:
228       case itBias:
229         break; 
230       }
231       cout << "      C1: " << C1 << endl;
232       cout << "      C2: " << C2 << endl;
233       cout << "      C3: " << C3 << endl;
234       cout << "      C4: " << C4 << endl;
235       cout << "      C5: " << C5 << endl;
236       cout << "      C6: " << C6 << endl;
237       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
238     }
239   }
240   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
241     if (from == 0) cout << "Instantiated: FGFilter" << endl;
242     if (from == 1) cout << "Destroyed:    FGFilter" << endl;
243   }
244   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
245   }
246   if (debug_lvl & 8 ) { // Runtime state variables
247   }
248   if (debug_lvl & 16) { // Sanity checking
249   }
250   if (debug_lvl & 64) {
251     if (from == 0) { // Constructor
252       cout << IdSrc << endl;
253       cout << IdHdr << endl;
254     }
255   }
256 }
257