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