]> git.mxchange.org Git - flightgear.git/blob - src/FDM/flight.cxx
Bugfix: no automatic runway selection with --parkpos=
[flightgear.git] / src / FDM / flight.cxx
1 // flight.cxx -- a general interface to the various flight models
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include "flight.hxx"
28
29 #include <simgear/constants.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/math/SGMath.hxx>
32 #include <simgear/timing/timestamp.hxx>
33
34 #include <Scenery/scenery.hxx>
35 #include <Main/globals.hxx>
36 #include <Main/fg_props.hxx>
37 #include <FDM/groundcache.hxx>
38
39
40 static inline void assign(double* ptr, const SGVec3d& vec)
41 {
42   ptr[0] = vec[0];
43   ptr[1] = vec[1];
44   ptr[2] = vec[2];
45 }
46
47 // Constructor
48 FGInterface::FGInterface()
49 {
50     _setup();
51 }
52
53 FGInterface::FGInterface( double dt )
54 {
55     _setup();
56 }
57
58 // Destructor
59 FGInterface::~FGInterface() {
60     // unbind();                   // FIXME: should be called explicitly
61 }
62
63 int
64 FGInterface::_calc_multiloop (double dt)
65 {
66   // Since some time the simulation time increments we get here are
67   // already a multiple of the basic update freqency.
68   // So, there is no need to do our own multiloop rounding with all bad
69   // roundoff problems when we already have nearly accurate values.
70   // Only the speedup thing must be still handled here
71   int hz = fgGetInt("/sim/model-hz");
72   int multiloop = SGMiscd::roundToInt(dt*hz);
73   int speedup = fgGetInt("/sim/speed-up");
74   return multiloop * speedup;
75 }
76
77
78 /**
79  * Set default values for the state of the FDM.
80  *
81  * This method is invoked by the constructors.
82  */
83 void
84 FGInterface::_setup ()
85 {
86     inited = false;
87     bound = false;
88
89     d_cg_rp_body_v = SGVec3d::zeros();
90     v_dot_local_v = SGVec3d::zeros();
91     v_dot_body_v = SGVec3d::zeros();
92     a_cg_body_v = SGVec3d::zeros();
93     a_pilot_body_v = SGVec3d::zeros();
94     n_cg_body_v = SGVec3d::zeros();
95     v_local_v = SGVec3d::zeros();
96     v_local_rel_ground_v = SGVec3d::zeros();
97     v_local_airmass_v = SGVec3d::zeros();
98     v_wind_body_v = SGVec3d::zeros();
99     omega_body_v = SGVec3d::zeros();
100     euler_rates_v = SGVec3d::zeros();
101     geocentric_rates_v = SGVec3d::zeros();
102     geodetic_position_v = SGGeod::fromRadM(0, 0, 0);
103     cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
104     geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
105     euler_angles_v = SGVec3d::zeros();
106     
107     nlf=0;
108     v_rel_wind=v_true_kts=0;
109     v_ground_speed=v_equiv_kts=0;
110     v_calibrated_kts=0;
111     alpha=beta=0;
112     gamma_vert_rad=0;
113     density=mach_number=0;
114     static_pressure=total_pressure=0;
115     dynamic_pressure=0;
116     static_temperature=total_temperature=0;
117     sea_level_radius=earth_position_angle=0;
118     runway_altitude=0;
119     climb_rate=0;
120     altitude_agl=0;
121     track=0;
122 }
123
124 void
125 FGInterface::init () {}
126
127 /**
128  * Initialize the state of the FDM.
129  *
130  * Subclasses of FGInterface may do their own, additional initialization,
131  * but there is some that is common to all.  Normally, they should call
132  * this before they begin their own init to make sure the basic structures
133  * are set up properly.
134  */
135 void
136 FGInterface::common_init ()
137 {
138     SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
139
140     set_inited( true );
141
142     ground_cache.set_cache_time_offset(globals->get_sim_time_sec());
143
144     // Set initial position
145     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
146     double lon = fgGetDouble("/sim/presets/longitude-deg")
147       * SGD_DEGREES_TO_RADIANS;
148     double lat = fgGetDouble("/sim/presets/latitude-deg")
149       * SGD_DEGREES_TO_RADIANS;
150     double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
151     double alt_m = alt_ft * SG_FEET_TO_METER;
152     set_Longitude( lon );
153     set_Latitude( lat );
154     SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
155             << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
156             << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
157             << alt_ft << "ft");
158
159     double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
160     double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
161     _set_Runway_altitude ( ground_elev_ft );
162     if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
163         fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
164         set_Altitude( ground_elev_ft + 0.1);
165     } else {
166         set_Altitude( alt_ft );
167     }
168
169     // Set ground elevation
170     SG_LOG( SG_FLIGHT, SG_INFO,
171             "...initializing ground elevation to " << ground_elev_ft
172             << "ft..." );
173
174     // Set sea-level radius
175     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
176     SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
177             << fgGetDouble("/sim/presets/latitude-deg")
178             << " alt = " << get_Altitude() );
179     double slr = SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v);
180     _set_Sea_level_radius( slr * SG_METER_TO_FEET );
181
182     // Set initial velocities
183     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
184     if ( !fgHasNode("/sim/presets/speed-set") ) {
185         set_V_calibrated_kts(0.0);
186     } else {
187         const string speedset = fgGetString("/sim/presets/speed-set");
188         if ( speedset == "knots" || speedset == "KNOTS" ) {
189             set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
190         } else if ( speedset == "mach" || speedset == "MACH" ) {
191             set_Mach_number( fgGetDouble("/sim/presets/mach") );
192         } else if ( speedset == "UVW" || speedset == "uvw" ) {
193             set_Velocities_Wind_Body(
194                                      fgGetDouble("/sim/presets/uBody-fps"),
195                                      fgGetDouble("/sim/presets/vBody-fps"),
196                                      fgGetDouble("/sim/presets/wBody-fps") );
197         } else if ( speedset == "NED" || speedset == "ned" ) {
198             set_Velocities_Local(
199                                  fgGetDouble("/sim/presets/speed-north-fps"),
200                                  fgGetDouble("/sim/presets/speed-east-fps"),
201                                  fgGetDouble("/sim/presets/speed-down-fps") );
202         } else {
203             SG_LOG( SG_FLIGHT, SG_ALERT,
204                     "Unrecognized value for /sim/presets/speed-set: "
205                     << speedset);
206             set_V_calibrated_kts( 0.0 );
207         }
208     }
209
210     // Set initial Euler angles
211     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
212     set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
213                         * SGD_DEGREES_TO_RADIANS,
214                       fgGetDouble("/sim/presets/pitch-deg")
215                         * SGD_DEGREES_TO_RADIANS,
216                       fgGetDouble("/sim/presets/heading-deg")
217                         * SGD_DEGREES_TO_RADIANS );
218
219     SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
220 }
221
222
223 /**
224  * Bind getters and setters to properties.
225  *
226  * The bind() method will be invoked after init().  Note that unlike
227  * the usual implementations of FGSubsystem::bind(), this method does
228  * not automatically pick up existing values for the properties at
229  * bind time; instead, all values are set explicitly in the init()
230  * method.
231  */
232 void
233 FGInterface::bind ()
234 {
235   bound = true;
236
237                         // Aircraft position
238   fgTie("/position/latitude-deg", this,
239         &FGInterface::get_Latitude_deg,
240         &FGInterface::set_Latitude_deg,
241         false);
242   fgSetArchivable("/position/latitude-deg");
243   fgTie("/position/longitude-deg", this,
244         &FGInterface::get_Longitude_deg,
245         &FGInterface::set_Longitude_deg,
246         false);
247   fgSetArchivable("/position/longitude-deg");
248   fgTie("/position/altitude-ft", this,
249         &FGInterface::get_Altitude,
250         &FGInterface::set_Altitude,
251         false);
252   fgSetArchivable("/position/altitude-ft");
253   fgTie("/position/altitude-agl-ft", this,
254         &FGInterface::get_Altitude_AGL, &FGInterface::set_AltitudeAGL);
255   fgSetArchivable("/position/ground-elev-ft");
256   fgTie("/position/ground-elev-ft", this,
257         &FGInterface::get_Runway_altitude); // read-only
258   fgSetArchivable("/position/ground-elev-m");
259   fgTie("/position/ground-elev-m", this,
260         &FGInterface::get_Runway_altitude_m); // read-only
261   fgTie("/environment/ground-elevation-m", this,
262         &FGInterface::get_Runway_altitude_m); // read-only
263   fgSetArchivable("/position/sea-level-radius-ft");
264   fgTie("/position/sea-level-radius-ft", this,
265         &FGInterface::get_Sea_level_radius,
266         &FGInterface::_set_Sea_level_radius);
267
268                                 // Orientation
269   fgTie("/orientation/roll-deg", this,
270         &FGInterface::get_Phi_deg,
271         &FGInterface::set_Phi_deg);
272   fgSetArchivable("/orientation/roll-deg");
273   fgTie("/orientation/pitch-deg", this,
274         &FGInterface::get_Theta_deg,
275         &FGInterface::set_Theta_deg);
276   fgSetArchivable("/orientation/pitch-deg");
277   fgTie("/orientation/heading-deg", this,
278         &FGInterface::get_Psi_deg,
279         &FGInterface::set_Psi_deg);
280   fgSetArchivable("/orientation/heading-deg");
281   fgTie("/orientation/track-deg", this,
282         &FGInterface::get_Track);
283
284   // Body-axis "euler rates" (rotation speed, but in a funny
285   // representation).
286   fgTie("/orientation/roll-rate-degps", this,
287         &FGInterface::get_Phi_dot_degps, &FGInterface::set_Phi_dot_degps);
288   fgTie("/orientation/pitch-rate-degps", this,
289         &FGInterface::get_Theta_dot_degps, &FGInterface::set_Theta_dot_degps);
290   fgTie("/orientation/yaw-rate-degps", this,
291         &FGInterface::get_Psi_dot_degps, &FGInterface::set_Psi_dot_degps);
292
293   fgTie("/orientation/p-body", this, &FGInterface::get_P_body);
294   fgTie("/orientation/q-body", this, &FGInterface::get_Q_body);
295   fgTie("/orientation/r-body", this, &FGInterface::get_R_body);
296   
297                                 // Ground speed knots
298   fgTie("/velocities/groundspeed-kt", this,
299         &FGInterface::get_V_ground_speed_kt);
300
301                                 // Calibrated airspeed
302   fgTie("/velocities/airspeed-kt", this,
303         &FGInterface::get_V_calibrated_kts,
304         &FGInterface::set_V_calibrated_kts,
305         false);
306
307     fgTie("/velocities/equivalent-kt", this,
308         &FGInterface::get_V_equiv_kts);
309
310                                 // Mach number
311   fgTie("/velocities/mach", this,
312         &FGInterface::get_Mach_number,
313         &FGInterface::set_Mach_number,
314         false);
315
316                                 // Local velocities
317 //   fgTie("/velocities/speed-north-fps", this,
318 //      &FGInterface::get_V_north,
319 //      &FGInterface::set_V_north);
320 //   fgSetArchivable("/velocities/speed-north-fps");
321 //   fgTie("/velocities/speed-east-fps", this,
322 //      &FGInterface::get_V_east,
323 //      &FGInterface::set_V_east);
324 //   fgSetArchivable("/velocities/speed-east-fps");
325 //   fgTie("/velocities/speed-down-fps", this,
326 //      &FGInterface::get_V_down,
327 //      &FGInterface::set_V_down);
328 //   fgSetArchivable("/velocities/speed-down-fps");
329                                 // FIXME: Temporarily read-only, until the
330                                 // incompatibilities between JSBSim and
331                                 // LaRCSim are fixed (LaRCSim adds the
332                                 // earth's rotation to the east velocity).
333   fgTie("/velocities/speed-north-fps", this,
334         &FGInterface::get_V_north, &FGInterface::set_V_north);
335   fgTie("/velocities/speed-east-fps", this,
336         &FGInterface::get_V_east, &FGInterface::set_V_east);
337   fgTie("/velocities/speed-down-fps", this,
338         &FGInterface::get_V_down, &FGInterface::set_V_down);
339
340   fgTie("/velocities/north-relground-fps", this,
341     &FGInterface::get_V_north_rel_ground);
342   fgTie("/velocities/east-relground-fps", this,
343     &FGInterface::get_V_east_rel_ground);
344   fgTie("/velocities/down-relground-fps", this,
345     &FGInterface::get_V_down_rel_ground);
346
347
348                                 // Relative wind
349                                 // FIXME: temporarily archivable, until
350                                 // the NED problem is fixed.
351   fgTie("/velocities/uBody-fps", this,
352         &FGInterface::get_uBody,
353         &FGInterface::set_uBody,
354         false);
355   fgSetArchivable("/velocities/uBody-fps");
356   fgTie("/velocities/vBody-fps", this,
357         &FGInterface::get_vBody,
358         &FGInterface::set_vBody,
359         false);
360   fgSetArchivable("/velocities/vBody-fps");
361   fgTie("/velocities/wBody-fps", this,
362         &FGInterface::get_wBody,
363         &FGInterface::set_wBody,
364         false);
365   fgSetArchivable("/velocities/wBody-fps");
366
367                                 // Climb and slip (read-only)
368   fgTie("/velocities/vertical-speed-fps", this,
369         &FGInterface::get_Climb_Rate,
370   &FGInterface::set_Climb_Rate ); 
371   fgTie("/velocities/glideslope", this,
372   &FGInterface::get_Gamma_vert_rad,
373   &FGInterface::set_Gamma_vert_rad );
374   fgTie("/orientation/side-slip-rad", this,
375         &FGInterface::get_Beta, &FGInterface::_set_Beta);
376   fgTie("/orientation/side-slip-deg", this,
377   &FGInterface::get_Beta_deg); // read-only
378   fgTie("/orientation/alpha-deg", this,
379   &FGInterface::get_Alpha_deg, &FGInterface::set_Alpha_deg); // read-only
380   fgTie("/accelerations/nlf", this,
381   &FGInterface::get_Nlf); // read-only
382
383                                 // NED accelerations
384   fgTie("/accelerations/ned/north-accel-fps_sec",
385         this, &FGInterface::get_V_dot_north);
386   fgTie("/accelerations/ned/east-accel-fps_sec",
387         this, &FGInterface::get_V_dot_east);
388   fgTie("/accelerations/ned/down-accel-fps_sec",
389         this, &FGInterface::get_V_dot_down);
390
391                                 // Pilot accelerations
392   fgTie("/accelerations/pilot/x-accel-fps_sec",
393         this, &FGInterface::get_A_X_pilot, &FGInterface::set_A_X_pilot);
394   fgTie("/accelerations/pilot/y-accel-fps_sec",
395         this, &FGInterface::get_A_Y_pilot, &FGInterface::set_A_Y_pilot);
396   fgTie("/accelerations/pilot/z-accel-fps_sec",
397         this, &FGInterface::get_A_Z_pilot, &FGInterface::set_A_Z_pilot);
398         
399   fgTie("/accelerations/n-z-cg-fps_sec", this, &FGInterface::get_N_Z_cg);
400
401 }
402
403
404 /**
405  * Unbind any properties bound to this FDM.
406  *
407  * This method allows the FDM to release properties so that a new
408  * FDM can bind them instead.
409  */
410 void
411 FGInterface::unbind ()
412 {
413   if (!bound) {
414     return;
415   }
416   
417   bound = false;
418
419   fgUntie("/position/latitude-deg");
420   fgUntie("/position/longitude-deg");
421   fgUntie("/position/altitude-ft");
422   fgUntie("/position/altitude-agl-ft");
423   fgUntie("/position/ground-elev-ft");
424   fgUntie("/position/ground-elev-m");
425   fgUntie("/environment/ground-elevation-m");
426   fgUntie("/position/sea-level-radius-ft");
427   fgUntie("/orientation/roll-deg");
428   fgUntie("/orientation/pitch-deg");
429   fgUntie("/orientation/heading-deg");
430   fgUntie("/orientation/track-deg");
431   fgUntie("/orientation/roll-rate-degps");
432   fgUntie("/orientation/pitch-rate-degps");
433   fgUntie("/orientation/yaw-rate-degps");
434   fgUntie("/orientation/p-body");
435   fgUntie("/orientation/q-body");
436   fgUntie("/orientation/r-body");
437   fgUntie("/orientation/side-slip-rad");
438   fgUntie("/orientation/side-slip-deg");
439   fgUntie("/orientation/alpha-deg");
440   fgUntie("/velocities/airspeed-kt");
441   fgUntie("/velocities/groundspeed-kt");
442   fgUntie("/velocities/equivalent-kt");
443   fgUntie("/velocities/mach");
444   fgUntie("/velocities/speed-north-fps");
445   fgUntie("/velocities/speed-east-fps");
446   fgUntie("/velocities/speed-down-fps");
447   fgUntie("/velocities/north-relground-fps");
448   fgUntie("/velocities/east-relground-fps");
449   fgUntie("/velocities/down-relground-fps");
450   fgUntie("/velocities/uBody-fps");
451   fgUntie("/velocities/vBody-fps");
452   fgUntie("/velocities/wBody-fps");
453   fgUntie("/velocities/vertical-speed-fps");
454   fgUntie("/velocities/glideslope");
455   fgUntie("/accelerations/nlf");
456   fgUntie("/accelerations/pilot/x-accel-fps_sec");
457   fgUntie("/accelerations/pilot/y-accel-fps_sec");
458   fgUntie("/accelerations/pilot/z-accel-fps_sec");
459   fgUntie("/accelerations/ned/north-accel-fps_sec");
460   fgUntie("/accelerations/ned/east-accel-fps_sec");
461   fgUntie("/accelerations/ned/down-accel-fps_sec");
462   fgUntie("/accelerations/n-z-cg-fps_sec");
463 }
464
465 /**
466  * Update the state of the FDM (i.e. run the equations of motion).
467  */
468 void
469 FGInterface::update (double dt)
470 {
471     SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
472 }
473
474
475 void FGInterface::_updatePositionM(const SGVec3d& cartPos)
476 {
477     TrackComputer tracker( track, geodetic_position_v );
478     cartesian_position_v = cartPos;
479     geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
480     geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
481     _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
482     _update_ground_elev_at_pos();
483 }
484
485
486 void FGInterface::_updatePosition(const SGGeod& geod)
487 {
488     TrackComputer tracker( track, geodetic_position_v );
489     geodetic_position_v = geod;
490     cartesian_position_v = SGVec3d::fromGeod(geodetic_position_v);
491     geocentric_position_v = SGGeoc::fromCart(cartesian_position_v);
492
493     _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
494     _update_ground_elev_at_pos();
495 }
496
497
498 void FGInterface::_updatePosition(const SGGeoc& geoc)
499 {
500     TrackComputer tracker( track, geodetic_position_v );
501     geocentric_position_v = geoc;
502     cartesian_position_v = SGVec3d::fromGeoc(geocentric_position_v);
503     geodetic_position_v = SGGeod::fromCart(cartesian_position_v);
504
505     _set_Sea_level_radius( SGGeodesy::SGGeodToSeaLevelRadius(geodetic_position_v)*SG_METER_TO_FEET );
506     _update_ground_elev_at_pos();
507 }
508
509
510 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
511 {
512     _updatePosition(SGGeod::fromRadFt(lon, lat, alt));
513 }
514
515
516 void FGInterface::_updateGeocentricPosition( double lat, double lon,
517                                              double alt )
518 {
519     _updatePosition(SGGeoc::fromRadFt(lon, lat, get_Sea_level_radius() + alt));
520 }
521
522 void FGInterface::_update_ground_elev_at_pos( void ) {
523     double groundlevel_m = get_groundlevel_m(geodetic_position_v);
524     _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
525 }
526
527 // Positions
528 void FGInterface::set_Latitude(double lat) {
529     geodetic_position_v.setLatitudeRad(lat);
530 }
531
532 void FGInterface::set_Longitude(double lon) {
533     geodetic_position_v.setLongitudeRad(lon);
534 }
535
536 void FGInterface::set_Altitude(double alt) {
537     geodetic_position_v.setElevationFt(alt);
538 }
539
540 void FGInterface::set_AltitudeAGL(double altagl) {
541     altitude_agl=altagl;
542 }
543
544 // Velocities
545 void FGInterface::set_V_calibrated_kts(double vc) {
546     v_calibrated_kts = vc;
547 }
548
549 void FGInterface::set_Mach_number(double mach) {
550     mach_number = mach;
551 }
552
553 void FGInterface::set_Velocities_Local( double north, 
554                                         double east, 
555                                         double down ){
556     v_local_v[0] = north;
557     v_local_v[1] = east;
558     v_local_v[2] = down;
559 }
560
561 void FGInterface::set_Velocities_Wind_Body( double u, 
562                                             double v, 
563                                             double w){
564     v_wind_body_v[0] = u;
565     v_wind_body_v[1] = v;
566     v_wind_body_v[2] = w;
567 }
568
569 // Euler angles 
570 void FGInterface::set_Euler_Angles( double phi, 
571                                     double theta, 
572                                     double psi ) {
573     euler_angles_v[0] = phi;
574     euler_angles_v[1] = theta;
575     euler_angles_v[2] = psi;                                            
576 }  
577
578 // Flight Path
579 void FGInterface::set_Climb_Rate( double roc) {
580     climb_rate = roc;
581 }
582
583 void FGInterface::set_Gamma_vert_rad( double gamma) {
584     gamma_vert_rad = gamma;
585 }
586
587 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
588 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
589 void FGInterface::set_Density(double rho) { density = rho; }
590
591 void FGInterface::set_Velocities_Local_Airmass (double wnorth, 
592                                                 double weast, 
593                                                 double wdown ) {
594     v_local_airmass_v[0] = wnorth;
595     v_local_airmass_v[1] = weast;
596     v_local_airmass_v[2] = wdown;
597 }
598
599
600 void FGInterface::_busdump(void) {
601
602     SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v: " << d_cg_rp_body_v);
603     SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v: " << v_dot_local_v);
604     SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v: " << v_dot_body_v);
605     SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v: " << a_cg_body_v);
606     SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v: " << a_pilot_body_v);
607     SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v: " << n_cg_body_v);
608     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v: " << v_local_v);
609     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v: " << v_local_rel_ground_v);
610     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v: " << v_local_airmass_v);
611     SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v: " << v_wind_body_v);
612     SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v: " << omega_body_v);
613     SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v: " << euler_rates_v);
614     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v: " << geocentric_rates_v);
615     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v: " << geocentric_position_v);
616     SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v: " << geodetic_position_v);
617     SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v: " << euler_angles_v);
618
619     SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
620     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
621     SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
622     SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
623     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
624     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
625     SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
626     SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
627     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
628     SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
629     SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
630     SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
631     SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
632     SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
633     SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
634     SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
635     SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
636     SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
637     SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
638     SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
639     SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
640 }
641
642 bool
643 FGInterface::prepare_ground_cache_m(double startSimTime, double endSimTime,
644                                     const double pt[3], double rad)
645 {
646   return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
647                                            SGVec3d(pt), rad);
648 }
649
650 bool
651 FGInterface::prepare_ground_cache_ft(double startSimTime, double endSimTime,
652                                      const double pt[3], double rad)
653 {
654   // Convert units and do the real work.
655   SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
656   return ground_cache.prepare_ground_cache(startSimTime, endSimTime,
657                                            pt_ft, rad*SG_FEET_TO_METER);
658 }
659
660 bool
661 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
662 {
663   SGVec3d _pt;
664   bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
665   assign(pt, _pt);
666   return valid;
667 }
668
669 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
670 {
671   // Convert units and do the real work.
672   SGVec3d _pt;
673   bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
674   assign(pt, SG_METER_TO_FEET*_pt);
675   *rad *= SG_METER_TO_FEET;
676   return found_ground;
677 }
678
679 double
680 FGInterface::get_cat_m(double t, const double pt[3],
681                        double end[2][3], double vel[2][3])
682 {
683   SGVec3d _end[2], _vel[2];
684   double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
685   for (int k=0; k<2; ++k) {
686     assign( end[k], _end[k] );
687     assign( vel[k], _vel[k] );
688   }
689   return dist;
690 }
691
692 double
693 FGInterface::get_cat_ft(double t, const double pt[3],
694                         double end[2][3], double vel[2][3])
695 {
696   // Convert units and do the real work.
697   SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
698   SGVec3d _end[2], _vel[2];
699   double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
700   for (int k=0; k<2; ++k) {
701     assign( end[k], SG_METER_TO_FEET*_end[k] );
702     assign( vel[k], SG_METER_TO_FEET*_vel[k] );
703   }
704   return dist*SG_METER_TO_FEET;
705 }
706
707 bool
708 FGInterface::get_body_m(double t, simgear::BVHNode::Id id,
709                         double bodyToWorld[16], double linearVel[3],
710                         double angularVel[3])
711 {
712   SGMatrixd _bodyToWorld;
713   SGVec3d _linearVel, _angularVel;
714   if (!ground_cache.get_body(t, _bodyToWorld, _linearVel, _angularVel, id))
715     return false;
716
717   assign(linearVel, _linearVel);
718   assign(angularVel, _angularVel);
719   for (unsigned i = 0; i < 16; ++i)
720       bodyToWorld[i] = _bodyToWorld.data()[i];
721
722   return true;
723 }
724
725 bool
726 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
727                        double contact[3], double normal[3],
728                        double linearVel[3], double angularVel[3],
729                        SGMaterial const*& material, simgear::BVHNode::Id& id)
730 {
731   SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
732   SGVec3d _contact, _normal, _linearVel, _angularVel;
733   material = 0;
734   bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
735                                   _angularVel, id, material);
736   // correct the linear velocity, since the line intersector delivers
737   // values for the start point and the get_agl function should
738   // traditionally deliver for the contact point
739   _linearVel += cross(_angularVel, _contact - pt_m);
740
741   assign(contact, _contact);
742   assign(normal, _normal);
743   assign(linearVel, _linearVel);
744   assign(angularVel, _angularVel);
745   return ret;
746 }
747
748 bool
749 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
750                         double contact[3], double normal[3],
751                         double linearVel[3], double angularVel[3],
752                         SGMaterial const*& material, simgear::BVHNode::Id& id)
753 {
754   // Convert units and do the real work.
755   SGVec3d pt_m = SGVec3d(pt) - max_altoff*ground_cache.get_down();
756   pt_m *= SG_FEET_TO_METER;
757   SGVec3d _contact, _normal, _linearVel, _angularVel;
758   material = 0;
759   bool ret = ground_cache.get_agl(t, pt_m, _contact, _normal, _linearVel,
760                                   _angularVel, id, material);
761   // correct the linear velocity, since the line intersector delivers
762   // values for the start point and the get_agl function should
763   // traditionally deliver for the contact point
764   _linearVel += cross(_angularVel, _contact - pt_m);
765
766   // Convert units back ...
767   assign( contact, SG_METER_TO_FEET*_contact );
768   assign( normal, _normal );
769   assign( linearVel, SG_METER_TO_FEET*_linearVel );
770   assign( angularVel, _angularVel );
771   return ret;
772 }
773
774 bool
775 FGInterface::get_nearest_m(double t, const double pt[3], double maxDist,
776                            double contact[3], double normal[3],
777                            double linearVel[3], double angularVel[3],
778                            SGMaterial const*& material,
779                            simgear::BVHNode::Id& id)
780 {
781   SGVec3d _contact, _linearVel, _angularVel;
782   if (!ground_cache.get_nearest(t, SGVec3d(pt), maxDist, _contact, _linearVel,
783                                 _angularVel, id, material))
784       return false;
785
786   assign(contact, _contact);
787   assign(linearVel, _linearVel);
788   assign(angularVel, _angularVel);
789   return true;
790 }
791
792 bool
793 FGInterface::get_nearest_ft(double t, const double pt[3], double maxDist,
794                             double contact[3], double normal[3],
795                             double linearVel[3], double angularVel[3],
796                             SGMaterial const*& material,
797                             simgear::BVHNode::Id& id)
798 {
799   SGVec3d _contact, _linearVel, _angularVel;
800   if (!ground_cache.get_nearest(t, SG_FEET_TO_METER*SGVec3d(pt),
801                                 SG_FEET_TO_METER*maxDist, _contact, _linearVel,
802                                 _angularVel, id, material))
803       return false;
804
805   assign(contact, SG_METER_TO_FEET*_contact);
806   assign(linearVel, SG_METER_TO_FEET*_linearVel);
807   assign(angularVel, _angularVel);
808   return true;
809 }
810
811 double
812 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
813 {
814   return get_groundlevel_m(SGGeod::fromRadM(lon, lat, alt));
815 }
816
817 double
818 FGInterface::get_groundlevel_m(const SGGeod& geod)
819 {
820   // Compute the cartesian position of the given lat/lon/alt.
821   SGVec3d pos = SGVec3d::fromGeod(geod);
822
823   // FIXME: how to handle t - ref_time differences ???
824   SGVec3d cpos;
825   double ref_time = 0, radius;
826   // Prepare the ground cache for that position.
827   if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
828     double startTime = ref_time;
829     double endTime = startTime + 1;
830     bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), 10);
831     /// This is most likely the case when the given altitude is
832     /// too low, try with a new altitude of 10000m, that should be
833     /// sufficient to find a ground level below everywhere on our planet
834     if (!ok) {
835       pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
836       /// If there is still no ground, return sea level radius
837       if (!prepare_ground_cache_m(startTime, endTime, pos.data(), 10))
838         return 0;
839     }
840   } else if (radius*radius <= distSqr(pos, cpos)) {
841     double startTime = ref_time;
842     double endTime = startTime + 1;
843
844     /// We reuse the old radius value, but only if it is at least 10 Meters ..
845     if (!(10 < radius)) // Well this strange compare is nan safe
846       radius = 10;
847
848     bool ok = prepare_ground_cache_m(startTime, endTime, pos.data(), radius);
849     /// This is most likely the case when the given altitude is
850     /// too low, try with a new altitude of 10000m, that should be
851     /// sufficient to find a ground level below everywhere on our planet
852     if (!ok) {
853       pos = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
854       /// If there is still no ground, return sea level radius
855       if (!prepare_ground_cache_m(startTime, endTime, pos.data(), radius))
856         return 0;
857     }
858   }
859   
860   double contact[3], normal[3], vel[3], angvel[3];
861   const SGMaterial* material;
862   simgear::BVHNode::Id id;
863   // Ignore the return value here, since it just tells us if
864   // the returns stem from the groundcache or from the coarse
865   // computations below the groundcache. The contact point is still something
866   // valid, the normals and the other returns just contain some defaults.
867   get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, angvel,
868             material, id);
869   return SGGeod::fromCart(SGVec3d(contact)).getElevationM();
870 }
871   
872 bool
873 FGInterface::caught_wire_m(double t, const double pt[4][3])
874 {
875   SGVec3d pt_m[4];
876   for (int i=0; i<4; ++i)
877     pt_m[i] = SGVec3d(pt[i]);
878   
879   return ground_cache.caught_wire(t, pt_m);
880 }
881
882 bool
883 FGInterface::caught_wire_ft(double t, const double pt[4][3])
884 {
885   // Convert units and do the real work.
886   SGVec3d pt_m[4];
887   for (int i=0; i<4; ++i)
888     pt_m[i] = SG_FEET_TO_METER*SGVec3d(pt[i]);
889     
890   return ground_cache.caught_wire(t, pt_m);
891 }
892   
893 bool
894 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
895 {
896   SGVec3d _end[2], _vel[2];
897   bool ret = ground_cache.get_wire_ends(t, _end, _vel);
898   for (int k=0; k<2; ++k) {
899     assign( end[k], _end[k] );
900     assign( vel[k], _vel[k] );
901   }
902   return ret;
903 }
904
905 bool
906 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
907 {
908   // Convert units and do the real work.
909   SGVec3d _end[2], _vel[2];
910   bool ret = ground_cache.get_wire_ends(t, _end, _vel);
911   for (int k=0; k<2; ++k) {
912     assign( end[k], SG_METER_TO_FEET*_end[k] );
913     assign( vel[k], SG_METER_TO_FEET*_vel[k] );
914   }
915   return ret;
916 }
917
918 void
919 FGInterface::release_wire(void)
920 {
921   ground_cache.release_wire();
922 }
923