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 <simgear/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 static const char *IdSrc = "$Header$";
132 static const char *IdHdr = ID_AIRCRAFT;
134 /*******************************************************************************
135 ************************************ CODE **************************************
136 *******************************************************************************/
138 FGAircraft::FGAircraft(FGFDMExec* fdmex) : FGModel(fdmex),
155 AxisIdx["PITCH"] = 4;
158 Coeff = new CoeffArray[6];
162 alphaclmin = alphaclmax = 0;
164 numTanks = numEngines = numSelectedFuelTanks = numSelectedOxiTanks = 0;
168 /******************************************************************************/
171 FGAircraft::~FGAircraft(void) {
174 if (Engine != NULL) {
175 for (i=0; i<numEngines; i++)
179 for (i=0; i<numTanks; i++)
182 for (i=0; i<6; i++) {
183 for (j=0; j<Coeff[i].size(); j++) {
190 /******************************************************************************/
192 bool FGAircraft::LoadAircraft(string aircraft_path, string engine_path, string fname) {
195 string aircraftCfgFileName;
198 AircraftPath = aircraft_path;
199 EnginePath = engine_path;
202 aircraftCfgFileName = AircraftPath + "/" + fname + "/" + fname + ".xml";
204 aircraftCfgFileName = AircraftPath + ";" + fname + ";" + fname + ".xml";
207 FGConfigFile AC_cfg(aircraftCfgFileName);
208 if (!AC_cfg.IsOpen()) return false;
210 ReadPrologue(&AC_cfg);
212 while ((AC_cfg.GetNextConfigLine() != "EOF") &&
213 (token = AC_cfg.GetValue()) != "/FDM_CONFIG") {
214 if (token == "METRICS") {
215 cout << " Reading Metrics" << endl;
216 ReadMetrics(&AC_cfg);
217 } else if (token == "AERODYNAMICS") {
218 cout << " Reading Aerodynamics" << endl;
219 ReadAerodynamics(&AC_cfg);
220 } else if (token == "UNDERCARRIAGE") {
221 cout << " Reading Landing Gear" << endl;
222 ReadUndercarriage(&AC_cfg);
223 } else if (token == "PROPULSION") {
224 cout << " Reading Propulsion" << endl;
225 ReadPropulsion(&AC_cfg);
226 } else if (token == "FLIGHT_CONTROL") {
227 cout << " Reading Flight Control" << endl;
228 ReadFlightControls(&AC_cfg);
229 } else if (token == "OUTPUT") {
237 /******************************************************************************/
239 bool FGAircraft::Run(void) {
240 if (!FGModel::Run()) { // if false then execute this Run()
243 for (int i = 1; i <= 3; i++) vForces(i) = vMoments(i) = 0.0;
252 if (fabs(Position->GetGamma()) < 1.57) {
253 nlf = vFs(eZ)/(Weight*cos(Position->GetGamma()));
256 } else { // skip Run() execution this time
263 /******************************************************************************/
265 void FGAircraft::MassChange() {
266 static FGColumnVector vXYZtank(3);
268 float IXXt, IYYt, IZZt, IXZt;
270 unsigned int axis_ctr;
272 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vXYZtank(axis_ctr) = 0.0;
274 // UPDATE TANK CONTENTS
276 // For each engine, cycle through the tanks and draw an equal amount of
277 // fuel (or oxidizer) from each active tank. The needed amount of fuel is
278 // determined by the engine in the FGEngine class. If more fuel is needed
279 // than is available in the tank, then that amount is considered a shortage,
280 // and will be drawn from the next tank. If the engine cannot be fed what it
281 // needs, it will be considered to be starved, and will shut down.
283 float Oshortage, Fshortage;
285 for (unsigned int e=0; e<numEngines; e++) {
286 Fshortage = Oshortage = 0.0;
287 for (t=0; t<numTanks; t++) {
288 switch(Engine[e]->GetType()) {
289 case FGEngine::etRocket:
291 switch(Tank[t]->GetType()) {
293 if (Tank[t]->GetSelected()) {
294 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
295 numSelectedFuelTanks)*(dt*rate) + Fshortage);
298 case FGTank::ttOXIDIZER:
299 if (Tank[t]->GetSelected()) {
300 Oshortage = Tank[t]->Reduce((Engine[e]->CalcOxidizerNeed()/
301 numSelectedOxiTanks)*(dt*rate) + Oshortage);
307 case FGEngine::etPiston:
308 case FGEngine::etTurboJet:
309 case FGEngine::etTurboProp:
311 if (Tank[t]->GetSelected()) {
312 Fshortage = Tank[t]->Reduce((Engine[e]->CalcFuelNeed()/
313 numSelectedFuelTanks)*(dt*rate) + Fshortage);
318 if ((Fshortage <= 0.0) || (Oshortage <= 0.0)) Engine[e]->SetStarved();
319 else Engine[e]->SetStarved(false);
322 Weight = EmptyWeight;
323 for (t=0; t<numTanks; t++)
324 Weight += Tank[t]->GetContents();
326 Mass = Weight / GRAVITY;
327 // Calculate new CG here.
330 for (t=0; t<numTanks; t++) {
331 vXYZtank(eX) += Tank[t]->GetX()*Tank[t]->GetContents();
332 vXYZtank(eY) += Tank[t]->GetY()*Tank[t]->GetContents();
333 vXYZtank(eZ) += Tank[t]->GetZ()*Tank[t]->GetContents();
335 Tw += Tank[t]->GetContents();
338 vXYZcg = (vXYZtank + EmptyWeight*vbaseXYZcg) / (Tw + EmptyWeight);
340 // Calculate new moments of inertia here
342 IXXt = IYYt = IZZt = IXZt = 0.0;
343 for (t=0; t<numTanks; t++) {
344 IXXt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetX() - vXYZcg(eX))/12.0)*Tank[t]->GetContents()/GRAVITY;
345 IYYt += ((Tank[t]->GetY()-vXYZcg(eY))/12.0)*((Tank[t]->GetY() - vXYZcg(eY))/12.0)*Tank[t]->GetContents()/GRAVITY;
346 IZZt += ((Tank[t]->GetZ()-vXYZcg(eZ))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
347 IXZt += ((Tank[t]->GetX()-vXYZcg(eX))/12.0)*((Tank[t]->GetZ() - vXYZcg(eZ))/12.0)*Tank[t]->GetContents()/GRAVITY;
350 Ixx = baseIxx + IXXt;
351 Iyy = baseIyy + IYYt;
352 Izz = baseIzz + IZZt;
353 Ixz = baseIxz + IXZt;
357 /******************************************************************************/
359 void FGAircraft::FMAero(void) {
360 static FGColumnVector vDXYZcg(3);
361 static FGColumnVector vAeroBodyForces(3);
362 unsigned int axis_ctr,ctr;
364 for (axis_ctr=1; axis_ctr<=3; axis_ctr++) vFs(axis_ctr) = 0.0;
366 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
367 for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
368 vFs(axis_ctr+1) += Coeff[axis_ctr][ctr]->TotalValue();
372 vAeroBodyForces = State->GetTs2b(alpha, beta)*vFs;
373 vForces += vAeroBodyForces;
375 // The d*cg distances below, given in inches, are the distances FROM the c.g.
376 // TO the reference point. Since the c.g. and ref point are given in inches in
377 // the structural system (X positive rearwards) and the body coordinate system
378 // is given with X positive out the nose, the dxcg and dzcg values are
379 // *rotated* 180 degrees about the Y axis.
381 vDXYZcg(eX) = -(vXYZrp(eX) - vXYZcg(eX))/12.0; //cg and rp values are in inches
382 vDXYZcg(eY) = (vXYZrp(eY) - vXYZcg(eY))/12.0;
383 vDXYZcg(eZ) = -(vXYZrp(eZ) - vXYZcg(eZ))/12.0;
385 vMoments(eL) += vAeroBodyForces(eZ)*vDXYZcg(eY) - vAeroBodyForces(eY)*vDXYZcg(eZ); // rolling moment
386 vMoments(eM) += vAeroBodyForces(eX)*vDXYZcg(eZ) - vAeroBodyForces(eZ)*vDXYZcg(eX); // pitching moment
387 vMoments(eN) += vAeroBodyForces(eY)*vDXYZcg(eX) - vAeroBodyForces(eX)*vDXYZcg(eY); // yawing moment
389 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
390 for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
391 vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->TotalValue();
396 /******************************************************************************/
398 void FGAircraft::FMGear(void) {
401 vector <FGLGear>::iterator iGear = lGear.begin();
402 while (iGear != lGear.end()) {
403 vForces += iGear->Force();
404 vMoments += iGear->Moment();
412 /******************************************************************************/
414 void FGAircraft::FMMass(void) {
415 vForces(eX) += -GRAVITY*sin(vEuler(eTht)) * Mass;
416 vForces(eY) += GRAVITY*sin(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
417 vForces(eZ) += GRAVITY*cos(vEuler(ePhi))*cos(vEuler(eTht)) * Mass;
420 /******************************************************************************/
422 void FGAircraft::FMProp(void) {
423 for (unsigned int i=0;i<numEngines;i++) {
425 // Changes required here for new engine placement parameters (i.e. location and direction)
427 vForces(eX) += Engine[i]->CalcThrust();
432 /******************************************************************************/
434 void FGAircraft::GetState(void) {
437 alpha = Translation->Getalpha();
438 beta = Translation->Getbeta();
439 vEuler = Rotation->GetEuler();
442 /******************************************************************************/
444 void FGAircraft::ReadMetrics(FGConfigFile* AC_cfg) {
448 AC_cfg->GetNextConfigLine();
450 while ((token = AC_cfg->GetValue()) != "/METRICS") {
451 *AC_cfg >> parameter;
452 if (parameter == "AC_WINGAREA") {
454 cout << " WingArea: " << WingArea << endl;
455 } else if (parameter == "AC_WINGSPAN") {
457 cout << " WingSpan: " << WingSpan << endl;
458 } else if (parameter == "AC_CHORD") {
460 cout << " Chord: " << cbar << endl;
461 } else if (parameter == "AC_IXX") {
463 cout << " baseIxx: " << baseIxx << endl;
464 } else if (parameter == "AC_IYY") {
466 cout << " baseIyy: " << baseIyy << endl;
467 } else if (parameter == "AC_IZZ") {
469 cout << " baseIzz: " << baseIzz << endl;
470 } else if (parameter == "AC_IXZ") {
472 cout << " baseIxz: " << baseIxz << endl;
473 } else if (parameter == "AC_EMPTYWT") {
474 *AC_cfg >> EmptyWeight;
475 cout << " EmptyWeight: " << EmptyWeight << endl;
476 } else if (parameter == "AC_CGLOC") {
477 *AC_cfg >> vbaseXYZcg(eX) >> vbaseXYZcg(eY) >> vbaseXYZcg(eZ);
478 cout << " CG (x, y, z): " << vbaseXYZcg << endl;
479 } else if (parameter == "AC_EYEPTLOC") {
480 *AC_cfg >> vXYZep(eX) >> vXYZep(eY) >> vXYZep(eZ);
481 cout << " Eyepoint (x, y, z): " << vXYZep << endl;
482 } else if (parameter == "AC_AERORP") {
483 *AC_cfg >> vXYZrp(eX) >> vXYZrp(eY) >> vXYZrp(eZ);
484 cout << " Ref Pt (x, y, z): " << vXYZrp << endl;
485 } else if (parameter == "AC_ALPHALIMITS") {
486 *AC_cfg >> alphaclmin >> alphaclmax;
487 cout << " Maximum Alpha: " << alphaclmax
488 << " Minimum Alpha: " << alphaclmin
494 /******************************************************************************/
496 void FGAircraft::ReadPropulsion(FGConfigFile* AC_cfg) {
501 AC_cfg->GetNextConfigLine();
503 while ((token = AC_cfg->GetValue()) != "/PROPULSION") {
504 *AC_cfg >> parameter;
506 if (parameter == "AC_ENGINE") {
508 *AC_cfg >> engine_name;
509 Engine[numEngines] = new FGEngine(FDMExec, EnginePath, engine_name, numEngines);
512 } else if (parameter == "AC_TANK") {
514 Tank[numTanks] = new FGTank(AC_cfg);
515 switch(Tank[numTanks]->GetType()) {
517 numSelectedFuelTanks++;
519 case FGTank::ttOXIDIZER:
520 numSelectedOxiTanks++;
528 /******************************************************************************/
530 void FGAircraft::ReadFlightControls(FGConfigFile* AC_cfg) {
533 FCS->LoadFCS(AC_cfg);
536 /******************************************************************************/
538 void FGAircraft::ReadAerodynamics(FGConfigFile* AC_cfg) {
541 AC_cfg->GetNextConfigLine();
543 while ((token = AC_cfg->GetValue()) != "/AERODYNAMICS") {
544 if (token == "AXIS") {
546 axis = AC_cfg->GetValue("NAME");
547 AC_cfg->GetNextConfigLine();
548 while ((token = AC_cfg->GetValue()) != "/AXIS") {
549 ca.push_back(new FGCoefficient(FDMExec, AC_cfg));
550 DisplayCoeffFactors(ca.back()->Getmultipliers());
552 Coeff[AxisIdx[axis]]=ca;
553 AC_cfg->GetNextConfigLine();
558 /******************************************************************************/
560 void FGAircraft::ReadUndercarriage(FGConfigFile* AC_cfg) {
563 AC_cfg->GetNextConfigLine();
565 while ((token = AC_cfg->GetValue()) != "/UNDERCARRIAGE") {
566 lGear.push_back(FGLGear(AC_cfg, FDMExec));
570 /******************************************************************************/
572 void FGAircraft::ReadOutput(FGConfigFile* AC_cfg) {
573 string token, parameter;
577 token = AC_cfg->GetValue("NAME");
578 Output->SetFilename(token);
579 token = AC_cfg->GetValue("TYPE");
580 Output->SetType(token);
581 AC_cfg->GetNextConfigLine();
583 while ((token = AC_cfg->GetValue()) != "/OUTPUT") {
584 *AC_cfg >> parameter;
585 if (parameter == "RATE_IN_HZ") *AC_cfg >> OutRate;
586 if (parameter == "SIMULATION") {
587 *AC_cfg >> parameter;
588 if (parameter == "ON") subsystems += ssSimulation;
590 if (parameter == "AEROSURFACES") {
591 *AC_cfg >> parameter;
592 if (parameter == "ON") subsystems += ssAerosurfaces;
594 if (parameter == "RATES") {
595 *AC_cfg >> parameter;
596 if (parameter == "ON") subsystems += ssRates;
598 if (parameter == "VELOCITIES") {
599 *AC_cfg >> parameter;
600 if (parameter == "ON") subsystems += ssVelocities;
602 if (parameter == "FORCES") {
603 *AC_cfg >> parameter;
604 if (parameter == "ON") subsystems += ssForces;
606 if (parameter == "MOMENTS") {
607 *AC_cfg >> parameter;
608 if (parameter == "ON") subsystems += ssMoments;
610 if (parameter == "ATMOSPHERE") {
611 *AC_cfg >> parameter;
612 if (parameter == "ON") subsystems += ssAtmosphere;
614 if (parameter == "MASSPROPS") {
615 *AC_cfg >> parameter;
616 if (parameter == "ON") subsystems += ssMassProps;
618 if (parameter == "POSITION") {
619 *AC_cfg >> parameter;
620 if (parameter == "ON") subsystems += ssPosition;
622 if (parameter == "COEFFICIENTS") {
623 *AC_cfg >> parameter;
624 if (parameter == "ON") subsystems += ssCoefficients;
626 if (parameter == "GROUND_REACTIONS") {
627 *AC_cfg >> parameter;
628 if (parameter == "ON") subsystems += ssGroundReactions;
632 Output->SetSubsystems(subsystems);
634 OutRate = OutRate>120?120:(OutRate<0?0:OutRate);
635 Output->SetRate( (int)(0.5 + 1.0/(State->Getdt()*OutRate)) );
638 /******************************************************************************/
640 void FGAircraft::ReadPrologue(FGConfigFile* AC_cfg) {
641 string token = AC_cfg->GetValue();
643 AircraftName = AC_cfg->GetValue("NAME");
644 cout << "Reading Aircraft Configuration File: " << AircraftName << endl;
645 scratch=AC_cfg->GetValue("VERSION").c_str();
647 CFGVersion = AC_cfg->GetValue("VERSION");
648 cout << " Version: " << CFGVersion << endl;
649 if (CFGVersion != NEEDED_CFG_VERSION) {
650 cout << endl << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
651 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
652 cout << "Current version needed is: " << NEEDED_CFG_VERSION << endl;
653 cout << " You have version: " << CFGVersion << endl << endl;
660 /******************************************************************************/
662 void FGAircraft::DisplayCoeffFactors(vector <eParam> multipliers) {
663 cout << " Non-Dimensionalized by: ";
665 for (unsigned int i=0; i<multipliers.size();i++)
666 cout << State->paramdef[multipliers[i]];
671 /******************************************************************************/
673 string FGAircraft::GetCoefficientStrings(void) {
674 string CoeffStrings = "";
675 bool firstime = true;
677 for (unsigned int axis = 0; axis < 6; axis++) {
678 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
682 CoeffStrings += ", ";
684 CoeffStrings += Coeff[axis][sd]->Getname();
691 /******************************************************************************/
693 string FGAircraft::GetCoefficientValues(void) {
694 string SDValues = "";
696 bool firstime = true;
698 for (unsigned int axis = 0; axis < 6; axis++) {
699 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
705 sprintf(buffer, "%9.6f", Coeff[axis][sd]->GetSD());
706 SDValues += string(buffer);
714 /******************************************************************************/
716 string FGAircraft::GetGroundReactionStrings(void) {
717 string GroundReactionStrings = "";
718 bool firstime = true;
720 for (unsigned int i=0;i<lGear.size();i++) {
721 if (!firstime) GroundReactionStrings += ", ";
722 GroundReactionStrings += (lGear[i].GetName() + "_WOW, ");
723 GroundReactionStrings += (lGear[i].GetName() + "_compressLength, ");
724 GroundReactionStrings += (lGear[i].GetName() + "_compressSpeed, ");
725 GroundReactionStrings += (lGear[i].GetName() + "_Force");
730 return GroundReactionStrings;
733 /******************************************************************************/
735 string FGAircraft::GetGroundReactionValues(void) {
737 string GroundReactionValues = "";
739 bool firstime = true;
741 for (unsigned int i=0;i<lGear.size();i++) {
742 if (!firstime) GroundReactionValues += ", ";
743 GroundReactionValues += string( lGear[i].GetWOW()?"1":"0" ) + ", ";
744 GroundReactionValues += (string(gcvt(lGear[i].GetCompLen(), 5, buff)) + ", ");
745 GroundReactionValues += (string(gcvt(lGear[i].GetCompVel(), 6, buff)) + ", ");
746 GroundReactionValues += (string(gcvt(lGear[i].GetCompForce(), 10, buff)));
751 return GroundReactionValues;