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