]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAircraft.cpp
Various SGI portability tweaks.
[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. LoadAircraft() is supplied with a
32 name of a valid, registered aircraft, and the data file is parsed.
33
34 HISTORY
35 --------------------------------------------------------------------------------
36 12/12/98   JSB   Created
37 04/03/99   JSB   Changed Aero() method to correct body axis force calculation
38                  from wind vector. Fix provided by Tony Peden.
39 05/03/99   JSB   Changed (for the better?) the way configurations are read in.
40
41 ********************************************************************************
42 COMMENTS, REFERENCES,  and NOTES
43 ********************************************************************************
44 [1] Cooke, Zyda, Pratt, and McGhee, "NPSNET: Flight Simulation Dynamic Modeling
45       Using Quaternions", Presence, Vol. 1, No. 4, pp. 404-420  Naval Postgraduate
46       School, January 1994
47 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
48       JSC 12960, July 1977
49 [3] Richard E. McFarland, "A Standard Kinematic Model for Flight Simulation at
50       NASA-Ames", NASA CR-2497, January 1975
51 [4] Barnes W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics",
52       Wiley & Sons, 1979 ISBN 0-471-03032-5
53 [5] Bernard Etkin, "Dynamics of Flight, Stability and Control", Wiley & Sons,
54       1982 ISBN 0-471-08936-2
55
56 The aerodynamic coefficients used in this model are:
57
58 Longitudinal
59   CL0 - Reference lift at zero alpha
60   CD0 - Reference drag at zero alpha
61   CDM - Drag due to Mach
62   CLa - Lift curve slope (w.r.t. alpha)
63   CDa - Drag curve slope (w.r.t. alpha)
64   CLq - Lift due to pitch rate
65   CLM - Lift due to Mach
66   CLadt - Lift due to alpha rate
67
68   Cmadt - Pitching Moment due to alpha rate
69   Cm0 - Reference Pitching moment at zero alpha
70   Cma - Pitching moment slope (w.r.t. alpha)
71   Cmq - Pitch damping (pitch moment due to pitch rate)
72   CmM - Pitch Moment due to Mach
73
74 Lateral
75   Cyb - Side force due to sideslip
76   Cyr - Side force due to yaw rate
77
78   Clb - Dihedral effect (roll moment due to sideslip)
79   Clp - Roll damping (roll moment due to roll rate)
80   Clr - Roll moment due to yaw rate
81   Cnb - Weathercocking stability (yaw moment due to sideslip)
82   Cnp - Rudder adverse yaw (yaw moment due to roll rate)
83   Cnr - Yaw damping (yaw moment due to yaw rate)
84
85 Control
86   CLDe - Lift due to elevator
87   CDDe - Drag due to elevator
88   CyDr - Side force due to rudder
89   CyDa - Side force due to aileron
90
91   CmDe - Pitch moment due to elevator
92   ClDa - Roll moment due to aileron
93   ClDr - Roll moment due to rudder
94   CnDr - Yaw moment due to rudder
95   CnDa - Yaw moment due to aileron
96
97 ********************************************************************************
98 INCLUDES
99 *******************************************************************************/
100
101 #include <sys/stat.h>
102 #include <sys/types.h>
103
104 #ifdef FGFS
105 # ifndef __BORLANDC__
106 #  include <Include/compiler.h>
107 # endif
108 #  ifdef FG_HAVE_STD_INCLUDES
109 #    include <cmath>
110 #  else
111 #    include <math.h>
112 #  endif
113 #else
114 #  include <cmath>
115 #endif
116
117 #include "FGAircraft.h"
118 #include "FGTranslation.h"
119 #include "FGRotation.h"
120 #include "FGAtmosphere.h"
121 #include "FGState.h"
122 #include "FGFDMExec.h"
123 #include "FGFCS.h"
124 #include "FGPosition.h"
125 #include "FGAuxiliary.h"
126 #include "FGOutput.h"
127
128 /*******************************************************************************
129 ************************************ CODE **************************************
130 *******************************************************************************/
131
132 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex)
133 {
134   int i;
135
136   Name = "FGAircraft";
137
138   for (i=0;i<6;i++) coeff_ctr[i] = 0;
139 }
140
141
142 FGAircraft::~FGAircraft(void)
143 {
144 }
145
146 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname)
147 {
148   string path;
149   string fullpath;
150   string filename;
151   string aircraftDef;
152   string tag;
153   string holding_string;
154   char scratch[128];
155   ifstream coeffInFile;
156   streampos gpos;
157   int axis;
158   string axis_descript;
159
160         axis = -1;
161   aircraftDef = aircraft_path + "/" + fname + "/" + fname + ".cfg";
162   ifstream aircraftfile(aircraftDef.c_str());
163   cout << "Reading Aircraft Configuration File: " << aircraftDef << endl;
164
165   numTanks = numEngines = 0;
166   numSelectedOxiTanks = numSelectedFuelTanks = 0;
167
168   while (!aircraftfile.fail()) {
169         holding_string.erase();
170     aircraftfile >> holding_string;
171 #if defined(__BORLANDC__) || defined(FG_HAVE_NATIVE_SGI_COMPILERS)
172     if (holding_string.compare(0, 2, "//") != 0) {
173 #else
174     if (holding_string.compare("//",0,2) != 0) {
175 #endif
176       if (holding_string == "AIRCRAFT") {
177         cout << "Reading in Aircraft parameters ..." << endl;
178       } else if (holding_string == "AERODYNAMICS") {
179         cout << "Reading in Aerodynamic parameters ..." << endl;
180                         } else if (holding_string == "AC_NAME") {
181                     aircraftfile >> AircraftName;   // String with no embedded spaces
182                     cout << "Aircraft Name: " << AircraftName << endl;
183                         } else if (holding_string == "AC_WINGAREA") {
184                                 aircraftfile >> WingArea;
185                                 cout << "Aircraft Wing Area: " << WingArea << endl;
186                         } else if (holding_string == "AC_WINGSPAN") {
187                                 aircraftfile >> WingSpan;
188                                 cout << "Aircraft WingSpan: " << WingSpan << endl;
189                         } else if (holding_string == "AC_CHORD") {
190                                 aircraftfile >> cbar;
191                                 cout << "Aircraft Chord: " << cbar << endl;
192                         } else if (holding_string == "AC_IXX") {
193                                 aircraftfile >> baseIxx;
194                                 cout << "Aircraft Base Ixx: " << baseIxx << endl;
195                         } else if (holding_string == "AC_IYY") {
196                                 aircraftfile >> baseIyy;
197                                 cout << "Aircraft Base Iyy: " << baseIyy << endl;
198                         } else if (holding_string == "AC_IZZ") {
199                                 aircraftfile >> baseIzz;
200                                 cout << "Aircraft Base Izz: " << baseIzz << endl;
201                         } else if (holding_string == "AC_IXZ") {
202                                 aircraftfile >> baseIxz;
203                                 cout << "Aircraft Base Ixz: " << baseIxz << endl;
204                         } else if (holding_string == "AC_EMPTYWT") {
205                                 aircraftfile >> EmptyWeight;
206                                 EmptyMass = EmptyWeight / GRAVITY;
207                                 cout << "Aircraft Empty Weight: " << EmptyWeight << endl;
208                         } else if (holding_string == "AC_CGLOC") {
209                                 aircraftfile >> baseXcg >> baseYcg >> baseZcg;
210                                 cout << "Aircraft Base C.G.: " << baseXcg << " " << baseYcg << " " << baseZcg << endl;
211                         } else if (holding_string == "AC_EYEPTLOC") {
212                                 aircraftfile >> Xep >> Yep >> Zep;
213                                 cout << "Pilot Eyepoint: " << Xep << " " << Yep << " " << Zep << endl;
214                         } else if (holding_string == "AC_TANK") {
215         Tank[numTanks] = new FGTank(aircraftfile);
216         switch(Tank[numTanks]->GetType()) {
217         case FGTank::ttFUEL:
218           numSelectedFuelTanks++;
219                                         cout << "Reading in Fuel Tank #" << numSelectedFuelTanks << " parameters ..." << endl;
220           break;
221         case FGTank::ttOXIDIZER:
222           numSelectedOxiTanks++;
223                                         cout << "Reading in Oxidizer Tank #" << numSelectedOxiTanks << " parameters ..." << endl;
224           break;
225         }
226         numTanks++;
227
228                         } else if (holding_string == "AC_ENGINE") {
229
230         aircraftfile >> tag;
231                                 cout << "Reading in " << tag << " Engine parameters ..." << endl;
232         Engine[numEngines] = new FGEngine(FDMExec, engine_path, tag, numEngines);
233         numEngines++;
234
235                         } else if (holding_string == "}") {
236
237                         } else if (holding_string == "{") {
238
239                         } else if (holding_string == "LIFT") {
240
241                                 axis_descript = "   Lift Coefficients ...";
242                                 axis = LiftCoeff;
243                                 
244                         } else if (holding_string == "DRAG") {
245
246                                 axis_descript = "   Drag Coefficients ...";
247                                 axis = DragCoeff;
248
249                         } else if (holding_string == "SIDE") {
250
251                                 axis_descript = "   Side Coefficients ...";
252                                 axis = SideCoeff;
253
254                         } else if (holding_string == "ROLL") {
255
256                                 axis_descript = "   Roll Coefficients ...";
257                                 axis = RollCoeff;
258
259                         } else if (holding_string == "PITCH") {
260
261                                 axis_descript = "   Pitch Coefficients ...";
262                                 axis = PitchCoeff;
263
264                         } else if (holding_string == "YAW") {
265
266                                 axis_descript = "   Yaw Coefficients ...";
267                                 axis = YawCoeff;
268
269                         }
270
271                         if (axis >= 0) {
272                                 cout << axis_descript << endl;
273             aircraftfile >> tag;
274           gpos = aircraftfile.tellg();
275                                 aircraftfile >> tag;
276                                 if ( !(tag == "}") ) {
277                                         while ( !(tag == "}") ) {
278                                                 aircraftfile.seekg(gpos);
279                     Coeff[axis][coeff_ctr[axis]] = new FGCoefficient(FDMExec, aircraftfile);
280                           coeff_ctr[axis]++;
281                         aircraftfile >> tag;
282                         gpos = aircraftfile.tellg();
283                                                 aircraftfile >> tag;
284                     }
285                 } else {
286                         cout << "      None found ..." << endl;
287                 }
288             }
289             axis = -1;
290                         
291     } else {
292         aircraftfile.getline(scratch, 127);
293     }
294   }
295         cout << "End of Configuration File Parsing." << endl;
296
297   return true;
298 }
299
300
301 bool FGAircraft::Run(void)
302 {
303   if (!FGModel::Run()) {                 // if false then execute this Run()
304     GetState();
305
306     for (int i = 0; i < 3; i++)  Forces[i] = Moments[i] = 0.0;
307
308     MassChange();
309
310     FProp(); FAero(); FGear(); FMass();
311     MProp(); MAero(); MGear(); MMass();
312
313     PutState();
314   } else {                               // skip Run() execution this time
315   }
316   return false;
317 }
318
319
320 void FGAircraft::MassChange()
321 {
322   float Xt, Xw, Yt, Yw, Zt, Zw, Tw;
323   float IXXt, IYYt, IZZt, IXZt;
324   int t;
325
326   // UPDATE TANK CONTENTS
327   //
328   // For each engine, cycle through the tanks and draw an equal amount of
329   // fuel (or oxidizer) from each active tank. The needed amount of fuel is
330   // determined by the engine in the FGEngine class. If more fuel is needed
331   // than is available in the tank, then that amount is considered a shortage,
332   // and will be drawn from the next tank. If the engine cannot be fed what it
333   // needs, it will be considered to be starved, and will shut down.
334
335   float Oshortage, Fshortage;
336
337   for (int e=0; e<numEngines; e++) {
338     Fshortage = Oshortage = 0.0;
339     for (t=0; t<numTanks; t++) {
340       switch(Engine[e]->GetType()) {
341       case FGEngine::etRocket:
342
343         switch(Tank[t]->GetType()) {
344         case FGTank::ttFUEL:
345           if (Tank[t]->GetSelected()) {
346             Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
347                                    numSelectedFuelTanks)*(dt*rate) + Fshortage);
348           }
349           break;
350         case FGTank::ttOXIDIZER:
351           if (Tank[t]->GetSelected()) {
352             Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
353                                     numSelectedOxiTanks)*(dt*rate) + Oshortage);
354           }
355           break;
356         }
357         break;
358
359       case FGEngine::etPiston:
360       case FGEngine::etTurboJet:
361       case FGEngine::etTurboProp:
362
363         if (Tank[t]->GetSelected()) {
364           Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
365                                    numSelectedFuelTanks)*(dt*rate) + Fshortage);
366         }
367         break;
368       }
369     }
370     if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
371     else Engine[e]->SetStarved(false);
372   }
373
374   Weight = EmptyWeight;
375   for (t=0; t<numTanks; t++)
376     Weight += Tank[t]->GetContents();
377
378   Mass = Weight / GRAVITY;
379
380   // Calculate new CG here.
381
382   Xt = Yt = Zt = 0;
383   Xw = Yw = Zw = 0;
384   for (t=0; t<numTanks; t++) {
385     Xt += Tank[t]->GetX()*Tank[t]->GetContents();
386     Yt += Tank[t]->GetY()*Tank[t]->GetContents();
387     Zt += Tank[t]->GetZ()*Tank[t]->GetContents();
388
389     Tw += Tank[t]->GetContents();
390   }
391
392   Xcg = (Xt + EmptyWeight*baseXcg) / (Tw + EmptyWeight);
393   Ycg = (Yt + EmptyWeight*baseYcg) / (Tw + EmptyWeight);
394   Zcg = (Zt + EmptyWeight*baseZcg) / (Tw + EmptyWeight);
395
396   // Calculate new moments of inertia here
397
398   IXXt = IYYt = IZZt = IXZt = 0.0;
399   for (t=0; t<numTanks; t++) {
400     IXXt += ((Tank[t]->GetX()-Xcg)/12.0)*((Tank[t]->GetX() - Xcg)/12.0)*Tank[t]->GetContents()/GRAVITY;
401     IYYt += ((Tank[t]->GetY()-Ycg)/12.0)*((Tank[t]->GetY() - Ycg)/12.0)*Tank[t]->GetContents()/GRAVITY;
402     IZZt += ((Tank[t]->GetZ()-Zcg)/12.0)*((Tank[t]->GetZ() - Zcg)/12.0)*Tank[t]->GetContents()/GRAVITY;
403     IXZt += ((Tank[t]->GetX()-Xcg)/12.0)*((Tank[t]->GetZ() - Zcg)/12.0)*Tank[t]->GetContents()/GRAVITY;
404   }
405
406   Ixx = baseIxx + IXXt;
407   Iyy = baseIyy + IYYt;
408   Izz = baseIzz + IZZt;
409   Ixz = baseIxz + IXZt;
410
411 }
412
413
414 void FGAircraft::FAero(void)
415 {
416   float F[3];
417
418   F[0] = F[1] = F[2] = 0.0;
419
420   for (int axis_ctr = 0; axis_ctr < 3; axis_ctr++)
421     for (int ctr=0; ctr < coeff_ctr[axis_ctr]; ctr++)
422       F[axis_ctr] += Coeff[axis_ctr][ctr]->TotalValue();
423
424   Forces[0] += - F[DragCoeff]*cos(alpha)*cos(beta)
425                - F[SideCoeff]*cos(alpha)*sin(beta)
426                + F[LiftCoeff]*sin(alpha);
427   Forces[1] +=   F[DragCoeff]*sin(beta)
428                + F[SideCoeff]*cos(beta);
429   Forces[2] += - F[DragCoeff]*sin(alpha)*cos(beta)
430                - F[SideCoeff]*sin(alpha)*sin(beta)
431                - F[LiftCoeff]*cos(alpha);
432 }
433
434
435 void FGAircraft::FGear(void)
436 {
437   if (GearUp) {
438     // crash routine
439   } else {
440     
441   }
442 }
443
444
445 void FGAircraft::FMass(void)
446 {
447   Forces[0] += -GRAVITY*sin(tht) * Mass;
448   Forces[1] +=  GRAVITY*sin(phi)*cos(tht) * Mass;
449   Forces[2] +=  GRAVITY*cos(phi)*cos(tht) * Mass;
450 }
451
452
453 void FGAircraft::FProp(void)
454 {
455   for (int i=0;i<numEngines;i++) {
456     Forces[0] += Engine[i]->CalcThrust();
457   }
458 }
459
460
461 void FGAircraft::MAero(void)
462 {
463         int axis_ctr, ctr;
464         
465   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
466     for (ctr = 0; ctr < coeff_ctr[axis_ctr+3]; ctr++) {
467       Moments[axis_ctr] += Coeff[axis_ctr+3][ctr]->TotalValue();
468     }
469   }
470 }
471
472
473 void FGAircraft::MGear(void)
474 {
475   if (GearUp) {
476   } else {
477   }
478 }
479
480
481 void FGAircraft::MMass(void)
482 {
483 }
484
485
486 void FGAircraft::MProp(void)
487 {
488 }
489
490
491 void FGAircraft::GetState(void)
492 {
493   dt = State->Getdt();
494
495   alpha = Translation->Getalpha();
496   beta = Translation->Getbeta();
497   phi = Rotation->Getphi();
498   tht = Rotation->Gettht();
499   psi = Rotation->Getpsi();
500 }
501
502
503 void FGAircraft::PutState(void)
504 {
505 }
506