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