]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGPropeller.cpp
Merge branch 'next' of gitorious.org:fg/flightgear into next
[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.30 2010/05/02 15:10:07 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     if (name == "C_THRUST") {
109       cThrust = new FGTable(PropertyManager, table_element);
110     } else if (name == "C_POWER") {
111       cPower = new FGTable(PropertyManager, table_element);
112     } else if (name == "CT_MACH") {
113       CtMach = new FGTable(PropertyManager, table_element);
114     } else if (name == "CP_MACH") {
115       CpMach = new FGTable(PropertyManager, table_element);
116     } else {
117       cerr << "Unknown table type: " << name << " in propeller definition." << endl;
118     }
119   }
120
121   local_element = prop_element->GetParent()->FindElement("sense");
122   if (local_element) {
123     double Sense = local_element->GetDataAsNumber();
124     SetSense(fabs(Sense)/Sense);
125   }
126   local_element = prop_element->GetParent()->FindElement("p_factor");
127   if (local_element) {
128     P_Factor = local_element->GetDataAsNumber();
129   }
130   if (P_Factor < 0) {
131     cerr << "P-Factor value in config file must be greater than zero" << endl;
132   }
133   if (prop_element->FindElement("ct_factor"))
134     SetCtFactor( prop_element->FindElementValueAsNumber("ct_factor") );
135   if (prop_element->FindElement("cp_factor"))
136     SetCpFactor( prop_element->FindElementValueAsNumber("cp_factor") );
137
138   Type = ttPropeller;
139   RPM = 0;
140   vTorque.InitMatrix();
141   D4 = Diameter*Diameter*Diameter*Diameter;
142   D5 = D4*Diameter;
143   Pitch = MinPitch;
144
145   string property_name, base_property_name;
146   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNum);
147   property_name = base_property_name + "/advance-ratio";
148   PropertyManager->Tie( property_name.c_str(), &J );
149   property_name = base_property_name + "/blade-angle";
150   PropertyManager->Tie( property_name.c_str(), &Pitch );
151   property_name = base_property_name + "/thrust-coefficient";
152   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetThrustCoefficient );
153   property_name = base_property_name + "/propeller-rpm";
154   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetRPM );
155   property_name = base_property_name + "/helical-tip-Mach";
156   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetHelicalTipMach );
157   property_name = base_property_name + "/constant-speed-mode";
158   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetConstantSpeed,
159                       &FGPropeller::SetConstantSpeed );
160   property_name = base_property_name + "/prop-induced-velocity_fps";
161   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetInducedVelocity,
162                       &FGPropeller::SetInducedVelocity );
163
164   Debug(0);
165 }
166
167 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168
169 FGPropeller::~FGPropeller()
170 {
171   delete cThrust;
172   delete cPower;
173   delete CtMach;
174   delete CpMach;
175
176   Debug(1);
177 }
178
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 //
181 // We must be getting the aerodynamic velocity here, NOT the inertial velocity.
182 // We need the velocity with respect to the wind.
183 //
184 // Note that PowerAvailable is the excess power available after the drag of the
185 // propeller has been subtracted. At equilibrium, PowerAvailable will be zero -
186 // indicating that the propeller will not accelerate or decelerate.
187 // Remembering that Torque * omega = Power, we can derive the torque on the
188 // propeller and its acceleration to give a new RPM. The current RPM will be
189 // used to calculate thrust.
190 //
191 // Because RPM could be zero, we need to be creative about what RPM is stated as.
192
193 double FGPropeller::Calculate(double PowerAvailable)
194 {
195   double omega, alpha, beta;
196
197   double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
198   double rho = fdmex->GetAtmosphere()->GetDensity();
199   double RPS = RPM/60.0;
200
201   // Calculate helical tip Mach
202   double Area = 0.25*Diameter*Diameter*M_PI;
203   double Vtip = RPS * Diameter * M_PI;
204   HelicalTipMach = sqrt(Vtip*Vtip + Vel*Vel) / 
205                    fdmex->GetAtmosphere()->GetSoundSpeed(); 
206
207   if (RPS > 0.0) J = Vel / (Diameter * RPS); // Calculate J normally
208   else           J = Vel / Diameter;      
209
210   if (MaxPitch == MinPitch) {    // Fixed pitch prop
211          ThrustCoeff = cThrust->GetValue(J);
212   } else {                       // Variable pitch prop
213          ThrustCoeff = cThrust->GetValue(J, Pitch);
214   }
215  
216   // Apply optional scaling factor to Ct (default value = 1)
217   ThrustCoeff *= CtFactor;
218
219   // Apply optional Mach effects from CT_MACH table
220   if (CtMach) ThrustCoeff *= CtMach->GetValue(HelicalTipMach);
221
222   if (P_Factor > 0.0001) {
223     alpha = fdmex->GetAuxiliary()->Getalpha();
224     beta  = fdmex->GetAuxiliary()->Getbeta();
225     SetActingLocationY( GetLocationY() + P_Factor*alpha*Sense);
226     SetActingLocationZ( GetLocationZ() + P_Factor*beta*Sense);
227   }
228
229   Thrust = ThrustCoeff*RPS*RPS*D4*rho;
230
231   // From B. W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics"
232   // first edition, eqn. 6.15 (propeller analysis chapter).
233   Vinduced = 0.5 * (-Vel + sqrt(Vel*Vel + 2.0*Thrust/(rho*Area)));
234
235   omega = RPS*2.0*M_PI;
236
237   vFn(1) = Thrust;
238
239   // The Ixx value and rotation speed given below are for rotation about the
240   // natural axis of the engine. The transform takes place in the base class
241   // FGForce::GetBodyForces() function.
242
243   vH(eX) = Ixx*omega*Sense;
244   vH(eY) = 0.0;
245   vH(eZ) = 0.0;
246
247   if (omega > 0.0) ExcessTorque = PowerAvailable / omega;
248   else             ExcessTorque = PowerAvailable / 1.0;
249
250   RPM = (RPS + ((ExcessTorque / Ixx) / (2.0 * M_PI)) * deltaT) * 60.0;
251
252   if (RPM < 0.0) RPM = 0.0; // Engine won't turn backwards
253
254   // Transform Torque and momentum first, as PQR is used in this
255   // equation and cannot be transformed itself.
256   vMn = fdmex->GetPropagate()->GetPQR()*(Transform()*vH) + Transform()*vTorque;
257
258   return Thrust; // return thrust in pounds
259 }
260
261 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262
263 double FGPropeller::GetPowerRequired(void)
264 {
265   double cPReq, J;
266   double rho = fdmex->GetAtmosphere()->GetDensity();
267   double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
268   double RPS = RPM / 60.0;
269
270   if (RPS != 0.0) J = Vel / (Diameter * RPS);
271   else            J = Vel / Diameter; 
272
273   if (MaxPitch == MinPitch) {   // Fixed pitch prop
274     cPReq = cPower->GetValue(J);
275
276   } else {                      // Variable pitch prop
277
278     if (ConstantSpeed != 0) {   // Constant Speed Mode
279
280       // do normal calculation when propeller is neither feathered nor reversed
281       // Note:  This method of feathering and reversing was added to support the
282       //        turboprop model.  It's left here for backward compatablity, but
283       //        now feathering and reversing should be done in Manual Pitch Mode.
284       if (!Feathered) {
285         if (!Reversed) {
286
287           double rpmReq = MinRPM + (MaxRPM - MinRPM) * Advance;
288           double dRPM = rpmReq - RPM;
289           // The pitch of a variable propeller cannot be changed when the RPMs are
290           // too low - the oil pump does not work.
291           if (RPM > 200) Pitch -= dRPM * deltaT;
292           if (Pitch < MinPitch)       Pitch = MinPitch;
293           else if (Pitch > MaxPitch)  Pitch = MaxPitch;
294
295         } else { // Reversed propeller
296
297           // when reversed calculate propeller pitch depending on throttle lever position
298           // (beta range for taxing full reverse for braking)
299           double PitchReq = MinPitch - ( MinPitch - ReversePitch ) * Reverse_coef;
300           // The pitch of a variable propeller cannot be changed when the RPMs are
301           // too low - the oil pump does not work.
302           if (RPM > 200) Pitch += (PitchReq - Pitch) / 200;
303           if (RPM > MaxRPM) {
304             Pitch += (MaxRPM - RPM) / 50;
305             if (Pitch < ReversePitch) Pitch = ReversePitch;
306             else if (Pitch > MaxPitch)  Pitch = MaxPitch;
307           }
308         }
309
310       } else { // Feathered propeller
311                // ToDo: Make feathered and reverse settings done via FGKinemat
312         Pitch += (MaxPitch - Pitch) / 300; // just a guess (about 5 sec to fully feathered)
313       }
314
315     } else { // Manual Pitch Mode, pitch is controlled externally
316
317     }
318   
319     cPReq = cPower->GetValue(J, Pitch);
320   }
321
322   // Apply optional scaling factor to Cp (default value = 1)
323   cPReq *= CpFactor;
324
325   // Apply optional Mach effects from CP_MACH table
326   if (CpMach) cPReq *= CpMach->GetValue(HelicalTipMach);
327
328   if (RPS > 0.1) {
329     PowerRequired = cPReq*RPS*RPS*RPS*D5*rho;
330     vTorque(eX) = -Sense*PowerRequired / (RPS*2.0*M_PI);
331   } else {
332      // For a stationary prop we have to estimate torque first.
333      double CL = (90.0 - Pitch) / 20.0;
334      if (CL > 1.5) CL = 1.5;
335      double BladeArea = Diameter * Diameter / 32.0 * numBlades;
336      vTorque(eX) = -Sense*BladeArea*Diameter*Vel*Vel*rho*0.19*CL;
337      PowerRequired = vTorque(eX)*0.2*M_PI;
338   }
339
340   return PowerRequired;
341 }
342
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344
345 FGColumnVector3 FGPropeller::GetPFactor()
346 {
347   double px=0.0, py, pz;
348
349   py = Thrust * Sense * (GetActingLocationY() - GetLocationY()) / 12.0;
350   pz = Thrust * Sense * (GetActingLocationZ() - GetLocationZ()) / 12.0;
351
352   return FGColumnVector3(px, py, pz);
353 }
354
355 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356
357 string FGPropeller::GetThrusterLabels(int id, string delimeter)
358 {
359   std::ostringstream buf;
360
361   buf << Name << " Torque (engine " << id << ")" << delimeter
362       << Name << " PFactor Pitch (engine " << id << ")" << delimeter
363       << Name << " PFactor Yaw (engine " << id << ")" << delimeter
364       << Name << " Thrust (engine " << id << " in lbs)" << delimeter;
365   if (IsVPitch())
366     buf << Name << " Pitch (engine " << id << ")" << delimeter;
367   buf << Name << " RPM (engine " << id << ")";
368
369   return buf.str();
370 }
371
372 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373
374 string FGPropeller::GetThrusterValues(int id, string delimeter)
375 {
376   std::ostringstream buf;
377
378   FGColumnVector3 vPFactor = GetPFactor();
379   buf << vTorque(eX) << delimeter
380       << vPFactor(ePitch) << delimeter
381       << vPFactor(eYaw) << delimeter
382       << Thrust << delimeter;
383   if (IsVPitch())
384     buf << Pitch << delimeter;
385   buf << RPM;
386
387   return buf.str();
388 }
389
390 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 //    The bitmasked value choices are as follows:
392 //    unset: In this case (the default) JSBSim would only print
393 //       out the normally expected messages, essentially echoing
394 //       the config files as they are read. If the environment
395 //       variable is not set, debug_lvl is set to 1 internally
396 //    0: This requests JSBSim not to output any messages
397 //       whatsoever.
398 //    1: This value explicity requests the normal JSBSim
399 //       startup messages
400 //    2: This value asks for a message to be printed out when
401 //       a class is instantiated
402 //    4: When this value is set, a message is displayed when a
403 //       FGModel object executes its Run() method
404 //    8: When this value is set, various runtime state variables
405 //       are printed out periodically
406 //    16: When set various parameters are sanity checked and
407 //       a message is printed out when they go out of bounds
408
409 void FGPropeller::Debug(int from)
410 {
411   if (debug_lvl <= 0) return;
412
413   if (debug_lvl & 1) { // Standard console startup message output
414     if (from == 0) { // Constructor
415       cout << "\n    Propeller Name: " << Name << endl;
416       cout << "      IXX = " << Ixx << endl;
417       cout << "      Diameter = " << Diameter << " ft." << endl;
418       cout << "      Number of Blades  = " << numBlades << endl;
419       cout << "      Gear Ratio  = " << GearRatio << endl;
420       cout << "      Minimum Pitch  = " << MinPitch << endl;
421       cout << "      Maximum Pitch  = " << MaxPitch << endl;
422       cout << "      Minimum RPM  = " << MinRPM << endl;
423       cout << "      Maximum RPM  = " << MaxRPM << endl;
424 //      cout << "      Thrust Coefficient: " <<  endl;
425 //      cThrust->Print();
426 //      cout << "      Power Coefficient: " <<  endl;
427 //      cPower->Print();
428     }
429   }
430   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
431     if (from == 0) cout << "Instantiated: FGPropeller" << endl;
432     if (from == 1) cout << "Destroyed:    FGPropeller" << endl;
433   }
434   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
435   }
436   if (debug_lvl & 8 ) { // Runtime state variables
437   }
438   if (debug_lvl & 16) { // Sanity checking
439   }
440   if (debug_lvl & 64) {
441     if (from == 0) { // Constructor
442       cout << IdSrc << endl;
443       cout << IdHdr << endl;
444     }
445   }
446 }
447 }