]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
Updates to JSBSim and FDM interface.
[flightgear.git] / src / FDM / JSBSim / FGAircraft.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  
3  Module:       FGAircraft.cpp
4  Author:       Jon S. Berndt
5  Date started: 12/12/98                                   
6  Purpose:      Encapsulates an aircraft
7  Called by:    FGFDMExec
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 Models the aircraft reactions and forces. This class is instantiated by the
31 FGFDMExec class and scheduled as an FDM entry. 
32  
33 HISTORY
34 --------------------------------------------------------------------------------
35 12/12/98   JSB   Created
36 04/03/99   JSB   Changed Aero() method to correct body axis force calculation
37                  from wind vector. Fix provided by Tony Peden.
38 05/03/99   JSB   Changed (for the better?) the way configurations are read in.
39 9/17/99     TP   Combined force and moment functions. Added aero reference 
40                  point to config file. Added calculations for moments due to 
41                  difference in cg and aero reference point
42  
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 COMMENTS, REFERENCES,  and NOTES
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46  
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 INCLUDES
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 #include <sys/stat.h>
52 #include <sys/types.h>
53
54 #ifdef FGFS
55 #  ifndef __BORLANDC__
56 #    include <simgear/compiler.h>
57 #  endif
58 #  ifdef SG_HAVE_STD_INCLUDES
59 #    include <cmath>
60 #  else
61 #    include <math.h>
62 #  endif
63 #else
64 #  if defined (sgi) && !defined(__GNUC__)
65 #    include <math.h>
66 #  else
67 #    include <cmath>
68 #  endif
69 #endif
70
71 #include "FGAircraft.h"
72 #include "FGMassBalance.h"
73 #include "FGInertial.h"
74 #include "FGGroundReactions.h"
75 #include "FGAerodynamics.h"
76 #include "FGTranslation.h"
77 #include "FGRotation.h"
78 #include "FGAtmosphere.h"
79 #include "FGState.h"
80 #include "FGFDMExec.h"
81 #include "FGFCS.h"
82 #include "FGPosition.h"
83 #include "FGAuxiliary.h"
84 #include "FGOutput.h"
85
86 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 DEFINITIONS
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
89
90 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 GLOBAL DATA
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
93
94 static const char *IdSrc = "$Id$";
95 static const char *IdHdr = ID_AIRCRAFT;
96
97 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 CLASS IMPLEMENTATION
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
100
101 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex),
102     vMoments(3),
103     vForces(3),
104     vXYZrp(3),
105     vXYZep(3),
106     vDXYZcg(3),
107     vBodyAccel(3),
108     vNcg(3)
109 {
110   Name = "FGAircraft";
111   alphaclmin = alphaclmax = 0;
112   HTailArea = VTailArea = HTailArm = VTailArm = 0.0;
113   lbarh = lbarv = vbarh = vbarv = 0.0;
114   WingIncidence=0;
115   impending_stall = 0;
116
117   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
118 }
119
120
121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
123
124 FGAircraft::~FGAircraft()
125 {
126   if (debug_lvl & 2) cout << "Destroyed:    FGAircraft" << endl;
127 }
128
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
131 bool FGAircraft::Load(FGConfigFile* AC_cfg)
132 {
133   string token;
134
135   ReadPrologue(AC_cfg);
136
137   while ((AC_cfg->GetNextConfigLine() != "EOF") &&
138          (token = AC_cfg->GetValue()) != "/FDM_CONFIG") {
139     if (token == "METRICS") {
140       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Metrics" << fgdef << endl;
141       ReadMetrics(AC_cfg);
142     } else if (token == "AERODYNAMICS") {
143       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Aerodynamics" << fgdef << endl;
144       ReadAerodynamics(AC_cfg);
145     } else if (token == "UNDERCARRIAGE") {
146       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Landing Gear" << fgdef << endl;
147       ReadUndercarriage(AC_cfg);
148     } else if (token == "PROPULSION") {
149       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Propulsion" << fgdef << endl;
150       ReadPropulsion(AC_cfg);
151     } else if (token == "FLIGHT_CONTROL") {
152       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Flight Control" << fgdef << endl;
153       ReadFlightControls(AC_cfg);
154     } else if (token == "OUTPUT") {
155       if (debug_lvl > 0) cout << fgcyan << "\n  Reading Output directives" << fgdef << endl;
156       ReadOutput(AC_cfg);
157     }
158   }
159   
160   return true;
161 }
162
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164
165 bool FGAircraft::Run(void)
166 {
167   if (!FGModel::Run()) {                 // if false then execute this Run()
168     vForces.InitMatrix();
169     vForces += Aerodynamics->GetForces();
170     vForces += Inertial->GetForces();
171     vForces += Propulsion->GetForces();
172     vForces += GroundReactions->GetForces();
173
174     vMoments.InitMatrix();
175     vMoments += Aerodynamics->GetMoments();
176     vMoments += Propulsion->GetMoments();
177     vMoments += GroundReactions->GetMoments();
178     
179     vBodyAccel = vForces/MassBalance->GetMass();
180     
181     vNcg = vBodyAccel*INVGRAVITY;
182     
183     if (alphaclmax != 0) {
184       if (Translation->Getalpha() > 0.85*alphaclmax) {
185         impending_stall = 10*(Translation->Getalpha()/alphaclmax - 0.85);
186       } else {
187         impending_stall = 0;
188       }
189     }      
190     
191     return false;
192   } else {                               // skip Run() execution this time
193     return true;
194   }
195 }
196
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198
199 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg)
200 {
201   string token = "";
202   string parameter;
203   float EW, bixx, biyy, bizz, bixz, biyz;
204   FGColumnVector3 vbaseXYZcg(3);
205
206   AC_cfg->GetNextConfigLine();
207
208   while ((token = AC_cfg->GetValue()) != "/METRICS") {
209     *AC_cfg >> parameter;
210     if (parameter == "AC_WINGAREA") {
211       *AC_cfg >> WingArea;
212       if (debug_lvl > 0) cout << "    WingArea: " << WingArea  << endl;
213     } else if (parameter == "AC_WINGSPAN") {
214       *AC_cfg >> WingSpan;
215       if (debug_lvl > 0) cout << "    WingSpan: " << WingSpan  << endl;
216     } else if (parameter == "AC_WINGINCIDENCE") {
217       *AC_cfg >> WingIncidence;
218       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
219     } else if (parameter == "AC_CHORD") {
220       *AC_cfg >> cbar;
221       if (debug_lvl > 0) cout << "    Chord: " << cbar << endl;
222     } else if (parameter == "AC_HTAILAREA") {
223       *AC_cfg >> HTailArea;
224       if (debug_lvl > 0) cout << "    H. Tail Area: " << HTailArea << endl;
225     } else if (parameter == "AC_HTAILARM") {
226       *AC_cfg >> HTailArm;
227       if (debug_lvl > 0) cout << "    H. Tail Arm: " << HTailArm << endl;
228     } else if (parameter == "AC_VTAILAREA") {
229       *AC_cfg >> VTailArea;
230       if (debug_lvl > 0) cout << "    V. Tail Area: " << VTailArea << endl;
231     } else if (parameter == "AC_VTAILARM") {
232       *AC_cfg >> VTailArm;
233       if (debug_lvl > 0) cout << "    V. Tail Arm: " << VTailArm << endl;
234     } else if (parameter == "AC_IXX") {
235       *AC_cfg >> bixx;
236       if (debug_lvl > 0) cout << "    baseIxx: " << bixx << endl;
237       MassBalance->SetBaseIxx(bixx);
238     } else if (parameter == "AC_IYY") {
239       *AC_cfg >> biyy;
240       if (debug_lvl > 0) cout << "    baseIyy: " << biyy << endl;
241       MassBalance->SetBaseIyy(biyy);
242     } else if (parameter == "AC_IZZ") {
243       *AC_cfg >> bizz;
244       if (debug_lvl > 0) cout << "    baseIzz: " << bizz << endl;
245       MassBalance->SetBaseIzz(bizz);
246     } else if (parameter == "AC_IXZ") {
247       *AC_cfg >> bixz;
248       if (debug_lvl > 0) cout << "    baseIxz: " << bixz  << endl;
249       MassBalance->SetBaseIxz(bixz);
250     } else if (parameter == "AC_IYZ") {
251       *AC_cfg >> biyz;
252       if (debug_lvl > 0) cout << "    baseIyz: " << biyz  << endl;
253       MassBalance->SetBaseIyz(biyz);
254     } else if (parameter == "AC_EMPTYWT") {
255       *AC_cfg >> EW;
256       MassBalance->SetEmptyWeight(EW);
257       if (debug_lvl > 0) cout << "    EmptyWeight: " << EW  << endl;
258     } else if (parameter == "AC_CGLOC") {
259       *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
260       MassBalance->SetBaseCG(vbaseXYZcg);
261       if (debug_lvl > 0) cout << "    CG (x, y, z): " << vbaseXYZcg << endl;
262     } else if (parameter == "AC_EYEPTLOC") {
263       *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
264       if (debug_lvl > 0) cout << "    Eyepoint (x, y, z): " << vXYZep << endl;
265     } else if (parameter == "AC_AERORP") {
266       *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
267       if (debug_lvl > 0) cout << "    Ref Pt (x, y, z): " << vXYZrp << endl;
268     } else if (parameter == "AC_ALPHALIMITS") {
269       *AC_cfg >> alphaclmin >> alphaclmax;
270       if (debug_lvl > 0) cout << "    Maximum Alpha: " << alphaclmax
271              << "    Minimum Alpha: " << alphaclmin
272              << endl;
273     }
274   }
275   
276   // calculate some derived parameters
277   if (cbar != 0.0) {
278     lbarh = HTailArm/cbar;
279     lbarv = VTailArm/cbar;
280     if (WingArea != 0.0) {
281       vbarh = HTailArm*HTailArea / (cbar*WingArea);
282       vbarv = VTailArm*VTailArea / (cbar*WingArea);
283     }
284   }     
285
286 }
287
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289
290 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg)
291 {
292   if (!Propulsion->Load(AC_cfg)) {
293     cerr << "Propulsion not successfully loaded" << endl;
294   }
295 }
296
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298
299 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg)
300 {
301   if (!FCS->Load(AC_cfg)) {
302     cerr << "Flight Controls not successfully loaded" << endl;
303   }
304 }
305
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307
308 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg)
309 {
310   if (!Aerodynamics->Load(AC_cfg)) {
311     cerr << "Aerodynamics not successfully loaded" << endl;
312   }
313 }
314
315 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316
317 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg)
318 {
319   if (!GroundReactions->Load(AC_cfg)) {
320     cerr << "Ground Reactions not successfully loaded" << endl;
321   }
322 }
323
324 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325
326 void FGAircraft::ReadOutput(FGConfigFile* AC_cfg)
327 {
328   string token, parameter;
329   int OutRate = 0;
330   int subsystems = 0;
331
332   token = AC_cfg->GetValue("NAME");
333   Output->SetFilename(token);
334   token = AC_cfg->GetValue("TYPE");
335   Output->SetType(token);
336   AC_cfg->GetNextConfigLine();
337
338   while ((token = AC_cfg->GetValue()) != "/OUTPUT") {
339     *AC_cfg >> parameter;
340     if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
341     if (parameter == "SIMULATION") {
342       *AC_cfg >> parameter;
343       if (parameter == "ON") subsystems += ssSimulation;
344     }
345     if (parameter == "AEROSURFACES") {
346       *AC_cfg >> parameter;
347       if (parameter == "ON") subsystems += ssAerosurfaces;
348     }
349     if (parameter == "RATES") {
350       *AC_cfg >> parameter;
351       if (parameter == "ON") subsystems += ssRates;
352     }
353     if (parameter == "VELOCITIES") {
354       *AC_cfg >> parameter;
355       if (parameter == "ON") subsystems += ssVelocities;
356     }
357     if (parameter == "FORCES") {
358       *AC_cfg >> parameter;
359       if (parameter == "ON") subsystems += ssForces;
360     }
361     if (parameter == "MOMENTS") {
362       *AC_cfg >> parameter;
363       if (parameter == "ON") subsystems += ssMoments;
364     }
365     if (parameter == "ATMOSPHERE") {
366       *AC_cfg >> parameter;
367       if (parameter == "ON") subsystems += ssAtmosphere;
368     }
369     if (parameter == "MASSPROPS") {
370       *AC_cfg >> parameter;
371       if (parameter == "ON") subsystems += ssMassProps;
372     }
373     if (parameter == "POSITION") {
374       *AC_cfg >> parameter;
375       if (parameter == "ON") subsystems += ssPosition;
376     }
377     if (parameter == "COEFFICIENTS") {
378       *AC_cfg >> parameter;
379       if (parameter == "ON") subsystems += ssCoefficients;
380     }
381     if (parameter == "GROUND_REACTIONS") {
382       *AC_cfg >> parameter;
383       if (parameter == "ON") subsystems += ssGroundReactions;
384     }
385     if (parameter == "FCS") {
386       *AC_cfg >> parameter;
387       if (parameter == "ON") subsystems += ssFCS;
388     }
389     if (parameter == "PROPULSION") {
390       *AC_cfg >> parameter;
391       if (parameter == "ON") subsystems += ssPropulsion;
392     }
393   }
394
395   Output->SetSubsystems(subsystems);
396
397   OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
398   Output->SetRate( (int)(0.5 + 1.0/(State->Getdt()*OutRate)) );
399 }
400
401 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402
403 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg)
404 {
405   string token = AC_cfg->GetValue();
406   string scratch;
407   AircraftName = AC_cfg->GetValue("NAME");
408   if (debug_lvl > 0) cout << underon << "Reading Aircraft Configuration File"
409             << underoff << ": " << highint << AircraftName << normint << endl;
410   scratch = AC_cfg->GetValue("VERSION").c_str();
411
412   CFGVersion = AC_cfg->GetValue("VERSION");
413
414   if (debug_lvl > 0)
415     cout << "                            Version: " << highint << CFGVersion
416                                                              << normint << endl;
417   if (CFGVersion != NEEDED_CFG_VERSION) {
418     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
419             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
420     cerr << "Current version needed is: " << NEEDED_CFG_VERSION << endl;
421     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
422   }
423 }
424
425 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426
427 void FGAircraft::Debug(void)
428 {
429     //TODO: Add your source code here
430 }
431