]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBsim/FGAircraft.cpp
source tree reorganization prior to flightgear 0.7
[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 #ifdef __BORLANDC__
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 >> Ixx;
194                                 cout << "Aircraft Ixx: " << Ixx << endl;
195                         } else if (holding_string == "AC_IYY") {
196                                 aircraftfile >> Iyy;
197                                 cout << "Aircraft Iyy: " << Iyy << endl;
198                         } else if (holding_string == "AC_IZZ") {
199                                 aircraftfile >> Izz;
200                                 cout << "Aircraft Izz: " << Izz << endl;
201                         } else if (holding_string == "AC_IXZ") {
202                                 aircraftfile >> Ixz;
203                                 cout << "Aircraft Ixz: " << Ixz << 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 >> Xcg >> Ycg >> Zcg;
210                                 cout << "Aircraft C.G.: " << Xcg << " " << Ycg << " " << Zcg << 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   // UPDATE TANK CONTENTS
323   //
324   // For each engine, cycle through the tanks and draw an equal amount of
325   // fuel (or oxidizer) from each active tank. The needed amount of fuel is
326   // determined by the engine in the FGEngine class. If more fuel is needed
327   // than is available in the tank, then that amount is considered a shortage,
328   // and will be drawn from the next tank. If the engine cannot be fed what it
329   // needs, it will be considered to be starved, and will shut down.
330
331   float Oshortage, Fshortage;
332
333   for (int e=0; e<numEngines; e++) {
334     Fshortage = Oshortage = 0.0;
335     for (int t=0; t<numTanks; t++) {
336       switch(Engine[e]->GetType()) {
337       case FGEngine::etRocket:
338
339         switch(Tank[t]->GetType()) {
340         case FGTank::ttFUEL:
341           if (Tank[t]->GetSelected()) {
342             Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
343                                    numSelectedFuelTanks)*(dt*rate) + Fshortage);
344           }
345           break;
346         case FGTank::ttOXIDIZER:
347           if (Tank[t]->GetSelected()) {
348             Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
349                                     numSelectedOxiTanks)*(dt*rate) + Oshortage);
350           }
351           break;
352         }
353         break;
354
355       case FGEngine::etPiston:
356       case FGEngine::etTurboJet:
357       case FGEngine::etTurboProp:
358
359         if (Tank[t]->GetSelected()) {
360           Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
361                                    numSelectedFuelTanks)*(dt*rate) + Fshortage);
362         }
363         break;
364       }
365     }
366     if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
367     else Engine[e]->SetStarved(false);
368   }
369
370   Weight = EmptyWeight;
371   for (int t=0; t<numTanks; t++)
372     Weight += Tank[t]->GetContents();
373
374   Mass = Weight / GRAVITY;
375 }
376
377
378 void FGAircraft::FAero(void)
379 {
380   float F[3];
381
382   F[0] = F[1] = F[2] = 0.0;
383
384   for (int axis_ctr = 0; axis_ctr < 3; axis_ctr++)
385     for (int ctr=0; ctr < coeff_ctr[axis_ctr]; ctr++)
386       F[axis_ctr] += Coeff[axis_ctr][ctr]->TotalValue();
387
388   Forces[0] += - F[DragCoeff]*cos(alpha)*cos(beta)
389                - F[SideCoeff]*cos(alpha)*sin(beta)
390                + F[LiftCoeff]*sin(alpha);
391   Forces[1] +=   F[DragCoeff]*sin(beta)
392                + F[SideCoeff]*cos(beta);
393   Forces[2] += - F[DragCoeff]*sin(alpha)*cos(beta)
394                - F[SideCoeff]*sin(alpha)*sin(beta)
395                - F[LiftCoeff]*cos(alpha);
396 }
397
398
399 void FGAircraft::FGear(void)
400 {
401   if (GearUp) {
402   } else {
403   }
404 }
405
406
407 void FGAircraft::FMass(void)
408 {
409   Forces[0] += -GRAVITY*sin(tht) * Mass;
410   Forces[1] +=  GRAVITY*sin(phi)*cos(tht) * Mass;
411   Forces[2] +=  GRAVITY*cos(phi)*cos(tht) * Mass;
412 }
413
414
415 void FGAircraft::FProp(void)
416 {
417   for (int i=0;i<numEngines;i++) {
418     Forces[0] += Engine[i]->CalcThrust();
419   }
420 }
421
422
423 void FGAircraft::MAero(void)
424 {
425         int axis_ctr, ctr;
426         
427   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
428     for (ctr = 0; ctr < coeff_ctr[axis_ctr+3]; ctr++) {
429       Moments[axis_ctr] += Coeff[axis_ctr+3][ctr]->TotalValue();
430     }
431   }
432 }
433
434
435 void FGAircraft::MGear(void)
436 {
437   if (GearUp) {
438   } else {
439   }
440 }
441
442
443 void FGAircraft::MMass(void)
444 {
445 }
446
447
448 void FGAircraft::MProp(void)
449 {
450 }
451
452
453 void FGAircraft::GetState(void)
454 {
455   dt = State->Getdt();
456
457   alpha = Translation->Getalpha();
458   beta = Translation->Getbeta();
459   phi = Rotation->Getphi();
460   tht = Rotation->Gettht();
461   psi = Rotation->Getpsi();
462 }
463
464
465 void FGAircraft::PutState(void)
466 {
467 }
468