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 details.
19 You should have received a copy of the GNU 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 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;
88 FuelNeed = OxidizerNeed = 0.0;
89 Starved = Running = Cranking = false;
97 State = FDMExec->GetState();
98 Atmosphere = FDMExec->GetAtmosphere();
99 FCS = FDMExec->GetFCS();
100 Propulsion = FDMExec->GetPropulsion();
101 Aircraft = FDMExec->GetAircraft();
102 Propagate = FDMExec->GetPropagate();
103 Auxiliary = FDMExec->GetAuxiliary();
105 PropertyManager = FDMExec->GetPropertyManager();
107 Name = engine_element->GetAttributeValue("name");
109 // Find and set engine location
111 local_element = engine_element->GetParent()->FindElement("location");
112 if (local_element) location = local_element->FindElementTripletConvertTo("IN");
113 else cerr << "No engine location found for this engine." << endl;
115 local_element = engine_element->GetParent()->FindElement("orient");
116 if (local_element) orientation = local_element->FindElementTripletConvertTo("IN");
117 else cerr << "No engine orientation found for this engine." << endl;
119 SetPlacement(location, orientation);
122 local_element = engine_element->GetParent()->FindElement("thruster");
124 LoadThruster(local_element);
126 cerr << "No thruster definition supplied with engine definition." << endl;
129 // Load feed tank[s] references
130 local_element = engine_element->GetParent()->FindElement("feed");
132 while (local_element) {
133 AddFeedTank((int)local_element->GetDataAsNumber());
134 local_element = engine_element->GetParent()->FindNextElement("feed");
137 cerr << "No feed tank specified in engine definition." << endl;
143 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 FGEngine::~FGEngine()
147 if (Thruster) delete Thruster;
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 // This base class function should be called from within the
153 // derived class' Calculate() function before any other calculations are done.
154 // This base class method removes fuel from the fuel tanks as appropriate,
155 // and sets the starved flag if necessary.
157 void FGEngine::ConsumeFuel(void)
159 if (FuelFreeze) return;
160 if (TrimMode) return;
163 double Fshortage, Oshortage, TanksWithFuel, TanksWithOxidizer;
165 bool haveOxTanks = false;
166 Fshortage = Oshortage = TanksWithFuel = TanksWithOxidizer = 0.0;
168 // count how many assigned tanks have fuel
169 for (i=0; i<SourceTanks.size(); i++) {
170 Tank = Propulsion->GetTank(SourceTanks[i]);
171 if (Tank->GetType() == FGTank::ttFUEL){
172 if (Tank->GetContents() > 0.0) ++TanksWithFuel;
173 } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
175 if (Tank->GetContents() > 0.0) ++TanksWithOxidizer;
178 if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
183 for (i=0; i<SourceTanks.size(); i++) {
184 Tank = Propulsion->GetTank(SourceTanks[i]);
185 if (Tank->GetType() == FGTank::ttFUEL) {
186 Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
187 } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
188 Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithOxidizer);
192 if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
193 else Starved = false;
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 double FGEngine::CalcFuelNeed(void)
200 FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 double FGEngine::CalcOxidizerNeed(void)
208 OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
219 EnginePitch = orientation(ePitch);
220 EngineYaw = orientation (eYaw);
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 void FGEngine::AddFeedTank(int tkID)
227 SourceTanks.push_back(tkID);
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 FGColumnVector3& FGEngine::GetBodyForces(void)
234 return Thruster->GetBodyForces();
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 FGColumnVector3& FGEngine::GetMoments(void)
241 return Thruster->GetMoments();
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 bool FGEngine::LoadThruster(Element *thruster_element)
248 string token, fullpath, localpath;
249 string thruster_filename, thruster_fullpathname, thrType;
250 double xLoc, yLoc, zLoc, Pitch, Yaw;
251 double P_Factor = 0, Sense = 0.0;
252 string enginePath = FDMExec->GetEnginePath();
253 string aircraftPath = FDMExec->GetAircraftPath();
254 FGXMLParse thruster_file_parser;
255 Element *document, *element;
256 ifstream thruster_file;
257 FGColumnVector3 location, orientation;
260 string separator = "/";
262 string separator = ";";
265 fullpath = enginePath + separator;
266 localpath = aircraftPath + separator + "Engines" + separator;
268 thruster_filename = thruster_element->GetAttributeValue("file");
269 if ( !thruster_filename.empty()) {
270 thruster_fullpathname = fullpath + thruster_filename + ".xml";
271 thruster_file.open(thruster_fullpathname.c_str());
272 if ( !thruster_file.is_open()) {
273 thruster_fullpathname = localpath + thruster_filename + ".xml";
274 thruster_file.open(thruster_fullpathname.c_str());
275 if ( !thruster_file.is_open()) {
276 cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
279 thruster_file.close();
282 thruster_file.close();
285 cerr << "No thruster filename given." << endl;
289 readXML(thruster_fullpathname, thruster_file_parser);
290 document = thruster_file_parser.GetDocument(); // document holds the thruster description
291 document->SetParent(thruster_element);
293 thrType = document->GetName();
295 if (thrType == "propeller") {
296 Thruster = new FGPropeller(FDMExec, document, EngineNumber);
297 } else if (thrType == "nozzle") {
298 Thruster = new FGNozzle(FDMExec, document, EngineNumber);
299 } else if (thrType == "direct") {
300 Thruster = new FGThruster( FDMExec, document, EngineNumber);
303 Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
309 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 // The bitmasked value choices are as follows:
311 // unset: In this case (the default) JSBSim would only print
312 // out the normally expected messages, essentially echoing
313 // the config files as they are read. If the environment
314 // variable is not set, debug_lvl is set to 1 internally
315 // 0: This requests JSBSim not to output any messages
317 // 1: This value explicity requests the normal JSBSim
319 // 2: This value asks for a message to be printed out when
320 // a class is instantiated
321 // 4: When this value is set, a message is displayed when a
322 // FGModel object executes its Run() method
323 // 8: When this value is set, various runtime state variables
324 // are printed out periodically
325 // 16: When set various parameters are sanity checked and
326 // a message is printed out when they go out of bounds
328 void FGEngine::Debug(int from)
330 if (debug_lvl <= 0) return;
332 if (debug_lvl & 1) { // Standard console startup message output
333 if (from == 0) { // Constructor
336 if (from == 2) { // After thruster loading
337 cout << " X = " << Thruster->GetLocationX() << endl;
338 cout << " Y = " << Thruster->GetLocationY() << endl;
339 cout << " Z = " << Thruster->GetLocationZ() << endl;
340 cout << " Pitch = " << Thruster->GetAnglesToBody(ePitch) << endl;
341 cout << " Yaw = " << Thruster->GetAnglesToBody(eYaw) << endl;
344 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
345 if (from == 0) cout << "Instantiated: FGEngine" << endl;
346 if (from == 1) cout << "Destroyed: FGEngine" << endl;
348 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
350 if (debug_lvl & 8 ) { // Runtime state variables
352 if (debug_lvl & 16) { // Sanity checking
354 if (debug_lvl & 64) {
355 if (from == 0) { // Constructor
356 cout << IdSrc << endl;
357 cout << IdHdr << endl;