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: FGAuxiliary.cpp,v 1.47 2011/03/29 11:49:27 jberndt Exp $";
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;
147 const double density = FDMExec->GetAtmosphere()->GetDensity();
148 const double soundspeed = FDMExec->GetAtmosphere()->GetSoundSpeed();
149 const double DistanceAGL = FDMExec->GetPropagate()->GetDistanceAGL();
150 const double wingspan = FDMExec->GetAircraft()->GetWingSpan();
151 const FGMatrix33& Tl2b = FDMExec->GetPropagate()->GetTl2b();
152 const FGMatrix33& Tb2l = FDMExec->GetPropagate()->GetTb2l();
154 const FGColumnVector3& vPQR = FDMExec->GetPropagate()->GetPQR();
155 const FGColumnVector3& vUVW = FDMExec->GetPropagate()->GetUVW();
156 const FGColumnVector3& vUVWdot = FDMExec->GetPropagate()->GetUVWdot();
157 const FGColumnVector3& vVel = FDMExec->GetPropagate()->GetVel();
159 p = FDMExec->GetAtmosphere()->GetPressure();
160 rhosl = FDMExec->GetAtmosphere()->GetDensitySL();
161 psl = FDMExec->GetAtmosphere()->GetPressureSL();
162 sat = FDMExec->GetAtmosphere()->GetTemperature();
166 double cTht = FDMExec->GetPropagate()->GetCosEuler(eTht);
167 double sTht = FDMExec->GetPropagate()->GetSinEuler(eTht);
168 double cPhi = FDMExec->GetPropagate()->GetCosEuler(ePhi);
169 double sPhi = FDMExec->GetPropagate()->GetSinEuler(ePhi);
171 vEulerRates(eTht) = vPQR(eQ)*cPhi - vPQR(eR)*sPhi;
173 vEulerRates(ePsi) = (vPQR(eQ)*sPhi + vPQR(eR)*cPhi)/cTht;
174 vEulerRates(ePhi) = vPQR(eP) + vEulerRates(ePsi)*sTht;
177 // Combine the wind speed with aircraft speed to obtain wind relative speed
178 FGColumnVector3 wind = Tl2b*FDMExec->GetAtmosphere()->GetTotalWindNED();
179 vAeroPQR = vPQR - FDMExec->GetAtmosphere()->GetTurbPQR();
180 vAeroUVW = vUVW - wind;
182 Vt = vAeroUVW.Magnitude();
184 alpha = beta = adot = bdot = 0;
185 double mUW = (vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eW)*vAeroUVW(eW));
188 if (vAeroUVW(eW) != 0.0)
189 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
190 if (vAeroUVW(eV) != 0.0)
191 beta = mUW > 0.0 ? atan2(vAeroUVW(eV), sqrt(mUW)) : 0.0;
194 if (vAeroUVW(eU) < 0.0) signU=-1;
197 adot = (vAeroUVW(eU)*vUVWdot(eW) - vAeroUVW(eW)*vUVWdot(eU))/mUW;
198 bdot = (signU*mUW*vUVWdot(eV)
199 - vAeroUVW(eV)*(vAeroUVW(eU)*vUVWdot(eU) + vAeroUVW(eW)*vUVWdot(eW)))/(Vt2*sqrt(mUW));
203 Re = Vt * FDMExec->GetAircraft()->Getcbar() / FDMExec->GetAtmosphere()->GetKinematicViscosity();
205 qbar = 0.5*density*Vt2;
206 qbarUW = 0.5*density*(mUW);
207 qbarUV = 0.5*density*(vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eV)*vAeroUVW(eV));
208 Mach = Vt / soundspeed;
209 MachU = vMachUVW(eU) = vAeroUVW(eU) / soundspeed;
210 vMachUVW(eV) = vAeroUVW(eV) / soundspeed;
211 vMachUVW(eW) = vAeroUVW(eW) / soundspeed;
215 Vground = sqrt( vVel(eNorth)*vVel(eNorth) + vVel(eEast)*vVel(eEast) );
217 psigt = atan2(vVel(eEast), vVel(eNorth));
218 if (psigt < 0.0) psigt += 2*M_PI;
219 gamma = atan2(-vVel(eDown), Vground);
221 tat = sat*(1 + 0.2*Mach*Mach); // Total Temperature, isentropic flow
222 tatc = RankineToCelsius(tat);
224 if (MachU < 1) { // Calculate total pressure assuming isentropic flow
225 pt = p*pow((1 + 0.2*MachU*MachU),3.5);
227 // Use Rayleigh pitot tube formula for normal shock in front of pitot tube
228 B = 5.76*MachU*MachU/(5.6*MachU*MachU - 0.8);
229 D = (2.8*MachU*MachU-0.4)*0.4167;
233 A = pow(((pt-p)/psl+1),0.28571);
235 vcas = sqrt(7*psl/rhosl*(A-1));
236 veas = sqrt(2*qbar/rhosl);
241 const double SLgravity = FDMExec->GetInertial()->SLgravity();
243 vPilotAccel.InitMatrix();
245 vAircraftAccel = FDMExec->GetAircraft()->GetBodyAccel();
246 // Nz is Acceleration in "g's", along normal axis (-Z body axis)
247 Nz = -vAircraftAccel(eZ)/SLgravity;
248 vToEyePt = FDMExec->GetMassBalance()->StructuralToBody(FDMExec->GetAircraft()->GetXYZep());
249 vPilotAccel = vAircraftAccel + FDMExec->GetPropagate()->GetPQRdot() * vToEyePt;
250 vPilotAccel += vPQR * (vPQR * vToEyePt);
252 // The line below handles low velocity (and on-ground) cases, basically
253 // representing the opposite of the force that the landing gear would
254 // exert on the ground (which is just the total weight). This eliminates
255 // any jitter that could be introduced by the landing gear. Theoretically,
256 // this branch could be eliminated, with a penalty of having a short
257 // transient at startup (lasting only a fraction of a second).
258 vPilotAccel = Tl2b * FGColumnVector3( 0.0, 0.0, -SLgravity );
259 Nz = -vPilotAccel(eZ)/SLgravity;
262 vPilotAccelN = vPilotAccel/SLgravity;
265 const FGLocation& vLocation = FDMExec->GetPropagate()->GetLocation();
266 const FGColumnVector3& vrpStructural = FDMExec->GetAircraft()->GetXYZvrp();
267 const FGColumnVector3 vrpBody = FDMExec->GetMassBalance()->StructuralToBody( vrpStructural );
268 const FGColumnVector3 vrpLocal = Tb2l * vrpBody;
269 vLocationVRP = vLocation.LocalToLocation( vrpLocal );
271 // Recompute some derived values now that we know the dependent parameters values ...
272 hoverbcg = DistanceAGL / wingspan;
274 FGColumnVector3 vMac = Tb2l*FDMExec->GetMassBalance()->StructuralToBody(FDMExec->GetAircraft()->GetXYZrp());
275 hoverbmac = (DistanceAGL + vMac(3)) / wingspan;
277 // when all model are executed,
278 // please calculate the distance from the initial point
280 CalculateRelativePosition();
287 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 // A positive headwind is blowing with you, a negative headwind is blowing against you.
290 // psi is the direction the wind is blowing *towards*.
291 // ToDo: should this simply be in the atmosphere class? Same with Get Crosswind.
293 double FGAuxiliary::GetHeadWind(void) const
297 psiw = FDMExec->GetAtmosphere()->GetWindPsi();
298 vw = FDMExec->GetAtmosphere()->GetTotalWindNED().Magnitude();
300 return vw*cos(psiw - FDMExec->GetPropagate()->GetEuler(ePsi));
303 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 // A positive crosswind is blowing towards the right (from teh perspective of the
306 // pilot). A negative crosswind is blowing towards the -Y direction (left).
307 // psi is the direction the wind is blowing *towards*.
309 double FGAuxiliary::GetCrossWind(void) const
313 psiw = FDMExec->GetAtmosphere()->GetWindPsi();
314 vw = FDMExec->GetAtmosphere()->GetTotalWindNED().Magnitude();
316 return vw*sin(psiw - FDMExec->GetPropagate()->GetEuler(ePsi));
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 double FGAuxiliary::GethVRP(void) const
323 return vLocationVRP.GetRadius() - FDMExec->GetPropagate()->GetSeaLevelRadius();
326 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 void FGAuxiliary::bind(void)
330 typedef double (FGAuxiliary::*PMF)(int) const;
331 typedef double (FGAuxiliary::*PF)(void) const;
332 PropertyManager->Tie("propulsion/tat-r", this, &FGAuxiliary::GetTotalTemperature);
333 PropertyManager->Tie("propulsion/tat-c", this, &FGAuxiliary::GetTAT_C);
334 PropertyManager->Tie("propulsion/pt-lbs_sqft", this, &FGAuxiliary::GetTotalPressure);
335 PropertyManager->Tie("velocities/vc-fps", this, &FGAuxiliary::GetVcalibratedFPS);
336 PropertyManager->Tie("velocities/vc-kts", this, &FGAuxiliary::GetVcalibratedKTS);
337 PropertyManager->Tie("velocities/ve-fps", this, &FGAuxiliary::GetVequivalentFPS);
338 PropertyManager->Tie("velocities/ve-kts", this, &FGAuxiliary::GetVequivalentKTS);
339 PropertyManager->Tie("velocities/machU", this, &FGAuxiliary::GetMachU);
340 PropertyManager->Tie("velocities/p-aero-rad_sec", this, eX, (PMF)&FGAuxiliary::GetAeroPQR);
341 PropertyManager->Tie("velocities/q-aero-rad_sec", this, eY, (PMF)&FGAuxiliary::GetAeroPQR);
342 PropertyManager->Tie("velocities/r-aero-rad_sec", this, eZ, (PMF)&FGAuxiliary::GetAeroPQR);
343 PropertyManager->Tie("velocities/phidot-rad_sec", this, ePhi, (PMF)&FGAuxiliary::GetEulerRates);
344 PropertyManager->Tie("velocities/thetadot-rad_sec", this, eTht, (PMF)&FGAuxiliary::GetEulerRates);
345 PropertyManager->Tie("velocities/psidot-rad_sec", this, ePsi, (PMF)&FGAuxiliary::GetEulerRates);
346 PropertyManager->Tie("velocities/u-aero-fps", this, eU, (PMF)&FGAuxiliary::GetAeroUVW);
347 PropertyManager->Tie("velocities/v-aero-fps", this, eV, (PMF)&FGAuxiliary::GetAeroUVW);
348 PropertyManager->Tie("velocities/w-aero-fps", this, eW, (PMF)&FGAuxiliary::GetAeroUVW);
349 PropertyManager->Tie("velocities/vt-fps", this, &FGAuxiliary::GetVt, &FGAuxiliary::SetVt, true);
350 PropertyManager->Tie("velocities/mach", this, &FGAuxiliary::GetMach, &FGAuxiliary::SetMach, true);
351 PropertyManager->Tie("velocities/vg-fps", this, &FGAuxiliary::GetVground);
352 PropertyManager->Tie("accelerations/a-pilot-x-ft_sec2", this, eX, (PMF)&FGAuxiliary::GetPilotAccel);
353 PropertyManager->Tie("accelerations/a-pilot-y-ft_sec2", this, eY, (PMF)&FGAuxiliary::GetPilotAccel);
354 PropertyManager->Tie("accelerations/a-pilot-z-ft_sec2", this, eZ, (PMF)&FGAuxiliary::GetPilotAccel);
355 PropertyManager->Tie("accelerations/n-pilot-x-norm", this, eX, (PMF)&FGAuxiliary::GetNpilot);
356 PropertyManager->Tie("accelerations/n-pilot-y-norm", this, eY, (PMF)&FGAuxiliary::GetNpilot);
357 PropertyManager->Tie("accelerations/n-pilot-z-norm", this, eZ, (PMF)&FGAuxiliary::GetNpilot);
358 PropertyManager->Tie("accelerations/Nz", this, &FGAuxiliary::GetNz);
359 /* PropertyManager->Tie("atmosphere/headwind-fps", this, &FGAuxiliary::GetHeadWind, true);
360 PropertyManager->Tie("atmosphere/crosswind-fps", this, &FGAuxiliary::GetCrossWind, true); */
361 PropertyManager->Tie("aero/alpha-rad", this, (PF)&FGAuxiliary::Getalpha, &FGAuxiliary::Setalpha, true);
362 PropertyManager->Tie("aero/beta-rad", this, (PF)&FGAuxiliary::Getbeta, &FGAuxiliary::Setbeta, true);
363 PropertyManager->Tie("aero/mag-beta-rad", this, (PF)&FGAuxiliary::GetMagBeta);
364 PropertyManager->Tie("aero/alpha-deg", this, inDegrees, (PMF)&FGAuxiliary::Getalpha);
365 PropertyManager->Tie("aero/beta-deg", this, inDegrees, (PMF)&FGAuxiliary::Getbeta);
366 PropertyManager->Tie("aero/mag-beta-deg", this, inDegrees, (PMF)&FGAuxiliary::GetMagBeta);
367 PropertyManager->Tie("aero/Re", this, &FGAuxiliary::GetReynoldsNumber);
368 PropertyManager->Tie("aero/qbar-psf", this, &FGAuxiliary::Getqbar, &FGAuxiliary::Setqbar, true);
369 PropertyManager->Tie("aero/qbarUW-psf", this, &FGAuxiliary::GetqbarUW, &FGAuxiliary::SetqbarUW, true);
370 PropertyManager->Tie("aero/qbarUV-psf", this, &FGAuxiliary::GetqbarUV, &FGAuxiliary::SetqbarUV, true);
371 PropertyManager->Tie("aero/alphadot-rad_sec", this, (PF)&FGAuxiliary::Getadot, &FGAuxiliary::Setadot, true);
372 PropertyManager->Tie("aero/betadot-rad_sec", this, (PF)&FGAuxiliary::Getbdot, &FGAuxiliary::Setbdot, true);
373 PropertyManager->Tie("aero/alphadot-deg_sec", this, inDegrees, (PMF)&FGAuxiliary::Getadot);
374 PropertyManager->Tie("aero/betadot-deg_sec", this, inDegrees, (PMF)&FGAuxiliary::Getbdot);
375 PropertyManager->Tie("aero/h_b-cg-ft", this, &FGAuxiliary::GetHOverBCG);
376 PropertyManager->Tie("aero/h_b-mac-ft", this, &FGAuxiliary::GetHOverBMAC);
377 PropertyManager->Tie("flight-path/gamma-rad", this, &FGAuxiliary::GetGamma, &FGAuxiliary::SetGamma);
378 PropertyManager->Tie("flight-path/psi-gt-rad", this, &FGAuxiliary::GetGroundTrack);
380 PropertyManager->Tie("position/distance-from-start-lon-mt", this, &FGAuxiliary::GetLongitudeRelativePosition);
381 PropertyManager->Tie("position/distance-from-start-lat-mt", this, &FGAuxiliary::GetLatitudeRelativePosition);
382 PropertyManager->Tie("position/distance-from-start-mag-mt", this, &FGAuxiliary::GetDistanceRelativePosition);
383 PropertyManager->Tie("position/vrp-gc-latitude_deg", &vLocationVRP, &FGLocation::GetLatitudeDeg);
384 PropertyManager->Tie("position/vrp-longitude_deg", &vLocationVRP, &FGLocation::GetLongitudeDeg);
385 PropertyManager->Tie("position/vrp-radius-ft", &vLocationVRP, &FGLocation::GetRadius);
388 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 void FGAuxiliary::CalculateRelativePosition(void)
392 const double earth_radius_mt = FDMExec->GetInertial()->GetRefRadius()*fttom;
393 lat_relative_position=(FDMExec->GetPropagate()->GetLatitude() - FDMExec->GetIC()->GetLatitudeDegIC() *degtorad)*earth_radius_mt;
394 lon_relative_position=(FDMExec->GetPropagate()->GetLongitude() - FDMExec->GetIC()->GetLongitudeDegIC()*degtorad)*earth_radius_mt;
395 relative_position = sqrt(lat_relative_position*lat_relative_position + lon_relative_position*lon_relative_position);
398 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 double FGAuxiliary::BadUnits(void) const
402 cerr << "Bad units" << endl; return 0.0;
405 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 // The bitmasked value choices are as follows:
407 // unset: In this case (the default) JSBSim would only print
408 // out the normally expected messages, essentially echoing
409 // the config files as they are read. If the environment
410 // variable is not set, debug_lvl is set to 1 internally
411 // 0: This requests JSBSim not to output any messages
413 // 1: This value explicity requests the normal JSBSim
415 // 2: This value asks for a message to be printed out when
416 // a class is instantiated
417 // 4: When this value is set, a message is displayed when a
418 // FGModel object executes its Run() method
419 // 8: When this value is set, various runtime state variables
420 // are printed out periodically
421 // 16: When set various parameters are sanity checked and
422 // a message is printed out when they go out of bounds
424 void FGAuxiliary::Debug(int from)
426 if (debug_lvl <= 0) return;
428 if (debug_lvl & 1) { // Standard console startup message output
429 if (from == 0) { // Constructor
433 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
434 if (from == 0) cout << "Instantiated: FGAuxiliary" << endl;
435 if (from == 1) cout << "Destroyed: FGAuxiliary" << endl;
437 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
439 if (debug_lvl & 8 ) { // Runtime state variables
441 if (debug_lvl & 16) { // Sanity checking
442 if (Mach > 100 || Mach < 0.00)
443 cout << "FGPropagate::Mach is out of bounds: " << Mach << endl;
444 if (qbar > 1e6 || qbar < 0.00)
445 cout << "FGPropagate::qbar is out of bounds: " << qbar << endl;
447 if (debug_lvl & 64) {
448 if (from == 0) { // Constructor
449 cout << IdSrc << endl;
450 cout << IdHdr << endl;
455 } // namespace JSBSim