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