1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
34 09/03/99 JSB Changed Rocket thrust equation to correct -= Thrust instead of
35 += Thrust (thanks to Tony Peden)
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 # include <simgear/compiler.h>
43 # ifdef SG_HAVE_STD_INCLUDES
49 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
58 #include "FGPropeller.h"
63 static const char *IdSrc = "$Id$";
64 static const char *IdHdr = ID_ENGINE;
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71 FGEngine::FGEngine(FGFDMExec* exec, int engine_number) : EngineNumber(engine_number)
76 EnginePitch = EngineYaw = 0.0;
77 SLFuelFlowMax = SLOxiFlowMax = 0.0;
84 FuelNeed = OxidizerNeed = 0.0;
85 Starved = Running = Cranking = false;
92 State = FDMExec->GetState();
93 Atmosphere = FDMExec->GetAtmosphere();
94 FCS = FDMExec->GetFCS();
95 Propulsion = FDMExec->GetPropulsion();
96 Aircraft = FDMExec->GetAircraft();
97 Propagate = FDMExec->GetPropagate();
98 Auxiliary = FDMExec->GetAuxiliary();
99 Output = FDMExec->GetOutput();
101 PropertyManager = FDMExec->GetPropertyManager();
106 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 FGEngine::~FGEngine()
113 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 // This base class function should be called from within the
115 // derived class' Calculate() function before any other calculations are done.
116 // This base class method removes fuel from the fuel tanks as appropriate,
117 // and sets the starved flag if necessary.
119 void FGEngine::ConsumeFuel(void)
121 double Fshortage, Oshortage, TanksWithFuel;
124 if (TrimMode) return;
125 Fshortage = Oshortage = TanksWithFuel = 0.0;
127 // count how many assigned tanks have fuel
128 for (unsigned int i=0; i<SourceTanks.size(); i++) {
129 Tank = Propulsion->GetTank(SourceTanks[i]);
130 if (Tank->GetContents() > 0.0) {
134 if (!TanksWithFuel) return;
136 for (unsigned int i=0; i<SourceTanks.size(); i++) {
137 Tank = Propulsion->GetTank(SourceTanks[i]);
138 if (Tank->GetType() == FGTank::ttFUEL) {
139 Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
141 Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithFuel);
145 if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
146 else Starved = false;
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 double FGEngine::CalcFuelNeed(void)
153 FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 double FGEngine::CalcOxidizerNeed(void)
161 OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 void FGEngine::SetPlacement(double x, double y, double z, double pitch, double yaw)
176 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 void FGEngine::AddFeedTank(int tkID)
180 SourceTanks.push_back(tkID);
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 FGColumnVector3& FGEngine::GetBodyForces(void)
187 return Thruster->GetBodyForces();
190 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 FGColumnVector3& FGEngine::GetMoments(void)
194 return Thruster->GetMoments();
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 bool FGEngine::LoadThruster(FGConfigFile* AC_cfg)
201 string token, fullpath, localpath;
202 string thrusterFileName, thrType, engineFileName;
203 FGConfigFile* Cfg_ptr = 0;
204 double xLoc, yLoc, zLoc, Pitch, Yaw;
205 double P_Factor = 0, Sense = 0.0;
206 string enginePath = FDMExec->GetEnginePath();
207 string aircraftPath = FDMExec->GetAircraftPath();
208 thrusterFileName = AC_cfg->GetValue("FILE");
211 fullpath = enginePath + "/";
212 localpath = aircraftPath + "/" + "/Engines/";
214 fullpath = enginePath + ";";
215 localpath = aircraftPath + ";" + ";Engines;";
218 // Look in the Aircraft/Engines directory first
219 FGConfigFile Local_Thruster_cfg(localpath + thrusterFileName + ".xml");
220 FGConfigFile Thruster_cfg(fullpath + thrusterFileName + ".xml");
222 if (Local_Thruster_cfg.IsOpen()) {
223 Cfg_ptr = &Local_Thruster_cfg;
224 if (debug_lvl > 0) cout << "\n Reading thruster from file: " << localpath
225 + thrusterFileName + ".xml"<< endl;
227 if (Thruster_cfg.IsOpen()) {
228 Cfg_ptr = &Thruster_cfg;
229 if (debug_lvl > 0) cout << "\n Reading thruster from file: " << fullpath
230 + thrusterFileName + ".xml"<< endl;
235 Cfg_ptr->GetNextConfigLine();
236 thrType = Cfg_ptr->GetValue();
238 if (thrType == "FG_PROPELLER") {
239 Thruster = new FGPropeller(FDMExec, Cfg_ptr, EngineNumber);
240 } else if (thrType == "FG_NOZZLE") {
241 Thruster = new FGNozzle(FDMExec, Cfg_ptr, EngineNumber);
242 } else if (thrType == "FG_DIRECT") {
243 Thruster = new FGThruster( FDMExec, Cfg_ptr, EngineNumber);
246 AC_cfg->GetNextConfigLine();
247 while ((token = AC_cfg->GetValue()) != string("/AC_THRUSTER")) {
249 if (token == "XLOC") *AC_cfg >> xLoc;
250 else if (token == "YLOC") *AC_cfg >> yLoc;
251 else if (token == "ZLOC") *AC_cfg >> zLoc;
252 else if (token == "PITCH") *AC_cfg >> Pitch;
253 else if (token == "YAW") *AC_cfg >> Yaw;
254 else if (token == "P_FACTOR") *AC_cfg >> P_Factor;
255 else if (token == "SENSE") *AC_cfg >> Sense;
256 else cerr << "Unknown identifier: " << token << " in engine file: "
257 << engineFileName << endl;
260 Thruster->SetLocation(xLoc, yLoc, zLoc);
261 Thruster->SetAnglesToBody(0, Pitch, Yaw);
262 if (thrType == "FG_PROPELLER" && P_Factor > 0.001) {
263 ((FGPropeller*)Thruster)->SetPFactor(P_Factor);
264 if (debug_lvl > 0) cout << " P-Factor: " << P_Factor << endl;
265 ((FGPropeller*)Thruster)->SetSense(fabs(Sense)/Sense);
266 if (debug_lvl > 0) cout << " Sense: " << Sense << endl;
268 Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
272 cerr << "Could not read thruster config file: " << fullpath
273 + thrusterFileName + ".xml" << endl;
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 // The bitmasked value choices are as follows:
281 // unset: In this case (the default) JSBSim would only print
282 // out the normally expected messages, essentially echoing
283 // the config files as they are read. If the environment
284 // variable is not set, debug_lvl is set to 1 internally
285 // 0: This requests JSBSim not to output any messages
287 // 1: This value explicity requests the normal JSBSim
289 // 2: This value asks for a message to be printed out when
290 // a class is instantiated
291 // 4: When this value is set, a message is displayed when a
292 // FGModel object executes its Run() method
293 // 8: When this value is set, various runtime state variables
294 // are printed out periodically
295 // 16: When set various parameters are sanity checked and
296 // a message is printed out when they go out of bounds
298 void FGEngine::Debug(int from)
300 if (debug_lvl <= 0) return;
302 if (debug_lvl & 1) { // Standard console startup message output
303 if (from == 0) { // Constructor
307 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
308 if (from == 0) cout << "Instantiated: FGEngine" << endl;
309 if (from == 1) cout << "Destroyed: FGEngine" << endl;
311 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
313 if (debug_lvl & 8 ) { // Runtime state variables
315 if (debug_lvl & 16) { // Sanity checking
317 if (debug_lvl & 64) {
318 if (from == 0) { // Constructor
319 cout << IdSrc << endl;
320 cout << IdHdr << endl;