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.
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
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
50 [2] D. M. Henderson, "Euler Angles, Quaternions, and Transformation Matrices",
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
59 The aerodynamic coefficients used in this model are:
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
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
78 Cyb - Side force due to sideslip
79 Cyr - Side force due to yaw rate
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)
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
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
100 ********************************************************************************
102 *******************************************************************************/
104 #include <sys/stat.h>
105 #include <sys/types.h>
108 # ifndef __BORLANDC__
109 # include <Include/compiler.h>
111 # ifdef FG_HAVE_STD_INCLUDES
120 #include "FGAircraft.h"
121 #include "FGTranslation.h"
122 #include "FGRotation.h"
123 #include "FGAtmosphere.h"
125 #include "FGFDMExec.h"
127 #include "FGPosition.h"
128 #include "FGAuxiliary.h"
129 #include "FGOutput.h"
131 /*******************************************************************************
132 ************************************ CODE **************************************
133 *******************************************************************************/
135 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex)
141 for (i=0;i<6;i++) coeff_ctr[i] = 0;
145 FGAircraft::~FGAircraft(void)
149 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname)
156 string holding_string;
158 ifstream coeffInFile;
161 string axis_descript;
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);
169 numTanks = numEngines = 0;
170 numSelectedOxiTanks = numSelectedFuelTanks = 0;
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) {
178 if (holding_string.compare("//",0,2) != 0) {
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()) {
225 numSelectedFuelTanks++;
226 cout << "Reading in Fuel Tank #" << numSelectedFuelTanks << " parameters ..." << endl;
228 case FGTank::ttOXIDIZER:
229 numSelectedOxiTanks++;
230 cout << "Reading in Oxidizer Tank #" << numSelectedOxiTanks << " parameters ..." << endl;
235 } else if (holding_string == "AC_GEAR") {
237 lGear.push_back(new FGLGear(aircraftfile));
239 } else if (holding_string == "AC_ENGINE") {
242 cout << "Reading in " << tag << " Engine parameters ..." << endl;
243 Engine[numEngines] = new FGEngine(FDMExec, engine_path, tag, numEngines);
246 } else if (holding_string == "}") {
248 } else if (holding_string == "{") {
250 } else if (holding_string == "LIFT") {
252 axis_descript = " Lift Coefficients ...";
255 } else if (holding_string == "DRAG") {
257 axis_descript = " Drag Coefficients ...";
260 } else if (holding_string == "SIDE") {
262 axis_descript = " Side Coefficients ...";
265 } else if (holding_string == "ROLL") {
267 axis_descript = " Roll Coefficients ...";
270 } else if (holding_string == "PITCH") {
272 axis_descript = " Pitch Coefficients ...";
275 } else if (holding_string == "YAW") {
277 axis_descript = " Yaw Coefficients ...";
283 cout << axis_descript << endl;
285 gpos = aircraftfile.tellg();
287 if ( !(tag == "}") ) {
288 while ( !(tag == "}") ) {
289 aircraftfile.seekg(gpos);
290 Coeff[axis][coeff_ctr[axis]] = new FGCoefficient(FDMExec, aircraftfile);
293 gpos = aircraftfile.tellg();
297 cout << " None found ..." << endl;
303 aircraftfile.getline(scratch, 127);
306 cout << "End of Configuration File Parsing." << endl;
312 bool FGAircraft::Run(void)
314 if (!FGModel::Run()) { // if false then execute this Run()
317 for (int i = 0; i < 3; i++) Forces[i] = Moments[i] = 0.0;
321 FMProp(); FMAero(); FMGear(); FMMass();
324 } else { // skip Run() execution this time
330 void FGAircraft::MassChange()
332 float Xt, Xw, Yt, Yw, Zt, Zw, Tw;
333 float IXXt, IYYt, IZZt, IXZt;
336 // UPDATE TANK CONTENTS
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.
345 float Oshortage, Fshortage;
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:
353 switch(Tank[t]->GetType()) {
355 if (Tank[t]->GetSelected()) {
356 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
357 numSelectedFuelTanks)*(dt*rate) + Fshortage);
360 case FGTank::ttOXIDIZER:
361 if (Tank[t]->GetSelected()) {
362 Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
363 numSelectedOxiTanks)*(dt*rate) + Oshortage);
369 case FGEngine::etPiston:
370 case FGEngine::etTurboJet:
371 case FGEngine::etTurboProp:
373 if (Tank[t]->GetSelected()) {
374 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
375 numSelectedFuelTanks)*(dt*rate) + Fshortage);
380 if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
381 else Engine[e]->SetStarved(false);
384 Weight = EmptyWeight;
385 for (t=0; t<numTanks; t++)
386 Weight += Tank[t]->GetContents();
388 Mass = Weight / GRAVITY;
390 // Calculate new CG here.
392 Xt = Yt = Zt = Tw = 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();
399 Tw += Tank[t]->GetContents();
402 Xcg = (Xt + EmptyWeight*baseXcg) / (Tw + EmptyWeight);
403 Ycg = (Yt + EmptyWeight*baseYcg) / (Tw + EmptyWeight);
404 Zcg = (Zt + EmptyWeight*baseZcg) / (Tw + EmptyWeight);
406 // Calculate new moments of inertia here
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;
416 Ixx = baseIxx + IXXt;
417 Iyy = baseIyy + IYYt;
418 Izz = baseIzz + IZZt;
419 Ixz = baseIxz + IXZt;
424 void FGAircraft::FMAero(void)
427 float Fxaero,Fyaero,Fzaero;
428 float dxcg,dycg,dzcg;
430 F[0] = F[1] = F[2] = 0.0;
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();
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);
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);
454 dxcg = (Xcg - Xrp)/12; //cg and rp values are in inches
455 dycg = (Ycg - Yrp)/12;
456 dzcg = (Zcg - Zrp)/12;
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
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();
470 void FGAircraft::FMGear(void)
475 for (int i=0;i<lGear.size();i++) {
482 void FGAircraft::FMMass(void)
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;
490 void FGAircraft::FMProp(void)
492 for (int i=0;i<numEngines;i++) {
493 Forces[0] += Engine[i]->CalcThrust();
497 void FGAircraft::GetState(void)
501 alpha = Translation->Getalpha();
502 beta = Translation->Getbeta();
503 phi = Rotation->Getphi();
504 tht = Rotation->Gettht();
505 psi = Rotation->Getpsi();
509 void FGAircraft::PutState(void)