]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGPropeller.cpp
Make yasim accept the launchbar and hook properties. They are not tied to anything...
[flightgear.git] / src / FDM / JSBSim / 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 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 General Public License for more
18  details.
19
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.
23
24  Further information about the GNU 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 "FGPropagate.h"
42 #include "FGAtmosphere.h"
43 #include "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, FGConfigFile* Prop_cfg, int num) : FGThruster(exec)
60 {
61   string token;
62   int rows, cols;
63
64   MaxPitch = MinPitch = P_Factor = Sense = Pitch = Advance = 0.0;
65   GearRatio = 1.0;
66
67   Name = Prop_cfg->GetValue("NAME");
68   Prop_cfg->GetNextConfigLine();
69   while (Prop_cfg->GetValue() != string("/FG_PROPELLER")) {
70     *Prop_cfg >> token;
71     if (token == "IXX") {
72       *Prop_cfg >> Ixx;
73     } else if (token == "DIAMETER") {
74       *Prop_cfg >> Diameter;
75       Diameter /= 12.0;
76     } else if (token == "NUMBLADES") {
77       *Prop_cfg >> numBlades;
78     } else if (token == "GEARRATIO") {
79       *Prop_cfg >> GearRatio;
80     } else if (token == "MINPITCH") {
81       *Prop_cfg >> MinPitch;
82     } else if (token == "MAXPITCH") {
83       *Prop_cfg >> MaxPitch;
84     } else if (token == "MINRPM") {
85       *Prop_cfg >> MinRPM;
86     } else if (token == "MAXRPM") {
87       *Prop_cfg >> MaxRPM;
88     } else if (token == "C_THRUST") {
89       *Prop_cfg >> rows >> cols;
90       if (cols == 1) cThrust = new FGTable(rows);
91       else           cThrust = new FGTable(rows, cols);
92       *cThrust << *Prop_cfg;
93     } else if (token == "C_POWER") {
94       *Prop_cfg >> rows >> cols;
95       if (cols == 1) cPower = new FGTable(rows);
96       else           cPower = new FGTable(rows, cols);
97       *cPower << *Prop_cfg;
98     } else if (token == "EOF") {
99       cerr << "      End of file reached" <<  endl;
100       break;
101     } else {
102       cerr << "Unhandled token in Propeller config file: " << token << endl;
103     }
104   }
105
106   Type = ttPropeller;
107   RPM = 0;
108   vTorque.InitMatrix();
109
110   char property_name[80];
111   snprintf(property_name, 80, "propulsion/c-thrust[%u]", EngineNum);
112   PropertyManager->Tie( property_name, &ThrustCoeff );
113
114   Debug(0);
115 }
116
117 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118
119 FGPropeller::~FGPropeller()
120 {
121   if (cThrust)    delete cThrust;
122   if (cPower)     delete cPower;
123
124   char property_name[80];
125   snprintf(property_name, 80, "propulsion/c-thrust[%u]", EngineNum);
126   PropertyManager->Untie( property_name );
127
128   Debug(1);
129 }
130
131 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 //
133 // We must be getting the aerodynamic velocity here, NOT the inertial velocity.
134 // We need the velocity with respect to the wind.
135 //
136 // Note that PowerAvailable is the excess power available after the drag of the
137 // propeller has been subtracted. At equilibrium, PowerAvailable will be zero -
138 // indicating that the propeller will not accelerate or decelerate.
139 // Remembering that Torque * omega = Power, we can derive the torque on the
140 // propeller and its acceleration to give a new RPM. The current RPM will be
141 // used to calculate thrust.
142 //
143 // Because RPM could be zero, we need to be creative about what RPM is stated as.
144
145 double FGPropeller::Calculate(double PowerAvailable)
146 {
147   double J, omega;
148   double Vel = fdmex->GetAuxiliary()->GetAeroUVW(eU);
149   double rho = fdmex->GetAtmosphere()->GetDensity();
150   double RPS = RPM/60.0;
151   double alpha, beta;
152
153   if (RPM > 0.10) {
154     J = Vel / (Diameter * RPS);
155   } else {
156     J = 0.0;
157   }
158
159   if (MaxPitch == MinPitch) { // Fixed pitch prop
160     ThrustCoeff = cThrust->GetValue(J);
161   } else {                    // Variable pitch prop
162     ThrustCoeff = cThrust->GetValue(J, Pitch);
163   }
164
165   if (P_Factor > 0.0001) {
166     alpha = fdmex->GetAuxiliary()->Getalpha();
167     beta  = fdmex->GetAuxiliary()->Getbeta();
168     SetActingLocationY( GetLocationY() + P_Factor*alpha*Sense);
169     SetActingLocationZ( GetLocationZ() + P_Factor*beta*Sense);
170   } else if (P_Factor < 0.000) {
171     cerr << "P-Factor value in config file must be greater than zero" << endl;
172   }
173
174   Thrust = ThrustCoeff*RPS*RPS*Diameter*Diameter*Diameter*Diameter*rho;
175   omega = RPS*2.0*M_PI;
176
177   vFn(1) = Thrust;
178
179   // The Ixx value and rotation speed given below are for rotation about the
180   // natural axis of the engine. The transform takes place in the base class
181   // FGForce::GetBodyForces() function.
182
183   vH(eX) = Ixx*omega*Sense;
184   vH(eY) = 0.0;
185   vH(eZ) = 0.0;
186
187   if (omega <= 5) omega = 1.0;
188
189   ExcessTorque = PowerAvailable / omega * GearRatio;
190   RPM = (RPS + ((ExcessTorque / Ixx) / (2.0 * M_PI)) * deltaT) * 60.0;
191
192         // The friction from the engine should
193         // stop it somewhere; I chose an
194         // arbitrary point.
195   if (RPM < 5.0)
196     RPM = 0;
197
198   vMn = fdmex->GetPropagate()->GetPQR()*vH + vTorque*Sense;
199
200   return Thrust; // return thrust in pounds
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205 double FGPropeller::GetPowerRequired(void)
206 {
207   if (RPM <= 0.10) return 0.0; // If the prop ain't turnin', the fuel ain't burnin'.
208
209   double cPReq, RPS = RPM / 60.0;
210
211   double J = fdmex->GetAuxiliary()->GetAeroUVW(eU) / (Diameter * RPS);
212   double rho = fdmex->GetAtmosphere()->GetDensity();
213
214   if (MaxPitch == MinPitch) { // Fixed pitch prop
215     Pitch = MinPitch;
216     cPReq = cPower->GetValue(J);
217   } else {                    // Variable pitch prop
218
219     if (MaxRPM != MinRPM) {   // fixed-speed prop
220       double rpmReq = MinRPM + (MaxRPM - MinRPM) * Advance;
221       double dRPM = rpmReq - RPM;
222
223       Pitch -= dRPM / 10;
224
225       if (Pitch < MinPitch)       Pitch = MinPitch;
226       else if (Pitch > MaxPitch)  Pitch = MaxPitch;
227
228     } else {
229       Pitch = MinPitch + (MaxPitch - MinPitch) * Advance;
230     }
231     cPReq = cPower->GetValue(J, Pitch);
232   }
233
234   PowerRequired = cPReq*RPS*RPS*RPS*Diameter*Diameter*Diameter*Diameter
235                                                        *Diameter*rho;
236   vTorque(eX) = -Sense*PowerRequired / (RPS*2.0*M_PI);
237
238   return PowerRequired;
239 }
240
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242
243 FGColumnVector3 FGPropeller::GetPFactor()
244 {
245   double px=0.0, py, pz;
246
247   py = Thrust * Sense * (GetActingLocationY() - GetLocationY()) / 12.0;
248   pz = Thrust * Sense * (GetActingLocationZ() - GetLocationZ()) / 12.0;
249
250   return FGColumnVector3(px, py, pz);
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254
255 string FGPropeller::GetThrusterLabels(int id, string delimeter)
256 {
257   std::ostringstream buf;
258
259   buf << Name << "_Torque[" << id << "]" << delimeter
260       << Name << "_PFactor_Pitch[" << id << "]" << delimeter
261       << Name << "_PFactor_Yaw[" << id << "]" << delimeter
262       << Name << "_Thrust[" << id << "]" << delimeter;
263   if (IsVPitch())
264     buf << Name << "_Pitch[" << id << "]" << delimeter;
265   buf << Name << "_RPM[" << id << "]";
266
267   return buf.str();
268 }
269
270 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271
272 string FGPropeller::GetThrusterValues(int id, string delimeter)
273 {
274   std::ostringstream buf;
275
276   FGColumnVector3 vPFactor = GetPFactor();
277   buf << vTorque(eX) << delimeter
278       << vPFactor(ePitch) << delimeter
279       << vPFactor(eYaw) << delimeter
280       << Thrust << delimeter;
281   if (IsVPitch())
282     buf << Pitch << delimeter;
283   buf << RPM;
284
285   return buf.str();
286 }
287
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 //    The bitmasked value choices are as follows:
290 //    unset: In this case (the default) JSBSim would only print
291 //       out the normally expected messages, essentially echoing
292 //       the config files as they are read. If the environment
293 //       variable is not set, debug_lvl is set to 1 internally
294 //    0: This requests JSBSim not to output any messages
295 //       whatsoever.
296 //    1: This value explicity requests the normal JSBSim
297 //       startup messages
298 //    2: This value asks for a message to be printed out when
299 //       a class is instantiated
300 //    4: When this value is set, a message is displayed when a
301 //       FGModel object executes its Run() method
302 //    8: When this value is set, various runtime state variables
303 //       are printed out periodically
304 //    16: When set various parameters are sanity checked and
305 //       a message is printed out when they go out of bounds
306
307 void FGPropeller::Debug(int from)
308 {
309   if (debug_lvl <= 0) return;
310
311   if (debug_lvl & 1) { // Standard console startup message output
312     if (from == 0) { // Constructor
313       cout << "\n    Propeller Name: " << Name << endl;
314       cout << "      IXX = " << Ixx << endl;
315       cout << "      Diameter = " << Diameter << " ft." << endl;
316       cout << "      Number of Blades  = " << numBlades << endl;
317       cout << "      Minimum Pitch  = " << MinPitch << endl;
318       cout << "      Maximum Pitch  = " << MaxPitch << endl;
319       cout << "      Thrust Coefficient: " <<  endl;
320       cThrust->Print();
321       cout << "      Power Coefficient: " <<  endl;
322       cPower->Print();
323     }
324   }
325   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
326     if (from == 0) cout << "Instantiated: FGPropeller" << endl;
327     if (from == 1) cout << "Destroyed:    FGPropeller" << endl;
328   }
329   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
330   }
331   if (debug_lvl & 8 ) { // Runtime state variables
332   }
333   if (debug_lvl & 16) { // Sanity checking
334   }
335   if (debug_lvl & 64) {
336     if (from == 0) { // Constructor
337       cout << IdSrc << endl;
338       cout << IdHdr << endl;
339     }
340   }
341 }
342 }