]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGPropeller.cpp
Andreas Gaeb: fix #222 (JSBSIm reset problems)
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGPropeller.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGPropeller.cpp
4  Author:       Jon S. Berndt
5  Date started: 08/24/00
6  Purpose:      Encapsulates the propeller object
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jon@jsbsim.org) -------------
9
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
13  version.
14
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
18  details.
19
20  You should have received a copy of the GNU Lesser 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.
23
24  Further information about the GNU Lesser General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29
30 HISTORY
31 --------------------------------------------------------------------------------
32 08/24/00  JSB  Created
33
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 INCLUDES
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37
38 #include <iostream>
39 #include <sstream>
40
41 #include "FGPropeller.h"
42 #include "models/FGPropagate.h"
43 #include "models/FGAtmosphere.h"
44 #include "models/FGAuxiliary.h"
45 #include "input_output/FGXMLElement.h"
46
47 using namespace std;
48
49 namespace JSBSim {
50
51 static const char *IdSrc = "$Id: FGPropeller.cpp,v 1.32 2010/10/21 03:27:40 jberndt Exp $";
52 static const char *IdHdr = ID_PROPELLER;
53
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 CLASS IMPLEMENTATION
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57
58 // This class currently makes certain assumptions when calculating torque and
59 // p-factor. That is, that the axis of rotation is the X axis of the aircraft -
60 // not just the X-axis of the engine/propeller. This may or may not work for a
61 // helicopter.
62
63 FGPropeller::FGPropeller(FGFDMExec* exec, Element* prop_element, int num)
64                        : FGThruster(exec, prop_element, num)
65 {
66   string token;
67   Element *table_element, *local_element;
68   string name="";
69   FGPropertyManager* PropertyManager = exec->GetPropertyManager();
70
71   MaxPitch = MinPitch = P_Factor = Pitch = Advance = MinRPM = MaxRPM = 0.0;
72   Sense = 1; // default clockwise rotation
73   ReversePitch = 0.0;
74   Reversed = false;
75   Feathered = false;
76   Reverse_coef = 0.0;
77   GearRatio = 1.0;
78   CtFactor = CpFactor = 1.0;
79   ConstantSpeed = 0;
80   cThrust = cPower = CtMach = CpMach = 0;
81   Vinduced = 0.0;
82
83   if (prop_element->FindElement("ixx"))
84     Ixx = prop_element->FindElementValueAsNumberConvertTo("ixx", "SLUG*FT2");
85   if (prop_element->FindElement("diameter"))
86     Diameter = prop_element->FindElementValueAsNumberConvertTo("diameter", "FT");
87   if (prop_element->FindElement("numblades"))
88     numBlades = (int)prop_element->FindElementValueAsNumber("numblades");
89   if (prop_element->FindElement("gearratio"))
90     GearRatio = prop_element->FindElementValueAsNumber("gearratio");
91   if (prop_element->FindElement("minpitch"))
92     MinPitch = prop_element->FindElementValueAsNumber("minpitch");
93   if (prop_element->FindElement("maxpitch"))
94     MaxPitch = prop_element->FindElementValueAsNumber("maxpitch");
95   if (prop_element->FindElement("minrpm"))
96     MinRPM = prop_element->FindElementValueAsNumber("minrpm");
97   if (prop_element->FindElement("maxrpm")) {
98     MaxRPM = prop_element->FindElementValueAsNumber("maxrpm");
99     ConstantSpeed = 1;
100     }
101   if (prop_element->FindElement("constspeed"))
102     ConstantSpeed = (int)prop_element->FindElementValueAsNumber("constspeed");
103   if (prop_element->FindElement("reversepitch"))
104     ReversePitch = prop_element->FindElementValueAsNumber("reversepitch");
105   for (int i=0; i<2; i++) {
106     table_element = prop_element->FindNextElement("table");
107     name = table_element->GetAttributeValue("name");
108     try {
109       if (name == "C_THRUST") {
110         cThrust = new FGTable(PropertyManager, table_element);
111       } else if (name == "C_POWER") {
112         cPower = new FGTable(PropertyManager, table_element);
113       } else if (name == "CT_MACH") {
114         CtMach = new FGTable(PropertyManager, table_element);
115       } else if (name == "CP_MACH") {
116         CpMach = new FGTable(PropertyManager, table_element);
117       } else {
118         cerr << "Unknown table type: " << name << " in propeller definition." << endl;
119       }
120     } catch (std::string str) {
121       throw("Error loading propeller table:" + name + ". " + str);
122     }
123   }
124
125   local_element = prop_element->GetParent()->FindElement("sense");
126   if (local_element) {
127     double Sense = local_element->GetDataAsNumber();
128     SetSense(fabs(Sense)/Sense);
129   }
130   local_element = prop_element->GetParent()->FindElement("p_factor");
131   if (local_element) {
132     P_Factor = local_element->GetDataAsNumber();
133   }
134   if (P_Factor < 0) {
135     cerr << "P-Factor value in config file must be greater than zero" << endl;
136   }
137   if (prop_element->FindElement("ct_factor"))
138     SetCtFactor( prop_element->FindElementValueAsNumber("ct_factor") );
139   if (prop_element->FindElement("cp_factor"))
140     SetCpFactor( prop_element->FindElementValueAsNumber("cp_factor") );
141
142   Type = ttPropeller;
143   RPM = 0;
144   vTorque.InitMatrix();
145   D4 = Diameter*Diameter*Diameter*Diameter;
146   D5 = D4*Diameter;
147   Pitch = MinPitch;
148
149   string property_name, base_property_name;
150   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNum);
151   property_name = base_property_name + "/advance-ratio";
152   PropertyManager->Tie( property_name.c_str(), &J );
153   property_name = base_property_name + "/blade-angle";
154   PropertyManager->Tie( property_name.c_str(), &Pitch );
155   property_name = base_property_name + "/thrust-coefficient";
156   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetThrustCoefficient );
157   property_name = base_property_name + "/propeller-rpm";
158   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetRPM );
159   property_name = base_property_name + "/helical-tip-Mach";
160   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetHelicalTipMach );
161   property_name = base_property_name + "/constant-speed-mode";
162   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetConstantSpeed,
163                       &FGPropeller::SetConstantSpeed );
164   property_name = base_property_name + "/prop-induced-velocity_fps";
165   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetInducedVelocity,
166                       &FGPropeller::SetInducedVelocity );
167
168   Debug(0);
169 }
170
171 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172
173 FGPropeller::~FGPropeller()
174 {
175   delete cThrust;
176   delete cPower;
177   delete CtMach;
178   delete CpMach;
179
180   Debug(1);
181 }
182
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 //
185 // We must be getting the aerodynamic velocity here, NOT the inertial velocity.
186 // We need the velocity with respect to the wind.
187 //
188 // Note that PowerAvailable is the excess power available after the drag of the
189 // propeller has been subtracted. At equilibrium, PowerAvailable will be zero -
190 // indicating that the propeller will not accelerate or decelerate.
191 // Remembering that Torque * omega = Power, we can derive the torque on the
192 // propeller and its acceleration to give a new RPM. The current RPM will be
193 // used to calculate thrust.
194 //
195 // Because RPM could be zero, we need to be creative about what RPM is stated as.
196
197 double FGPropeller::Calculate(double PowerAvailable)
198 {
199   double omega, alpha, beta;
200
201   double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
202   double rho = fdmex->GetAtmosphere()->GetDensity();
203   double RPS = RPM/60.0;
204
205   // Calculate helical tip Mach
206   double Area = 0.25*Diameter*Diameter*M_PI;
207   double Vtip = RPS * Diameter * M_PI;
208   HelicalTipMach = sqrt(Vtip*Vtip + Vel*Vel) / 
209                    fdmex->GetAtmosphere()->GetSoundSpeed(); 
210
211   if (RPS > 0.0) J = Vel / (Diameter * RPS); // Calculate J normally
212   else           J = Vel / Diameter;      
213
214   if (MaxPitch == MinPitch) {    // Fixed pitch prop
215          ThrustCoeff = cThrust->GetValue(J);
216   } else {                       // Variable pitch prop
217          ThrustCoeff = cThrust->GetValue(J, Pitch);
218   }
219  
220   // Apply optional scaling factor to Ct (default value = 1)
221   ThrustCoeff *= CtFactor;
222
223   // Apply optional Mach effects from CT_MACH table
224   if (CtMach) ThrustCoeff *= CtMach->GetValue(HelicalTipMach);
225
226   if (P_Factor > 0.0001) {
227     alpha = fdmex->GetAuxiliary()->Getalpha();
228     beta  = fdmex->GetAuxiliary()->Getbeta();
229     SetActingLocationY( GetLocationY() + P_Factor*alpha*Sense);
230     SetActingLocationZ( GetLocationZ() + P_Factor*beta*Sense);
231   }
232
233   Thrust = ThrustCoeff*RPS*RPS*D4*rho;
234
235   // From B. W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics"
236   // first edition, eqn. 6.15 (propeller analysis chapter).
237   Vinduced = 0.5 * (-Vel + sqrt(Vel*Vel + 2.0*Thrust/(rho*Area)));
238
239   omega = RPS*2.0*M_PI;
240
241   vFn(1) = Thrust;
242
243   // The Ixx value and rotation speed given below are for rotation about the
244   // natural axis of the engine. The transform takes place in the base class
245   // FGForce::GetBodyForces() function.
246
247   vH(eX) = Ixx*omega*Sense;
248   vH(eY) = 0.0;
249   vH(eZ) = 0.0;
250
251   if (omega > 0.0) ExcessTorque = PowerAvailable / omega;
252   else             ExcessTorque = PowerAvailable / 1.0;
253
254   RPM = (RPS + ((ExcessTorque / Ixx) / (2.0 * M_PI)) * deltaT) * 60.0;
255
256   if (RPM < 0.0) RPM = 0.0; // Engine won't turn backwards
257
258   // Transform Torque and momentum first, as PQR is used in this
259   // equation and cannot be transformed itself.
260   vMn = fdmex->GetPropagate()->GetPQR()*(Transform()*vH) + Transform()*vTorque;
261
262   return Thrust; // return thrust in pounds
263 }
264
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266
267 double FGPropeller::GetPowerRequired(void)
268 {
269   double cPReq, J;
270   double rho = fdmex->GetAtmosphere()->GetDensity();
271   double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
272   double RPS = RPM / 60.0;
273
274   if (RPS != 0.0) J = Vel / (Diameter * RPS);
275   else            J = Vel / Diameter; 
276
277   if (MaxPitch == MinPitch) {   // Fixed pitch prop
278     cPReq = cPower->GetValue(J);
279
280   } else {                      // Variable pitch prop
281
282     if (ConstantSpeed != 0) {   // Constant Speed Mode
283
284       // do normal calculation when propeller is neither feathered nor reversed
285       // Note:  This method of feathering and reversing was added to support the
286       //        turboprop model.  It's left here for backward compatablity, but
287       //        now feathering and reversing should be done in Manual Pitch Mode.
288       if (!Feathered) {
289         if (!Reversed) {
290
291           double rpmReq = MinRPM + (MaxRPM - MinRPM) * Advance;
292           double dRPM = rpmReq - RPM;
293           // The pitch of a variable propeller cannot be changed when the RPMs are
294           // too low - the oil pump does not work.
295           if (RPM > 200) Pitch -= dRPM * deltaT;
296           if (Pitch < MinPitch)       Pitch = MinPitch;
297           else if (Pitch > MaxPitch)  Pitch = MaxPitch;
298
299         } else { // Reversed propeller
300
301           // when reversed calculate propeller pitch depending on throttle lever position
302           // (beta range for taxing full reverse for braking)
303           double PitchReq = MinPitch - ( MinPitch - ReversePitch ) * Reverse_coef;
304           // The pitch of a variable propeller cannot be changed when the RPMs are
305           // too low - the oil pump does not work.
306           if (RPM > 200) Pitch += (PitchReq - Pitch) / 200;
307           if (RPM > MaxRPM) {
308             Pitch += (MaxRPM - RPM) / 50;
309             if (Pitch < ReversePitch) Pitch = ReversePitch;
310             else if (Pitch > MaxPitch)  Pitch = MaxPitch;
311           }
312         }
313
314       } else { // Feathered propeller
315                // ToDo: Make feathered and reverse settings done via FGKinemat
316         Pitch += (MaxPitch - Pitch) / 300; // just a guess (about 5 sec to fully feathered)
317       }
318
319     } else { // Manual Pitch Mode, pitch is controlled externally
320
321     }
322   
323     cPReq = cPower->GetValue(J, Pitch);
324   }
325
326   // Apply optional scaling factor to Cp (default value = 1)
327   cPReq *= CpFactor;
328
329   // Apply optional Mach effects from CP_MACH table
330   if (CpMach) cPReq *= CpMach->GetValue(HelicalTipMach);
331
332   if (RPS > 0.1) {
333     PowerRequired = cPReq*RPS*RPS*RPS*D5*rho;
334     vTorque(eX) = -Sense*PowerRequired / (RPS*2.0*M_PI);
335   } else {
336      // For a stationary prop we have to estimate torque first.
337      double CL = (90.0 - Pitch) / 20.0;
338      if (CL > 1.5) CL = 1.5;
339      double BladeArea = Diameter * Diameter / 32.0 * numBlades;
340      vTorque(eX) = -Sense*BladeArea*Diameter*Vel*Vel*rho*0.19*CL;
341      PowerRequired = fabs(vTorque(eX))*0.2*M_PI;
342   }
343
344   return PowerRequired;
345 }
346
347 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348
349 FGColumnVector3 FGPropeller::GetPFactor()
350 {
351   double px=0.0, py, pz;
352
353   py = Thrust * Sense * (GetActingLocationY() - GetLocationY()) / 12.0;
354   pz = Thrust * Sense * (GetActingLocationZ() - GetLocationZ()) / 12.0;
355
356   return FGColumnVector3(px, py, pz);
357 }
358
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360
361 string FGPropeller::GetThrusterLabels(int id, string delimeter)
362 {
363   std::ostringstream buf;
364
365   buf << Name << " Torque (engine " << id << ")" << delimeter
366       << Name << " PFactor Pitch (engine " << id << ")" << delimeter
367       << Name << " PFactor Yaw (engine " << id << ")" << delimeter
368       << Name << " Thrust (engine " << id << " in lbs)" << delimeter;
369   if (IsVPitch())
370     buf << Name << " Pitch (engine " << id << ")" << delimeter;
371   buf << Name << " RPM (engine " << id << ")";
372
373   return buf.str();
374 }
375
376 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377
378 string FGPropeller::GetThrusterValues(int id, string delimeter)
379 {
380   std::ostringstream buf;
381
382   FGColumnVector3 vPFactor = GetPFactor();
383   buf << vTorque(eX) << delimeter
384       << vPFactor(ePitch) << delimeter
385       << vPFactor(eYaw) << delimeter
386       << Thrust << delimeter;
387   if (IsVPitch())
388     buf << Pitch << delimeter;
389   buf << RPM;
390
391   return buf.str();
392 }
393
394 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 //    The bitmasked value choices are as follows:
396 //    unset: In this case (the default) JSBSim would only print
397 //       out the normally expected messages, essentially echoing
398 //       the config files as they are read. If the environment
399 //       variable is not set, debug_lvl is set to 1 internally
400 //    0: This requests JSBSim not to output any messages
401 //       whatsoever.
402 //    1: This value explicity requests the normal JSBSim
403 //       startup messages
404 //    2: This value asks for a message to be printed out when
405 //       a class is instantiated
406 //    4: When this value is set, a message is displayed when a
407 //       FGModel object executes its Run() method
408 //    8: When this value is set, various runtime state variables
409 //       are printed out periodically
410 //    16: When set various parameters are sanity checked and
411 //       a message is printed out when they go out of bounds
412
413 void FGPropeller::Debug(int from)
414 {
415   if (debug_lvl <= 0) return;
416
417   if (debug_lvl & 1) { // Standard console startup message output
418     if (from == 0) { // Constructor
419       cout << "\n    Propeller Name: " << Name << endl;
420       cout << "      IXX = " << Ixx << endl;
421       cout << "      Diameter = " << Diameter << " ft." << endl;
422       cout << "      Number of Blades  = " << numBlades << endl;
423       cout << "      Gear Ratio  = " << GearRatio << endl;
424       cout << "      Minimum Pitch  = " << MinPitch << endl;
425       cout << "      Maximum Pitch  = " << MaxPitch << endl;
426       cout << "      Minimum RPM  = " << MinRPM << endl;
427       cout << "      Maximum RPM  = " << MaxRPM << endl;
428 //      cout << "      Thrust Coefficient: " <<  endl;
429 //      cThrust->Print();
430 //      cout << "      Power Coefficient: " <<  endl;
431 //      cPower->Print();
432     }
433   }
434   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
435     if (from == 0) cout << "Instantiated: FGPropeller" << endl;
436     if (from == 1) cout << "Destroyed:    FGPropeller" << endl;
437   }
438   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
439   }
440   if (debug_lvl & 8 ) { // Runtime state variables
441   }
442   if (debug_lvl & 16) { // Sanity checking
443   }
444   if (debug_lvl & 64) {
445     if (from == 0) { // Constructor
446       cout << IdSrc << endl;
447       cout << IdHdr << endl;
448     }
449   }
450 }
451 }