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 Lesser 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 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA.
23 Further information about the GNU Lesser General Public License can also be found on
24 the world wide web at http://www.gnu.org.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
31 --------------------------------------------------------------------------------
33 09/03/99 JSB Changed Rocket thrust equation to correct -= Thrust instead of
34 += Thrust (thanks to Tony Peden)
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41 # include <simgear/compiler.h>
42 # ifdef SG_HAVE_STD_INCLUDES
48 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
57 #include "FGPropeller.h"
59 #include <input_output/FGXMLParse.h>
60 #include <math/FGColumnVector3.h>
64 static const char *IdSrc = "$Id$";
65 static const char *IdHdr = ID_ENGINE;
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
72 : EngineNumber(engine_number)
74 Element* local_element;
75 FGColumnVector3 location, orientation;
80 EnginePitch = EngineYaw = 0.0;
81 SLFuelFlowMax = SLOxiFlowMax = 0.0;
85 ResetToIC(); // initialize dynamic terms
88 State = FDMExec->GetState();
89 Atmosphere = FDMExec->GetAtmosphere();
90 FCS = FDMExec->GetFCS();
91 Propulsion = FDMExec->GetPropulsion();
92 Aircraft = FDMExec->GetAircraft();
93 Propagate = FDMExec->GetPropagate();
94 Auxiliary = FDMExec->GetAuxiliary();
96 PropertyManager = FDMExec->GetPropertyManager();
98 Name = engine_element->GetAttributeValue("name");
100 // Find and set engine location
102 local_element = engine_element->GetParent()->FindElement("location");
103 if (local_element) location = local_element->FindElementTripletConvertTo("IN");
104 else cerr << "No engine location found for this engine." << endl;
106 local_element = engine_element->GetParent()->FindElement("orient");
107 if (local_element) orientation = local_element->FindElementTripletConvertTo("RAD");
108 // else cerr << "No engine orientation found for this engine." << endl;
109 // Jon: The engine orientation has a default and is not normally used.
111 SetPlacement(location, orientation);
114 local_element = engine_element->GetParent()->FindElement("thruster");
116 if (!LoadThruster(local_element)) exit(-1);
118 cerr << "No thruster definition supplied with engine definition." << endl;
121 // Load feed tank[s] references
122 local_element = engine_element->GetParent()->FindElement("feed");
124 while (local_element) {
125 AddFeedTank((int)local_element->GetDataAsNumber());
126 local_element = engine_element->GetParent()->FindNextElement("feed");
129 cerr << "No feed tank specified in engine definition." << endl;
132 char property_name[80];
133 snprintf(property_name, 80, "propulsion/engine[%d]/set-running", EngineNumber);
134 PropertyManager->Tie( property_name, (FGEngine*)this, &FGEngine::GetRunning,
135 &FGEngine::SetRunning );
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 FGEngine::~FGEngine()
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 void FGEngine::ResetToIC(void)
156 FuelNeed = OxidizerNeed = 0.0;
157 Starved = Running = Cranking = false;
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 // This base class function should be called from within the
167 // derived class' Calculate() function before any other calculations are done.
168 // This base class method removes fuel from the fuel tanks as appropriate,
169 // and sets the starved flag if necessary.
171 void FGEngine::ConsumeFuel(void)
173 if (FuelFreeze) return;
174 if (TrimMode) return;
177 double Fshortage, Oshortage, TanksWithFuel, TanksWithOxidizer;
179 bool haveOxTanks = false;
180 Fshortage = Oshortage = TanksWithFuel = TanksWithOxidizer = 0.0;
182 // count how many assigned tanks have fuel
183 for (i=0; i<SourceTanks.size(); i++) {
184 Tank = Propulsion->GetTank(SourceTanks[i]);
185 if (Tank->GetType() == FGTank::ttFUEL){
186 if (Tank->GetContents() > 0.0) ++TanksWithFuel;
187 } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
189 if (Tank->GetContents() > 0.0) ++TanksWithOxidizer;
192 if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
197 for (i=0; i<SourceTanks.size(); i++) {
198 Tank = Propulsion->GetTank(SourceTanks[i]);
199 if (Tank->GetType() == FGTank::ttFUEL) {
200 Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
201 } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
202 Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithOxidizer);
206 if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
207 else Starved = false;
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 double FGEngine::CalcFuelNeed(void)
214 FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 double FGEngine::CalcOxidizerNeed(void)
222 OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
233 EnginePitch = orientation(ePitch);
234 EngineYaw = orientation (eYaw);
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 void FGEngine::AddFeedTank(int tkID)
241 SourceTanks.push_back(tkID);
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 FGColumnVector3& FGEngine::GetBodyForces(void)
248 return Thruster->GetBodyForces();
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 FGColumnVector3& FGEngine::GetMoments(void)
255 return Thruster->GetMoments();
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 bool FGEngine::LoadThruster(Element *thruster_element)
262 string token, fullpath, localpath;
263 string thruster_filename, thruster_fullpathname, thrType;
264 double P_Factor = 0, Sense = 0.0;
265 string enginePath = FDMExec->GetEnginePath();
266 string aircraftPath = FDMExec->GetFullAircraftPath();
267 ifstream thruster_file;
268 FGColumnVector3 location, orientation;
269 string separator = "/";
275 fullpath = enginePath + separator;
276 localpath = aircraftPath + separator + "Engines" + separator;
278 thruster_filename = thruster_element->GetAttributeValue("file");
279 if ( !thruster_filename.empty()) {
280 thruster_fullpathname = fullpath + thruster_filename + ".xml";
281 thruster_file.open(thruster_fullpathname.c_str());
282 if ( !thruster_file.is_open()) {
283 thruster_fullpathname = localpath + thruster_filename + ".xml";
284 thruster_file.open(thruster_fullpathname.c_str());
285 if ( !thruster_file.is_open()) {
286 cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
289 thruster_file.close();
292 thruster_file.close();
295 cerr << "No thruster filename given." << endl;
299 document = LoadXMLDocument(thruster_fullpathname);
300 document->SetParent(thruster_element);
302 thrType = document->GetName();
304 if (thrType == "propeller") {
305 Thruster = new FGPropeller(FDMExec, document, EngineNumber);
306 } else if (thrType == "nozzle") {
307 Thruster = new FGNozzle(FDMExec, document, EngineNumber);
308 } else if (thrType == "direct") {
309 Thruster = new FGThruster( FDMExec, document, EngineNumber);
312 Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 // The bitmasked value choices are as follows:
320 // unset: In this case (the default) JSBSim would only print
321 // out the normally expected messages, essentially echoing
322 // the config files as they are read. If the environment
323 // variable is not set, debug_lvl is set to 1 internally
324 // 0: This requests JSBSim not to output any messages
326 // 1: This value explicity requests the normal JSBSim
328 // 2: This value asks for a message to be printed out when
329 // a class is instantiated
330 // 4: When this value is set, a message is displayed when a
331 // FGModel object executes its Run() method
332 // 8: When this value is set, various runtime state variables
333 // are printed out periodically
334 // 16: When set various parameters are sanity checked and
335 // a message is printed out when they go out of bounds
337 void FGEngine::Debug(int from)
339 if (debug_lvl <= 0) return;
341 if (debug_lvl & 1) { // Standard console startup message output
342 if (from == 0) { // Constructor
345 if (from == 2) { // After thruster loading
346 cout << " X = " << Thruster->GetLocationX() << endl;
347 cout << " Y = " << Thruster->GetLocationY() << endl;
348 cout << " Z = " << Thruster->GetLocationZ() << endl;
349 cout << " Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
350 cout << " Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
353 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
354 if (from == 0) cout << "Instantiated: FGEngine" << endl;
355 if (from == 1) cout << "Destroyed: FGEngine" << endl;
357 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
359 if (debug_lvl & 8 ) { // Runtime state variables
361 if (debug_lvl & 16) { // Sanity checking
363 if (debug_lvl & 64) {
364 if (from == 0) { // Constructor
365 cout << IdSrc << endl;
366 cout << IdHdr << endl;