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