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