]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFCS.cpp
Syncing with latest JSBSim code with retractable landing gear support.
[flightgear.git] / src / FDM / JSBSim / FGFCS.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  
3  Module:       FGFCS.cpp
4  Author:       Jon Berndt
5  Date started: 12/12/98
6  Purpose:      Model the flight controls
7  Called by:    FDMExec
8  
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10  
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15  
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  details.
20  
21  You should have received a copy of the GNU General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24  
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27  
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class models the flight controls for a specific airplane
31  
32 HISTORY
33 --------------------------------------------------------------------------------
34 12/12/98   JSB   Created
35  
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGFCS.h"
41 #include "FGState.h"
42 #include "FGFDMExec.h"
43 #include "FGAtmosphere.h"
44 #include "FGAircraft.h"
45 #include "FGTranslation.h"
46 #include "FGRotation.h"
47 #include "FGPosition.h"
48 #include "FGAuxiliary.h"
49 #include "FGOutput.h"
50
51 #include "filtersjb/FGFilter.h"
52 #include "filtersjb/FGDeadBand.h"
53 #include "filtersjb/FGGain.h"
54 #include "filtersjb/FGGradient.h"
55 #include "filtersjb/FGSwitch.h"
56 #include "filtersjb/FGSummer.h"
57 #include "filtersjb/FGKinemat.h"
58
59 static const char *IdSrc = "$Id$";
60 static const char *IdHdr = ID_FCS;
61
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 CLASS IMPLEMENTATION
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65
66 FGFCS::FGFCS(FGFDMExec* fdmex) : FGModel(fdmex)
67 {
68   Name = "FGFCS";
69
70   DaCmd = DeCmd = DrCmd = DfCmd = DsbCmd = DspCmd = PTrimCmd = 0.0;
71   DaPos = DePos = DrPos = DfPos = DsbPos = DspPos = 0.0;
72   GearCmd = GearPos = 1; // default to gear down
73   LeftBrake = RightBrake = CenterBrake = 0.0;
74
75   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
76 }
77
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80 FGFCS::~FGFCS()
81 {
82   ThrottleCmd.clear();
83   ThrottlePos.clear();
84   MixtureCmd.clear();
85   MixturePos.clear();
86   PropPitchCmd.clear();
87   PropPitchPos.clear();
88
89   unsigned int i;
90
91   for (i=0;i<Components.size();i++) delete Components[i];
92   if (debug_lvl & 2) cout << "Destroyed:    FGFCS" << endl;
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96
97 bool FGFCS::Run(void)
98 {
99   unsigned int i;
100
101   if (!FGModel::Run()) {
102     for (i=0; i<ThrottlePos.size(); i++) ThrottlePos[i] = ThrottleCmd[i];
103     for (i=0; i<MixturePos.size(); i++) MixturePos[i] = MixtureCmd[i];
104     for (i=0; i<PropPitchPos.size(); i++) PropPitchPos[i] = PropPitchCmd[i];
105     for (i=0; i<Components.size(); i++)  Components[i]->Run();
106   } else {
107   }
108
109   return false;
110 }
111
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114 void FGFCS::SetThrottleCmd(int engineNum, double setting)
115 {
116   unsigned int ctr;
117
118   if (engineNum < (int)ThrottlePos.size()) {
119     if (engineNum < 0) {
120       for (ctr=0;ctr<ThrottleCmd.size();ctr++) ThrottleCmd[ctr] = setting;
121     } else {
122       ThrottleCmd[engineNum] = setting;
123     }
124   } else {
125     cerr << "Throttle " << engineNum << " does not exist! " << ThrottleCmd.size()
126          << " engines exist, but attempted throttle command is for engine "
127          << engineNum << endl;
128   }
129 }
130
131 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132
133 void FGFCS::SetThrottlePos(int engineNum, double setting)
134 {
135   unsigned int ctr;
136
137   if (engineNum < (int)ThrottlePos.size()) {
138     if (engineNum < 0) {
139       for (ctr=0;ctr<ThrottlePos.size();ctr++) ThrottlePos[ctr] = setting;
140     } else {
141       ThrottlePos[engineNum] = setting;
142     }
143   } else {
144     cerr << "Throttle " << engineNum << " does not exist! " << ThrottlePos.size()
145          << " engines exist, but attempted throttle position setting is for engine "
146          << engineNum << endl;
147   }
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 double FGFCS::GetThrottleCmd(int engineNum)
153 {
154   if (engineNum < (int)ThrottlePos.size()) {
155     if (engineNum < 0) {
156        cerr << "Cannot get throttle value for ALL engines" << endl;
157     } else {
158       return ThrottleCmd[engineNum];
159     }
160   } else {
161     cerr << "Throttle " << engineNum << " does not exist! " << ThrottleCmd.size()
162          << " engines exist, but throttle setting for engine " << engineNum
163                << " is selected" << endl;
164   }
165   return 0.0;
166 }
167
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169
170 double FGFCS::GetThrottlePos(int engineNum)
171 {
172   if (engineNum < (int)ThrottlePos.size()) {
173     if (engineNum < 0) {
174        cerr << "Cannot get throttle value for ALL engines" << endl;
175     } else {
176       return ThrottlePos[engineNum];
177     }
178   } else {
179     cerr << "Throttle " << engineNum << " does not exist! " << ThrottlePos.size()
180          << " engines exist, but attempted throttle position setting is for engine "
181          << engineNum << endl;
182   }
183   return 0.0; 
184 }
185
186 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187
188 void FGFCS::SetMixtureCmd(int engineNum, double setting)
189 {
190   unsigned int ctr;
191
192   if (engineNum < (int)ThrottlePos.size()) {
193     if (engineNum < 0) {
194       for (ctr=0;ctr<MixtureCmd.size();ctr++) MixtureCmd[ctr] = setting;
195     } else {
196       MixtureCmd[engineNum] = setting;
197     }
198   }
199 }
200
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
203 void FGFCS::SetMixturePos(int engineNum, double setting)
204 {
205   unsigned int ctr;
206
207   if (engineNum < (int)ThrottlePos.size()) {
208     if (engineNum < 0) {
209       for (ctr=0;ctr<=MixtureCmd.size();ctr++) MixturePos[ctr] = MixtureCmd[ctr];
210     } else {
211       MixturePos[engineNum] = setting;
212     }
213   }
214 }
215
216 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217
218 void FGFCS::SetPropPitchCmd(int engineNum, double setting)
219 {
220   unsigned int ctr;
221
222   if (engineNum < (int)ThrottlePos.size()) {
223     if (engineNum < 0) {
224       for (ctr=0;ctr<PropPitchCmd.size();ctr++) PropPitchCmd[ctr] = setting;
225     } else {
226       PropPitchCmd[engineNum] = setting;
227     }
228   }
229 }
230
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232
233 void FGFCS::SetPropPitchPos(int engineNum, double setting)
234 {
235   unsigned int ctr;
236
237   if (engineNum < (int)ThrottlePos.size()) {
238     if (engineNum < 0) {
239       for (ctr=0;ctr<=PropPitchCmd.size();ctr++) PropPitchPos[ctr] = PropPitchCmd[ctr];
240     } else {
241       PropPitchPos[engineNum] = setting;
242     }
243   }
244 }
245
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247
248 bool FGFCS::Load(FGConfigFile* AC_cfg)
249 {
250   string token;
251
252   Name = Name + ":" + AC_cfg->GetValue("NAME");
253   if (debug_lvl > 0) cout << "    Control System Name: " << Name << endl;
254   AC_cfg->GetNextConfigLine();
255   while ((token = AC_cfg->GetValue()) != string("/FLIGHT_CONTROL")) {
256     if (token == "COMPONENT") {
257       token = AC_cfg->GetValue("TYPE");
258       if (debug_lvl > 0) cout << "    Loading Component \""
259                               << AC_cfg->GetValue("NAME")
260                                                 << "\" of type: " << token << endl;
261       if ((token == "LAG_FILTER") ||
262           (token == "LEAD_LAG_FILTER") ||
263           (token == "SECOND_ORDER_FILTER") ||
264           (token == "WASHOUT_FILTER") ||
265           (token == "INTEGRATOR") ) {
266         Components.push_back(new FGFilter(this, AC_cfg));
267       } else if ((token == "PURE_GAIN") ||
268                  (token == "SCHEDULED_GAIN") ||
269                  (token == "AEROSURFACE_SCALE") ) {
270
271         Components.push_back(new FGGain(this, AC_cfg));
272
273       } else if (token == "SUMMER") {
274         Components.push_back(new FGSummer(this, AC_cfg));
275       } else if (token == "DEADBAND") {
276         Components.push_back(new FGDeadBand(this, AC_cfg));
277       } else if (token == "GRADIENT") {
278         Components.push_back(new FGGradient(this, AC_cfg));
279       } else if (token == "SWITCH") {
280         Components.push_back(new FGSwitch(this, AC_cfg));
281       } else if (token == "KINEMAT") {
282         Components.push_back(new FGKinemat(this, AC_cfg));
283       } else {
284         cerr << "Unknown token [" << token << "] in FCS portion of config file" << endl;
285         return false;
286       }
287       AC_cfg->GetNextConfigLine();
288     }
289   }
290   return true;
291 }
292
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294
295 double FGFCS::GetComponentOutput(eParam idx) {
296   return Components[idx]->GetOutput();
297 }
298
299 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300
301 string FGFCS::GetComponentName(int idx) {
302   return Components[idx]->GetName();
303 }
304
305 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306
307 double FGFCS::GetBrake(FGLGear::BrakeGroup bg) {
308   switch (bg) {
309   case FGLGear::bgLeft:
310     return LeftBrake;
311   case FGLGear::bgRight:
312     return RightBrake;
313   case FGLGear::bgCenter:
314     return CenterBrake;
315   default:
316     cerr << "GetBrake asked to return a bogus brake value" << endl;
317   }
318   return 0.0;
319 }
320
321 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322
323 string FGFCS::GetComponentStrings(void)
324 {
325   unsigned int comp;
326
327   string CompStrings = "";
328   bool firstime = true;
329
330   for (comp = 0; comp < Components.size(); comp++) {
331     if (firstime) firstime = false;
332     else          CompStrings += ", ";
333
334     CompStrings += Components[comp]->GetName();
335   }
336
337   return CompStrings;
338 }
339
340 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341
342 string FGFCS::GetComponentValues(void)
343 {
344   unsigned int comp;
345
346   string CompValues = "";
347   char buffer[10];
348   bool firstime = true;
349
350   for (comp = 0; comp < Components.size(); comp++) {
351     if (firstime) firstime = false;
352     else          CompValues += ", ";
353
354     sprintf(buffer, "%9.6f", Components[comp]->GetOutput());
355     CompValues += string(buffer);
356   }
357
358   return CompValues;
359 }
360
361 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362
363 void FGFCS::AddThrottle(void)
364 {
365   ThrottleCmd.push_back(0.0);
366   ThrottlePos.push_back(0.0);
367   MixtureCmd.push_back(0.0);    // assume throttle and mixture are coupled
368   MixturePos.push_back(0.0);
369   PropPitchCmd.push_back(0.0);  // assume throttle and prop pitch are coupled
370   PropPitchPos.push_back(0.0);
371 }
372
373 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374
375 void FGFCS::Debug(void)
376 {
377     //TODO: Add your source code here
378 }
379