1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGPropeller.cpp
6 Purpose: Encapsulates the propeller object
8 ------------- Copyright (C) 2000 Jon S. Berndt (jsb@hal-pc.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
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 General Public License for more
20 You should have received a copy of the GNU 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.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
31 --------------------------------------------------------------------------------
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGPropeller.h"
41 #include <models/FGPropagate.h>
42 #include <models/FGAtmosphere.h>
43 #include <models/FGAuxiliary.h>
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_PROPELLER;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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
59 FGPropeller::FGPropeller(FGFDMExec* exec, Element* prop_element, int num)
60 : FGThruster(exec, prop_element, num)
64 Element *table_element, *local_element;
66 FGPropertyManager* PropertyManager = exec->GetPropertyManager();
68 MaxPitch = MinPitch = P_Factor = Pitch = Advance = MinRPM = MaxRPM = 0.0;
69 Sense = 1; // default clockwise rotation
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);
102 cerr << "Unknown table type: " << name << " in propeller definition." << endl;
106 local_element = prop_element->GetParent()->FindElement("sense");
108 double Sense = local_element->GetDataAsNumber();
109 SetSense(fabs(Sense)/Sense);
111 local_element = prop_element->GetParent()->FindElement("p_factor");
113 P_Factor = local_element->GetDataAsNumber();
116 cerr << "P-Factor value in config file must be greater than zero" << endl;
121 vTorque.InitMatrix();
122 D4 = Diameter*Diameter*Diameter*Diameter;
125 char property_name[80];
126 snprintf(property_name, 80, "propulsion/engine[%d]/advance-ratio", EngineNum);
127 PropertyManager->Tie( property_name, &J );
128 snprintf(property_name, 80, "propulsion/engine[%d]/blade-angle", EngineNum);
129 PropertyManager->Tie( property_name, &Pitch );
130 snprintf(property_name, 80, "propulsion/engine[%d]/thrust-coefficient", EngineNum);
131 PropertyManager->Tie( property_name, this, &FGPropeller::GetThrustCoefficient );
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 FGPropeller::~FGPropeller()
140 if (cThrust) delete cThrust;
141 if (cPower) delete cPower;
143 char property_name[80];
144 snprintf(property_name, 80, "propulsion/engine[%d]/advance-ratio", EngineNum);
145 PropertyManager->Untie( property_name );
146 snprintf(property_name, 80, "propulsion/engine[%d]/blade-angle", EngineNum);
147 PropertyManager->Untie( property_name );
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 // We must be getting the aerodynamic velocity here, NOT the inertial velocity.
155 // We need the velocity with respect to the wind.
157 // Note that PowerAvailable is the excess power available after the drag of the
158 // propeller has been subtracted. At equilibrium, PowerAvailable will be zero -
159 // indicating that the propeller will not accelerate or decelerate.
160 // Remembering that Torque * omega = Power, we can derive the torque on the
161 // propeller and its acceleration to give a new RPM. The current RPM will be
162 // used to calculate thrust.
164 // Because RPM could be zero, we need to be creative about what RPM is stated as.
166 double FGPropeller::Calculate(double PowerAvailable)
168 double omega, alpha, beta;
170 double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
171 double rho = fdmex->GetAtmosphere()->GetDensity();
172 double RPS = RPM/60.0;
174 if (RPS > 0.00) J = Vel / (Diameter * RPS); // Calculate J normally
175 else J = 1000.0; // Set J to a high number
177 if (MaxPitch == MinPitch) ThrustCoeff = cThrust->GetValue(J);
178 else ThrustCoeff = cThrust->GetValue(J, Pitch);
180 if (P_Factor > 0.0001) {
181 alpha = fdmex->GetAuxiliary()->Getalpha();
182 beta = fdmex->GetAuxiliary()->Getbeta();
183 SetActingLocationY( GetLocationY() + P_Factor*alpha*Sense);
184 SetActingLocationZ( GetLocationZ() + P_Factor*beta*Sense);
187 Thrust = ThrustCoeff*RPS*RPS*D4*rho;
188 omega = RPS*2.0*M_PI;
192 // The Ixx value and rotation speed given below are for rotation about the
193 // natural axis of the engine. The transform takes place in the base class
194 // FGForce::GetBodyForces() function.
196 vH(eX) = Ixx*omega*Sense;
200 if (omega > 0.0) ExcessTorque = GearRatio * PowerAvailable / omega;
201 else ExcessTorque = GearRatio * PowerAvailable / 1.0;
203 RPM = (RPS + ((ExcessTorque / Ixx) / (2.0 * M_PI)) * deltaT) * 60.0;
205 if (RPM < 1.0) RPM = 0; // Engine friction stops rotation arbitrarily at 1 RPM.
207 vMn = fdmex->GetPropagate()->GetPQR()*vH + vTorque*Sense;
209 return Thrust; // return thrust in pounds
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 double FGPropeller::GetPowerRequired(void)
217 double rho = fdmex->GetAtmosphere()->GetDensity();
218 double RPS = RPM / 60.0;
220 if (RPS != 0) J = fdmex->GetAuxiliary()->GetAeroUVW(eU) / (Diameter * RPS);
221 else J = 1000.0; // Set J to a high number
223 if (MaxPitch == MinPitch) { // Fixed pitch prop
225 cPReq = cPower->GetValue(J);
226 } else { // Variable pitch prop
228 if (MaxRPM != MinRPM) { // fixed-speed prop
230 // do normal calculation when propeller is neither feathered nor reversed
234 double rpmReq = MinRPM + (MaxRPM - MinRPM) * Advance;
235 double dRPM = rpmReq - RPM;
236 // The pitch of a variable propeller cannot be changed when the RPMs are
237 // too low - the oil pump does not work.
238 if (RPM > 200) Pitch -= dRPM / 10;
240 if (Pitch < MinPitch) Pitch = MinPitch;
241 else if (Pitch > MaxPitch) Pitch = MaxPitch;
243 } else { // Reversed propeller
245 // when reversed calculate propeller pitch depending on throttle lever position
246 // (beta range for taxing full reverse for braking)
247 double PitchReq = MinPitch - ( MinPitch - ReversePitch ) * Reverse_coef;
248 // The pitch of a variable propeller cannot be changed when the RPMs are
249 // too low - the oil pump does not work.
250 if (RPM > 200) Pitch += (PitchReq - Pitch) / 200;
252 Pitch += (MaxRPM - RPM) / 50;
253 if (Pitch < ReversePitch) Pitch = ReversePitch;
254 else if (Pitch > MaxPitch) Pitch = MaxPitch;
258 } else { // Feathered propeller
259 // ToDo: Make feathered and reverse settings done via FGKinemat
260 Pitch += (MaxPitch - Pitch) / 300; // just a guess (about 5 sec to fully feathered)
263 } else { // Variable Speed Prop
264 Pitch = MinPitch + (MaxPitch - MinPitch) * Advance;
266 cPReq = cPower->GetValue(J, Pitch);
270 PowerRequired = cPReq*RPS*RPS*RPS*D5*rho;
271 vTorque(eX) = -Sense*PowerRequired / (RPS*2.0*M_PI);
277 return PowerRequired;
280 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 FGColumnVector3 FGPropeller::GetPFactor()
284 double px=0.0, py, pz;
286 py = Thrust * Sense * (GetActingLocationY() - GetLocationY()) / 12.0;
287 pz = Thrust * Sense * (GetActingLocationZ() - GetLocationZ()) / 12.0;
289 return FGColumnVector3(px, py, pz);
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 string FGPropeller::GetThrusterLabels(int id, string delimeter)
296 std::ostringstream buf;
298 buf << Name << "_Torque[" << id << "]" << delimeter
299 << Name << "_PFactor_Pitch[" << id << "]" << delimeter
300 << Name << "_PFactor_Yaw[" << id << "]" << delimeter
301 << Name << "_Thrust[" << id << "]" << delimeter;
303 buf << Name << "_Pitch[" << id << "]" << delimeter;
304 buf << Name << "_RPM[" << id << "]";
309 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 string FGPropeller::GetThrusterValues(int id, string delimeter)
313 std::ostringstream buf;
315 FGColumnVector3 vPFactor = GetPFactor();
316 buf << vTorque(eX) << delimeter
317 << vPFactor(ePitch) << delimeter
318 << vPFactor(eYaw) << delimeter
319 << Thrust << delimeter;
321 buf << Pitch << delimeter;
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 // The bitmasked value choices are as follows:
329 // unset: In this case (the default) JSBSim would only print
330 // out the normally expected messages, essentially echoing
331 // the config files as they are read. If the environment
332 // variable is not set, debug_lvl is set to 1 internally
333 // 0: This requests JSBSim not to output any messages
335 // 1: This value explicity requests the normal JSBSim
337 // 2: This value asks for a message to be printed out when
338 // a class is instantiated
339 // 4: When this value is set, a message is displayed when a
340 // FGModel object executes its Run() method
341 // 8: When this value is set, various runtime state variables
342 // are printed out periodically
343 // 16: When set various parameters are sanity checked and
344 // a message is printed out when they go out of bounds
346 void FGPropeller::Debug(int from)
348 if (debug_lvl <= 0) return;
350 if (debug_lvl & 1) { // Standard console startup message output
351 if (from == 0) { // Constructor
352 cout << "\n Propeller Name: " << Name << endl;
353 cout << " IXX = " << Ixx << endl;
354 cout << " Diameter = " << Diameter << " ft." << endl;
355 cout << " Number of Blades = " << numBlades << endl;
356 cout << " Gear Ratio = " << GearRatio << endl;
357 cout << " Minimum Pitch = " << MinPitch << endl;
358 cout << " Maximum Pitch = " << MaxPitch << endl;
359 cout << " Minimum RPM = " << MinRPM << endl;
360 cout << " Maximum RPM = " << MaxRPM << endl;
361 // cout << " Thrust Coefficient: " << endl;
363 // cout << " Power Coefficient: " << endl;
367 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
368 if (from == 0) cout << "Instantiated: FGPropeller" << endl;
369 if (from == 1) cout << "Destroyed: FGPropeller" << endl;
371 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
373 if (debug_lvl & 8 ) { // Runtime state variables
375 if (debug_lvl & 16) { // Sanity checking
377 if (debug_lvl & 64) {
378 if (from == 0) { // Constructor
379 cout << IdSrc << endl;
380 cout << IdHdr << endl;