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