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