1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGAuxiliary.cpp
4 Author: Tony Peden, Jon Berndt
6 Purpose: Calculates additional parameters needed by the visual system, etc.
9 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU Lesser General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 You should have received a copy of the GNU Lesser General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
25 Further information about the GNU Lesser General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class calculates various auxiliary parameters.
33 Anderson, John D. "Introduction to Flight", 3rd Edition, McGraw-Hill, 1989
36 --------------------------------------------------------------------------------
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43 #include "FGAuxiliary.h"
44 #include "FGAerodynamics.h"
45 #include "FGPropagate.h"
46 #include "FGAtmosphere.h"
47 #include "FGFDMExec.h"
48 #include "FGAircraft.h"
49 #include "FGInertial.h"
50 #include "FGExternalReactions.h"
51 #include "FGBuoyantForces.h"
52 #include "FGGroundReactions.h"
53 #include "FGPropulsion.h"
54 #include "FGMassBalance.h"
55 #include "input_output/FGPropertyManager.h"
62 static const char *IdSrc = "$Id$";
63 static const char *IdHdr = ID_AUXILIARY;
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 FGAuxiliary::FGAuxiliary(FGFDMExec* fdmex) : FGModel(fdmex)
73 vcas = veas = pt = tat = 0;
82 gamma = Vt = Vground = 0.0;
86 hoverbmac = hoverbcg = 0.0;
87 tatc = RankineToCelsius(tat);
89 vPilotAccel.InitMatrix();
90 vPilotAccelN.InitMatrix();
91 vToEyePt.InitMatrix();
92 vAeroPQR.InitMatrix();
93 vEulerRates.InitMatrix();
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 bool FGAuxiliary::InitModel(void)
104 if (!FGModel::InitModel()) return false;
106 vcas = veas = pt = tat = 0;
114 gamma = Vt = Vground = 0.0;
117 seconds_in_day = 0.0;
118 hoverbmac = hoverbcg = 0.0;
120 vPilotAccel.InitMatrix();
121 vPilotAccelN.InitMatrix();
122 vToEyePt.InitMatrix();
123 vAeroPQR.InitMatrix();
124 vEulerRates.InitMatrix();
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 FGAuxiliary::~FGAuxiliary()
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 bool FGAuxiliary::Run()
142 if (FGModel::Run()) return true; // return true if error returned from base class
143 if (FDMExec->Holding()) return false;
145 const FGColumnVector3& vPQR = Propagate->GetPQR();
146 const FGColumnVector3& vUVW = Propagate->GetUVW();
147 const FGColumnVector3& vUVWdot = Propagate->GetUVWdot();
148 const FGColumnVector3& vVel = Propagate->GetVel();
150 p = Atmosphere->GetPressure();
151 rhosl = Atmosphere->GetDensitySL();
152 psl = Atmosphere->GetPressureSL();
153 sat = Atmosphere->GetTemperature();
157 double cTht = Propagate->GetCosEuler(eTht);
158 double sTht = Propagate->GetSinEuler(eTht);
159 double cPhi = Propagate->GetCosEuler(ePhi);
160 double sPhi = Propagate->GetSinEuler(ePhi);
162 vEulerRates(eTht) = vPQR(eQ)*cPhi - vPQR(eR)*sPhi;
164 vEulerRates(ePsi) = (vPQR(eQ)*sPhi + vPQR(eR)*cPhi)/cTht;
165 vEulerRates(ePhi) = vPQR(eP) + vEulerRates(ePsi)*sTht;
168 // 12/16/2005, JSB: For ground handling purposes, at this time, let's ramp
169 // in the effects of wind from 10 fps to 30 fps when there is weight on the
170 // landing gear wheels.
172 if (GroundReactions->GetWOW() && vUVW(eU) < 10) {
175 } else if (GroundReactions->GetWOW() && vUVW(eU) < 30) {
176 double factor = (vUVW(eU) - 10.0)/20.0;
177 vAeroPQR = vPQR - factor*Atmosphere->GetTurbPQR();
178 vAeroUVW = vUVW - factor*Propagate->GetTl2b()*Atmosphere->GetTotalWindNED();
180 FGColumnVector3 wind = Propagate->GetTl2b()*Atmosphere->GetTotalWindNED();
181 vAeroPQR = vPQR - Atmosphere->GetTurbPQR();
182 vAeroUVW = vUVW - wind;
185 Vt = vAeroUVW.Magnitude();
187 if (vAeroUVW(eW) != 0.0)
188 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
189 if (vAeroUVW(eV) != 0.0)
190 beta = vAeroUVW(eU)*vAeroUVW(eU)+vAeroUVW(eW)*vAeroUVW(eW) > 0.0 ? atan2(vAeroUVW(eV),
191 sqrt(vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eW)*vAeroUVW(eW))) : 0.0;
193 double mUW = (vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eW)*vAeroUVW(eW));
195 if (vAeroUVW(eU) != 0.0)
196 signU = vAeroUVW(eU)/fabs(vAeroUVW(eU));
198 if ( (mUW == 0.0) || (Vt == 0.0) ) {
202 adot = (vAeroUVW(eU)*vUVWdot(eW) - vAeroUVW(eW)*vUVWdot(eU))/mUW;
203 bdot = (signU*mUW*vUVWdot(eV) - vAeroUVW(eV)*(vAeroUVW(eU)*vUVWdot(eU)
204 + vAeroUVW(eW)*vUVWdot(eW)))/(Vt*Vt*sqrt(mUW));
207 alpha = beta = adot = bdot = 0;
210 Re = Vt * Aircraft->Getcbar() / Atmosphere->GetKinematicViscosity();
212 qbar = 0.5*Atmosphere->GetDensity()*Vt*Vt;
213 qbarUW = 0.5*Atmosphere->GetDensity()*(vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eW)*vAeroUVW(eW));
214 qbarUV = 0.5*Atmosphere->GetDensity()*(vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eV)*vAeroUVW(eV));
215 Mach = Vt / Atmosphere->GetSoundSpeed();
216 MachU = vMachUVW(eU) = vAeroUVW(eU) / Atmosphere->GetSoundSpeed();
217 vMachUVW(eV) = vAeroUVW(eV) / Atmosphere->GetSoundSpeed();
218 vMachUVW(eW) = vAeroUVW(eW) / Atmosphere->GetSoundSpeed();
222 Vground = sqrt( vVel(eNorth)*vVel(eNorth) + vVel(eEast)*vVel(eEast) );
224 psigt = atan2(vVel(eEast), vVel(eNorth));
225 if (psigt < 0.0) psigt += 2*M_PI;
226 gamma = atan2(-vVel(eDown), Vground);
228 tat = sat*(1 + 0.2*Mach*Mach); // Total Temperature, isentropic flow
229 tatc = RankineToCelsius(tat);
231 if (MachU < 1) { // Calculate total pressure assuming isentropic flow
232 pt = p*pow((1 + 0.2*MachU*MachU),3.5);
234 // Use Rayleigh pitot tube formula for normal shock in front of pitot tube
235 B = 5.76*MachU*MachU/(5.6*MachU*MachU - 0.8);
236 D = (2.8*MachU*MachU-0.4)*0.4167;
240 A = pow(((pt-p)/psl+1),0.28571);
242 vcas = sqrt(7*psl/rhosl*(A-1));
243 veas = sqrt(2*qbar/rhosl);
248 vPilotAccel.InitMatrix();
250 vAircraftAccel = Aerodynamics->GetForces()
251 + Propulsion->GetForces()
252 + GroundReactions->GetForces()
253 + ExternalReactions->GetForces()
254 + BuoyantForces->GetForces();
256 vAircraftAccel /= MassBalance->GetMass();
257 // Nz is Acceleration in "g's", along normal axis (-Z body axis)
258 Nz = -vAircraftAccel(eZ)/Inertial->gravity();
259 vToEyePt = MassBalance->StructuralToBody(Aircraft->GetXYZep());
260 vPilotAccel = vAircraftAccel + Propagate->GetPQRdot() * vToEyePt;
261 vPilotAccel += vPQR * (vPQR * vToEyePt);
263 // The line below handles low velocity (and on-ground) cases, basically
264 // representing the opposite of the force that the landing gear would
265 // exert on the ground (which is just the total weight). This eliminates
266 // any jitter that could be introduced by the landing gear. Theoretically,
267 // this branch could be eliminated, with a penalty of having a short
268 // transient at startup (lasting only a fraction of a second).
269 vPilotAccel = Propagate->GetTl2b() * FGColumnVector3( 0.0, 0.0, -Inertial->gravity() );
270 Nz = -vPilotAccel(eZ)/Inertial->gravity();
273 vPilotAccelN = vPilotAccel/Inertial->gravity();
276 const FGLocation& vLocation = Propagate->GetLocation();
277 FGColumnVector3 vrpStructural = Aircraft->GetXYZvrp();
278 FGColumnVector3 vrpBody = MassBalance->StructuralToBody( vrpStructural );
279 FGColumnVector3 vrpLocal = Propagate->GetTb2l() * vrpBody;
280 vLocationVRP = vLocation.LocalToLocation( vrpLocal );
282 // Recompute some derived values now that we know the dependent parameters values ...
283 hoverbcg = Propagate->GetDistanceAGL() / Aircraft->GetWingSpan();
285 FGColumnVector3 vMac = Propagate->GetTb2l()*MassBalance->StructuralToBody(Aircraft->GetXYZrp());
286 hoverbmac = (Propagate->GetDistanceAGL() + vMac(3)) / Aircraft->GetWingSpan();
288 // when all model are executed,
289 // please calculate the distance from the initial point
291 CalculateRelativePosition();
296 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 // A positive headwind is blowing with you, a negative headwind is blowing against you.
299 // psi is the direction the wind is blowing *towards*.
301 double FGAuxiliary::GetHeadWind(void) const
305 psiw = Atmosphere->GetWindPsi();
306 vw = Atmosphere->GetTotalWindNED().Magnitude();
308 return vw*cos(psiw - Propagate->GetEuler(ePsi));
311 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 // A positive crosswind is blowing towards the right (from teh perspective of the
314 // pilot). A negative crosswind is blowing towards the -Y direction (left).
315 // psi is the direction the wind is blowing *towards*.
317 double FGAuxiliary::GetCrossWind(void) const
321 psiw = Atmosphere->GetWindPsi();
322 vw = Atmosphere->GetTotalWindNED().Magnitude();
324 return vw*sin(psiw - Propagate->GetEuler(ePsi));
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 void FGAuxiliary::bind(void)
331 typedef double (FGAuxiliary::*PMF)(int) const;
332 typedef double (FGAuxiliary::*PF)(void) const;
333 PropertyManager->Tie("propulsion/tat-r", this, &FGAuxiliary::GetTotalTemperature);
334 PropertyManager->Tie("propulsion/tat-c", this, &FGAuxiliary::GetTAT_C);
335 PropertyManager->Tie("propulsion/pt-lbs_sqft", this, &FGAuxiliary::GetTotalPressure);
336 PropertyManager->Tie("velocities/vc-fps", this, &FGAuxiliary::GetVcalibratedFPS);
337 PropertyManager->Tie("velocities/vc-kts", this, &FGAuxiliary::GetVcalibratedKTS);
338 PropertyManager->Tie("velocities/ve-fps", this, &FGAuxiliary::GetVequivalentFPS);
339 PropertyManager->Tie("velocities/ve-kts", this, &FGAuxiliary::GetVequivalentKTS);
340 PropertyManager->Tie("velocities/machU", this, &FGAuxiliary::GetMachU);
341 PropertyManager->Tie("velocities/p-aero-rad_sec", this, eX, (PMF)&FGAuxiliary::GetAeroPQR);
342 PropertyManager->Tie("velocities/q-aero-rad_sec", this, eY, (PMF)&FGAuxiliary::GetAeroPQR);
343 PropertyManager->Tie("velocities/r-aero-rad_sec", this, eZ, (PMF)&FGAuxiliary::GetAeroPQR);
344 PropertyManager->Tie("velocities/phidot-rad_sec", this, ePhi, (PMF)&FGAuxiliary::GetEulerRates);
345 PropertyManager->Tie("velocities/thetadot-rad_sec", this, eTht, (PMF)&FGAuxiliary::GetEulerRates);
346 PropertyManager->Tie("velocities/psidot-rad_sec", this, ePsi, (PMF)&FGAuxiliary::GetEulerRates);
347 PropertyManager->Tie("velocities/u-aero-fps", this, eU, (PMF)&FGAuxiliary::GetAeroUVW);
348 PropertyManager->Tie("velocities/v-aero-fps", this, eV, (PMF)&FGAuxiliary::GetAeroUVW);
349 PropertyManager->Tie("velocities/w-aero-fps", this, eW, (PMF)&FGAuxiliary::GetAeroUVW);
350 PropertyManager->Tie("velocities/vt-fps", this, &FGAuxiliary::GetVt, &FGAuxiliary::SetVt, true);
351 PropertyManager->Tie("velocities/mach", this, &FGAuxiliary::GetMach, &FGAuxiliary::SetMach, true);
352 PropertyManager->Tie("velocities/vg-fps", this, &FGAuxiliary::GetVground);
353 PropertyManager->Tie("accelerations/a-pilot-x-ft_sec2", this, eX, (PMF)&FGAuxiliary::GetPilotAccel);
354 PropertyManager->Tie("accelerations/a-pilot-y-ft_sec2", this, eY, (PMF)&FGAuxiliary::GetPilotAccel);
355 PropertyManager->Tie("accelerations/a-pilot-z-ft_sec2", this, eZ, (PMF)&FGAuxiliary::GetPilotAccel);
356 PropertyManager->Tie("accelerations/n-pilot-x-norm", this, eX, (PMF)&FGAuxiliary::GetNpilot);
357 PropertyManager->Tie("accelerations/n-pilot-y-norm", this, eY, (PMF)&FGAuxiliary::GetNpilot);
358 PropertyManager->Tie("accelerations/n-pilot-z-norm", this, eZ, (PMF)&FGAuxiliary::GetNpilot);
359 PropertyManager->Tie("accelerations/Nz", this, &FGAuxiliary::GetNz);
360 /* PropertyManager->Tie("atmosphere/headwind-fps", this, &FGAuxiliary::GetHeadWind, true);
361 PropertyManager->Tie("atmosphere/crosswind-fps", this, &FGAuxiliary::GetCrossWind, true); */
362 PropertyManager->Tie("aero/alpha-rad", this, (PF)&FGAuxiliary::Getalpha, &FGAuxiliary::Setalpha, true);
363 PropertyManager->Tie("aero/beta-rad", this, (PF)&FGAuxiliary::Getbeta, &FGAuxiliary::Setbeta, true);
364 PropertyManager->Tie("aero/mag-beta-rad", this, (PF)&FGAuxiliary::GetMagBeta);
365 PropertyManager->Tie("aero/alpha-deg", this, inDegrees, (PMF)&FGAuxiliary::Getalpha);
366 PropertyManager->Tie("aero/beta-deg", this, inDegrees, (PMF)&FGAuxiliary::Getbeta);
367 PropertyManager->Tie("aero/mag-beta-deg", this, inDegrees, (PMF)&FGAuxiliary::GetMagBeta);
368 PropertyManager->Tie("aero/Re", this, &FGAuxiliary::GetReynoldsNumber);
369 PropertyManager->Tie("aero/qbar-psf", this, &FGAuxiliary::Getqbar, &FGAuxiliary::Setqbar, true);
370 PropertyManager->Tie("aero/qbarUW-psf", this, &FGAuxiliary::GetqbarUW, &FGAuxiliary::SetqbarUW, true);
371 PropertyManager->Tie("aero/qbarUV-psf", this, &FGAuxiliary::GetqbarUV, &FGAuxiliary::SetqbarUV, true);
372 PropertyManager->Tie("aero/alphadot-rad_sec", this, (PF)&FGAuxiliary::Getadot, &FGAuxiliary::Setadot, true);
373 PropertyManager->Tie("aero/betadot-rad_sec", this, (PF)&FGAuxiliary::Getbdot, &FGAuxiliary::Setbdot, true);
374 PropertyManager->Tie("aero/alphadot-deg_sec", this, inDegrees, (PMF)&FGAuxiliary::Getadot);
375 PropertyManager->Tie("aero/betadot-deg_sec", this, inDegrees, (PMF)&FGAuxiliary::Getbdot);
376 PropertyManager->Tie("aero/h_b-cg-ft", this, &FGAuxiliary::GetHOverBCG);
377 PropertyManager->Tie("aero/h_b-mac-ft", this, &FGAuxiliary::GetHOverBMAC);
378 PropertyManager->Tie("flight-path/gamma-rad", this, &FGAuxiliary::GetGamma, &FGAuxiliary::SetGamma);
379 PropertyManager->Tie("flight-path/psi-gt-rad", this, &FGAuxiliary::GetGroundTrack);
381 PropertyManager->Tie("position/distance-from-start-lon-mt", this, &FGAuxiliary::GetLongitudeRelativePosition);
382 PropertyManager->Tie("position/distance-from-start-lat-mt", this, &FGAuxiliary::GetLatitudeRelativePosition);
383 PropertyManager->Tie("position/distance-from-start-mag-mt", this, &FGAuxiliary::GetDistanceRelativePosition);
384 PropertyManager->Tie("position/vrp-gc-latitude_deg", &vLocationVRP, &FGLocation::GetLatitudeDeg);
385 PropertyManager->Tie("position/vrp-longitude_deg", &vLocationVRP, &FGLocation::GetLongitudeDeg);
386 PropertyManager->Tie("position/vrp-radius-ft", &vLocationVRP, &FGLocation::GetRadius);
389 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 void FGAuxiliary::CalculateRelativePosition(void)
393 const double earth_radius_mt = Inertial->GetRefRadius()*fttom;
394 lat_relative_position=(FDMExec->GetPropagate()->GetLatitude() - FDMExec->GetIC()->GetLatitudeDegIC() *degtorad)*earth_radius_mt;
395 lon_relative_position=(FDMExec->GetPropagate()->GetLongitude() - FDMExec->GetIC()->GetLongitudeDegIC()*degtorad)*earth_radius_mt;
396 relative_position = sqrt(lat_relative_position*lat_relative_position + lon_relative_position*lon_relative_position);
399 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 double FGAuxiliary::BadUnits(void) const
403 cerr << "Bad units" << endl; return 0.0;
406 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 // The bitmasked value choices are as follows:
408 // unset: In this case (the default) JSBSim would only print
409 // out the normally expected messages, essentially echoing
410 // the config files as they are read. If the environment
411 // variable is not set, debug_lvl is set to 1 internally
412 // 0: This requests JSBSim not to output any messages
414 // 1: This value explicity requests the normal JSBSim
416 // 2: This value asks for a message to be printed out when
417 // a class is instantiated
418 // 4: When this value is set, a message is displayed when a
419 // FGModel object executes its Run() method
420 // 8: When this value is set, various runtime state variables
421 // are printed out periodically
422 // 16: When set various parameters are sanity checked and
423 // a message is printed out when they go out of bounds
425 void FGAuxiliary::Debug(int from)
427 if (debug_lvl <= 0) return;
429 if (debug_lvl & 1) { // Standard console startup message output
430 if (from == 0) { // Constructor
434 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
435 if (from == 0) cout << "Instantiated: FGAuxiliary" << endl;
436 if (from == 1) cout << "Destroyed: FGAuxiliary" << endl;
438 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
440 if (debug_lvl & 8 ) { // Runtime state variables
442 if (debug_lvl & 16) { // Sanity checking
443 if (Mach > 100 || Mach < 0.00)
444 cout << "FGPropagate::Mach is out of bounds: " << Mach << endl;
445 if (qbar > 1e6 || qbar < 0.00)
446 cout << "FGPropagate::qbar is out of bounds: " << qbar << endl;
448 if (debug_lvl & 64) {
449 if (from == 0) { // Constructor
450 cout << IdSrc << endl;
451 cout << IdHdr << endl;
456 } // namespace JSBSim