]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/filtersjb/FGGain.cpp
Fixes to jsbsim.
[flightgear.git] / src / FDM / JSBSim / filtersjb / FGGain.cpp
1 /*******************************************************************************
2
3  Module:       FGGain.cpp
4  Author:       
5  Date started: 
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 /*******************************************************************************
43 ************************************ CODE **************************************
44 *******************************************************************************/
45
46 // *****************************************************************************
47 //  Function:   Constructor
48 //  Purpose:    Builds a Gain-type of FCS component.
49 //  Parameters: void
50 //  Comments:   Types are PURE_GAIN, SCHEDULED_GAIN, and AEROSURFACE_SCALE
51
52 FGGain::FGGain(FGFCS* fcs, FGConfigFile* AC_cfg) : FGFCSComponent(fcs),
53                                                    AC_cfg(AC_cfg)
54 {
55   string token;
56
57   lookup = NULL;
58   Schedule.clear();
59   Gain = 1.000;
60   Min = Max = 0;
61   ScheduledBy = 0;
62
63   Type = AC_cfg->GetValue("TYPE");
64   Name = AC_cfg->GetValue("NAME");
65   AC_cfg->GetNextConfigLine();
66
67   while ((token = AC_cfg->GetValue()) != "/COMPONENT") {
68     *AC_cfg >> token;
69     if (token == "ID") {
70       *AC_cfg >> ID;
71           cout << "      ID: " << ID << endl;
72     } else if (token == "INPUT") {
73       token = AC_cfg->GetValue("INPUT");
74           cout << "      INPUT: " << token << endl;
75       if (token.find("FG_") != token.npos) {
76         *AC_cfg >> token;
77         InputIdx = fcs->GetState()->GetParameterIndex(token);
78         InputType = itPilotAC;
79       } else {
80         *AC_cfg >> InputIdx;
81         InputType = itFCS;
82       }
83     } else if (token == "GAIN") {
84       *AC_cfg >> Gain;
85       cout << "      GAIN: " << Gain << endl;
86     } else if (token == "MIN") {
87       *AC_cfg >> Min;
88           cout << "      MIN: " << Min << endl;
89     } else if (token == "MAX") {
90       *AC_cfg >> Max;
91           cout << "      MAX: " << Max << endl;
92     } else if (token == "SCHEDULED_BY") {
93       *AC_cfg >> ScheduledBy;
94     } else if (token == "OUTPUT") {
95       IsOutput = true;
96       *AC_cfg >> sOutputIdx;
97       OutputIdx = fcs->GetState()->GetParameterIndex(sOutputIdx);
98       cout << "      OUTPUT: " << sOutputIdx << endl;
99     } else {
100       AC_cfg->ResetLineIndexToZero();
101       lookup = new float[2];
102       *AC_cfg >> lookup[0] >> lookup[1];
103       Schedule.push_back(lookup);
104     }
105   }
106 }
107
108 // *****************************************************************************
109 //  Function:   Run
110 //  Purpose:
111 //  Parameters: void
112 //  Comments:
113
114 bool FGGain::Run(void ) 
115 {
116   float SchedGain = 1.0;
117
118   FGFCSComponent::Run(); // call the base class for initialization of Input
119
120   if (Type == "PURE_GAIN") {
121
122     Output = Gain * Input;
123
124   } else if (Type == "SCHEDULED_GAIN") {
125
126     float LookupVal = fcs->GetState()->GetParameter(ScheduledBy);
127     unsigned int last = Schedule.size()-1;
128     float lowVal = Schedule[0][0], hiVal = Schedule[last][0];
129     float factor = 1.0;
130
131     if (LookupVal <= lowVal) Output = Gain * Schedule[0][1];
132     else if (LookupVal >= hiVal) Output = Gain * Schedule[last][1];
133     else {
134       for (unsigned int ctr = 1; ctr < last; ctr++) {
135         if (LookupVal < Schedule[ctr][0]) {
136           hiVal = Schedule[ctr][0];
137           lowVal = Schedule[ctr-1][0];
138           factor = (LookupVal - lowVal) / (hiVal - lowVal);
139           SchedGain = Schedule[ctr-1][1] + factor*(Schedule[ctr][1] - Schedule[ctr-1][1]);
140           Output = Gain * SchedGain * Input;
141           break;
142         }
143       }
144     }
145
146   } else if (Type == "AEROSURFACE_SCALE") {
147
148     if (Output >= 0.0) Output = Input * Max;
149     else Output = Input * (-Min);
150
151     Output *= Gain;
152   }
153
154   if (IsOutput) SetOutput();
155
156   return true;
157 }
158