]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGFilter.cpp
Compiler warning fixes and small updates
[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 namespace JSBSim {
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_FILTER;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 FGFilter::FGFilter(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
52                                                        AC_cfg(AC_cfg)
53 {
54   string token;
55   double denom;
56   string sOutputIdx;
57
58   Type = AC_cfg->GetValue("TYPE");
59   Name = AC_cfg->GetValue("NAME");
60   AC_cfg->GetNextConfigLine();
61   dt = fcs->GetState()->Getdt();
62
63   C1 = C2 = C3 = C4 = C5 = C6 = 0.0;
64
65   if      (Type == "LAG_FILTER")          FilterType = eLag        ;
66   else if (Type == "LEAD_LAG_FILTER")     FilterType = eLeadLag    ;
67   else if (Type == "SECOND_ORDER_FILTER") FilterType = eOrder2     ;
68   else if (Type == "WASHOUT_FILTER")      FilterType = eWashout    ;
69   else if (Type == "INTEGRATOR")          FilterType = eIntegrator ;
70   else                                    FilterType = eUnknown    ;
71
72   while ((token = AC_cfg->GetValue()) != string("/COMPONENT")) {
73     *AC_cfg >> token;
74     if (token == "C1")          *AC_cfg >> C1;
75     else if (token == "C2")     *AC_cfg >> C2;
76     else if (token == "C3")     *AC_cfg >> C3;
77     else if (token == "C4")     *AC_cfg >> C4;
78     else if (token == "C5")     *AC_cfg >> C5;
79     else if (token == "C6")     *AC_cfg >> C6;
80     else if (token == "INPUT")
81     {
82       token = AC_cfg->GetValue("INPUT");
83       if( InputNodes.size() > 0 ) {
84         cerr << "Filters can only accept one input" << endl;
85       } else  {
86         *AC_cfg >> token;
87         InputNodes.push_back( resolveSymbol(token) );
88       }  
89     }
90     else if (token == "OUTPUT")
91     {
92       IsOutput = true;
93       *AC_cfg >> sOutputIdx;
94       OutputNode = PropertyManager->GetNode( sOutputIdx );
95     }
96     else cerr << "Unknown filter type: " << token << endl;
97   }
98
99   Initialize = true;
100
101   switch (FilterType) {
102     case eLag:
103       denom = 2.00 + dt*C1;
104       ca = dt*C1 / denom;
105       cb = (2.00 - dt*C1) / denom;
106       break;
107     case eLeadLag:
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;
112       break;
113     case eOrder2:
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;
120       break;
121     case eWashout:
122       denom = 2.00 + dt*C1;
123       ca = 2.00 / denom;
124       cb = (2.00 - dt*C1) / denom;
125       break;
126     case eIntegrator:
127       ca = dt*C1 / 2.00;
128       break;
129     case eUnknown:
130       cerr << "Unknown filter type" << endl;
131     break;
132   }
133   FGFCSComponent::bind();
134
135   Debug(0);
136 }
137
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139
140 FGFilter::~FGFilter()
141 {
142   Debug(1);
143 }
144
145 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
147 bool FGFilter::Run(void)
148 {
149   FGFCSComponent::Run(); // call the base class for initialization of Input
150
151   if (Initialize) {
152
153     PreviousOutput1 = PreviousInput1 = Output = Input;
154     Initialize = false;
155
156   } else {
157     Input = InputNodes[0]->getDoubleValue();
158     switch (FilterType) {
159       case eLag:
160         Output = Input * ca + PreviousInput1 * ca + PreviousOutput1 * cb;
161         break;
162       case eLeadLag:
163         Output = Input * ca + PreviousInput1 * cb + PreviousOutput1 * cc;
164         break;
165       case eOrder2:
166         Output = Input * ca + PreviousInput1 * cb + PreviousInput2 * cc
167                                   - PreviousOutput1 * cd - PreviousOutput2 * ce;
168         break;
169       case eWashout:
170         Output = Input * ca - PreviousInput1 * ca + PreviousOutput1 * cb;
171         break;
172       case eIntegrator:
173         Output = Input * ca + PreviousInput1 * ca + PreviousOutput1;
174         break;
175       case eUnknown:
176         break;
177     }
178
179   }
180
181   PreviousOutput2 = PreviousOutput1;
182   PreviousOutput1 = Output;
183   PreviousInput2  = PreviousInput1;
184   PreviousInput1  = Input;
185
186   if (IsOutput) SetOutput();
187
188   return true;
189 }
190
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 //    The bitmasked value choices are as follows:
193 //    unset: In this case (the default) JSBSim would only print
194 //       out the normally expected messages, essentially echoing
195 //       the config files as they are read. If the environment
196 //       variable is not set, debug_lvl is set to 1 internally
197 //    0: This requests JSBSim not to output any messages
198 //       whatsoever.
199 //    1: This value explicity requests the normal JSBSim
200 //       startup messages
201 //    2: This value asks for a message to be printed out when
202 //       a class is instantiated
203 //    4: When this value is set, a message is displayed when a
204 //       FGModel object executes its Run() method
205 //    8: When this value is set, various runtime state variables
206 //       are printed out periodically
207 //    16: When set various parameters are sanity checked and
208 //       a message is printed out when they go out of bounds
209
210 void FGFilter::Debug(int from)
211 {
212   if (debug_lvl <= 0) return;
213
214   if (debug_lvl & 1) { // Standard console startup message output
215     if (from == 0) { // Constructor
216       cout << "      INPUT: " << InputNodes[0]->getName() << endl;
217       cout << "      C1: " << C1 << endl;
218       cout << "      C2: " << C2 << endl;
219       cout << "      C3: " << C3 << endl;
220       cout << "      C4: " << C4 << endl;
221       cout << "      C5: " << C5 << endl;
222       cout << "      C6: " << C6 << endl;
223       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
224     }
225   }
226   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
227     if (from == 0) cout << "Instantiated: FGFilter" << endl;
228     if (from == 1) cout << "Destroyed:    FGFilter" << endl;
229   }
230   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
231   }
232   if (debug_lvl & 8 ) { // Runtime state variables
233   }
234   if (debug_lvl & 16) { // Sanity checking
235   }
236   if (debug_lvl & 64) {
237     if (from == 0) { // Constructor
238       cout << IdSrc << endl;
239       cout << IdHdr << endl;
240     }
241   }
242 }
243 }