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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 #include "FGPropeller.h"
44 #include <input_output/FGXMLParse.h>
45 #include <math/FGColumnVector3.h>
50 static const char *IdSrc = "$Id$";
51 static const char *IdHdr = ID_ENGINE;
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
58 : EngineNumber(engine_number)
60 Element* local_element;
61 FGColumnVector3 location, orientation;
66 EnginePitch = EngineYaw = 0.0;
71 ResetToIC(); // initialize dynamic terms
74 State = FDMExec->GetState();
75 Atmosphere = FDMExec->GetAtmosphere();
76 FCS = FDMExec->GetFCS();
77 Propulsion = FDMExec->GetPropulsion();
78 Aircraft = FDMExec->GetAircraft();
79 Propagate = FDMExec->GetPropagate();
80 Auxiliary = FDMExec->GetAuxiliary();
82 PropertyManager = FDMExec->GetPropertyManager();
84 Name = engine_element->GetAttributeValue("name");
86 // Find and set engine location
88 local_element = engine_element->GetParent()->FindElement("location");
89 if (local_element) location = local_element->FindElementTripletConvertTo("IN");
90 else cerr << "No engine location found for this engine." << endl;
92 local_element = engine_element->GetParent()->FindElement("orient");
93 if (local_element) orientation = local_element->FindElementTripletConvertTo("RAD");
94 // else cerr << "No engine orientation found for this engine." << endl;
95 // Jon: The engine orientation has a default and is not normally used.
97 SetPlacement(location, orientation);
100 local_element = engine_element->GetParent()->FindElement("thruster");
102 if (!LoadThruster(local_element)) exit(-1);
104 cerr << "No thruster definition supplied with engine definition." << endl;
107 // Load feed tank[s] references
108 local_element = engine_element->GetParent()->FindElement("feed");
110 while (local_element) {
111 AddFeedTank((int)local_element->GetDataAsNumber());
112 local_element = engine_element->GetParent()->FindNextElement("feed");
115 cerr << "No feed tank specified in engine definition." << endl;
118 char property_name[80];
119 snprintf(property_name, 80, "propulsion/engine[%d]/set-running", EngineNumber);
120 PropertyManager->Tie( property_name, this, &FGEngine::GetRunning, &FGEngine::SetRunning );
121 snprintf(property_name, 80, "propulsion/engine[%u]/thrust-lbs", EngineNumber);
122 PropertyManager->Tie( property_name, Thruster, &FGThruster::GetThrust);
123 snprintf(property_name, 80, "propulsion/engine[%u]/fuel-flow-rate-pps", EngineNumber);
124 PropertyManager->Tie( property_name, this, &FGEngine::GetFuelFlowRate);
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 FGEngine::~FGEngine()
137 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 void FGEngine::ResetToIC(void)
145 Starved = Running = Cranking = false;
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 // This base class function should be called from within the
155 // derived class' Calculate() function before any other calculations are done.
156 // This base class method removes fuel from the fuel tanks as appropriate,
157 // and sets the starved flag if necessary.
158 // This version of the fuel consumption code should never see an oxidizer tank.
160 void FGEngine::ConsumeFuel(void)
162 if (FuelFreeze) return;
163 if (TrimMode) return;
166 double Fshortage, TanksWithFuel;
168 Fshortage = TanksWithFuel = 0.0;
170 // count how many assigned tanks have fuel
171 for (i=0; i<SourceTanks.size(); i++) {
172 Tank = Propulsion->GetTank(SourceTanks[i]);
173 if (Tank->GetType() == FGTank::ttFUEL){
174 if (Tank->GetContents() > 0.0) ++TanksWithFuel;
176 cerr << "No oxidizer tanks should be used for this engine type." << endl;
179 if (TanksWithFuel==0) {
184 for (i=0; i<SourceTanks.size(); i++) {
185 Tank = Propulsion->GetTank(SourceTanks[i]);
186 if (Tank->GetType() == FGTank::ttFUEL) {
187 Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
189 cerr << "No oxidizer tanks should be used for this engine type." << endl;
193 if (Fshortage < 0.00) Starved = true;
194 else Starved = false;
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 double FGEngine::CalcFuelNeed(void)
201 double dT = State->Getdt()*Propulsion->GetRate();
202 FuelFlowRate = SLFuelFlowMax*PctPower;
203 FuelExpended = FuelFlowRate*dT;
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
214 EnginePitch = orientation(ePitch);
215 EngineYaw = orientation (eYaw);
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 void FGEngine::AddFeedTank(int tkID)
222 SourceTanks.push_back(tkID);
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 FGColumnVector3& FGEngine::GetBodyForces(void)
229 return Thruster->GetBodyForces();
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 FGColumnVector3& FGEngine::GetMoments(void)
236 return Thruster->GetMoments();
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 bool FGEngine::LoadThruster(Element *thruster_element)
243 string token, fullpath, localpath;
244 string thruster_filename, thruster_fullpathname, thrType;
245 double P_Factor = 0, Sense = 0.0;
246 string enginePath = FDMExec->GetEnginePath();
247 string aircraftPath = FDMExec->GetFullAircraftPath();
248 ifstream thruster_file;
249 FGColumnVector3 location, orientation;
250 string separator = "/";
252 fullpath = enginePath + separator;
253 localpath = aircraftPath + separator + "Engines" + separator;
255 thruster_filename = thruster_element->GetAttributeValue("file");
256 if ( !thruster_filename.empty()) {
257 thruster_fullpathname = fullpath + thruster_filename + ".xml";
258 thruster_file.open(thruster_fullpathname.c_str());
259 if ( !thruster_file.is_open()) {
260 thruster_fullpathname = localpath + thruster_filename + ".xml";
261 thruster_file.open(thruster_fullpathname.c_str());
262 if ( !thruster_file.is_open()) {
263 cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
266 thruster_file.close();
269 thruster_file.close();
272 cerr << "No thruster filename given." << endl;
276 document = LoadXMLDocument(thruster_fullpathname);
277 document->SetParent(thruster_element);
279 thrType = document->GetName();
281 if (thrType == "propeller") {
282 Thruster = new FGPropeller(FDMExec, document, EngineNumber);
283 } else if (thrType == "nozzle") {
284 Thruster = new FGNozzle(FDMExec, document, EngineNumber);
285 } else if (thrType == "direct") {
286 Thruster = new FGThruster( FDMExec, document, EngineNumber);
289 Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
295 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 // The bitmasked value choices are as follows:
297 // unset: In this case (the default) JSBSim would only print
298 // out the normally expected messages, essentially echoing
299 // the config files as they are read. If the environment
300 // variable is not set, debug_lvl is set to 1 internally
301 // 0: This requests JSBSim not to output any messages
303 // 1: This value explicity requests the normal JSBSim
305 // 2: This value asks for a message to be printed out when
306 // a class is instantiated
307 // 4: When this value is set, a message is displayed when a
308 // FGModel object executes its Run() method
309 // 8: When this value is set, various runtime state variables
310 // are printed out periodically
311 // 16: When set various parameters are sanity checked and
312 // a message is printed out when they go out of bounds
314 void FGEngine::Debug(int from)
316 if (debug_lvl <= 0) return;
318 if (debug_lvl & 1) { // Standard console startup message output
319 if (from == 0) { // Constructor
322 if (from == 2) { // After thruster loading
323 cout << " X = " << Thruster->GetLocationX() << endl;
324 cout << " Y = " << Thruster->GetLocationY() << endl;
325 cout << " Z = " << Thruster->GetLocationZ() << endl;
326 cout << " Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
327 cout << " Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
330 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
331 if (from == 0) cout << "Instantiated: FGEngine" << endl;
332 if (from == 1) cout << "Destroyed: FGEngine" << endl;
334 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
336 if (debug_lvl & 8 ) { // Runtime state variables
338 if (debug_lvl & 16) { // Sanity checking
340 if (debug_lvl & 64) {
341 if (from == 0) { // Constructor
342 cout << IdSrc << endl;
343 cout << IdHdr << endl;