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