]> git.mxchange.org Git - flightgear.git/blob - src/FDM/flight.cxx
Sync. w. JSB CVS as of 15/01/2007
[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 <stdio.h>
28
29 #include <plib/sg.h>
30
31 #include <simgear/constants.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/scene/model/placement.hxx>
35 #include <simgear/scene/material/mat.hxx>
36 #include <simgear/timing/timestamp.hxx>
37
38 #include <Scenery/scenery.hxx>
39 #include <Main/globals.hxx>
40 #include <Main/fg_props.hxx>
41 #include <FDM/groundcache.hxx>
42
43 #include "flight.hxx"
44
45
46 // base_fdm_state is the internal state that is updated in integer
47 // multiples of "dt".  This leads to "jitter" with respect to the real
48 // world time, so we introduce cur_fdm_state which is extrapolated by
49 // the difference between sim time and real world time
50
51 FGInterface *cur_fdm_state = 0;
52 FGInterface base_fdm_state;
53
54 inline void init_vec(FG_VECTOR_3 vec) {
55     vec[0] = 0.0; vec[1] = 0.0; vec[2] = 0.0;
56 }
57
58 // Constructor
59 FGInterface::FGInterface()
60   : remainder(0)
61 {
62     _setup();
63 }
64
65 FGInterface::FGInterface( double dt )
66   : remainder(0)
67 {
68     _setup();
69 }
70
71 // Destructor
72 FGInterface::~FGInterface() {
73     // unbind();                   // FIXME: should be called explicitly
74 }
75
76
77 int
78 FGInterface::_calc_multiloop (double dt)
79 {
80   int hz = fgGetInt("/sim/model-hz");
81   int speedup = fgGetInt("/sim/speed-up");
82
83   dt += remainder;
84   remainder = 0;
85   double ml = dt * hz;
86   // Avoid roundoff problems by adding the roundoff itself.
87   // ... ok, two times the roundoff to have enough room.
88   int multiloop = int(floor(ml * (1.0 + 2.0*DBL_EPSILON)));
89   remainder = (ml - multiloop) / hz;
90
91   // If we artificially inflate ml above by a tiny amount to get the
92   // closest integer, then subtract the integer from the original
93   // slightly smaller value, we can get a negative remainder.
94   // Logically this should never happen, and we definitely don't want
95   // to carry a negative remainder over to the next iteration, so
96   // never let the remainder go below zero.
97   // 
98   // Note: this fixes a problem where we run 1, 3, 1, 3, 1, 3... loops
99   // of the FDM when in fact we want to run 2, 2, 2, 2, 2...
100   if ( remainder < 0 ) { remainder = 0; }
101
102   return (multiloop * speedup);
103 }
104
105
106 /**
107  * Set default values for the state of the FDM.
108  *
109  * This method is invoked by the constructors.
110  */
111 void
112 FGInterface::_setup ()
113 {
114     inited = false;
115     bound = false;
116
117     init_vec( d_pilot_rp_body_v );
118     init_vec( d_cg_rp_body_v );
119     init_vec( f_body_total_v );
120     init_vec( f_local_total_v );
121     init_vec( f_aero_v );
122     init_vec( f_engine_v );
123     init_vec( f_gear_v );
124     init_vec( m_total_rp_v );
125     init_vec( m_total_cg_v );
126     init_vec( m_aero_v );
127     init_vec( m_engine_v );
128     init_vec( m_gear_v );
129     init_vec( v_dot_local_v );
130     init_vec( v_dot_body_v );
131     init_vec( a_cg_body_v );
132     init_vec( a_pilot_body_v );
133     init_vec( n_cg_body_v );
134     init_vec( n_pilot_body_v );
135     init_vec( omega_dot_body_v );
136     init_vec( v_local_v );
137     init_vec( v_local_rel_ground_v );
138     init_vec( v_local_airmass_v );
139     init_vec( v_local_rel_airmass_v );
140     init_vec( v_local_gust_v );
141     init_vec( v_wind_body_v );
142     init_vec( omega_body_v );
143     init_vec( omega_local_v );
144     init_vec( omega_total_v );
145     init_vec( euler_rates_v );
146     init_vec( geocentric_rates_v );
147     init_vec( geocentric_position_v );
148     init_vec( geodetic_position_v );
149     init_vec( euler_angles_v );
150     init_vec( d_cg_rwy_local_v );
151     init_vec( d_cg_rwy_rwy_v );
152     init_vec( d_pilot_rwy_local_v );
153     init_vec( d_pilot_rwy_rwy_v );
154     init_vec( t_local_to_body_m[0] );
155     init_vec( t_local_to_body_m[1] );
156     init_vec( t_local_to_body_m[2] );
157
158     mass=i_xx=i_yy=i_zz=i_xz=0;
159     nlf=0;
160     v_rel_wind=v_true_kts=v_rel_ground=v_inertial=0;
161     v_ground_speed=v_equiv=v_equiv_kts=0;
162     v_calibrated=v_calibrated_kts=0;
163     gravity=0;
164     centrifugal_relief=0;
165     alpha=beta=alpha_dot=beta_dot=0;
166     cos_alpha=sin_alpha=cos_beta=sin_beta=0;
167     cos_phi=sin_phi=cos_theta=sin_theta=cos_psi=sin_psi=0;
168     gamma_vert_rad=gamma_horiz_rad=0;
169     sigma=density=v_sound=mach_number=0;
170     static_pressure=total_pressure=impact_pressure=0;
171     dynamic_pressure=0;
172     static_temperature=total_temperature=0;
173     sea_level_radius=earth_position_angle=0;
174     runway_altitude=runway_latitude=runway_longitude=0;
175     runway_heading=0;
176     radius_to_rwy=0;
177     climb_rate=0;
178     sin_lat_geocentric=cos_lat_geocentric=0;
179     sin_latitude=cos_latitude=0;
180     sin_longitude=cos_longitude=0;
181     altitude_agl=0;
182 }
183
184 void
185 FGInterface::init () {}
186
187 /**
188  * Initialize the state of the FDM.
189  *
190  * Subclasses of FGInterface may do their own, additional initialization,
191  * but there is some that is common to all.  Normally, they should call
192  * this before they begin their own init to make sure the basic structures
193  * are set up properly.
194  */
195 void
196 FGInterface::common_init ()
197 {
198     SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
199
200     set_inited( true );
201
202 //     stamp();
203 //     set_remainder( 0 );
204
205     // Set initial position
206     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
207     double lon = fgGetDouble("/sim/presets/longitude-deg")
208       * SGD_DEGREES_TO_RADIANS;
209     double lat = fgGetDouble("/sim/presets/latitude-deg")
210       * SGD_DEGREES_TO_RADIANS;
211     double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
212     double alt_m = alt_ft * SG_FEET_TO_METER;
213     set_Longitude( lon );
214     set_Latitude( lat );
215     SG_LOG( SG_FLIGHT, SG_INFO, "Checking for lon = "
216             << lon*SGD_RADIANS_TO_DEGREES << "deg, lat = "
217             << lat*SGD_RADIANS_TO_DEGREES << "deg, alt = "
218             << alt_ft << "ft");
219
220     double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
221     double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
222     _set_Runway_altitude ( ground_elev_ft );
223     if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
224         fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
225         set_Altitude( ground_elev_ft + 0.1);
226     } else {
227         set_Altitude( alt_ft );
228     }
229
230     // Set ground elevation
231     SG_LOG( SG_FLIGHT, SG_INFO,
232             "...initializing ground elevation to " << ground_elev_ft
233             << "ft..." );
234
235     // Set sea-level radius
236     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
237     SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
238             << fgGetDouble("/sim/presets/latitude-deg")
239             << " alt = " << get_Altitude() );
240     double sea_level_radius_meters;
241     double lat_geoc;
242     sgGeodToGeoc( fgGetDouble("/sim/presets/latitude-deg")
243                     * SGD_DEGREES_TO_RADIANS,
244                   get_Altitude() * SG_FEET_TO_METER,
245                   &sea_level_radius_meters, &lat_geoc );
246     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
247
248     // Set initial velocities
249     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
250     if ( !fgHasNode("/sim/presets/speed-set") ) {
251         set_V_calibrated_kts(0.0);
252     } else {
253         const string speedset = fgGetString("/sim/presets/speed-set");
254         if ( speedset == "knots" || speedset == "KNOTS" ) {
255             set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
256         } else if ( speedset == "mach" || speedset == "MACH" ) {
257             set_Mach_number( fgGetDouble("/sim/presets/mach") );
258         } else if ( speedset == "UVW" || speedset == "uvw" ) {
259             set_Velocities_Wind_Body(
260                                      fgGetDouble("/sim/presets/uBody-fps"),
261                                      fgGetDouble("/sim/presets/vBody-fps"),
262                                      fgGetDouble("/sim/presets/wBody-fps") );
263         } else if ( speedset == "NED" || speedset == "ned" ) {
264             set_Velocities_Local(
265                                  fgGetDouble("/sim/presets/speed-north-fps"),
266                                  fgGetDouble("/sim/presets/speed-east-fps"),
267                                  fgGetDouble("/sim/presets/speed-down-fps") );
268         } else {
269             SG_LOG( SG_FLIGHT, SG_ALERT,
270                     "Unrecognized value for /sim/presets/speed-set: "
271                     << speedset);
272             set_V_calibrated_kts( 0.0 );
273         }
274     }
275
276     // Set initial Euler angles
277     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
278     set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
279                         * SGD_DEGREES_TO_RADIANS,
280                       fgGetDouble("/sim/presets/pitch-deg")
281                         * SGD_DEGREES_TO_RADIANS,
282                       fgGetDouble("/sim/presets/heading-deg")
283                         * SGD_DEGREES_TO_RADIANS );
284
285     SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
286 }
287
288
289 /**
290  * Bind getters and setters to properties.
291  *
292  * The bind() method will be invoked after init().  Note that unlike
293  * the usual implementations of FGSubsystem::bind(), this method does
294  * not automatically pick up existing values for the properties at
295  * bind time; instead, all values are set explicitly in the init()
296  * method.
297  */
298 void
299 FGInterface::bind ()
300 {
301   bound = true;
302
303                                 // Time management (read-only)
304 //   fgTie("/fdm/time/delta_t", this,
305 //         &FGInterface::get_delta_t); // read-only
306 //   fgTie("/fdm/time/elapsed", this,
307 //         &FGInterface::get_elapsed); // read-only
308 //   fgTie("/fdm/time/remainder", this,
309 //         &FGInterface::get_remainder); // read-only
310 //   fgTie("/fdm/time/multi_loop", this,
311 //         &FGInterface::get_multi_loop); // read-only
312
313                         // Aircraft position
314   fgTie("/position/latitude-deg", this,
315         &FGInterface::get_Latitude_deg,
316         &FGInterface::set_Latitude_deg,
317         false);
318   fgSetArchivable("/position/latitude-deg");
319   fgTie("/position/longitude-deg", this,
320         &FGInterface::get_Longitude_deg,
321         &FGInterface::set_Longitude_deg,
322         false);
323   fgSetArchivable("/position/longitude-deg");
324   fgTie("/position/altitude-ft", this,
325         &FGInterface::get_Altitude,
326         &FGInterface::set_Altitude,
327         false);
328   fgSetArchivable("/position/altitude-ft");
329   fgTie("/position/altitude-agl-ft", this,
330         &FGInterface::get_Altitude_AGL); // read-only
331   fgSetArchivable("/position/ground-elev-ft");
332   fgTie("/position/ground-elev-ft", this,
333         &FGInterface::get_Runway_altitude); // read-only
334   fgSetArchivable("/position/ground-elev-m");
335   fgTie("/position/ground-elev-m", this,
336         &FGInterface::get_Runway_altitude_m); // read-only
337   fgTie("/environment/ground-elevation-m", this,
338         &FGInterface::get_Runway_altitude_m); // read-only
339   fgSetArchivable("/position/sea-level-radius-ft");
340   fgTie("/position/sea-level-radius-ft", this,
341         &FGInterface::get_Sea_level_radius); // read-only
342
343                                 // Orientation
344   fgTie("/orientation/roll-deg", this,
345         &FGInterface::get_Phi_deg,
346         &FGInterface::set_Phi_deg);
347   fgSetArchivable("/orientation/roll-deg");
348   fgTie("/orientation/pitch-deg", this,
349         &FGInterface::get_Theta_deg,
350         &FGInterface::set_Theta_deg);
351   fgSetArchivable("/orientation/pitch-deg");
352   fgTie("/orientation/heading-deg", this,
353         &FGInterface::get_Psi_deg,
354         &FGInterface::set_Psi_deg);
355   fgSetArchivable("/orientation/heading-deg");
356
357   // Body-axis "euler rates" (rotation speed, but in a funny
358   // representation).
359   fgTie("/orientation/roll-rate-degps", this,
360         &FGInterface::get_Phi_dot_degps);
361   fgTie("/orientation/pitch-rate-degps", this,
362         &FGInterface::get_Theta_dot_degps);
363   fgTie("/orientation/yaw-rate-degps", this,
364         &FGInterface::get_Psi_dot_degps);
365
366                                 // Calibrated airspeed
367   fgTie("/velocities/airspeed-kt", this,
368         &FGInterface::get_V_calibrated_kts,
369         &FGInterface::set_V_calibrated_kts,
370         false);
371
372                                 // Mach number
373   fgTie("/velocities/mach", this,
374         &FGInterface::get_Mach_number,
375         &FGInterface::set_Mach_number,
376         false);
377
378                                 // Local velocities
379 //   fgTie("/velocities/speed-north-fps", this,
380 //      &FGInterface::get_V_north,
381 //      &FGInterface::set_V_north);
382 //   fgSetArchivable("/velocities/speed-north-fps");
383 //   fgTie("/velocities/speed-east-fps", this,
384 //      &FGInterface::get_V_east,
385 //      &FGInterface::set_V_east);
386 //   fgSetArchivable("/velocities/speed-east-fps");
387 //   fgTie("/velocities/speed-down-fps", this,
388 //      &FGInterface::get_V_down,
389 //      &FGInterface::set_V_down);
390 //   fgSetArchivable("/velocities/speed-down-fps");
391                                 // FIXME: Temporarily read-only, until the
392                                 // incompatibilities between JSBSim and
393                                 // LaRCSim are fixed (LaRCSim adds the
394                                 // earth's rotation to the east velocity).
395   fgTie("/velocities/speed-north-fps", this,
396         &FGInterface::get_V_north);
397   fgTie("/velocities/speed-east-fps", this,
398         &FGInterface::get_V_east);
399   fgTie("/velocities/speed-down-fps", this,
400         &FGInterface::get_V_down);
401
402                                 // Relative wind
403                                 // FIXME: temporarily archivable, until
404                                 // the NED problem is fixed.
405   fgTie("/velocities/uBody-fps", this,
406         &FGInterface::get_uBody,
407         &FGInterface::set_uBody,
408         false);
409   fgSetArchivable("/velocities/uBody-fps");
410   fgTie("/velocities/vBody-fps", this,
411         &FGInterface::get_vBody,
412         &FGInterface::set_vBody,
413         false);
414   fgSetArchivable("/velocities/vBody-fps");
415   fgTie("/velocities/wBody-fps", this,
416         &FGInterface::get_wBody,
417         &FGInterface::set_wBody,
418         false);
419   fgSetArchivable("/velocities/wBody-fps");
420
421                                 // Climb and slip (read-only)
422   fgTie("/velocities/vertical-speed-fps", this,
423         &FGInterface::get_Climb_Rate,
424   &FGInterface::set_Climb_Rate ); 
425   fgTie("/velocities/glideslope", this,
426   &FGInterface::get_Gamma_vert_rad,
427   &FGInterface::set_Gamma_vert_rad );
428   fgTie("/orientation/side-slip-rad", this,
429         &FGInterface::get_Beta); // read-only
430   fgTie("/orientation/side-slip-deg", this,
431   &FGInterface::get_Beta_deg); // read-only
432   fgTie("/orientation/alpha-deg", this,
433   &FGInterface::get_Alpha_deg); // read-only
434   fgTie("/accelerations/nlf", this,
435   &FGInterface::get_Nlf); // read-only
436
437                                 // NED accelerations
438   fgTie("/accelerations/ned/north-accel-fps_sec",
439         this, &FGInterface::get_V_dot_north);
440   fgTie("/accelerations/ned/east-accel-fps_sec",
441         this, &FGInterface::get_V_dot_east);
442   fgTie("/accelerations/ned/down-accel-fps_sec",
443         this, &FGInterface::get_V_dot_down);
444
445                                 // Pilot accelerations
446   fgTie("/accelerations/pilot/x-accel-fps_sec",
447         this, &FGInterface::get_A_X_pilot);
448   fgTie("/accelerations/pilot/y-accel-fps_sec",
449         this, &FGInterface::get_A_Y_pilot);
450   fgTie("/accelerations/pilot/z-accel-fps_sec",
451         this, &FGInterface::get_A_Z_pilot);
452
453 }
454
455
456 /**
457  * Unbind any properties bound to this FDM.
458  *
459  * This method allows the FDM to release properties so that a new
460  * FDM can bind them instead.
461  */
462 void
463 FGInterface::unbind ()
464 {
465   bound = false;
466
467   // fgUntie("/fdm/time/delta_t");
468   // fgUntie("/fdm/time/elapsed");
469   // fgUntie("/fdm/time/remainder");
470   // fgUntie("/fdm/time/multi_loop");
471   fgUntie("/position/latitude-deg");
472   fgUntie("/position/longitude-deg");
473   fgUntie("/position/altitude-ft");
474   fgUntie("/position/altitude-agl-ft");
475   fgUntie("/position/ground-elev-ft");
476   fgUntie("/position/ground-elev-m");
477   fgUntie("/environment/ground-elevation-m");
478   fgUntie("/position/sea-level-radius-ft");
479   fgUntie("/orientation/roll-deg");
480   fgUntie("/orientation/pitch-deg");
481   fgUntie("/orientation/heading-deg");
482   fgUntie("/orientation/roll-rate-degps");
483   fgUntie("/orientation/pitch-rate-degps");
484   fgUntie("/orientation/yaw-rate-degps");
485   fgUntie("/orientation/side-slip-rad");
486   fgUntie("/orientation/side-slip-deg");
487   fgUntie("/orientation/alpha-deg");
488   fgUntie("/velocities/airspeed-kt");
489   fgUntie("/velocities/mach");
490   fgUntie("/velocities/speed-north-fps");
491   fgUntie("/velocities/speed-east-fps");
492   fgUntie("/velocities/speed-down-fps");
493   fgUntie("/velocities/uBody-fps");
494   fgUntie("/velocities/vBody-fps");
495   fgUntie("/velocities/wBody-fps");
496   fgUntie("/velocities/vertical-speed-fps");
497   fgUntie("/velocities/glideslope");
498   fgUntie("/accelerations/nlf");
499   fgUntie("/accelerations/pilot/x-accel-fps_sec");
500   fgUntie("/accelerations/pilot/y-accel-fps_sec");
501   fgUntie("/accelerations/pilot/z-accel-fps_sec");
502   fgUntie("/accelerations/ned/north-accel-fps_sec");
503   fgUntie("/accelerations/ned/east-accel-fps_sec");
504   fgUntie("/accelerations/ned/down-accel-fps_sec");
505 }
506
507 /**
508  * Update the state of the FDM (i.e. run the equations of motion).
509  */
510 void
511 FGInterface::update (double dt)
512 {
513     SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
514 }
515
516
517 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
518 {
519     double lat_geoc, sl_radius;
520
521     // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
522
523     sgGeodToGeoc( lat, alt * SG_FEET_TO_METER, &sl_radius, &lat_geoc );
524
525     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon 
526             << " lat_geod = " << lat
527             << " lat_geoc = " << lat_geoc
528             << " alt = " << alt 
529             << " sl_radius = " << sl_radius * SG_METER_TO_FEET
530             << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
531
532     _set_Geocentric_Position( lat_geoc, lon, 
533                               sl_radius * SG_METER_TO_FEET + alt );
534         
535     _set_Geodetic_Position( lat, lon, alt );
536
537     _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET );
538     _update_ground_elev_at_pos();
539
540     _set_sin_lat_geocentric( lat_geoc );
541     _set_cos_lat_geocentric( lat_geoc );
542
543     _set_sin_cos_longitude( lon );
544
545     _set_sin_cos_latitude( lat );
546 }
547
548
549 void FGInterface::_updateGeocentricPosition( double lat_geoc, double lon,
550                                              double alt )
551 {
552     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
553
554     // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
555
556     sgGeocToGeod( lat_geoc, ( get_Sea_level_radius() + alt ) * SG_FEET_TO_METER,
557                   &lat_geod, &tmp_alt, &sl_radius1 );
558     sgGeodToGeoc( lat_geod, alt * SG_FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
559
560     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon 
561             << " lat_geod = " << lat_geod
562             << " lat_geoc = " << lat_geoc
563             << " alt = " << alt 
564             << " tmp_alt = " << tmp_alt * SG_METER_TO_FEET
565             << " sl_radius1 = " << sl_radius1 * SG_METER_TO_FEET
566             << " sl_radius2 = " << sl_radius2 * SG_METER_TO_FEET
567             << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
568
569     _set_Geocentric_Position( lat_geoc, lon, 
570                               sl_radius2 * SG_METER_TO_FEET + alt );
571         
572     _set_Geodetic_Position( lat_geod, lon, alt );
573
574     _set_Sea_level_radius( sl_radius2 * SG_METER_TO_FEET );
575     _update_ground_elev_at_pos();
576
577     _set_sin_lat_geocentric( lat_geoc );
578     _set_cos_lat_geocentric( lat_geoc );
579
580     _set_sin_cos_longitude( lon );
581
582     _set_sin_cos_latitude( lat_geod );
583 }
584
585 void FGInterface::_update_ground_elev_at_pos( void ) {
586     double lat = get_Latitude();
587     double lon = get_Longitude();
588     double alt_m = get_Altitude()*SG_FEET_TO_METER;
589     double groundlevel_m = get_groundlevel_m(lat, lon, alt_m);
590     _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
591 }
592
593 // Extrapolate fdm based on time_offset (in usec)
594 void FGInterface::extrapolate( int time_offset ) {
595     double dt = time_offset / 1000000.0;
596
597     // -dw- metrowerks complains about ambiguous access, not critical
598     // to keep this ;)
599 #ifndef __MWERKS__
600     SG_LOG(SG_FLIGHT, SG_INFO, "extrapolating FDM by dt = " << dt);
601 #endif
602
603     double lat = geodetic_position_v[0] + geocentric_rates_v[0] * dt;
604     double lat_geoc = geocentric_position_v[0] + geocentric_rates_v[0] * dt;
605
606     double lon = geodetic_position_v[1] + geocentric_rates_v[1] * dt;
607     double lon_geoc = geocentric_position_v[1] + geocentric_rates_v[1] * dt;
608
609     double alt = geodetic_position_v[2] + geocentric_rates_v[2] * dt;
610     double radius = geocentric_position_v[2] + geocentric_rates_v[2] * dt;
611
612     geodetic_position_v[0] = lat;
613     geocentric_position_v[0] = lat_geoc;
614
615     geodetic_position_v[1] = lon;
616     geocentric_position_v[1] = lon_geoc;
617
618     geodetic_position_v[2] = alt;
619     geocentric_position_v[2] = radius;
620 }
621
622 // Positions
623 void FGInterface::set_Latitude(double lat) {
624     geodetic_position_v[0] = lat;
625 }
626
627 void FGInterface::set_Longitude(double lon) {
628     geodetic_position_v[1] = lon;
629 }
630
631 void FGInterface::set_Altitude(double alt) {
632     geodetic_position_v[2] = alt;
633 }
634
635 void FGInterface::set_AltitudeAGL(double altagl) {
636     altitude_agl=altagl;
637 }
638
639 // Velocities
640 void FGInterface::set_V_calibrated_kts(double vc) {
641     v_calibrated_kts = vc;
642 }
643
644 void FGInterface::set_Mach_number(double mach) {
645     mach_number = mach;
646 }
647
648 void FGInterface::set_Velocities_Local( double north, 
649                                         double east, 
650                                         double down ){
651     v_local_v[0] = north;
652     v_local_v[1] = east;
653     v_local_v[2] = down;
654 }
655
656 void FGInterface::set_Velocities_Wind_Body( double u, 
657                                             double v, 
658                                             double w){
659     v_wind_body_v[0] = u;
660     v_wind_body_v[1] = v;
661     v_wind_body_v[2] = w;
662 }
663
664 // Euler angles 
665 void FGInterface::set_Euler_Angles( double phi, 
666                                     double theta, 
667                                     double psi ) {
668     euler_angles_v[0] = phi;
669     euler_angles_v[1] = theta;
670     euler_angles_v[2] = psi;                                            
671 }  
672
673 // Flight Path
674 void FGInterface::set_Climb_Rate( double roc) {
675     climb_rate = roc;
676 }
677
678 void FGInterface::set_Gamma_vert_rad( double gamma) {
679     gamma_vert_rad = gamma;
680 }
681
682 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
683 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
684 void FGInterface::set_Density(double rho) { density = rho; }
685
686 void FGInterface::set_Velocities_Local_Airmass (double wnorth, 
687                                                 double weast, 
688                                                 double wdown ) {
689     v_local_airmass_v[0] = wnorth;
690     v_local_airmass_v[1] = weast;
691     v_local_airmass_v[2] = wdown;
692 }
693
694
695 void FGInterface::_busdump(void) {
696
697     SG_LOG(SG_FLIGHT,SG_INFO,"d_pilot_rp_body_v[3]: " << d_pilot_rp_body_v[0] << ", " << d_pilot_rp_body_v[1] << ", " << d_pilot_rp_body_v[2]);
698     SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rp_body_v[3]: " << d_cg_rp_body_v[0] << ", " << d_cg_rp_body_v[1] << ", " << d_cg_rp_body_v[2]);
699     SG_LOG(SG_FLIGHT,SG_INFO,"f_body_total_v[3]: " << f_body_total_v[0] << ", " << f_body_total_v[1] << ", " << f_body_total_v[2]);
700     SG_LOG(SG_FLIGHT,SG_INFO,"f_local_total_v[3]: " << f_local_total_v[0] << ", " << f_local_total_v[1] << ", " << f_local_total_v[2]);
701     SG_LOG(SG_FLIGHT,SG_INFO,"f_aero_v[3]: " << f_aero_v[0] << ", " << f_aero_v[1] << ", " << f_aero_v[2]);
702     SG_LOG(SG_FLIGHT,SG_INFO,"f_engine_v[3]: " << f_engine_v[0] << ", " << f_engine_v[1] << ", " << f_engine_v[2]);
703     SG_LOG(SG_FLIGHT,SG_INFO,"f_gear_v[3]: " << f_gear_v[0] << ", " << f_gear_v[1] << ", " << f_gear_v[2]);
704     SG_LOG(SG_FLIGHT,SG_INFO,"m_total_rp_v[3]: " << m_total_rp_v[0] << ", " << m_total_rp_v[1] << ", " << m_total_rp_v[2]);
705     SG_LOG(SG_FLIGHT,SG_INFO,"m_total_cg_v[3]: " << m_total_cg_v[0] << ", " << m_total_cg_v[1] << ", " << m_total_cg_v[2]);
706     SG_LOG(SG_FLIGHT,SG_INFO,"m_aero_v[3]: " << m_aero_v[0] << ", " << m_aero_v[1] << ", " << m_aero_v[2]);
707     SG_LOG(SG_FLIGHT,SG_INFO,"m_engine_v[3]: " << m_engine_v[0] << ", " << m_engine_v[1] << ", " << m_engine_v[2]);
708     SG_LOG(SG_FLIGHT,SG_INFO,"m_gear_v[3]: " << m_gear_v[0] << ", " << m_gear_v[1] << ", " << m_gear_v[2]);
709     SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_local_v[3]: " << v_dot_local_v[0] << ", " << v_dot_local_v[1] << ", " << v_dot_local_v[2]);
710     SG_LOG(SG_FLIGHT,SG_INFO,"v_dot_body_v[3]: " << v_dot_body_v[0] << ", " << v_dot_body_v[1] << ", " << v_dot_body_v[2]);
711     SG_LOG(SG_FLIGHT,SG_INFO,"a_cg_body_v[3]: " << a_cg_body_v[0] << ", " << a_cg_body_v[1] << ", " << a_cg_body_v[2]);
712     SG_LOG(SG_FLIGHT,SG_INFO,"a_pilot_body_v[3]: " << a_pilot_body_v[0] << ", " << a_pilot_body_v[1] << ", " << a_pilot_body_v[2]);
713     SG_LOG(SG_FLIGHT,SG_INFO,"n_cg_body_v[3]: " << n_cg_body_v[0] << ", " << n_cg_body_v[1] << ", " << n_cg_body_v[2]);
714     SG_LOG(SG_FLIGHT,SG_INFO,"n_pilot_body_v[3]: " << n_pilot_body_v[0] << ", " << n_pilot_body_v[1] << ", " << n_pilot_body_v[2]);
715     SG_LOG(SG_FLIGHT,SG_INFO,"omega_dot_body_v[3]: " << omega_dot_body_v[0] << ", " << omega_dot_body_v[1] << ", " << omega_dot_body_v[2]);
716     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v[3]: " << v_local_v[0] << ", " << v_local_v[1] << ", " << v_local_v[2]);
717     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_ground_v[3]: " << v_local_rel_ground_v[0] << ", " << v_local_rel_ground_v[1] << ", " << v_local_rel_ground_v[2]);
718     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_airmass_v[3]: " << v_local_airmass_v[0] << ", " << v_local_airmass_v[1] << ", " << v_local_airmass_v[2]);
719     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_rel_airmass_v[3]: " << v_local_rel_airmass_v[0] << ", " << v_local_rel_airmass_v[1] << ", " << v_local_rel_airmass_v[2]);
720     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_gust_v[3]: " << v_local_gust_v[0] << ", " << v_local_gust_v[1] << ", " << v_local_gust_v[2]);
721     SG_LOG(SG_FLIGHT,SG_INFO,"v_wind_body_v[3]: " << v_wind_body_v[0] << ", " << v_wind_body_v[1] << ", " << v_wind_body_v[2]);
722     SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v[3]: " << omega_body_v[0] << ", " << omega_body_v[1] << ", " << omega_body_v[2]);
723     SG_LOG(SG_FLIGHT,SG_INFO,"omega_local_v[3]: " << omega_local_v[0] << ", " << omega_local_v[1] << ", " << omega_local_v[2]);
724     SG_LOG(SG_FLIGHT,SG_INFO,"omega_total_v[3]: " << omega_total_v[0] << ", " << omega_total_v[1] << ", " << omega_total_v[2]);
725     SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v[3]: " << euler_rates_v[0] << ", " << euler_rates_v[1] << ", " << euler_rates_v[2]);
726     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v[3]: " << geocentric_rates_v[0] << ", " << geocentric_rates_v[1] << ", " << geocentric_rates_v[2]);
727     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v[3]: " << geocentric_position_v[0] << ", " << geocentric_position_v[1] << ", " << geocentric_position_v[2]);
728     SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v[3]: " << geodetic_position_v[0] << ", " << geodetic_position_v[1] << ", " << geodetic_position_v[2]);
729     SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v[3]: " << euler_angles_v[0] << ", " << euler_angles_v[1] << ", " << euler_angles_v[2]);
730     SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rwy_local_v[3]: " << d_cg_rwy_local_v[0] << ", " << d_cg_rwy_local_v[1] << ", " << d_cg_rwy_local_v[2]);
731     SG_LOG(SG_FLIGHT,SG_INFO,"d_cg_rwy_rwy_v[3]: " << d_cg_rwy_rwy_v[0] << ", " << d_cg_rwy_rwy_v[1] << ", " << d_cg_rwy_rwy_v[2]);
732     SG_LOG(SG_FLIGHT,SG_INFO,"d_pilot_rwy_local_v[3]: " << d_pilot_rwy_local_v[0] << ", " << d_pilot_rwy_local_v[1] << ", " << d_pilot_rwy_local_v[2]);
733     SG_LOG(SG_FLIGHT,SG_INFO,"d_pilot_rwy_rwy_v[3]: " << d_pilot_rwy_rwy_v[0] << ", " << d_pilot_rwy_rwy_v[1] << ", " << d_pilot_rwy_rwy_v[2]);
734
735     SG_LOG(SG_FLIGHT,SG_INFO,"t_local_to_body_m[0][3]: " << t_local_to_body_m[0][0] << ", " << t_local_to_body_m[0][1] << ", " << t_local_to_body_m[0][2]);
736     SG_LOG(SG_FLIGHT,SG_INFO,"t_local_to_body_m[1][3]: " << t_local_to_body_m[1][0] << ", " << t_local_to_body_m[1][1] << ", " << t_local_to_body_m[1][2]);
737     SG_LOG(SG_FLIGHT,SG_INFO,"t_local_to_body_m[2][3]: " << t_local_to_body_m[2][0] << ", " << t_local_to_body_m[2][1] << ", " << t_local_to_body_m[2][2]);
738
739     SG_LOG(SG_FLIGHT,SG_INFO,"mass: " << mass );
740     SG_LOG(SG_FLIGHT,SG_INFO,"i_xx: " << i_xx );
741     SG_LOG(SG_FLIGHT,SG_INFO,"i_yy: " << i_yy );
742     SG_LOG(SG_FLIGHT,SG_INFO,"i_zz: " << i_zz );
743     SG_LOG(SG_FLIGHT,SG_INFO,"i_xz: " << i_xz );
744     SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
745     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
746     SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
747     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_ground: " << v_rel_ground );
748     SG_LOG(SG_FLIGHT,SG_INFO,"v_inertial: " << v_inertial );
749     SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
750     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv: " << v_equiv );
751     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
752     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated: " << v_calibrated );
753     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
754     SG_LOG(SG_FLIGHT,SG_INFO,"gravity: " << gravity );
755     SG_LOG(SG_FLIGHT,SG_INFO,"centrifugal_relief: " << centrifugal_relief );
756     SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
757     SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
758     SG_LOG(SG_FLIGHT,SG_INFO,"alpha_dot: " << alpha_dot );
759     SG_LOG(SG_FLIGHT,SG_INFO,"beta_dot: " << beta_dot );
760     SG_LOG(SG_FLIGHT,SG_INFO,"cos_alpha: " << cos_alpha );
761     SG_LOG(SG_FLIGHT,SG_INFO,"sin_alpha: " << sin_alpha );
762     SG_LOG(SG_FLIGHT,SG_INFO,"cos_beta: " << cos_beta );
763     SG_LOG(SG_FLIGHT,SG_INFO,"sin_beta: " << sin_beta );
764     SG_LOG(SG_FLIGHT,SG_INFO,"cos_phi: " << cos_phi );
765     SG_LOG(SG_FLIGHT,SG_INFO,"sin_phi: " << sin_phi );
766     SG_LOG(SG_FLIGHT,SG_INFO,"cos_theta: " << cos_theta );
767     SG_LOG(SG_FLIGHT,SG_INFO,"sin_theta: " << sin_theta );
768     SG_LOG(SG_FLIGHT,SG_INFO,"cos_psi: " << cos_psi );
769     SG_LOG(SG_FLIGHT,SG_INFO,"sin_psi: " << sin_psi );
770     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
771     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_horiz_rad: " << gamma_horiz_rad );
772     SG_LOG(SG_FLIGHT,SG_INFO,"sigma: " << sigma );
773     SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
774     SG_LOG(SG_FLIGHT,SG_INFO,"v_sound: " << v_sound );
775     SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
776     SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
777     SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
778     SG_LOG(SG_FLIGHT,SG_INFO,"impact_pressure: " << impact_pressure );
779     SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
780     SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
781     SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
782     SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
783     SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
784     SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
785     SG_LOG(SG_FLIGHT,SG_INFO,"runway_latitude: " << runway_latitude );
786     SG_LOG(SG_FLIGHT,SG_INFO,"runway_longitude: " << runway_longitude );
787     SG_LOG(SG_FLIGHT,SG_INFO,"runway_heading: " << runway_heading );
788     SG_LOG(SG_FLIGHT,SG_INFO,"radius_to_rwy: " << radius_to_rwy );
789     SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
790     SG_LOG(SG_FLIGHT,SG_INFO,"sin_lat_geocentric: " << sin_lat_geocentric );
791     SG_LOG(SG_FLIGHT,SG_INFO,"cos_lat_geocentric: " << cos_lat_geocentric );
792     SG_LOG(SG_FLIGHT,SG_INFO,"sin_longitude: " << sin_longitude );
793     SG_LOG(SG_FLIGHT,SG_INFO,"cos_longitude: " << cos_longitude );
794     SG_LOG(SG_FLIGHT,SG_INFO,"sin_latitude: " << sin_latitude );
795     SG_LOG(SG_FLIGHT,SG_INFO,"cos_latitude: " << cos_latitude );
796     SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
797 }
798
799 bool
800 FGInterface::prepare_ground_cache_m(double ref_time, const double pt[3],
801                                     double rad)
802 {
803   return ground_cache.prepare_ground_cache(ref_time, SGVec3d(pt), rad);
804 }
805
806 bool FGInterface::prepare_ground_cache_ft(double ref_time, const double pt[3],
807                                           double rad)
808 {
809   // Convert units and do the real work.
810   SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
811   return ground_cache.prepare_ground_cache(ref_time, pt_ft, rad*SG_FEET_TO_METER);
812 }
813
814 bool
815 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
816 {
817   SGVec3d _pt;
818   bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
819   sgdCopyVec3(pt, _pt.data());
820   return valid;
821 }
822
823 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
824 {
825   // Convert units and do the real work.
826   SGVec3d _pt;
827   bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
828   sgdScaleVec3(pt, _pt.data(), SG_METER_TO_FEET);
829   *rad *= SG_METER_TO_FEET;
830   return found_ground;
831 }
832
833 double
834 FGInterface::get_cat_m(double t, const double pt[3],
835                        double end[2][3], double vel[2][3])
836 {
837   SGVec3d _end[2], _vel[2];
838   double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
839   for (int k=0; k<2; ++k) {
840     sgdCopyVec3( end[k], _end[k].data() );
841     sgdCopyVec3( vel[k], _vel[k].data() );
842   }
843   return dist;
844 }
845
846 double
847 FGInterface::get_cat_ft(double t, const double pt[3],
848                         double end[2][3], double vel[2][3])
849 {
850   // Convert units and do the real work.
851   SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
852   SGVec3d _end[2], _vel[2];
853   double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
854   for (int k=0; k<2; ++k) {
855     sgdScaleVec3( end[k], _end[k].data(), SG_METER_TO_FEET );
856     sgdScaleVec3( vel[k], _vel[k].data(), SG_METER_TO_FEET );
857   }
858   return dist*SG_METER_TO_FEET;
859 }
860
861 // Legacy interface just kept because of JSBSim
862 bool
863 FGInterface::get_agl_m(double t, const double pt[3],
864                        double contact[3], double normal[3], double vel[3],
865                        int *type, double *loadCapacity,
866                        double *frictionFactor, double *agl)
867 {
868   const SGMaterial* material;
869   SGVec3d _contact, _normal, _vel;
870   bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
871                                   _vel, type, &material, agl);
872   sgdCopyVec3(contact, _contact.data());
873   sgdCopyVec3(normal, _normal.data());
874   sgdCopyVec3(vel, _vel.data());
875   if (material) {
876     *loadCapacity = material->get_load_resistence();
877     *frictionFactor = material->get_friction_factor();
878
879   } else {
880     *loadCapacity = DBL_MAX;
881     *frictionFactor = 1.0;
882   }
883   return ret;
884 }
885
886 bool
887 FGInterface::get_agl_m(double t, const double pt[3],
888                        double contact[3], double normal[3], double vel[3],
889                        int *type, const SGMaterial **material, double *agl)
890 {
891   SGVec3d _contact, _normal, _vel;
892   bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
893                                   _vel, type, material, agl);
894   sgdCopyVec3(contact, _contact.data());
895   sgdCopyVec3(normal, _normal.data());
896   sgdCopyVec3(vel, _vel.data());
897   return ret;
898 }
899
900 // Legacy interface just kept because of JSBSim
901 bool
902 FGInterface::get_agl_ft(double t, const double pt[3],
903                         double contact[3], double normal[3], double vel[3],
904                         int *type, double *loadCapacity,
905                         double *frictionFactor, double *agl)
906 {
907   // Convert units and do the real work.
908   SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
909
910   const SGMaterial* material;
911   SGVec3d _contact, _normal, _vel;
912   bool ret = ground_cache.get_agl(t, pt_m, 2.0, _contact, _normal, _vel,
913                                   type, &material, agl);
914   // Convert units back ...
915   sgdScaleVec3( contact, _contact.data(), SG_METER_TO_FEET );
916   sgdScaleVec3( vel, _vel.data(), SG_METER_TO_FEET );
917   sgdCopyVec3( normal, _normal.data() );
918   *agl *= SG_METER_TO_FEET;
919
920   // return material properties if available
921   if (material) {
922     // FIXME: convert units?? now pascal to lbf/ft^2
923     *loadCapacity = 0.020885434*material->get_load_resistence();
924     *frictionFactor = material->get_friction_factor();
925   } else {
926     *loadCapacity = DBL_MAX;
927     *frictionFactor = 1.0;
928   }
929   return ret;
930 }
931
932 bool
933 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
934                        double contact[3], double normal[3], double vel[3],
935                        int *type, const SGMaterial** material, double *agl)
936 {
937   SGVec3d _contact, _normal, _vel;
938   bool found = ground_cache.get_agl(t, SGVec3d(pt), max_altoff, _contact,
939                                     _normal, _vel, type, material, agl);
940   sgdCopyVec3(contact, _contact.data());
941   sgdCopyVec3(normal, _normal.data());
942   sgdCopyVec3(vel, _vel.data());
943   return found;
944 }
945
946 bool
947 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
948                         double contact[3], double normal[3], double vel[3],
949                         int *type, const SGMaterial** material, double *agl)
950 {
951   // Convert units and do the real work.
952   SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
953   SGVec3d _contact, _normal, _vel;
954   bool ret = ground_cache.get_agl(t, pt_m, SG_FEET_TO_METER * max_altoff,
955                                   _contact, _normal, _vel,
956                                   type, material, agl);
957   // Convert units back ...
958   sgdScaleVec3( contact, _contact.data(), SG_METER_TO_FEET );
959   sgdScaleVec3( vel, _vel.data(), SG_METER_TO_FEET );
960   sgdCopyVec3( normal, _normal.data() );
961   *agl *= SG_METER_TO_FEET;
962   return ret;
963 }
964
965
966 double
967 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
968 {
969   // Compute the cartesian position of the given lat/lon/alt.
970   SGVec3d pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, alt));
971
972   // FIXME: how to handle t - ref_time differences ???
973   SGVec3d cpos;
974   double ref_time, radius;
975   // Prepare the ground cache for that position.
976   if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
977     bool ok = prepare_ground_cache_m(ref_time, pos.data(), 10);
978     /// This is most likely the case when the given altitude is
979     /// too low, try with a new altitude of 10000m, that should be
980     /// sufficient to find a ground level below everywhere on our planet
981     if (!ok) {
982       pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, 10000));
983       /// If there is still no ground, return sea level radius
984       if (!prepare_ground_cache_m(ref_time, pos.data(), 10))
985         return 0;
986     }
987   } else if (radius*radius <= distSqr(pos, cpos)) {
988     /// We reuse the old radius value, but only if it is at least 10 Meters ..
989     if (!(10 < radius)) // Well this strange compare is nan safe
990       radius = 10;
991
992     bool ok = prepare_ground_cache_m(ref_time, pos.data(), radius);
993     /// This is most likely the case when the given altitude is
994     /// too low, try with a new altitude of 10000m, that should be
995     /// sufficient to find a ground level below everywhere on our planet
996     if (!ok) {
997       pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, 10000));
998       /// If there is still no ground, return sea level radius
999       if (!prepare_ground_cache_m(ref_time, pos.data(), radius))
1000         return 0;
1001     }
1002   }
1003   
1004   double contact[3], normal[3], vel[3], agl;
1005   int type;
1006   // Ignore the return value here, since it just tells us if
1007   // the returns stem from the groundcache or from the coarse
1008   // computations below the groundcache. The contact point is still something
1009   // valid, the normals and the other returns just contain some defaults.
1010   get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, &type, 0, &agl);
1011   SGGeod geod = SGGeod::fromCart(SGVec3d(contact));
1012   return geod.getElevationM();
1013 }
1014   
1015 bool
1016 FGInterface::caught_wire_m(double t, const double pt[4][3])
1017 {
1018   SGVec3d pt_m[4];
1019   for (int i=0; i<4; ++i)
1020     sgdCopyVec3(pt_m[i].data(), pt[i]);
1021   
1022   return ground_cache.caught_wire(t, pt_m);
1023 }
1024
1025 bool
1026 FGInterface::caught_wire_ft(double t, const double pt[4][3])
1027 {
1028   // Convert units and do the real work.
1029   SGVec3d pt_m[4];
1030   for (int i=0; i<4; ++i)
1031     sgdScaleVec3(pt_m[i].data(), pt[i], SG_FEET_TO_METER);
1032     
1033   return ground_cache.caught_wire(t, pt_m);
1034 }
1035   
1036 bool
1037 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
1038 {
1039   SGVec3d _end[2], _vel[2];
1040   bool ret = ground_cache.get_wire_ends(t, _end, _vel);
1041   for (int k=0; k<2; ++k) {
1042     sgdCopyVec3( end[k], _end[k].data() );
1043     sgdCopyVec3( vel[k], _vel[k].data() );
1044   }
1045   return ret;
1046 }
1047
1048 bool
1049 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
1050 {
1051   // Convert units and do the real work.
1052   SGVec3d _end[2], _vel[2];
1053   bool ret = ground_cache.get_wire_ends(t, _end, _vel);
1054   for (int k=0; k<2; ++k) {
1055     sgdScaleVec3( end[k], _end[k].data(), SG_METER_TO_FEET );
1056     sgdScaleVec3( vel[k], _vel[k].data(), SG_METER_TO_FEET );
1057   }
1058   return ret;
1059 }
1060
1061 void
1062 FGInterface::release_wire(void)
1063 {
1064   ground_cache.release_wire();
1065 }
1066
1067 void fgToggleFDMdataLogging(void) {
1068   cur_fdm_state->ToggleDataLogging();
1069 }