1 /*******************************************************************************
6 Purpose: Encapsulates an aircraft
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
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
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
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.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
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.
35 --------------------------------------------------------------------------------
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.
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
47 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
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
56 The aerodynamic coefficients used in this model are:
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
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
75 Cyb - Side force due to sideslip
76 Cyr - Side force due to yaw rate
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)
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
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
97 ********************************************************************************
99 *******************************************************************************/
101 #include <sys/stat.h>
102 #include <sys/types.h>
105 # ifndef __BORLANDC__
106 # include <Include/compiler.h>
108 # ifdef FG_HAVE_STD_INCLUDES
117 #include "FGAircraft.h"
118 #include "FGTranslation.h"
119 #include "FGRotation.h"
120 #include "FGAtmosphere.h"
122 #include "FGFDMExec.h"
124 #include "FGPosition.h"
125 #include "FGAuxiliary.h"
126 #include "FGOutput.h"
128 /*******************************************************************************
129 ************************************ CODE **************************************
130 *******************************************************************************/
132 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex)
138 for (i=0;i<6;i++) coeff_ctr[i] = 0;
142 FGAircraft::~FGAircraft(void)
146 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname)
153 string holding_string;
155 ifstream coeffInFile;
158 string axis_descript;
161 aircraftDef = aircraft_path + "/" + fname + "/" + fname + ".cfg";
162 ifstream aircraftfile(aircraftDef.c_str());
163 cout << "Reading Aircraft Configuration File: " << aircraftDef << endl;
165 numTanks = numEngines = 0;
166 numSelectedOxiTanks = numSelectedFuelTanks = 0;
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) {
174 if (holding_string.compare("//",0,2) != 0) {
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()) {
218 numSelectedFuelTanks++;
219 cout << "Reading in Fuel Tank #" << numSelectedFuelTanks << " parameters ..." << endl;
221 case FGTank::ttOXIDIZER:
222 numSelectedOxiTanks++;
223 cout << "Reading in Oxidizer Tank #" << numSelectedOxiTanks << " parameters ..." << endl;
228 } else if (holding_string == "AC_ENGINE") {
231 cout << "Reading in " << tag << " Engine parameters ..." << endl;
232 Engine[numEngines] = new FGEngine(FDMExec, engine_path, tag, numEngines);
235 } else if (holding_string == "}") {
237 } else if (holding_string == "{") {
239 } else if (holding_string == "LIFT") {
241 axis_descript = " Lift Coefficients ...";
244 } else if (holding_string == "DRAG") {
246 axis_descript = " Drag Coefficients ...";
249 } else if (holding_string == "SIDE") {
251 axis_descript = " Side Coefficients ...";
254 } else if (holding_string == "ROLL") {
256 axis_descript = " Roll Coefficients ...";
259 } else if (holding_string == "PITCH") {
261 axis_descript = " Pitch Coefficients ...";
264 } else if (holding_string == "YAW") {
266 axis_descript = " Yaw Coefficients ...";
272 cout << axis_descript << endl;
274 gpos = aircraftfile.tellg();
276 if ( !(tag == "}") ) {
277 while ( !(tag == "}") ) {
278 aircraftfile.seekg(gpos);
279 Coeff[axis][coeff_ctr[axis]] = new FGCoefficient(FDMExec, aircraftfile);
282 gpos = aircraftfile.tellg();
286 cout << " None found ..." << endl;
292 aircraftfile.getline(scratch, 127);
295 cout << "End of Configuration File Parsing." << endl;
301 bool FGAircraft::Run(void)
303 if (!FGModel::Run()) { // if false then execute this Run()
306 for (int i = 0; i < 3; i++) Forces[i] = Moments[i] = 0.0;
310 FProp(); FAero(); FGear(); FMass();
311 MProp(); MAero(); MGear(); MMass();
314 } else { // skip Run() execution this time
320 void FGAircraft::MassChange()
322 float Xt, Xw, Yt, Yw, Zt, Zw, Tw;
323 float IXXt, IYYt, IZZt, IXZt;
326 // UPDATE TANK CONTENTS
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.
335 float Oshortage, Fshortage;
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:
343 switch(Tank[t]->GetType()) {
345 if (Tank[t]->GetSelected()) {
346 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
347 numSelectedFuelTanks)*(dt*rate) + Fshortage);
350 case FGTank::ttOXIDIZER:
351 if (Tank[t]->GetSelected()) {
352 Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
353 numSelectedOxiTanks)*(dt*rate) + Oshortage);
359 case FGEngine::etPiston:
360 case FGEngine::etTurboJet:
361 case FGEngine::etTurboProp:
363 if (Tank[t]->GetSelected()) {
364 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
365 numSelectedFuelTanks)*(dt*rate) + Fshortage);
370 if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
371 else Engine[e]->SetStarved(false);
374 Weight = EmptyWeight;
375 for (t=0; t<numTanks; t++)
376 Weight += Tank[t]->GetContents();
378 Mass = Weight / GRAVITY;
380 // Calculate new CG here.
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();
389 Tw += Tank[t]->GetContents();
392 Xcg = (Xt + EmptyWeight*baseXcg) / (Tw + EmptyWeight);
393 Ycg = (Yt + EmptyWeight*baseYcg) / (Tw + EmptyWeight);
394 Zcg = (Zt + EmptyWeight*baseZcg) / (Tw + EmptyWeight);
396 // Calculate new moments of inertia here
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;
406 Ixx = baseIxx + IXXt;
407 Iyy = baseIyy + IYYt;
408 Izz = baseIzz + IZZt;
409 Ixz = baseIxz + IXZt;
414 void FGAircraft::FAero(void)
418 F[0] = F[1] = F[2] = 0.0;
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();
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);
435 void FGAircraft::FGear(void)
445 void FGAircraft::FMass(void)
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;
453 void FGAircraft::FProp(void)
455 for (int i=0;i<numEngines;i++) {
456 Forces[0] += Engine[i]->CalcThrust();
461 void FGAircraft::MAero(void)
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();
473 void FGAircraft::MGear(void)
481 void FGAircraft::MMass(void)
486 void FGAircraft::MProp(void)
491 void FGAircraft::GetState(void)
495 alpha = Translation->Getalpha();
496 beta = Translation->Getbeta();
497 phi = Rotation->Getphi();
498 tht = Rotation->Gettht();
499 psi = Rotation->Getpsi();
503 void FGAircraft::PutState(void)