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