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