]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGPropeller.cpp
Merge branch 'next' of http://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.33 2011/03/10 01:35:25 dpculp 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 // Remembering that Torque * omega = Power, we can derive the torque on the
189 // propeller and its acceleration to give a new RPM. The current RPM will be
190 // used to calculate thrust.
191 //
192 // Because RPM could be zero, we need to be creative about what RPM is stated as.
193
194 double FGPropeller::Calculate(double EnginePower)
195 {
196   double omega, alpha, beta, PowerAvailable;
197
198   double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
199   double rho = fdmex->GetAtmosphere()->GetDensity();
200   double RPS = RPM/60.0;
201
202   PowerAvailable = EnginePower - GetPowerRequired();
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) / 
208                    fdmex->GetAtmosphere()->GetSoundSpeed(); 
209
210   if (RPS > 0.0) J = Vel / (Diameter * RPS); // Calculate J normally
211   else           J = Vel / Diameter;      
212
213   if (MaxPitch == MinPitch) {    // Fixed pitch prop
214          ThrustCoeff = cThrust->GetValue(J);
215   } else {                       // Variable pitch prop
216          ThrustCoeff = cThrust->GetValue(J, Pitch);
217   }
218  
219   // Apply optional scaling factor to Ct (default value = 1)
220   ThrustCoeff *= CtFactor;
221
222   // Apply optional Mach effects from CT_MACH table
223   if (CtMach) ThrustCoeff *= CtMach->GetValue(HelicalTipMach);
224
225   if (P_Factor > 0.0001) {
226     alpha = fdmex->GetAuxiliary()->Getalpha();
227     beta  = fdmex->GetAuxiliary()->Getbeta();
228     SetActingLocationY( GetLocationY() + P_Factor*alpha*Sense);
229     SetActingLocationZ( GetLocationZ() + P_Factor*beta*Sense);
230   }
231
232   Thrust = ThrustCoeff*RPS*RPS*D4*rho;
233
234   // From B. W. McCormick, "Aerodynamics, Aeronautics, and Flight Mechanics"
235   // first edition, eqn. 6.15 (propeller analysis chapter).
236   Vinduced = 0.5 * (-Vel + sqrt(Vel*Vel + 2.0*Thrust/(rho*Area)));
237
238   omega = RPS*2.0*M_PI;
239
240   vFn(1) = Thrust;
241
242   // The Ixx value and rotation speed given below are for rotation about the
243   // natural axis of the engine. The transform takes place in the base class
244   // FGForce::GetBodyForces() function.
245
246   vH(eX) = Ixx*omega*Sense;
247   vH(eY) = 0.0;
248   vH(eZ) = 0.0;
249
250   if (omega > 0.0) ExcessTorque = PowerAvailable / omega;
251   else             ExcessTorque = PowerAvailable / 1.0;
252
253   RPM = (RPS + ((ExcessTorque / Ixx) / (2.0 * M_PI)) * deltaT) * 60.0;
254
255   if (RPM < 0.0) RPM = 0.0; // Engine won't turn backwards
256
257   // Transform Torque and momentum first, as PQR is used in this
258   // equation and cannot be transformed itself.
259   vMn = fdmex->GetPropagate()->GetPQR()*(Transform()*vH) + Transform()*vTorque;
260
261   return Thrust; // return thrust in pounds
262 }
263
264 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265
266 double FGPropeller::GetPowerRequired(void)
267 {
268   double cPReq, J;
269   double rho = fdmex->GetAtmosphere()->GetDensity();
270   double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
271   double RPS = RPM / 60.0;
272
273   if (RPS != 0.0) J = Vel / (Diameter * RPS);
274   else            J = Vel / Diameter; 
275
276   if (MaxPitch == MinPitch) {   // Fixed pitch prop
277     cPReq = cPower->GetValue(J);
278
279   } else {                      // Variable pitch prop
280
281     if (ConstantSpeed != 0) {   // Constant Speed Mode
282
283       // do normal calculation when propeller is neither feathered nor reversed
284       // Note:  This method of feathering and reversing was added to support the
285       //        turboprop model.  It's left here for backward compatablity, but
286       //        now feathering and reversing should be done in Manual Pitch Mode.
287       if (!Feathered) {
288         if (!Reversed) {
289
290           double rpmReq = MinRPM + (MaxRPM - MinRPM) * Advance;
291           double dRPM = rpmReq - RPM;
292           // The pitch of a variable propeller cannot be changed when the RPMs are
293           // too low - the oil pump does not work.
294           if (RPM > 200) Pitch -= dRPM * deltaT;
295           if (Pitch < MinPitch)       Pitch = MinPitch;
296           else if (Pitch > MaxPitch)  Pitch = MaxPitch;
297
298         } else { // Reversed propeller
299
300           // when reversed calculate propeller pitch depending on throttle lever position
301           // (beta range for taxing full reverse for braking)
302           double PitchReq = MinPitch - ( MinPitch - ReversePitch ) * Reverse_coef;
303           // The pitch of a variable propeller cannot be changed when the RPMs are
304           // too low - the oil pump does not work.
305           if (RPM > 200) Pitch += (PitchReq - Pitch) / 200;
306           if (RPM > MaxRPM) {
307             Pitch += (MaxRPM - RPM) / 50;
308             if (Pitch < ReversePitch) Pitch = ReversePitch;
309             else if (Pitch > MaxPitch)  Pitch = MaxPitch;
310           }
311         }
312
313       } else { // Feathered propeller
314                // ToDo: Make feathered and reverse settings done via FGKinemat
315         Pitch += (MaxPitch - Pitch) / 300; // just a guess (about 5 sec to fully feathered)
316       }
317
318     } else { // Manual Pitch Mode, pitch is controlled externally
319
320     }
321   
322     cPReq = cPower->GetValue(J, Pitch);
323   }
324
325   // Apply optional scaling factor to Cp (default value = 1)
326   cPReq *= CpFactor;
327
328   // Apply optional Mach effects from CP_MACH table
329   if (CpMach) cPReq *= CpMach->GetValue(HelicalTipMach);
330
331   if (RPS > 0.1) {
332     PowerRequired = cPReq*RPS*RPS*RPS*D5*rho;
333     vTorque(eX) = -Sense*PowerRequired / (RPS*2.0*M_PI);
334   } else {
335      // For a stationary prop we have to estimate torque first.
336      double CL = (90.0 - Pitch) / 20.0;
337      if (CL > 1.5) CL = 1.5;
338      double BladeArea = Diameter * Diameter / 32.0 * numBlades;
339      vTorque(eX) = -Sense*BladeArea*Diameter*Vel*Vel*rho*0.19*CL;
340      PowerRequired = fabs(vTorque(eX))*0.2*M_PI;
341   }
342
343   return PowerRequired;
344 }
345
346 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347
348 FGColumnVector3 FGPropeller::GetPFactor()
349 {
350   double px=0.0, py, pz;
351
352   py = Thrust * Sense * (GetActingLocationY() - GetLocationY()) / 12.0;
353   pz = Thrust * Sense * (GetActingLocationZ() - GetLocationZ()) / 12.0;
354
355   return FGColumnVector3(px, py, pz);
356 }
357
358 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359
360 string FGPropeller::GetThrusterLabels(int id, string delimeter)
361 {
362   std::ostringstream buf;
363
364   buf << Name << " Torque (engine " << id << ")" << delimeter
365       << Name << " PFactor Pitch (engine " << id << ")" << delimeter
366       << Name << " PFactor Yaw (engine " << id << ")" << delimeter
367       << Name << " Thrust (engine " << id << " in lbs)" << delimeter;
368   if (IsVPitch())
369     buf << Name << " Pitch (engine " << id << ")" << delimeter;
370   buf << Name << " RPM (engine " << id << ")";
371
372   return buf.str();
373 }
374
375 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376
377 string FGPropeller::GetThrusterValues(int id, string delimeter)
378 {
379   std::ostringstream buf;
380
381   FGColumnVector3 vPFactor = GetPFactor();
382   buf << vTorque(eX) << delimeter
383       << vPFactor(ePitch) << delimeter
384       << vPFactor(eYaw) << delimeter
385       << Thrust << delimeter;
386   if (IsVPitch())
387     buf << Pitch << delimeter;
388   buf << RPM;
389
390   return buf.str();
391 }
392
393 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 //    The bitmasked value choices are as follows:
395 //    unset: In this case (the default) JSBSim would only print
396 //       out the normally expected messages, essentially echoing
397 //       the config files as they are read. If the environment
398 //       variable is not set, debug_lvl is set to 1 internally
399 //    0: This requests JSBSim not to output any messages
400 //       whatsoever.
401 //    1: This value explicity requests the normal JSBSim
402 //       startup messages
403 //    2: This value asks for a message to be printed out when
404 //       a class is instantiated
405 //    4: When this value is set, a message is displayed when a
406 //       FGModel object executes its Run() method
407 //    8: When this value is set, various runtime state variables
408 //       are printed out periodically
409 //    16: When set various parameters are sanity checked and
410 //       a message is printed out when they go out of bounds
411
412 void FGPropeller::Debug(int from)
413 {
414   if (debug_lvl <= 0) return;
415
416   if (debug_lvl & 1) { // Standard console startup message output
417     if (from == 0) { // Constructor
418       cout << "\n    Propeller Name: " << Name << endl;
419       cout << "      IXX = " << Ixx << endl;
420       cout << "      Diameter = " << Diameter << " ft." << endl;
421       cout << "      Number of Blades  = " << numBlades << endl;
422       cout << "      Gear Ratio  = " << GearRatio << endl;
423       cout << "      Minimum Pitch  = " << MinPitch << endl;
424       cout << "      Maximum Pitch  = " << MaxPitch << endl;
425       cout << "      Minimum RPM  = " << MinRPM << endl;
426       cout << "      Maximum RPM  = " << MaxRPM << endl;
427 //      cout << "      Thrust Coefficient: " <<  endl;
428 //      cThrust->Print();
429 //      cout << "      Power Coefficient: " <<  endl;
430 //      cPower->Print();
431     }
432   }
433   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
434     if (from == 0) cout << "Instantiated: FGPropeller" << endl;
435     if (from == 1) cout << "Destroyed:    FGPropeller" << endl;
436   }
437   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
438   }
439   if (debug_lvl & 8 ) { // Runtime state variables
440   }
441   if (debug_lvl & 16) { // Sanity checking
442   }
443   if (debug_lvl & 64) {
444     if (from == 0) { // Constructor
445       cout << IdSrc << endl;
446       cout << IdHdr << endl;
447     }
448   }
449 }
450 }