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;
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") {
194 cout << "Aircraft Ixx: " << Ixx << endl;
195 } else if (holding_string == "AC_IYY") {
197 cout << "Aircraft Iyy: " << Iyy << endl;
198 } else if (holding_string == "AC_IZZ") {
200 cout << "Aircraft Izz: " << Izz << endl;
201 } else if (holding_string == "AC_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()) {
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();
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 // UPDATE TANK CONTENTS
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.
331 float Oshortage, Fshortage;
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:
339 switch(Tank[t]->GetType()) {
341 if (Tank[t]->GetSelected()) {
342 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
343 numSelectedFuelTanks)*(dt*rate) + Fshortage);
346 case FGTank::ttOXIDIZER:
347 if (Tank[t]->GetSelected()) {
348 Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
349 numSelectedOxiTanks)*(dt*rate) + Oshortage);
355 case FGEngine::etPiston:
356 case FGEngine::etTurboJet:
357 case FGEngine::etTurboProp:
359 if (Tank[t]->GetSelected()) {
360 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
361 numSelectedFuelTanks)*(dt*rate) + Fshortage);
366 if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
367 else Engine[e]->SetStarved(false);
370 Weight = EmptyWeight;
371 for (int t=0; t<numTanks; t++)
372 Weight += Tank[t]->GetContents();
374 Mass = Weight / GRAVITY;
378 void FGAircraft::FAero(void)
382 F[0] = F[1] = F[2] = 0.0;
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();
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);
399 void FGAircraft::FGear(void)
407 void FGAircraft::FMass(void)
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;
415 void FGAircraft::FProp(void)
417 for (int i=0;i<numEngines;i++) {
418 Forces[0] += Engine[i]->CalcThrust();
423 void FGAircraft::MAero(void)
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();
435 void FGAircraft::MGear(void)
443 void FGAircraft::MMass(void)
448 void FGAircraft::MProp(void)
453 void FGAircraft::GetState(void)
457 alpha = Translation->Getalpha();
458 beta = Translation->Getbeta();
459 phi = Rotation->Getphi();
460 tht = Rotation->Gettht();
461 psi = Rotation->Getpsi();
465 void FGAircraft::PutState(void)