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