]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGPropeller.cpp
Merge branch 'ehofman/sound'
[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$";
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
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   if (prop_element->FindElement("reversepitch"))
97     ReversePitch = prop_element->FindElementValueAsNumber("reversepitch");
98   for (int i=0; i<2; i++) {
99     table_element = prop_element->FindNextElement("table");
100     name = table_element->GetAttributeValue("name");
101     if (name == "C_THRUST") {
102       cThrust = new FGTable(PropertyManager, table_element);
103     } else if (name == "C_POWER") {
104       cPower = new FGTable(PropertyManager, table_element);
105     } else {
106       cerr << "Unknown table type: " << name << " in propeller definition." << endl;
107     }
108   }
109
110   local_element = prop_element->GetParent()->FindElement("sense");
111   if (local_element) {
112     double Sense = local_element->GetDataAsNumber();
113     SetSense(fabs(Sense)/Sense);
114   }
115   local_element = prop_element->GetParent()->FindElement("p_factor");
116   if (local_element) {
117     P_Factor = local_element->GetDataAsNumber();
118   }
119   if (P_Factor < 0) {
120     cerr << "P-Factor value in config file must be greater than zero" << endl;
121   }
122   if (prop_element->FindElement("ct_factor"))
123     SetCtFactor( prop_element->FindElementValueAsNumber("ct_factor") );
124   if (prop_element->FindElement("cp_factor"))
125     SetCpFactor( prop_element->FindElementValueAsNumber("cp_factor") );
126
127   Type = ttPropeller;
128   RPM = 0;
129   vTorque.InitMatrix();
130   D4 = Diameter*Diameter*Diameter*Diameter;
131   D5 = D4*Diameter;
132
133   string property_name, base_property_name;
134   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNum);
135   property_name = base_property_name + "/advance-ratio";
136   PropertyManager->Tie( property_name.c_str(), &J );
137   property_name = base_property_name + "/blade-angle";
138   PropertyManager->Tie( property_name.c_str(), &Pitch );
139   property_name = base_property_name + "/thrust-coefficient";
140   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetThrustCoefficient );
141   property_name = base_property_name + "/propeller-rpm";
142   PropertyManager->Tie( property_name.c_str(), this, &FGPropeller::GetRPM );
143
144   Debug(0);
145 }
146
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148
149 FGPropeller::~FGPropeller()
150 {
151   delete cThrust;
152   delete cPower;
153
154   Debug(1);
155 }
156
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 //
159 // We must be getting the aerodynamic velocity here, NOT the inertial velocity.
160 // We need the velocity with respect to the wind.
161 //
162 // Note that PowerAvailable is the excess power available after the drag of the
163 // propeller has been subtracted. At equilibrium, PowerAvailable will be zero -
164 // indicating that the propeller will not accelerate or decelerate.
165 // Remembering that Torque * omega = Power, we can derive the torque on the
166 // propeller and its acceleration to give a new RPM. The current RPM will be
167 // used to calculate thrust.
168 //
169 // Because RPM could be zero, we need to be creative about what RPM is stated as.
170
171 double FGPropeller::Calculate(double PowerAvailable)
172 {
173   double omega, alpha, beta;
174
175   double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
176   double rho = fdmex->GetAtmosphere()->GetDensity();
177   double RPS = RPM/60.0;
178
179   if (RPS > 0.00) J = Vel / (Diameter * RPS); // Calculate J normally
180   else            J = 1000.0;                 // Set J to a high number
181
182   if (MaxPitch == MinPitch)  ThrustCoeff = cThrust->GetValue(J);
183   else                       ThrustCoeff = cThrust->GetValue(J, Pitch);
184   ThrustCoeff *= CtFactor;
185
186   if (P_Factor > 0.0001) {
187     alpha = fdmex->GetAuxiliary()->Getalpha();
188     beta  = fdmex->GetAuxiliary()->Getbeta();
189     SetActingLocationY( GetLocationY() + P_Factor*alpha*Sense);
190     SetActingLocationZ( GetLocationZ() + P_Factor*beta*Sense);
191   }
192
193   Thrust = ThrustCoeff*RPS*RPS*D4*rho;
194   omega = RPS*2.0*M_PI;
195
196   vFn(1) = Thrust;
197
198   // The Ixx value and rotation speed given below are for rotation about the
199   // natural axis of the engine. The transform takes place in the base class
200   // FGForce::GetBodyForces() function.
201
202   vH(eX) = Ixx*omega*Sense;
203   vH(eY) = 0.0;
204   vH(eZ) = 0.0;
205
206   if (omega > 0.0) ExcessTorque = GearRatio * PowerAvailable / omega;
207   else             ExcessTorque = GearRatio * PowerAvailable / 1.0;
208
209   RPM = (RPS + ((ExcessTorque / Ixx) / (2.0 * M_PI)) * deltaT) * 60.0;
210
211   if (RPM < 1.0) RPM = 0; // Engine friction stops rotation arbitrarily at 1 RPM.
212
213   // Transform Torque and momentum first, as PQR is used in this
214   // equation and cannot be transformed itself.
215   vMn = fdmex->GetPropagate()->GetPQR()*(Transform()*vH) + Transform()*vTorque;
216
217   return Thrust; // return thrust in pounds
218 }
219
220 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221
222 double FGPropeller::GetPowerRequired(void)
223 {
224   double cPReq, J;
225   double rho = fdmex->GetAtmosphere()->GetDensity();
226   double RPS = RPM / 60.0;
227
228   if (RPS != 0) J = fdmex->GetAuxiliary()->GetAeroUVW(eU) / (Diameter * RPS);
229   else          J = 1000.0; // Set J to a high number
230
231   if (MaxPitch == MinPitch) { // Fixed pitch prop
232     Pitch = MinPitch;
233     cPReq = cPower->GetValue(J);
234   } else {                      // Variable pitch prop
235
236     if (MaxRPM != MinRPM) {   // fixed-speed prop
237
238       // do normal calculation when propeller is neither feathered nor reversed
239       if (!Feathered) {
240         if (!Reversed) {
241
242           double rpmReq = MinRPM + (MaxRPM - MinRPM) * Advance;
243           double dRPM = rpmReq - RPM;
244           // The pitch of a variable propeller cannot be changed when the RPMs are
245           // too low - the oil pump does not work.
246           if (RPM > 200) Pitch -= dRPM * deltaT;
247           if (Pitch < MinPitch)       Pitch = MinPitch;
248           else if (Pitch > MaxPitch)  Pitch = MaxPitch;
249
250         } else { // Reversed propeller
251
252           // when reversed calculate propeller pitch depending on throttle lever position
253           // (beta range for taxing full reverse for braking)
254           double PitchReq = MinPitch - ( MinPitch - ReversePitch ) * Reverse_coef;
255           // The pitch of a variable propeller cannot be changed when the RPMs are
256           // too low - the oil pump does not work.
257           if (RPM > 200) Pitch += (PitchReq - Pitch) / 200;
258           if (RPM > MaxRPM) {
259             Pitch += (MaxRPM - RPM) / 50;
260             if (Pitch < ReversePitch) Pitch = ReversePitch;
261             else if (Pitch > MaxPitch)  Pitch = MaxPitch;
262           }
263         }
264
265       } else { // Feathered propeller
266                // ToDo: Make feathered and reverse settings done via FGKinemat
267         Pitch += (MaxPitch - Pitch) / 300; // just a guess (about 5 sec to fully feathered)
268       }
269
270     } else { // Variable Speed Prop
271       Pitch = MinPitch + (MaxPitch - MinPitch) * Advance;
272     }
273     cPReq = cPower->GetValue(J, Pitch);
274   }
275   cPReq *= CpFactor;
276
277   if (RPS > 0) {
278     PowerRequired = cPReq*RPS*RPS*RPS*D5*rho;
279     vTorque(eX) = -Sense*PowerRequired / (RPS*2.0*M_PI);
280   } else {
281     PowerRequired = 0.0;
282     vTorque(eX) = 0.0;
283   }
284
285   return PowerRequired;
286 }
287
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289
290 FGColumnVector3 FGPropeller::GetPFactor()
291 {
292   double px=0.0, py, pz;
293
294   py = Thrust * Sense * (GetActingLocationY() - GetLocationY()) / 12.0;
295   pz = Thrust * Sense * (GetActingLocationZ() - GetLocationZ()) / 12.0;
296
297   return FGColumnVector3(px, py, pz);
298 }
299
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301
302 string FGPropeller::GetThrusterLabels(int id, string delimeter)
303 {
304   std::ostringstream buf;
305
306   buf << Name << " Torque (engine " << id << ")" << delimeter
307       << Name << " PFactor Pitch (engine " << id << ")" << delimeter
308       << Name << " PFactor Yaw (engine " << id << ")" << delimeter
309       << Name << " Thrust (engine " << id << " in lbs)" << delimeter;
310   if (IsVPitch())
311     buf << Name << " Pitch (engine " << id << ")" << delimeter;
312   buf << Name << " RPM (engine " << id << ")";
313
314   return buf.str();
315 }
316
317 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318
319 string FGPropeller::GetThrusterValues(int id, string delimeter)
320 {
321   std::ostringstream buf;
322
323   FGColumnVector3 vPFactor = GetPFactor();
324   buf << vTorque(eX) << delimeter
325       << vPFactor(ePitch) << delimeter
326       << vPFactor(eYaw) << delimeter
327       << Thrust << delimeter;
328   if (IsVPitch())
329     buf << Pitch << delimeter;
330   buf << RPM;
331
332   return buf.str();
333 }
334
335 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 //    The bitmasked value choices are as follows:
337 //    unset: In this case (the default) JSBSim would only print
338 //       out the normally expected messages, essentially echoing
339 //       the config files as they are read. If the environment
340 //       variable is not set, debug_lvl is set to 1 internally
341 //    0: This requests JSBSim not to output any messages
342 //       whatsoever.
343 //    1: This value explicity requests the normal JSBSim
344 //       startup messages
345 //    2: This value asks for a message to be printed out when
346 //       a class is instantiated
347 //    4: When this value is set, a message is displayed when a
348 //       FGModel object executes its Run() method
349 //    8: When this value is set, various runtime state variables
350 //       are printed out periodically
351 //    16: When set various parameters are sanity checked and
352 //       a message is printed out when they go out of bounds
353
354 void FGPropeller::Debug(int from)
355 {
356   if (debug_lvl <= 0) return;
357
358   if (debug_lvl & 1) { // Standard console startup message output
359     if (from == 0) { // Constructor
360       cout << "\n    Propeller Name: " << Name << endl;
361       cout << "      IXX = " << Ixx << endl;
362       cout << "      Diameter = " << Diameter << " ft." << endl;
363       cout << "      Number of Blades  = " << numBlades << endl;
364       cout << "      Gear Ratio  = " << GearRatio << endl;
365       cout << "      Minimum Pitch  = " << MinPitch << endl;
366       cout << "      Maximum Pitch  = " << MaxPitch << endl;
367       cout << "      Minimum RPM  = " << MinRPM << endl;
368       cout << "      Maximum RPM  = " << MaxRPM << endl;
369 //      cout << "      Thrust Coefficient: " <<  endl;
370 //      cThrust->Print();
371 //      cout << "      Power Coefficient: " <<  endl;
372 //      cPower->Print();
373     }
374   }
375   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
376     if (from == 0) cout << "Instantiated: FGPropeller" << endl;
377     if (from == 1) cout << "Destroyed:    FGPropeller" << endl;
378   }
379   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
380   }
381   if (debug_lvl & 8 ) { // Runtime state variables
382   }
383   if (debug_lvl & 16) { // Sanity checking
384   }
385   if (debug_lvl & 64) {
386     if (from == 0) { // Constructor
387       cout << IdSrc << endl;
388       cout << IdHdr << endl;
389     }
390   }
391 }
392 }