]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGGain.cpp
sync JSBSim and FlightGear
[flightgear.git] / src / FDM / JSBSim / filtersjb / FGGain.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGGain.cpp
4  Author:       Jon S. Berndt
5  Date started: 4/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 "FGGain.h" 
41
42 namespace JSBSim {
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_GAIN;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51
52 FGGain::FGGain(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
53                                                    AC_cfg(AC_cfg)
54 {
55   string token;
56   string strScheduledBy;
57   string sOutputIdx;
58
59   State = fcs->GetState();
60
61   Gain = 1.000;
62   Rows = 0;
63   Min = Max = 0.0;
64   OutputPct=0;
65   invert=false;
66   ScheduledBy = 0;
67
68   Type = AC_cfg->GetValue("TYPE");
69   Name = AC_cfg->GetValue("NAME");
70   AC_cfg->GetNextConfigLine();
71
72   while ((token = AC_cfg->GetValue()) != string("/COMPONENT")) {
73     *AC_cfg >> token;
74     if (token == "INPUT") {
75       token = AC_cfg->GetValue("INPUT");
76       if (InputNodes.size() > 0) {
77         cerr << "Gains can only accept one input" << endl;
78       } else  {
79         *AC_cfg >> token;
80         InputNodes.push_back( resolveSymbol(token) );
81       }  
82     } else if (token == "GAIN") {
83       *AC_cfg >> Gain;
84     } else if (token == "MIN") {
85       *AC_cfg >> Min;
86     } else if (token == "MAX") {
87       *AC_cfg >> Max;
88     } else if (token == "INVERT") {
89       invert=true;  
90     } else if (token == "ROWS") {
91       *AC_cfg >> Rows;
92       Table = new FGTable(Rows);
93     } else if (token == "SCHEDULED_BY") {
94       token = AC_cfg->GetValue("SCHEDULED_BY");
95       *AC_cfg >> strScheduledBy;
96       ScheduledBy = PropertyManager->GetNode( strScheduledBy ); 
97     } else if (token == "OUTPUT") {
98       IsOutput = true;
99       *AC_cfg >> sOutputIdx;      
100       OutputNode = PropertyManager->GetNode( sOutputIdx );
101
102     } else {
103       AC_cfg->ResetLineIndexToZero();
104       *Table << *AC_cfg;
105     }
106   }
107   
108   FGFCSComponent::bind();
109   if (Type == "AEROSURFACE_SCALE")
110     treenode->Tie( "output-norm", this, &FGGain::GetOutputPct );
111
112   Debug(0);
113 }
114
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116
117 FGGain::~FGGain()
118 {
119   Debug(1);
120 }
121
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123
124 bool FGGain::Run(void )
125 {
126   double SchedGain = 1.0;
127   double LookupVal = 0;
128
129   FGFCSComponent::Run(); // call the base class for initialization of Input
130   Input = InputNodes[0]->getDoubleValue();
131   if (Type == "PURE_GAIN") {
132     Output = Gain * Input;
133   } else if (Type == "SCHEDULED_GAIN") {
134     LookupVal = ScheduledBy->getDoubleValue();
135     SchedGain = Table->GetValue(LookupVal);
136     Output = Gain * SchedGain * Input;
137   } else if (Type == "AEROSURFACE_SCALE") {
138     if (!invert) {
139       OutputPct = Input;
140       if (Input >= 0.0) Output = Input * Max;
141       else Output = Input * -Min;
142     } else {
143       OutputPct=-1*Input;
144       if (Input <= 0.0) Output = Input * -Max;
145       else Output = Input * Min;
146     } 
147     Output *= Gain;
148   }
149
150   if (IsOutput) SetOutput();
151
152   return true;
153 }
154
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 //    The bitmasked value choices are as follows:
157 //    unset: In this case (the default) JSBSim would only print
158 //       out the normally expected messages, essentially echoing
159 //       the config files as they are read. If the environment
160 //       variable is not set, debug_lvl is set to 1 internally
161 //    0: This requests JSBSim not to output any messages
162 //       whatsoever.
163 //    1: This value explicity requests the normal JSBSim
164 //       startup messages
165 //    2: This value asks for a message to be printed out when
166 //       a class is instantiated
167 //    4: When this value is set, a message is displayed when a
168 //       FGModel object executes its Run() method
169 //    8: When this value is set, various runtime state variables
170 //       are printed out periodically
171 //    16: When set various parameters are sanity checked and
172 //       a message is printed out when they go out of bounds
173
174 void FGGain::Debug(int from)
175 {
176   if (debug_lvl <= 0) return;
177
178   if (debug_lvl & 1) { // Standard console startup message output
179     if (from == 0) { // Constructor
180       cout << "      INPUT: " << InputNodes[0]->getName() << endl;
181       cout << "      GAIN: " << Gain << endl;
182       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
183       cout << "      MIN: " << Min << endl;
184       cout << "      MAX: " << Max << endl;
185       if(invert) cout << "      Invert mapping" << endl;
186       if (ScheduledBy != 0) {
187         cout << "      Scheduled by parameter: " << ScheduledBy->getName() << endl;
188         Table->Print();
189       }
190     }
191   }
192   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
193     if (from == 0) cout << "Instantiated: FGGain" << endl;
194     if (from == 1) cout << "Destroyed:    FGGain" << endl;
195   }
196   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
197   }
198   if (debug_lvl & 8 ) { // Runtime state variables
199   }
200   if (debug_lvl & 16) { // Sanity checking
201   }
202   if (debug_lvl & 64) {
203     if (from == 0) { // Constructor
204       cout << IdSrc << endl;
205       cout << IdHdr << endl;
206     }
207   }
208 }
209 }