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