]> git.mxchange.org Git - flightgear.git/blob - src/FDM/flight.cxx
- Added ultra-light traffic is now a separate traffic class that can have its
[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                                 // Ground speed knots
367   fgTie("/velocities/groundspeed-kt", this,
368         &FGInterface::get_V_ground_speed_kt);
369
370                                 // Calibrated airspeed
371   fgTie("/velocities/airspeed-kt", this,
372         &FGInterface::get_V_calibrated_kts,
373         &FGInterface::set_V_calibrated_kts,
374         false);
375
376                                 // Mach number
377   fgTie("/velocities/mach", this,
378         &FGInterface::get_Mach_number,
379         &FGInterface::set_Mach_number,
380         false);
381
382                                 // Local velocities
383 //   fgTie("/velocities/speed-north-fps", this,
384 //      &FGInterface::get_V_north,
385 //      &FGInterface::set_V_north);
386 //   fgSetArchivable("/velocities/speed-north-fps");
387 //   fgTie("/velocities/speed-east-fps", this,
388 //      &FGInterface::get_V_east,
389 //      &FGInterface::set_V_east);
390 //   fgSetArchivable("/velocities/speed-east-fps");
391 //   fgTie("/velocities/speed-down-fps", this,
392 //      &FGInterface::get_V_down,
393 //      &FGInterface::set_V_down);
394 //   fgSetArchivable("/velocities/speed-down-fps");
395                                 // FIXME: Temporarily read-only, until the
396                                 // incompatibilities between JSBSim and
397                                 // LaRCSim are fixed (LaRCSim adds the
398                                 // earth's rotation to the east velocity).
399   fgTie("/velocities/speed-north-fps", this,
400         &FGInterface::get_V_north);
401   fgTie("/velocities/speed-east-fps", this,
402         &FGInterface::get_V_east);
403   fgTie("/velocities/speed-down-fps", this,
404         &FGInterface::get_V_down);
405
406                                 // Relative wind
407                                 // FIXME: temporarily archivable, until
408                                 // the NED problem is fixed.
409   fgTie("/velocities/uBody-fps", this,
410         &FGInterface::get_uBody,
411         &FGInterface::set_uBody,
412         false);
413   fgSetArchivable("/velocities/uBody-fps");
414   fgTie("/velocities/vBody-fps", this,
415         &FGInterface::get_vBody,
416         &FGInterface::set_vBody,
417         false);
418   fgSetArchivable("/velocities/vBody-fps");
419   fgTie("/velocities/wBody-fps", this,
420         &FGInterface::get_wBody,
421         &FGInterface::set_wBody,
422         false);
423   fgSetArchivable("/velocities/wBody-fps");
424
425                                 // Climb and slip (read-only)
426   fgTie("/velocities/vertical-speed-fps", this,
427         &FGInterface::get_Climb_Rate,
428   &FGInterface::set_Climb_Rate ); 
429   fgTie("/velocities/glideslope", this,
430   &FGInterface::get_Gamma_vert_rad,
431   &FGInterface::set_Gamma_vert_rad );
432   fgTie("/orientation/side-slip-rad", this,
433         &FGInterface::get_Beta); // read-only
434   fgTie("/orientation/side-slip-deg", this,
435   &FGInterface::get_Beta_deg); // read-only
436   fgTie("/orientation/alpha-deg", this,
437   &FGInterface::get_Alpha_deg); // read-only
438   fgTie("/accelerations/nlf", this,
439   &FGInterface::get_Nlf); // read-only
440
441                                 // NED accelerations
442   fgTie("/accelerations/ned/north-accel-fps_sec",
443         this, &FGInterface::get_V_dot_north);
444   fgTie("/accelerations/ned/east-accel-fps_sec",
445         this, &FGInterface::get_V_dot_east);
446   fgTie("/accelerations/ned/down-accel-fps_sec",
447         this, &FGInterface::get_V_dot_down);
448
449                                 // Pilot accelerations
450   fgTie("/accelerations/pilot/x-accel-fps_sec",
451         this, &FGInterface::get_A_X_pilot);
452   fgTie("/accelerations/pilot/y-accel-fps_sec",
453         this, &FGInterface::get_A_Y_pilot);
454   fgTie("/accelerations/pilot/z-accel-fps_sec",
455         this, &FGInterface::get_A_Z_pilot);
456
457 }
458
459
460 /**
461  * Unbind any properties bound to this FDM.
462  *
463  * This method allows the FDM to release properties so that a new
464  * FDM can bind them instead.
465  */
466 void
467 FGInterface::unbind ()
468 {
469   bound = false;
470
471   // fgUntie("/fdm/time/delta_t");
472   // fgUntie("/fdm/time/elapsed");
473   // fgUntie("/fdm/time/remainder");
474   // fgUntie("/fdm/time/multi_loop");
475   fgUntie("/position/latitude-deg");
476   fgUntie("/position/longitude-deg");
477   fgUntie("/position/altitude-ft");
478   fgUntie("/position/altitude-agl-ft");
479   fgUntie("/position/ground-elev-ft");
480   fgUntie("/position/ground-elev-m");
481   fgUntie("/environment/ground-elevation-m");
482   fgUntie("/position/sea-level-radius-ft");
483   fgUntie("/orientation/roll-deg");
484   fgUntie("/orientation/pitch-deg");
485   fgUntie("/orientation/heading-deg");
486   fgUntie("/orientation/roll-rate-degps");
487   fgUntie("/orientation/pitch-rate-degps");
488   fgUntie("/orientation/yaw-rate-degps");
489   fgUntie("/orientation/side-slip-rad");
490   fgUntie("/orientation/side-slip-deg");
491   fgUntie("/orientation/alpha-deg");
492   fgUntie("/velocities/airspeed-kt");
493   fgUntie("/velocities/groundspeed-kt");
494   fgUntie("/velocities/mach");
495   fgUntie("/velocities/speed-north-fps");
496   fgUntie("/velocities/speed-east-fps");
497   fgUntie("/velocities/speed-down-fps");
498   fgUntie("/velocities/uBody-fps");
499   fgUntie("/velocities/vBody-fps");
500   fgUntie("/velocities/wBody-fps");
501   fgUntie("/velocities/vertical-speed-fps");
502   fgUntie("/velocities/glideslope");
503   fgUntie("/accelerations/nlf");
504   fgUntie("/accelerations/pilot/x-accel-fps_sec");
505   fgUntie("/accelerations/pilot/y-accel-fps_sec");
506   fgUntie("/accelerations/pilot/z-accel-fps_sec");
507   fgUntie("/accelerations/ned/north-accel-fps_sec");
508   fgUntie("/accelerations/ned/east-accel-fps_sec");
509   fgUntie("/accelerations/ned/down-accel-fps_sec");
510 }
511
512 /**
513  * Update the state of the FDM (i.e. run the equations of motion).
514  */
515 void
516 FGInterface::update (double dt)
517 {
518     SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
519 }
520
521
522 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
523 {
524     double lat_geoc, sl_radius;
525
526     // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
527
528     sgGeodToGeoc( lat, alt * SG_FEET_TO_METER, &sl_radius, &lat_geoc );
529
530     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon 
531             << " lat_geod = " << lat
532             << " lat_geoc = " << lat_geoc
533             << " alt = " << alt 
534             << " sl_radius = " << sl_radius * SG_METER_TO_FEET
535             << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
536
537     _set_Geocentric_Position( lat_geoc, lon, 
538                               sl_radius * SG_METER_TO_FEET + alt );
539         
540     _set_Geodetic_Position( lat, lon, alt );
541
542     _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET );
543     _update_ground_elev_at_pos();
544
545     _set_sin_lat_geocentric( lat_geoc );
546     _set_cos_lat_geocentric( lat_geoc );
547
548     _set_sin_cos_longitude( lon );
549
550     _set_sin_cos_latitude( lat );
551 }
552
553
554 void FGInterface::_updateGeocentricPosition( double lat_geoc, double lon,
555                                              double alt )
556 {
557     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
558
559     // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
560
561     sgGeocToGeod( lat_geoc, ( get_Sea_level_radius() + alt ) * SG_FEET_TO_METER,
562                   &lat_geod, &tmp_alt, &sl_radius1 );
563     sgGeodToGeoc( lat_geod, alt * SG_FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
564
565     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon 
566             << " lat_geod = " << lat_geod
567             << " lat_geoc = " << lat_geoc
568             << " alt = " << alt 
569             << " tmp_alt = " << tmp_alt * SG_METER_TO_FEET
570             << " sl_radius1 = " << sl_radius1 * SG_METER_TO_FEET
571             << " sl_radius2 = " << sl_radius2 * SG_METER_TO_FEET
572             << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
573
574     _set_Geocentric_Position( lat_geoc, lon, 
575                               sl_radius2 * SG_METER_TO_FEET + alt );
576         
577     _set_Geodetic_Position( lat_geod, lon, alt );
578
579     _set_Sea_level_radius( sl_radius2 * SG_METER_TO_FEET );
580     _update_ground_elev_at_pos();
581
582     _set_sin_lat_geocentric( lat_geoc );
583     _set_cos_lat_geocentric( lat_geoc );
584
585     _set_sin_cos_longitude( lon );
586
587     _set_sin_cos_latitude( lat_geod );
588 }
589
590 void FGInterface::_update_ground_elev_at_pos( void ) {
591     double lat = get_Latitude();
592     double lon = get_Longitude();
593     double alt_m = get_Altitude()*SG_FEET_TO_METER;
594     double groundlevel_m = get_groundlevel_m(lat, lon, alt_m);
595     _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
596 }
597
598 // Extrapolate fdm based on time_offset (in usec)
599 void FGInterface::extrapolate( int time_offset ) {
600     double dt = time_offset / 1000000.0;
601
602     // -dw- metrowerks complains about ambiguous access, not critical
603     // to keep this ;)
604 #ifndef __MWERKS__
605     SG_LOG(SG_FLIGHT, SG_INFO, "extrapolating FDM by dt = " << dt);
606 #endif
607
608     double lat = geodetic_position_v[0] + geocentric_rates_v[0] * dt;
609     double lat_geoc = geocentric_position_v[0] + geocentric_rates_v[0] * dt;
610
611     double lon = geodetic_position_v[1] + geocentric_rates_v[1] * dt;
612     double lon_geoc = geocentric_position_v[1] + geocentric_rates_v[1] * dt;
613
614     double alt = geodetic_position_v[2] + geocentric_rates_v[2] * dt;
615     double radius = geocentric_position_v[2] + geocentric_rates_v[2] * dt;
616
617     geodetic_position_v[0] = lat;
618     geocentric_position_v[0] = lat_geoc;
619
620     geodetic_position_v[1] = lon;
621     geocentric_position_v[1] = lon_geoc;
622
623     geodetic_position_v[2] = alt;
624     geocentric_position_v[2] = radius;
625 }
626
627 // Positions
628 void FGInterface::set_Latitude(double lat) {
629     geodetic_position_v[0] = lat;
630 }
631
632 void FGInterface::set_Longitude(double lon) {
633     geodetic_position_v[1] = lon;
634 }
635
636 void FGInterface::set_Altitude(double alt) {
637     geodetic_position_v[2] = alt;
638 }
639
640 void FGInterface::set_AltitudeAGL(double altagl) {
641     altitude_agl=altagl;
642 }
643
644 // Velocities
645 void FGInterface::set_V_calibrated_kts(double vc) {
646     v_calibrated_kts = vc;
647 }
648
649 void FGInterface::set_Mach_number(double mach) {
650     mach_number = mach;
651 }
652
653 void FGInterface::set_Velocities_Local( double north, 
654                                         double east, 
655                                         double down ){
656     v_local_v[0] = north;
657     v_local_v[1] = east;
658     v_local_v[2] = down;
659 }
660
661 void FGInterface::set_Velocities_Wind_Body( double u, 
662                                             double v, 
663                                             double w){
664     v_wind_body_v[0] = u;
665     v_wind_body_v[1] = v;
666     v_wind_body_v[2] = w;
667 }
668
669 // Euler angles 
670 void FGInterface::set_Euler_Angles( double phi, 
671                                     double theta, 
672                                     double psi ) {
673     euler_angles_v[0] = phi;
674     euler_angles_v[1] = theta;
675     euler_angles_v[2] = psi;                                            
676 }  
677
678 // Flight Path
679 void FGInterface::set_Climb_Rate( double roc) {
680     climb_rate = roc;
681 }
682
683 void FGInterface::set_Gamma_vert_rad( double gamma) {
684     gamma_vert_rad = gamma;
685 }
686
687 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
688 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
689 void FGInterface::set_Density(double rho) { density = rho; }
690
691 void FGInterface::set_Velocities_Local_Airmass (double wnorth, 
692                                                 double weast, 
693                                                 double wdown ) {
694     v_local_airmass_v[0] = wnorth;
695     v_local_airmass_v[1] = weast;
696     v_local_airmass_v[2] = wdown;
697 }
698
699
700 void FGInterface::_busdump(void) {
701
702     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]);
703     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]);
704     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]);
705     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]);
706     SG_LOG(SG_FLIGHT,SG_INFO,"f_aero_v[3]: " << f_aero_v[0] << ", " << f_aero_v[1] << ", " << f_aero_v[2]);
707     SG_LOG(SG_FLIGHT,SG_INFO,"f_engine_v[3]: " << f_engine_v[0] << ", " << f_engine_v[1] << ", " << f_engine_v[2]);
708     SG_LOG(SG_FLIGHT,SG_INFO,"f_gear_v[3]: " << f_gear_v[0] << ", " << f_gear_v[1] << ", " << f_gear_v[2]);
709     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]);
710     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]);
711     SG_LOG(SG_FLIGHT,SG_INFO,"m_aero_v[3]: " << m_aero_v[0] << ", " << m_aero_v[1] << ", " << m_aero_v[2]);
712     SG_LOG(SG_FLIGHT,SG_INFO,"m_engine_v[3]: " << m_engine_v[0] << ", " << m_engine_v[1] << ", " << m_engine_v[2]);
713     SG_LOG(SG_FLIGHT,SG_INFO,"m_gear_v[3]: " << m_gear_v[0] << ", " << m_gear_v[1] << ", " << m_gear_v[2]);
714     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]);
715     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]);
716     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]);
717     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]);
718     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]);
719     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]);
720     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]);
721     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v[3]: " << v_local_v[0] << ", " << v_local_v[1] << ", " << v_local_v[2]);
722     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]);
723     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]);
724     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]);
725     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]);
726     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]);
727     SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v[3]: " << omega_body_v[0] << ", " << omega_body_v[1] << ", " << omega_body_v[2]);
728     SG_LOG(SG_FLIGHT,SG_INFO,"omega_local_v[3]: " << omega_local_v[0] << ", " << omega_local_v[1] << ", " << omega_local_v[2]);
729     SG_LOG(SG_FLIGHT,SG_INFO,"omega_total_v[3]: " << omega_total_v[0] << ", " << omega_total_v[1] << ", " << omega_total_v[2]);
730     SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v[3]: " << euler_rates_v[0] << ", " << euler_rates_v[1] << ", " << euler_rates_v[2]);
731     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v[3]: " << geocentric_rates_v[0] << ", " << geocentric_rates_v[1] << ", " << geocentric_rates_v[2]);
732     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v[3]: " << geocentric_position_v[0] << ", " << geocentric_position_v[1] << ", " << geocentric_position_v[2]);
733     SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v[3]: " << geodetic_position_v[0] << ", " << geodetic_position_v[1] << ", " << geodetic_position_v[2]);
734     SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v[3]: " << euler_angles_v[0] << ", " << euler_angles_v[1] << ", " << euler_angles_v[2]);
735     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]);
736     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]);
737     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]);
738     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]);
739
740     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]);
741     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]);
742     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]);
743
744     SG_LOG(SG_FLIGHT,SG_INFO,"mass: " << mass );
745     SG_LOG(SG_FLIGHT,SG_INFO,"i_xx: " << i_xx );
746     SG_LOG(SG_FLIGHT,SG_INFO,"i_yy: " << i_yy );
747     SG_LOG(SG_FLIGHT,SG_INFO,"i_zz: " << i_zz );
748     SG_LOG(SG_FLIGHT,SG_INFO,"i_xz: " << i_xz );
749     SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
750     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
751     SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
752     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_ground: " << v_rel_ground );
753     SG_LOG(SG_FLIGHT,SG_INFO,"v_inertial: " << v_inertial );
754     SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
755     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv: " << v_equiv );
756     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
757     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated: " << v_calibrated );
758     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
759     SG_LOG(SG_FLIGHT,SG_INFO,"gravity: " << gravity );
760     SG_LOG(SG_FLIGHT,SG_INFO,"centrifugal_relief: " << centrifugal_relief );
761     SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
762     SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
763     SG_LOG(SG_FLIGHT,SG_INFO,"alpha_dot: " << alpha_dot );
764     SG_LOG(SG_FLIGHT,SG_INFO,"beta_dot: " << beta_dot );
765     SG_LOG(SG_FLIGHT,SG_INFO,"cos_alpha: " << cos_alpha );
766     SG_LOG(SG_FLIGHT,SG_INFO,"sin_alpha: " << sin_alpha );
767     SG_LOG(SG_FLIGHT,SG_INFO,"cos_beta: " << cos_beta );
768     SG_LOG(SG_FLIGHT,SG_INFO,"sin_beta: " << sin_beta );
769     SG_LOG(SG_FLIGHT,SG_INFO,"cos_phi: " << cos_phi );
770     SG_LOG(SG_FLIGHT,SG_INFO,"sin_phi: " << sin_phi );
771     SG_LOG(SG_FLIGHT,SG_INFO,"cos_theta: " << cos_theta );
772     SG_LOG(SG_FLIGHT,SG_INFO,"sin_theta: " << sin_theta );
773     SG_LOG(SG_FLIGHT,SG_INFO,"cos_psi: " << cos_psi );
774     SG_LOG(SG_FLIGHT,SG_INFO,"sin_psi: " << sin_psi );
775     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
776     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_horiz_rad: " << gamma_horiz_rad );
777     SG_LOG(SG_FLIGHT,SG_INFO,"sigma: " << sigma );
778     SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
779     SG_LOG(SG_FLIGHT,SG_INFO,"v_sound: " << v_sound );
780     SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
781     SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
782     SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
783     SG_LOG(SG_FLIGHT,SG_INFO,"impact_pressure: " << impact_pressure );
784     SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
785     SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
786     SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
787     SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
788     SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
789     SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
790     SG_LOG(SG_FLIGHT,SG_INFO,"runway_latitude: " << runway_latitude );
791     SG_LOG(SG_FLIGHT,SG_INFO,"runway_longitude: " << runway_longitude );
792     SG_LOG(SG_FLIGHT,SG_INFO,"runway_heading: " << runway_heading );
793     SG_LOG(SG_FLIGHT,SG_INFO,"radius_to_rwy: " << radius_to_rwy );
794     SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
795     SG_LOG(SG_FLIGHT,SG_INFO,"sin_lat_geocentric: " << sin_lat_geocentric );
796     SG_LOG(SG_FLIGHT,SG_INFO,"cos_lat_geocentric: " << cos_lat_geocentric );
797     SG_LOG(SG_FLIGHT,SG_INFO,"sin_longitude: " << sin_longitude );
798     SG_LOG(SG_FLIGHT,SG_INFO,"cos_longitude: " << cos_longitude );
799     SG_LOG(SG_FLIGHT,SG_INFO,"sin_latitude: " << sin_latitude );
800     SG_LOG(SG_FLIGHT,SG_INFO,"cos_latitude: " << cos_latitude );
801     SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
802 }
803
804 bool
805 FGInterface::prepare_ground_cache_m(double ref_time, const double pt[3],
806                                     double rad)
807 {
808   return ground_cache.prepare_ground_cache(ref_time, SGVec3d(pt), rad);
809 }
810
811 bool FGInterface::prepare_ground_cache_ft(double ref_time, const double pt[3],
812                                           double rad)
813 {
814   // Convert units and do the real work.
815   SGVec3d pt_ft = SG_FEET_TO_METER*SGVec3d(pt);
816   return ground_cache.prepare_ground_cache(ref_time, pt_ft, rad*SG_FEET_TO_METER);
817 }
818
819 bool
820 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
821 {
822   SGVec3d _pt;
823   bool valid = ground_cache.is_valid(*ref_time, _pt, *rad);
824   sgdCopyVec3(pt, _pt.data());
825   return valid;
826 }
827
828 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
829 {
830   // Convert units and do the real work.
831   SGVec3d _pt;
832   bool found_ground = ground_cache.is_valid(*ref_time, _pt, *rad);
833   sgdScaleVec3(pt, _pt.data(), SG_METER_TO_FEET);
834   *rad *= SG_METER_TO_FEET;
835   return found_ground;
836 }
837
838 double
839 FGInterface::get_cat_m(double t, const double pt[3],
840                        double end[2][3], double vel[2][3])
841 {
842   SGVec3d _end[2], _vel[2];
843   double dist = ground_cache.get_cat(t, SGVec3d(pt), _end, _vel);
844   for (int k=0; k<2; ++k) {
845     sgdCopyVec3( end[k], _end[k].data() );
846     sgdCopyVec3( vel[k], _vel[k].data() );
847   }
848   return dist;
849 }
850
851 double
852 FGInterface::get_cat_ft(double t, const double pt[3],
853                         double end[2][3], double vel[2][3])
854 {
855   // Convert units and do the real work.
856   SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
857   SGVec3d _end[2], _vel[2];
858   double dist = ground_cache.get_cat(t, pt_m, _end, _vel);
859   for (int k=0; k<2; ++k) {
860     sgdScaleVec3( end[k], _end[k].data(), SG_METER_TO_FEET );
861     sgdScaleVec3( vel[k], _vel[k].data(), SG_METER_TO_FEET );
862   }
863   return dist*SG_METER_TO_FEET;
864 }
865
866 // Legacy interface just kept because of JSBSim
867 bool
868 FGInterface::get_agl_m(double t, const double pt[3],
869                        double contact[3], double normal[3], double vel[3],
870                        int *type, double *loadCapacity,
871                        double *frictionFactor, double *agl)
872 {
873   const SGMaterial* material;
874   SGVec3d _contact, _normal, _vel;
875   bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
876                                   _vel, type, &material, agl);
877   sgdCopyVec3(contact, _contact.data());
878   sgdCopyVec3(normal, _normal.data());
879   sgdCopyVec3(vel, _vel.data());
880   if (material) {
881     *loadCapacity = material->get_load_resistance();
882     *frictionFactor = material->get_friction_factor();
883
884   } else {
885     *loadCapacity = DBL_MAX;
886     *frictionFactor = 1.0;
887   }
888   return ret;
889 }
890
891 bool
892 FGInterface::get_agl_m(double t, const double pt[3],
893                        double contact[3], double normal[3], double vel[3],
894                        int *type, const SGMaterial **material, double *agl)
895 {
896   SGVec3d _contact, _normal, _vel;
897   bool ret = ground_cache.get_agl(t, SGVec3d(pt), 2.0, _contact, _normal,
898                                   _vel, type, material, agl);
899   sgdCopyVec3(contact, _contact.data());
900   sgdCopyVec3(normal, _normal.data());
901   sgdCopyVec3(vel, _vel.data());
902   return ret;
903 }
904
905 // Legacy interface just kept because of JSBSim
906 bool
907 FGInterface::get_agl_ft(double t, const double pt[3],
908                         double contact[3], double normal[3], double vel[3],
909                         int *type, double *loadCapacity,
910                         double *frictionFactor, double *agl)
911 {
912   // Convert units and do the real work.
913   SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
914
915   const SGMaterial* material;
916   SGVec3d _contact, _normal, _vel;
917   bool ret = ground_cache.get_agl(t, pt_m, 2.0, _contact, _normal, _vel,
918                                   type, &material, agl);
919   // Convert units back ...
920   sgdScaleVec3( contact, _contact.data(), SG_METER_TO_FEET );
921   sgdScaleVec3( vel, _vel.data(), SG_METER_TO_FEET );
922   sgdCopyVec3( normal, _normal.data() );
923   *agl *= SG_METER_TO_FEET;
924
925   // return material properties if available
926   if (material) {
927     // FIXME: convert units?? now pascal to lbf/ft^2
928     *loadCapacity = 0.020885434*material->get_load_resistance();
929     *frictionFactor = material->get_friction_factor();
930   } else {
931     *loadCapacity = DBL_MAX;
932     *frictionFactor = 1.0;
933   }
934   return ret;
935 }
936
937 bool
938 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
939                        double contact[3], double normal[3], double vel[3],
940                        int *type, const SGMaterial** material, double *agl)
941 {
942   SGVec3d _contact, _normal, _vel;
943   bool found = ground_cache.get_agl(t, SGVec3d(pt), max_altoff, _contact,
944                                     _normal, _vel, type, material, agl);
945   sgdCopyVec3(contact, _contact.data());
946   sgdCopyVec3(normal, _normal.data());
947   sgdCopyVec3(vel, _vel.data());
948   return found;
949 }
950
951 bool
952 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
953                         double contact[3], double normal[3], double vel[3],
954                         int *type, const SGMaterial** material, double *agl)
955 {
956   // Convert units and do the real work.
957   SGVec3d pt_m = SG_FEET_TO_METER*SGVec3d(pt);
958   SGVec3d _contact, _normal, _vel;
959   bool ret = ground_cache.get_agl(t, pt_m, SG_FEET_TO_METER * max_altoff,
960                                   _contact, _normal, _vel,
961                                   type, material, agl);
962   // Convert units back ...
963   sgdScaleVec3( contact, _contact.data(), SG_METER_TO_FEET );
964   sgdScaleVec3( vel, _vel.data(), SG_METER_TO_FEET );
965   sgdCopyVec3( normal, _normal.data() );
966   *agl *= SG_METER_TO_FEET;
967   return ret;
968 }
969
970
971 double
972 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
973 {
974   // Compute the cartesian position of the given lat/lon/alt.
975   SGVec3d pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, alt));
976
977   // FIXME: how to handle t - ref_time differences ???
978   SGVec3d cpos;
979   double ref_time, radius;
980   // Prepare the ground cache for that position.
981   if (!is_valid_m(&ref_time, cpos.data(), &radius)) {
982     bool ok = prepare_ground_cache_m(ref_time, pos.data(), 10);
983     /// This is most likely the case when the given altitude is
984     /// too low, try with a new altitude of 10000m, that should be
985     /// sufficient to find a ground level below everywhere on our planet
986     if (!ok) {
987       pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, 10000));
988       /// If there is still no ground, return sea level radius
989       if (!prepare_ground_cache_m(ref_time, pos.data(), 10))
990         return 0;
991     }
992   } else if (radius*radius <= distSqr(pos, cpos)) {
993     /// We reuse the old radius value, but only if it is at least 10 Meters ..
994     if (!(10 < radius)) // Well this strange compare is nan safe
995       radius = 10;
996
997     bool ok = prepare_ground_cache_m(ref_time, pos.data(), radius);
998     /// This is most likely the case when the given altitude is
999     /// too low, try with a new altitude of 10000m, that should be
1000     /// sufficient to find a ground level below everywhere on our planet
1001     if (!ok) {
1002       pos = SGVec3d::fromGeod(SGGeod::fromRadM(lon, lat, 10000));
1003       /// If there is still no ground, return sea level radius
1004       if (!prepare_ground_cache_m(ref_time, pos.data(), radius))
1005         return 0;
1006     }
1007   }
1008   
1009   double contact[3], normal[3], vel[3], agl;
1010   int type;
1011   // Ignore the return value here, since it just tells us if
1012   // the returns stem from the groundcache or from the coarse
1013   // computations below the groundcache. The contact point is still something
1014   // valid, the normals and the other returns just contain some defaults.
1015   get_agl_m(ref_time, pos.data(), 2.0, contact, normal, vel, &type, 0, &agl);
1016   SGGeod geod = SGGeod::fromCart(SGVec3d(contact));
1017   return geod.getElevationM();
1018 }
1019   
1020 bool
1021 FGInterface::caught_wire_m(double t, const double pt[4][3])
1022 {
1023   SGVec3d pt_m[4];
1024   for (int i=0; i<4; ++i)
1025     sgdCopyVec3(pt_m[i].data(), pt[i]);
1026   
1027   return ground_cache.caught_wire(t, pt_m);
1028 }
1029
1030 bool
1031 FGInterface::caught_wire_ft(double t, const double pt[4][3])
1032 {
1033   // Convert units and do the real work.
1034   SGVec3d pt_m[4];
1035   for (int i=0; i<4; ++i)
1036     sgdScaleVec3(pt_m[i].data(), pt[i], SG_FEET_TO_METER);
1037     
1038   return ground_cache.caught_wire(t, pt_m);
1039 }
1040   
1041 bool
1042 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
1043 {
1044   SGVec3d _end[2], _vel[2];
1045   bool ret = ground_cache.get_wire_ends(t, _end, _vel);
1046   for (int k=0; k<2; ++k) {
1047     sgdCopyVec3( end[k], _end[k].data() );
1048     sgdCopyVec3( vel[k], _vel[k].data() );
1049   }
1050   return ret;
1051 }
1052
1053 bool
1054 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
1055 {
1056   // Convert units and do the real work.
1057   SGVec3d _end[2], _vel[2];
1058   bool ret = ground_cache.get_wire_ends(t, _end, _vel);
1059   for (int k=0; k<2; ++k) {
1060     sgdScaleVec3( end[k], _end[k].data(), SG_METER_TO_FEET );
1061     sgdScaleVec3( vel[k], _vel[k].data(), SG_METER_TO_FEET );
1062   }
1063   return ret;
1064 }
1065
1066 void
1067 FGInterface::release_wire(void)
1068 {
1069   ground_cache.release_wire();
1070 }
1071
1072 void fgToggleFDMdataLogging(void) {
1073   cur_fdm_state->ToggleDataLogging();
1074 }