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