1 // steam.cxx - Steam Gauge Calculations
3 // Copyright (C) 2000 Alexander Perry - alex.perry@ieee.org
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #if defined( FG_HAVE_NATIVE_SGI_COMPILERS )
27 # include <iostream.h>
32 #include <simgear/constants.h>
33 #include <simgear/math/sg_types.hxx>
34 #include <simgear/misc/props.hxx>
35 #include <Aircraft/aircraft.hxx>
36 #include <Main/bfi.hxx>
37 #include <NetworkOLK/features.hxx>
39 FG_USING_NAMESPACE(std);
41 #include "radiostack.hxx"
44 static bool isTied = false;
48 ////////////////////////////////////////////////////////////////////////
49 // Declare the functions that read the variables
50 ////////////////////////////////////////////////////////////////////////
52 // Anything that reads the BFI directly is not implemented at all!
55 double FGSteam::the_STATIC_inhg = 29.92;
56 double FGSteam::the_ALT_ft = 0.0; // Indicated altitude
57 double FGSteam::get_ALT_ft() { _CatchUp(); return the_ALT_ft; }
59 double FGSteam::the_ALT_datum_mb = 1013.0;
60 double FGSteam::get_ALT_datum_mb() { return the_ALT_datum_mb; }
62 void FGSteam::set_ALT_datum_mb ( double datum_mb ) {
63 the_ALT_datum_mb = datum_mb;
66 double FGSteam::get_ASI_kias() { return FGBFI::getAirspeed(); }
68 double FGSteam::the_VSI_case = 29.92;
69 double FGSteam::the_VSI_fps = 0.0;
70 double FGSteam::get_VSI_fps() { _CatchUp(); return the_VSI_fps; }
72 double FGSteam::the_VACUUM_inhg = 0.0;
73 double FGSteam::get_VACUUM_inhg() { _CatchUp(); return the_VACUUM_inhg; }
75 double FGSteam::the_MH_err = 0.0;
76 double FGSteam::the_MH_deg = 0.0;
77 double FGSteam::the_MH_degps = 0.0;
78 double FGSteam::get_MH_deg () { _CatchUp(); return the_MH_deg; }
80 double FGSteam::the_DG_err = 0.0;
81 double FGSteam::the_DG_deg = 0.0;
82 double FGSteam::the_DG_degps = 0.0;
83 double FGSteam::the_DG_inhg = 0.0;
84 double FGSteam::get_DG_deg () { _CatchUp(); return the_DG_deg; }
85 double FGSteam::get_DG_err () { _CatchUp(); return the_DG_err; }
87 void FGSteam::set_DG_err ( double approx_magvar ) {
88 the_DG_err = approx_magvar;
91 double FGSteam::the_TC_rad = 0.0;
92 double FGSteam::the_TC_std = 0.0;
93 double FGSteam::get_TC_rad () { _CatchUp(); return the_TC_rad; }
94 double FGSteam::get_TC_std () { _CatchUp(); return the_TC_std; }
97 ////////////////////////////////////////////////////////////////////////
98 // Recording the current time
99 ////////////////////////////////////////////////////////////////////////
102 int FGSteam::_UpdatesPending = 1000000; /* Forces filter to reset */
105 // FIXME: no need to use static
106 // functions any longer.
108 void FGSteam::update ( int timesteps )
112 fgTie("/steam/airspeed", FGSteam::get_ASI_kias);
113 fgTie("/steam/altitude", FGSteam::get_ALT_ft);
114 fgTie("/steam/altimeter-datum-mb",
115 FGSteam::get_ALT_datum_mb, FGSteam::set_ALT_datum_mb,
116 false); /* don't modify the value */
117 fgTie("/steam/turn-rate", FGSteam::get_TC_std);
118 fgTie("/steam/slip-skid", FGSteam::get_TC_rad);
119 fgTie("/steam/vertical-speed", FGSteam::get_VSI_fps);
120 fgTie("/steam/gyro-compass", FGSteam::get_DG_deg);
121 // fgTie("/steam/vor1", FGSteam::get_HackVOR1_deg);
122 // fgTie("/steam/vor2", FGSteam::get_HackVOR2_deg);
123 // fgTie("/steam/glidescope1", FGSteam::get_HackGS_deg);
124 fgTie("/steam/adf", FGSteam::get_HackADF_deg);
125 fgTie("/steam/gyro-compass-error",
126 FGSteam::get_DG_err, FGSteam::set_DG_err,
127 false); /* don't modify the value */
128 fgTie("/steam/mag-compass", FGSteam::get_MH_deg);
130 _UpdatesPending += timesteps;
134 /* tc should be (elapsed_time_between_updates / desired_smoothing_time) */
135 void FGSteam::set_lowpass ( double *outthe, double inthe, double tc )
139 { /* time went backwards; kill the filter */
142 { /* ignore mildly negative time */
146 { /* Normal mode of operation; fast approximation to exp(-tc) */
147 (*outthe) = (*outthe) * ( 1.0 - tc )
151 { /* Huge time step; assume filter has settled */
154 { /* Moderate time step; non linear response */
155 double keep = exp ( -tc );
156 // printf ( "ARP: Keep is %f\n", keep );
157 (*outthe) = (*outthe) * keep
158 + inthe * ( 1.0 - keep );
163 #define INHG_TO_MB 33.86388 /* Inches_of_mercury * INHG_TO_MB == millibars. */
165 // Convert air pressure to altitude by ICAO Standard Atmosphere
166 double pressInHgToAltFt(double p_inhg)
168 // Ref. Aviation Formulary, Ed Williams, www.best.com/~williams/avform.htm
169 const double P_0 = 29.92126; // Std. MSL pressure, inHg. (=1013.25 mb)
170 const double p_Tr = 0.2233609 * P_0; // Pressure at tropopause, same units.
171 const double h_Tr = 36089.24; // Alt of tropopause, ft. (=11.0 km)
173 if (p_inhg > p_Tr) // 0.0 to 11.0 km
174 return (1.0 - pow((p_inhg / P_0), 1.0 / 5.2558797)) / 6.8755856e-6;
175 return h_Tr + log10(p_inhg / p_Tr) / -4.806346e-5; // 11.0 to 20.0 km
176 // We could put more code for higher altitudes here.
180 // Convert altitude to air pressure by ICAO Standard Atmosphere
181 double altFtToPressInHg(double alt_ft)
183 // Ref. Aviation Formulary, Ed Williams, www.best.com/~williams/avform.htm
184 const double P_0 = 29.92126; // Std. MSL pressure, inHg. (=1013.25 mb)
185 const double p_Tr = 0.2233609 * P_0; // Pressure at tropopause, same units.
186 const double h_Tr = 36089.24; // Alt of tropopause, ft. (=11.0 km)
188 if (alt_ft < h_Tr) // 0.0 to 11.0 km
189 return P_0 * pow(1.0 - 6.8755856e-6 * alt_ft, 5.2558797);
190 return p_Tr * exp(-4.806346e-5 * (alt_ft - h_Tr)); // 11.0 to 20.0 km
191 // We could put more code for higher altitudes here.
196 ////////////////////////////////////////////////////////////////////////
197 // Here the fun really begins
198 ////////////////////////////////////////////////////////////////////////
201 void FGSteam::_CatchUp()
202 { if ( _UpdatesPending != 0 )
203 { double dt = _UpdatesPending * 1.0 /
204 fgGetInt("/sim/model-hz"); // FIXME: inefficient
205 double AccN, AccE, AccU;
207 double d, the_ENGINE_rpm;
210 /**************************
211 There is the possibility that this is the first call.
212 If this is the case, we will emit the feature registrations
213 just to be on the safe side. Doing it more than once will
214 waste CPU time but doesn't hurt anything really.
216 if ( _UpdatesPending > 999999 )
217 { FGFeature::register_int ( "Avionics/NAV1/Localizer", &NAV1_LOC );
218 FGFeature::register_double ( "Avionics/NAV1/Latitude", &NAV1_Lat );
219 FGFeature::register_double ( "Avionics/NAV1/Longitude", &NAV1_Lon );
220 FGFeature::register_double ( "Avionics/NAV1/Radial", &NAV1_Rad );
221 FGFeature::register_double ( "Avionics/NAV1/Altitude", &NAV1_Alt );
222 FGFeature::register_int ( "Avionics/NAV2/Localizer", &NAV2_LOC );
223 FGFeature::register_double ( "Avionics/NAV2/Latitude", &NAV2_Lat );
224 FGFeature::register_double ( "Avionics/NAV2/Longitude", &NAV2_Lon );
225 FGFeature::register_double ( "Avionics/NAV2/Radial", &NAV2_Rad );
226 FGFeature::register_double ( "Avionics/NAV2/Altitude", &NAV2_Alt );
227 FGFeature::register_double ( "Avionics/ADF/Latitude", &ADF_Lat );
228 FGFeature::register_double ( "Avionics/ADF/Longitude", &ADF_Lon );
232 /**************************
233 Someone has called our update function and
234 it turns out that we are running somewhat behind.
235 Here, we recalculate everything for a 'dt' second step.
238 /**************************
239 The ball responds to the acceleration vector in the body
240 frame, only the components perpendicular to the longitudinal
241 axis of the aircraft. This is only related to actual
242 side slip for a symmetrical aircraft which is not touching
243 the ground and not changing its attitude. Math simplifies
244 by assuming (for small angles) arctan(x)=x in radians.
245 Obvious failure mode is the absence of liquid in the
246 tube, which is there to damp the motion, so that instead
247 the ball will bounce around, hitting the tube ends.
248 More subtle flaw is having it not move or a travel limit
249 occasionally due to some dirt in the tube or on the ball.
251 // the_TC_rad = - ( FGBFI::getSideSlip () ); /* incorrect */
252 d = - current_aircraft.fdm_state->get_A_Z_pilot();
254 set_lowpass ( & the_TC_rad,
255 current_aircraft.fdm_state->get_A_Y_pilot () / d,
258 /**************************
259 The rate of turn indication is from an electric gyro.
260 We should have it spin up with the master switch.
261 It is mounted at a funny angle so that it detects
262 both rate of bank (i.e. rolling into and out of turns)
263 and the rate of turn (i.e. how fast heading is changing).
265 set_lowpass ( & the_TC_std,
266 current_aircraft.fdm_state->get_Phi_dot ()
267 * RAD_TO_DEG / 20.0 +
268 current_aircraft.fdm_state->get_Psi_dot ()
269 * RAD_TO_DEG / 3.0 , dt );
271 /**************************
272 We want to know the pilot accelerations,
273 to compute the magnetic compass errors.
275 AccN = current_aircraft.fdm_state->get_V_dot_north();
276 AccE = current_aircraft.fdm_state->get_V_dot_east();
277 AccU = current_aircraft.fdm_state->get_V_dot_down()
279 if ( fabs(the_TC_rad) > 0.2 )
280 { /* Massive sideslip jams it; it stops turning */
282 the_MH_err = FGBFI::getHeading () - the_MH_deg;
284 { double MagDip, MagVar, CosDip;
285 double FrcN, FrcE, FrcU, AccTot;
286 double EdgN, EdgE, EdgU;
287 double TrqN, TrqE, TrqU, Torque;
288 /* Find a force vector towards exact magnetic north */
289 MagVar = FGBFI::getMagVar() / RAD_TO_DEG;
290 MagDip = FGBFI::getMagDip() / RAD_TO_DEG;
291 CosDip = cos ( MagDip );
292 FrcN = CosDip * cos ( MagVar );
293 FrcE = CosDip * sin ( MagVar );
294 FrcU = sin ( MagDip );
295 /* Rotation occurs around acceleration axis,
296 but axis magnitude is irrelevant. So compute it. */
297 AccTot = AccN*AccN + AccE*AccE + AccU*AccU;
298 if ( AccTot > 1.0 ) AccTot = sqrt ( AccTot );
300 /* Force applies to north marking on compass card */
301 EdgN = cos ( the_MH_err / RAD_TO_DEG );
302 EdgE = sin ( the_MH_err / RAD_TO_DEG );
304 /* Apply the force to the edge to get torques */
305 TrqN = EdgE * FrcU - EdgU * FrcE;
306 TrqE = EdgU * FrcN - EdgN * FrcU;
307 TrqU = EdgN * FrcE - EdgE * FrcN;
308 /* Select the component parallel to the axis */
309 Torque = ( TrqN * AccN +
311 TrqU * AccU ) * 5.0 / AccTot;
312 /* The magnetic compass has angular momentum,
313 so we apply a torque to it and wait */
315 { the_MH_degps= the_MH_degps * (1.0 - dt) - Torque;
316 the_MH_err += dt * the_MH_degps;
318 if ( the_MH_err > 180.0 ) the_MH_err -= 360.0; else
319 if ( the_MH_err < -180.0 ) the_MH_err += 360.0;
320 the_MH_deg = FGBFI::getHeading () - the_MH_err;
323 /**************************
324 This is not actually correct, but provides a
325 scaling capability for the vacuum pump later on.
326 When we have a real engine model, we can ask it.
328 the_ENGINE_rpm = controls.get_throttle(0) * 26.0;
330 /**************************
331 First, we need to know what the static line is reporting,
332 which is a whole simulation area in itself. For now, we cheat.
333 We filter the actual value by one second to
334 account for the line impedance of the plumbing.
336 double static_inhg = altFtToPressInHg(FGBFI::getAltitude());
337 set_lowpass ( & the_STATIC_inhg, static_inhg, dt );
340 NO alternate static source error (student feature),
341 NO possibility of blockage (instructor feature),
342 NO slip-induced error, important for C172 for example.
345 /**************************
347 ICAO standard atmosphere MSL pressure is 1013.25 mb, and pressure
348 gradient is about 28 ft per mb at MSL increasing to about 32 at
349 5000 and 38 at 10000 ft.
350 Standard altimeters apply the subscale offset to the output altitude,
351 not to the input pressure; I don't know exactly what pressure gradient
352 they assume for this. I choose to make it accurate at low altitudes.
353 Remember, we are trying to simulate a real altimeter, not an ideal one.
355 set_lowpass ( & the_ALT_ft,
356 pressInHgToAltFt(the_STATIC_inhg) +
357 (the_ALT_datum_mb - 1013.25) * 28.0, /* accurate at low alt. */
358 dt * 10 ); /* smoothing time 0.1 s */
360 /**************************
361 The VSI case is a low-pass filter of the static line pressure.
362 The instrument reports the difference, scaled to approx ft.
363 NO option for student to break glass when static source fails.
364 NO capability for a fixed non-zero reading when level.
365 NO capability to have a scaling error of maybe a factor of two.
367 the_VSI_fps = ( the_VSI_case - the_STATIC_inhg )
368 * 10000.0; /* manual scaling factor */
369 set_lowpass ( & the_VSI_case, the_STATIC_inhg, dt/6.0 );
371 /**************************
372 The engine driven vacuum pump is directly attached
373 to the engine shaft, so each engine rotation pumps
374 a fixed volume. The amount of air in that volume
375 is determined by the vacuum line's internal pressure.
376 The instruments are essentially leaking air like
377 a fixed source impedance from atmospheric pressure.
378 The regulator provides a digital limit setting,
379 which is open circuit unless the pressure drop is big.
380 Thus, we can compute the vacuum line pressure directly.
381 We assume that there is negligible reservoir space.
382 NO failure of the pump supported (yet)
384 the_VACUUM_inhg = the_STATIC_inhg *
385 the_ENGINE_rpm / ( the_ENGINE_rpm + 10000.0 );
386 if ( the_VACUUM_inhg > 5.0 )
387 the_VACUUM_inhg = 5.0;
390 > I was merely going to do the engine rpm driven vacuum pump for both
391 > the AI and DG, have the gyros spin down down in power off descents,
392 > have it tumble when you exceed the usual pitch or bank limits,
393 > put in those insidious turning errors ... for now anyway.
395 if ( _UpdatesPending > 999999 )
396 the_DG_err = FGBFI::getMagVar();
397 the_DG_degps = 0.01; /* HACK! */
398 if (dt<1.0) the_DG_err += dt * the_DG_degps;
399 the_DG_deg = FGBFI::getHeading () - the_DG_err;
401 /**************************
402 Finished updates, now clear the timer
406 // cout << "0 Updates pending" << endl;
411 ////////////////////////////////////////////////////////////////////////
412 // Everything below is a transient hack; expect it to disappear
413 ////////////////////////////////////////////////////////////////////////
418 double FGSteam::get_HackGS_deg () {
419 if ( current_radiostack->get_nav1_inrange() &&
420 current_radiostack->get_nav1_has_gs() )
422 double x = current_radiostack->get_nav1_gs_dist();
423 double y = (FGBFI::getAltitude() - current_radiostack->get_nav1_elev())
425 double angle = atan2( y, x ) * RAD_TO_DEG;
426 return (current_radiostack->get_nav1_target_gs() - angle) * 5.0;
433 double FGSteam::get_HackVOR1_deg () {
436 if ( current_radiostack->get_nav1_inrange() ) {
437 r = current_radiostack->get_nav1_heading()
438 - current_radiostack->get_nav1_radial();
439 // cout << "Radial = " << current_radiostack->get_nav1_radial()
440 // << " Bearing = " << current_radiostack->get_nav1_heading()
443 if (r> 180.0) r-=360.0; else
444 if (r<-180.0) r+=360.0;
445 if ( fabs(r) > 90.0 )
446 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
447 // According to Robin Peel, the ILS is 4x more sensitive than a vor
448 if ( current_radiostack->get_nav1_loc() ) r *= 4.0;
457 double FGSteam::get_HackVOR2_deg () {
460 if ( current_radiostack->get_nav2_inrange() ) {
461 r = current_radiostack->get_nav2_heading()
462 - current_radiostack->get_nav2_radial();
463 // cout << "Radial = " << current_radiostack->get_nav1_radial()
464 // << " Bearing = " << current_radiostack->get_nav1_heading() << endl;
466 if (r> 180.0) r-=360.0; else
467 if (r<-180.0) r+=360.0;
468 if ( fabs(r) > 90.0 )
469 r = ( r<0.0 ? -r-180.0 : -r+180.0 );
479 double FGSteam::get_HackOBS1_deg () {
480 return current_radiostack->get_nav1_radial();
484 double FGSteam::get_HackOBS2_deg () {
485 return current_radiostack->get_nav2_radial();
489 double FGSteam::get_HackADF_deg () {
492 if ( current_radiostack->get_adf_inrange() ) {
493 r = current_radiostack->get_adf_heading() - FGBFI::getHeading();
495 // cout << "Radial = " << current_radiostack->get_adf_heading()
496 // << " Heading = " << FGBFI::getHeading() << endl;