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