]> git.mxchange.org Git - flightgear.git/blob - src/FDM/flight.cxx
9bda7929a94ccee04dc0a25c3bd01f27240f8466
[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     double lon = fgGetDouble("/sim/presets/longitude-deg")
192       * SGD_DEGREES_TO_RADIANS;
193     double lat = fgGetDouble("/sim/presets/latitude-deg")
194       * SGD_DEGREES_TO_RADIANS;
195     double alt_ft = fgGetDouble("/sim/presets/altitude-ft");
196     double alt_m = alt_ft * SG_FEET_TO_METER;
197     set_Longitude( lon );
198     set_Latitude( lat );
199
200     double ground_elev_m = get_groundlevel_m(lat, lon, alt_m);
201     double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
202     _set_Runway_altitude ( ground_elev_ft );
203     if ( fgGetBool("/sim/presets/onground") || alt_ft < ground_elev_ft ) {
204         fgSetDouble("/position/altitude-ft", ground_elev_ft + 0.1);
205         set_Altitude( ground_elev_ft + 0.1);
206     } else {
207         set_Altitude( alt_ft );
208     }
209
210     // Set ground elevation
211     SG_LOG( SG_FLIGHT, SG_INFO,
212             "...initializing ground elevation to " << ground_elev_ft
213             << "ft..." );
214
215     // Set sea-level radius
216     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
217     SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
218             << fgGetDouble("/sim/presets/latitude-deg")
219             << " alt = " << get_Altitude() );
220     double sea_level_radius_meters;
221     double lat_geoc;
222     sgGeodToGeoc( fgGetDouble("/sim/presets/latitude-deg")
223                     * SGD_DEGREES_TO_RADIANS,
224                   get_Altitude() * SG_FEET_TO_METER,
225                   &sea_level_radius_meters, &lat_geoc );
226     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
227
228     // Set initial velocities
229     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
230     if ( !fgHasNode("/sim/presets/speed-set") ) {
231         set_V_calibrated_kts(0.0);
232     } else {
233         const string speedset = fgGetString("/sim/presets/speed-set");
234         if ( speedset == "knots" || speedset == "KNOTS" ) {
235             set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
236         } else if ( speedset == "mach" || speedset == "MACH" ) {
237             set_Mach_number( fgGetDouble("/sim/presets/mach") );
238         } else if ( speedset == "UVW" || speedset == "uvw" ) {
239             set_Velocities_Wind_Body(
240                                      fgGetDouble("/sim/presets/uBody-fps"),
241                                      fgGetDouble("/sim/presets/vBody-fps"),
242                                      fgGetDouble("/sim/presets/wBody-fps") );
243         } else if ( speedset == "NED" || speedset == "ned" ) {
244             set_Velocities_Local(
245                                  fgGetDouble("/sim/presets/speed-north-fps"),
246                                  fgGetDouble("/sim/presets/speed-east-fps"),
247                                  fgGetDouble("/sim/presets/speed-down-fps") );
248         } else {
249             SG_LOG( SG_FLIGHT, SG_ALERT,
250                     "Unrecognized value for /sim/presets/speed-set: "
251                     << speedset);
252             set_V_calibrated_kts( 0.0 );
253         }
254     }
255
256     // Set initial Euler angles
257     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
258     set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
259                         * SGD_DEGREES_TO_RADIANS,
260                       fgGetDouble("/sim/presets/pitch-deg")
261                         * SGD_DEGREES_TO_RADIANS,
262                       fgGetDouble("/sim/presets/heading-deg")
263                         * SGD_DEGREES_TO_RADIANS );
264
265     SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
266 }
267
268
269 /**
270  * Bind getters and setters to properties.
271  *
272  * The bind() method will be invoked after init().  Note that unlike
273  * the usual implementations of FGSubsystem::bind(), this method does
274  * not automatically pick up existing values for the properties at
275  * bind time; instead, all values are set explicitly in the init()
276  * method.
277  */
278 void
279 FGInterface::bind ()
280 {
281   bound = true;
282
283                                 // Time management (read-only)
284 //   fgTie("/fdm/time/delta_t", this,
285 //         &FGInterface::get_delta_t); // read-only
286 //   fgTie("/fdm/time/elapsed", this,
287 //         &FGInterface::get_elapsed); // read-only
288 //   fgTie("/fdm/time/remainder", this,
289 //         &FGInterface::get_remainder); // read-only
290 //   fgTie("/fdm/time/multi_loop", this,
291 //         &FGInterface::get_multi_loop); // read-only
292
293                         // Aircraft position
294   fgTie("/position/latitude-deg", this,
295         &FGInterface::get_Latitude_deg,
296         &FGInterface::set_Latitude_deg,
297         false);
298   fgSetArchivable("/position/latitude-deg");
299   fgTie("/position/longitude-deg", this,
300         &FGInterface::get_Longitude_deg,
301         &FGInterface::set_Longitude_deg,
302         false);
303   fgSetArchivable("/position/longitude-deg");
304   fgTie("/position/altitude-ft", this,
305         &FGInterface::get_Altitude,
306         &FGInterface::set_Altitude,
307         false);
308   fgSetArchivable("/position/altitude-ft");
309   fgTie("/position/altitude-agl-ft", this,
310         &FGInterface::get_Altitude_AGL); // read-only
311   fgSetArchivable("/position/ground-elev-ft");
312   fgTie("/position/ground-elev-ft", this,
313         &FGInterface::get_Runway_altitude); // read-only
314   fgSetArchivable("/position/ground-elev-m");
315   fgTie("/position/ground-elev-m", this,
316         &FGInterface::get_Runway_altitude_m); // read-only
317   fgTie("/environment/ground-elevation-m", this,
318         &FGInterface::get_Runway_altitude_m); // read-only
319   fgSetArchivable("/position/sea-level-radius-ft");
320   fgTie("/position/sea-level-radius-ft", this,
321         &FGInterface::get_Sea_level_radius); // read-only
322
323                                 // Orientation
324   fgTie("/orientation/roll-deg", this,
325         &FGInterface::get_Phi_deg,
326         &FGInterface::set_Phi_deg);
327   fgSetArchivable("/orientation/roll-deg");
328   fgTie("/orientation/pitch-deg", this,
329         &FGInterface::get_Theta_deg,
330         &FGInterface::set_Theta_deg);
331   fgSetArchivable("/orientation/pitch-deg");
332   fgTie("/orientation/heading-deg", this,
333         &FGInterface::get_Psi_deg,
334         &FGInterface::set_Psi_deg);
335   fgSetArchivable("/orientation/heading-deg");
336
337   // Body-axis "euler rates" (rotation speed, but in a funny
338   // representation).
339   fgTie("/orientation/roll-rate-degps", this,
340         &FGInterface::get_Phi_dot_degps);
341   fgTie("/orientation/pitch-rate-degps", this,
342         &FGInterface::get_Theta_dot_degps);
343   fgTie("/orientation/yaw-rate-degps", this,
344         &FGInterface::get_Psi_dot_degps);
345
346                                 // Calibrated airspeed
347   fgTie("/velocities/airspeed-kt", this,
348         &FGInterface::get_V_calibrated_kts,
349         &FGInterface::set_V_calibrated_kts,
350         false);
351
352                                 // Mach number
353   fgTie("/velocities/mach", this,
354         &FGInterface::get_Mach_number,
355         &FGInterface::set_Mach_number,
356         false);
357
358                                 // Local velocities
359 //   fgTie("/velocities/speed-north-fps", this,
360 //      &FGInterface::get_V_north,
361 //      &FGInterface::set_V_north);
362 //   fgSetArchivable("/velocities/speed-north-fps");
363 //   fgTie("/velocities/speed-east-fps", this,
364 //      &FGInterface::get_V_east,
365 //      &FGInterface::set_V_east);
366 //   fgSetArchivable("/velocities/speed-east-fps");
367 //   fgTie("/velocities/speed-down-fps", this,
368 //      &FGInterface::get_V_down,
369 //      &FGInterface::set_V_down);
370 //   fgSetArchivable("/velocities/speed-down-fps");
371                                 // FIXME: Temporarily read-only, until the
372                                 // incompatibilities between JSBSim and
373                                 // LaRCSim are fixed (LaRCSim adds the
374                                 // earth's rotation to the east velocity).
375   fgTie("/velocities/speed-north-fps", this,
376         &FGInterface::get_V_north);
377   fgTie("/velocities/speed-east-fps", this,
378         &FGInterface::get_V_east);
379   fgTie("/velocities/speed-down-fps", this,
380         &FGInterface::get_V_down);
381
382                                 // Relative wind
383                                 // FIXME: temporarily archivable, until
384                                 // the NED problem is fixed.
385   fgTie("/velocities/uBody-fps", this,
386         &FGInterface::get_uBody,
387         &FGInterface::set_uBody,
388         false);
389   fgSetArchivable("/velocities/uBody-fps");
390   fgTie("/velocities/vBody-fps", this,
391         &FGInterface::get_vBody,
392         &FGInterface::set_vBody,
393         false);
394   fgSetArchivable("/velocities/vBody-fps");
395   fgTie("/velocities/wBody-fps", this,
396         &FGInterface::get_wBody,
397         &FGInterface::set_wBody,
398         false);
399   fgSetArchivable("/velocities/wBody-fps");
400
401                                 // Climb and slip (read-only)
402   fgTie("/velocities/vertical-speed-fps", this,
403         &FGInterface::get_Climb_Rate,
404   &FGInterface::set_Climb_Rate ); 
405   fgTie("/velocities/glideslope", this,
406   &FGInterface::get_Gamma_vert_rad,
407   &FGInterface::set_Gamma_vert_rad );
408   fgTie("/orientation/side-slip-rad", this,
409         &FGInterface::get_Beta); // read-only
410   fgTie("/orientation/side-slip-deg", this,
411   &FGInterface::get_Beta_deg); // read-only
412   fgTie("/orientation/alpha-deg", this,
413   &FGInterface::get_Alpha_deg); // read-only
414   fgTie("/accelerations/nlf", this,
415   &FGInterface::get_Nlf); // read-only
416
417                                 // NED accelerations
418   fgTie("/accelerations/ned/north-accel-fps_sec",
419         this, &FGInterface::get_V_dot_north);
420   fgTie("/accelerations/ned/east-accel-fps_sec",
421         this, &FGInterface::get_V_dot_east);
422   fgTie("/accelerations/ned/down-accel-fps_sec",
423         this, &FGInterface::get_V_dot_down);
424
425                                 // Pilot accelerations
426   fgTie("/accelerations/pilot/x-accel-fps_sec",
427         this, &FGInterface::get_A_X_pilot);
428   fgTie("/accelerations/pilot/y-accel-fps_sec",
429         this, &FGInterface::get_A_Y_pilot);
430   fgTie("/accelerations/pilot/z-accel-fps_sec",
431         this, &FGInterface::get_A_Z_pilot);
432
433 }
434
435
436 /**
437  * Unbind any properties bound to this FDM.
438  *
439  * This method allows the FDM to release properties so that a new
440  * FDM can bind them instead.
441  */
442 void
443 FGInterface::unbind ()
444 {
445   bound = false;
446
447   // fgUntie("/fdm/time/delta_t");
448   // fgUntie("/fdm/time/elapsed");
449   // fgUntie("/fdm/time/remainder");
450   // fgUntie("/fdm/time/multi_loop");
451   fgUntie("/position/latitude-deg");
452   fgUntie("/position/longitude-deg");
453   fgUntie("/position/altitude-ft");
454   fgUntie("/position/altitude-agl-ft");
455   fgUntie("/position/ground-elev-ft");
456   fgUntie("/position/ground-elev-m");
457   fgUntie("/environment/ground-elevation-m");
458   fgUntie("/position/sea-level-radius-ft");
459   fgUntie("/orientation/roll-deg");
460   fgUntie("/orientation/pitch-deg");
461   fgUntie("/orientation/heading-deg");
462   fgUntie("/orientation/roll-rate-degps");
463   fgUntie("/orientation/pitch-rate-degps");
464   fgUntie("/orientation/yaw-rate-degps");
465   fgUntie("/orientation/side-slip-rad");
466   fgUntie("/orientation/side-slip-deg");
467   fgUntie("/orientation/alpha-deg");
468   fgUntie("/velocities/airspeed-kt");
469   fgUntie("/velocities/mach");
470   fgUntie("/velocities/speed-north-fps");
471   fgUntie("/velocities/speed-east-fps");
472   fgUntie("/velocities/speed-down-fps");
473   fgUntie("/velocities/uBody-fps");
474   fgUntie("/velocities/vBody-fps");
475   fgUntie("/velocities/wBody-fps");
476   fgUntie("/velocities/vertical-speed-fps");
477   fgUntie("/velocities/glideslope");
478   fgUntie("/accelerations/nlf");
479   fgUntie("/accelerations/pilot/x-accel-fps_sec");
480   fgUntie("/accelerations/pilot/y-accel-fps_sec");
481   fgUntie("/accelerations/pilot/z-accel-fps_sec");
482   fgUntie("/accelerations/ned/north-accel-fps_sec");
483   fgUntie("/accelerations/ned/east-accel-fps_sec");
484   fgUntie("/accelerations/ned/down-accel-fps_sec");
485 }
486
487 /**
488  * Update the state of the FDM (i.e. run the equations of motion).
489  */
490 void
491 FGInterface::update (double dt)
492 {
493     SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
494 }
495
496
497 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
498 {
499     double lat_geoc, sl_radius;
500
501     // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
502
503     sgGeodToGeoc( lat, alt * SG_FEET_TO_METER, &sl_radius, &lat_geoc );
504
505     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon 
506             << " lat_geod = " << lat
507             << " lat_geoc = " << lat_geoc
508             << " alt = " << alt 
509             << " sl_radius = " << sl_radius * SG_METER_TO_FEET
510             << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
511
512     _set_Geocentric_Position( lat_geoc, lon, 
513                               sl_radius * SG_METER_TO_FEET + alt );
514         
515     _set_Geodetic_Position( lat, lon, alt );
516
517     _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET );
518     double alt_m = alt*SG_FEET_TO_METER;
519     double groundlevel_m = get_groundlevel_m(lat, lon, alt_m);
520     _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
521
522     _set_sin_lat_geocentric( lat_geoc );
523     _set_cos_lat_geocentric( lat_geoc );
524
525     _set_sin_cos_longitude( lon );
526
527     _set_sin_cos_latitude( lat );
528 }
529
530
531 void FGInterface::_updateGeocentricPosition( double lat_geoc, double lon,
532                                              double alt )
533 {
534     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
535
536     // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
537
538     sgGeocToGeod( lat_geoc, ( get_Sea_level_radius() + alt ) * SG_FEET_TO_METER,
539                   &lat_geod, &tmp_alt, &sl_radius1 );
540     sgGeodToGeoc( lat_geod, alt * SG_FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
541
542     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon 
543             << " lat_geod = " << lat_geod
544             << " lat_geoc = " << lat_geoc
545             << " alt = " << alt 
546             << " tmp_alt = " << tmp_alt * SG_METER_TO_FEET
547             << " sl_radius1 = " << sl_radius1 * SG_METER_TO_FEET
548             << " sl_radius2 = " << sl_radius2 * SG_METER_TO_FEET
549             << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
550
551     _set_Geocentric_Position( lat_geoc, lon, 
552                               sl_radius2 * SG_METER_TO_FEET + alt );
553         
554     _set_Geodetic_Position( lat_geod, lon, alt );
555
556     _set_Sea_level_radius( sl_radius2 * SG_METER_TO_FEET );
557     double alt_m = alt*SG_FEET_TO_METER;
558     double groundlevel_m = get_groundlevel_m(lat_geod, lon, alt_m);
559     _set_Runway_altitude( groundlevel_m * SG_METER_TO_FEET );
560
561     _set_sin_lat_geocentric( lat_geoc );
562     _set_cos_lat_geocentric( lat_geoc );
563
564     _set_sin_cos_longitude( lon );
565
566     _set_sin_cos_latitude( lat_geod );
567 }
568
569
570 // Extrapolate fdm based on time_offset (in usec)
571 void FGInterface::extrapolate( int time_offset ) {
572     double dt = time_offset / 1000000.0;
573
574     // -dw- metrowerks complains about ambiguous access, not critical
575     // to keep this ;)
576 #ifndef __MWERKS__
577     SG_LOG(SG_FLIGHT, SG_INFO, "extrapolating FDM by dt = " << dt);
578 #endif
579
580     double lat = geodetic_position_v[0] + geocentric_rates_v[0] * dt;
581     double lat_geoc = geocentric_position_v[0] + geocentric_rates_v[0] * dt;
582
583     double lon = geodetic_position_v[1] + geocentric_rates_v[1] * dt;
584     double lon_geoc = geocentric_position_v[1] + geocentric_rates_v[1] * dt;
585
586     double alt = geodetic_position_v[2] + geocentric_rates_v[2] * dt;
587     double radius = geocentric_position_v[2] + geocentric_rates_v[2] * dt;
588
589     geodetic_position_v[0] = lat;
590     geocentric_position_v[0] = lat_geoc;
591
592     geodetic_position_v[1] = lon;
593     geocentric_position_v[1] = lon_geoc;
594
595     geodetic_position_v[2] = alt;
596     geocentric_position_v[2] = radius;
597 }
598
599 // Positions
600 void FGInterface::set_Latitude(double lat) {
601     geodetic_position_v[0] = lat;
602 }
603
604 void FGInterface::set_Longitude(double lon) {
605     geodetic_position_v[1] = lon;
606 }
607
608 void FGInterface::set_Altitude(double alt) {
609     geodetic_position_v[2] = alt;
610 }
611
612 void FGInterface::set_AltitudeAGL(double altagl) {
613     altitude_agl=altagl;
614 }
615
616 // Velocities
617 void FGInterface::set_V_calibrated_kts(double vc) {
618     v_calibrated_kts = vc;
619 }
620
621 void FGInterface::set_Mach_number(double mach) {
622     mach_number = mach;
623 }
624
625 void FGInterface::set_Velocities_Local( double north, 
626                                         double east, 
627                                         double down ){
628     v_local_v[0] = north;
629     v_local_v[1] = east;
630     v_local_v[2] = down;
631 }
632
633 void FGInterface::set_Velocities_Wind_Body( double u, 
634                                             double v, 
635                                             double w){
636     v_wind_body_v[0] = u;
637     v_wind_body_v[1] = v;
638     v_wind_body_v[2] = w;
639 }
640
641 // Euler angles 
642 void FGInterface::set_Euler_Angles( double phi, 
643                                     double theta, 
644                                     double psi ) {
645     euler_angles_v[0] = phi;
646     euler_angles_v[1] = theta;
647     euler_angles_v[2] = psi;                                            
648 }  
649
650 // Flight Path
651 void FGInterface::set_Climb_Rate( double roc) {
652     climb_rate = roc;
653 }
654
655 void FGInterface::set_Gamma_vert_rad( double gamma) {
656     gamma_vert_rad = gamma;
657 }
658
659 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
660 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
661 void FGInterface::set_Density(double rho) { density = rho; }
662
663 void FGInterface::set_Velocities_Local_Airmass (double wnorth, 
664                                                 double weast, 
665                                                 double wdown ) {
666     v_local_airmass_v[0] = wnorth;
667     v_local_airmass_v[1] = weast;
668     v_local_airmass_v[2] = wdown;
669 }
670
671
672 void FGInterface::_busdump(void) {
673
674     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]);
675     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]);
676     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]);
677     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]);
678     SG_LOG(SG_FLIGHT,SG_INFO,"f_aero_v[3]: " << f_aero_v[0] << ", " << f_aero_v[1] << ", " << f_aero_v[2]);
679     SG_LOG(SG_FLIGHT,SG_INFO,"f_engine_v[3]: " << f_engine_v[0] << ", " << f_engine_v[1] << ", " << f_engine_v[2]);
680     SG_LOG(SG_FLIGHT,SG_INFO,"f_gear_v[3]: " << f_gear_v[0] << ", " << f_gear_v[1] << ", " << f_gear_v[2]);
681     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]);
682     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]);
683     SG_LOG(SG_FLIGHT,SG_INFO,"m_aero_v[3]: " << m_aero_v[0] << ", " << m_aero_v[1] << ", " << m_aero_v[2]);
684     SG_LOG(SG_FLIGHT,SG_INFO,"m_engine_v[3]: " << m_engine_v[0] << ", " << m_engine_v[1] << ", " << m_engine_v[2]);
685     SG_LOG(SG_FLIGHT,SG_INFO,"m_gear_v[3]: " << m_gear_v[0] << ", " << m_gear_v[1] << ", " << m_gear_v[2]);
686     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]);
687     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]);
688     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]);
689     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]);
690     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]);
691     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]);
692     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]);
693     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v[3]: " << v_local_v[0] << ", " << v_local_v[1] << ", " << v_local_v[2]);
694     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]);
695     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]);
696     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]);
697     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]);
698     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]);
699     SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v[3]: " << omega_body_v[0] << ", " << omega_body_v[1] << ", " << omega_body_v[2]);
700     SG_LOG(SG_FLIGHT,SG_INFO,"omega_local_v[3]: " << omega_local_v[0] << ", " << omega_local_v[1] << ", " << omega_local_v[2]);
701     SG_LOG(SG_FLIGHT,SG_INFO,"omega_total_v[3]: " << omega_total_v[0] << ", " << omega_total_v[1] << ", " << omega_total_v[2]);
702     SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v[3]: " << euler_rates_v[0] << ", " << euler_rates_v[1] << ", " << euler_rates_v[2]);
703     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v[3]: " << geocentric_rates_v[0] << ", " << geocentric_rates_v[1] << ", " << geocentric_rates_v[2]);
704     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v[3]: " << geocentric_position_v[0] << ", " << geocentric_position_v[1] << ", " << geocentric_position_v[2]);
705     SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v[3]: " << geodetic_position_v[0] << ", " << geodetic_position_v[1] << ", " << geodetic_position_v[2]);
706     SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v[3]: " << euler_angles_v[0] << ", " << euler_angles_v[1] << ", " << euler_angles_v[2]);
707     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]);
708     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]);
709     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]);
710     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]);
711
712     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]);
713     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]);
714     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]);
715
716     SG_LOG(SG_FLIGHT,SG_INFO,"mass: " << mass );
717     SG_LOG(SG_FLIGHT,SG_INFO,"i_xx: " << i_xx );
718     SG_LOG(SG_FLIGHT,SG_INFO,"i_yy: " << i_yy );
719     SG_LOG(SG_FLIGHT,SG_INFO,"i_zz: " << i_zz );
720     SG_LOG(SG_FLIGHT,SG_INFO,"i_xz: " << i_xz );
721     SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
722     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
723     SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
724     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_ground: " << v_rel_ground );
725     SG_LOG(SG_FLIGHT,SG_INFO,"v_inertial: " << v_inertial );
726     SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
727     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv: " << v_equiv );
728     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
729     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated: " << v_calibrated );
730     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
731     SG_LOG(SG_FLIGHT,SG_INFO,"gravity: " << gravity );
732     SG_LOG(SG_FLIGHT,SG_INFO,"centrifugal_relief: " << centrifugal_relief );
733     SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
734     SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
735     SG_LOG(SG_FLIGHT,SG_INFO,"alpha_dot: " << alpha_dot );
736     SG_LOG(SG_FLIGHT,SG_INFO,"beta_dot: " << beta_dot );
737     SG_LOG(SG_FLIGHT,SG_INFO,"cos_alpha: " << cos_alpha );
738     SG_LOG(SG_FLIGHT,SG_INFO,"sin_alpha: " << sin_alpha );
739     SG_LOG(SG_FLIGHT,SG_INFO,"cos_beta: " << cos_beta );
740     SG_LOG(SG_FLIGHT,SG_INFO,"sin_beta: " << sin_beta );
741     SG_LOG(SG_FLIGHT,SG_INFO,"cos_phi: " << cos_phi );
742     SG_LOG(SG_FLIGHT,SG_INFO,"sin_phi: " << sin_phi );
743     SG_LOG(SG_FLIGHT,SG_INFO,"cos_theta: " << cos_theta );
744     SG_LOG(SG_FLIGHT,SG_INFO,"sin_theta: " << sin_theta );
745     SG_LOG(SG_FLIGHT,SG_INFO,"cos_psi: " << cos_psi );
746     SG_LOG(SG_FLIGHT,SG_INFO,"sin_psi: " << sin_psi );
747     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
748     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_horiz_rad: " << gamma_horiz_rad );
749     SG_LOG(SG_FLIGHT,SG_INFO,"sigma: " << sigma );
750     SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
751     SG_LOG(SG_FLIGHT,SG_INFO,"v_sound: " << v_sound );
752     SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
753     SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
754     SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
755     SG_LOG(SG_FLIGHT,SG_INFO,"impact_pressure: " << impact_pressure );
756     SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
757     SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
758     SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
759     SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
760     SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
761     SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
762     SG_LOG(SG_FLIGHT,SG_INFO,"runway_latitude: " << runway_latitude );
763     SG_LOG(SG_FLIGHT,SG_INFO,"runway_longitude: " << runway_longitude );
764     SG_LOG(SG_FLIGHT,SG_INFO,"runway_heading: " << runway_heading );
765     SG_LOG(SG_FLIGHT,SG_INFO,"radius_to_rwy: " << radius_to_rwy );
766     SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
767     SG_LOG(SG_FLIGHT,SG_INFO,"sin_lat_geocentric: " << sin_lat_geocentric );
768     SG_LOG(SG_FLIGHT,SG_INFO,"cos_lat_geocentric: " << cos_lat_geocentric );
769     SG_LOG(SG_FLIGHT,SG_INFO,"sin_longitude: " << sin_longitude );
770     SG_LOG(SG_FLIGHT,SG_INFO,"cos_longitude: " << cos_longitude );
771     SG_LOG(SG_FLIGHT,SG_INFO,"sin_latitude: " << sin_latitude );
772     SG_LOG(SG_FLIGHT,SG_INFO,"cos_latitude: " << cos_latitude );
773     SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
774 }
775
776 bool
777 FGInterface::prepare_ground_cache_m(double ref_time, const double pt[3],
778                                     double rad)
779 {
780   return ground_cache.prepare_ground_cache(ref_time, pt, rad);
781 }
782
783 bool FGInterface::prepare_ground_cache_ft(double ref_time, const double pt[3],
784                                           double rad)
785 {
786   // Convert units and do the real work.
787   sgdVec3 pt_ft;
788   sgdScaleVec3( pt_ft, pt, SG_FEET_TO_METER );
789   return ground_cache.prepare_ground_cache(ref_time, pt_ft, rad*SG_FEET_TO_METER);
790 }
791
792 bool
793 FGInterface::is_valid_m(double *ref_time, double pt[3], double *rad)
794 {
795   return ground_cache.is_valid(ref_time, pt, rad);
796 }
797
798 bool FGInterface::is_valid_ft(double *ref_time, double pt[3], double *rad)
799 {
800   // Convert units and do the real work.
801   bool found_ground = ground_cache.is_valid(ref_time, pt, rad);
802   sgdScaleVec3(pt, SG_METER_TO_FEET);
803   *rad *= SG_METER_TO_FEET;
804   return found_ground;
805 }
806
807 double
808 FGInterface::get_cat_m(double t, const double pt[3],
809                        double end[2][3], double vel[2][3])
810 {
811   return ground_cache.get_cat(t, pt, end, vel);
812 }
813
814 double
815 FGInterface::get_cat_ft(double t, const double pt[3],
816                         double end[2][3], double vel[2][3])
817 {
818   // Convert units and do the real work.
819   sgdVec3 pt_m;
820   sgdScaleVec3( pt_m, pt, SG_FEET_TO_METER );
821   double dist = ground_cache.get_cat(t, pt_m, end, vel);
822   for (int k=0; k<2; ++k) {
823     sgdScaleVec3( end[k], SG_METER_TO_FEET );
824     sgdScaleVec3( vel[k], SG_METER_TO_FEET );
825   }
826   return dist*SG_METER_TO_FEET;
827 }
828
829 bool
830 FGInterface::get_agl_m(double t, const double pt[3],
831                        double contact[3], double normal[3], double vel[3],
832                        int *type, double *loadCapacity,
833                        double *frictionFactor, double *agl)
834 {
835   return ground_cache.get_agl(t, pt, 2.0, contact, normal, vel, type,
836                               loadCapacity, frictionFactor, agl);
837 }
838
839 bool
840 FGInterface::get_agl_ft(double t, const double pt[3],
841                         double contact[3], double normal[3], double vel[3],
842                         int *type, double *loadCapacity,
843                         double *frictionFactor, double *agl)
844 {
845   // Convert units and do the real work.
846   sgdVec3 pt_m;
847   sgdScaleVec3( pt_m, pt, SG_FEET_TO_METER );
848   bool ret = ground_cache.get_agl(t, pt_m, 2.0, contact, normal, vel,
849                                   type, loadCapacity, frictionFactor, agl);
850   // Convert units back ...
851   sgdScaleVec3( contact, SG_METER_TO_FEET );
852   sgdScaleVec3( vel, SG_METER_TO_FEET );
853   *agl *= SG_METER_TO_FEET;
854   // FIXME: scale the load limit to something in the english unit system.
855   // Be careful with the DBL_MAX which is returned by default.
856   return ret;
857 }
858
859 bool
860 FGInterface::get_agl_m(double t, const double pt[3], double max_altoff,
861                        double contact[3], double normal[3], double vel[3],
862                        int *type, double *loadCapacity,
863                        double *frictionFactor, double *agl)
864 {
865   return ground_cache.get_agl(t, pt, max_altoff, contact, normal, vel, type,
866                               loadCapacity, frictionFactor, agl);
867 }
868
869 bool
870 FGInterface::get_agl_ft(double t, const double pt[3], double max_altoff,
871                         double contact[3], double normal[3], double vel[3],
872                         int *type, double *loadCapacity,
873                         double *frictionFactor, double *agl)
874 {
875   // Convert units and do the real work.
876   sgdVec3 pt_m;
877   sgdScaleVec3( pt_m, pt, SG_FEET_TO_METER );
878   bool ret = ground_cache.get_agl(t, pt_m, SG_FEET_TO_METER * max_altoff,
879                                   contact, normal, vel,
880                                   type, loadCapacity, frictionFactor, agl);
881   // Convert units back ...
882   sgdScaleVec3( contact, SG_METER_TO_FEET );
883   sgdScaleVec3( vel, SG_METER_TO_FEET );
884   *agl *= SG_METER_TO_FEET;
885   // FIXME: scale the load limit to something in the english unit system.
886   // Be careful with the DBL_MAX which is returned by default.
887   return ret;
888 }
889
890
891 double
892 FGInterface::get_groundlevel_m(double lat, double lon, double alt)
893 {
894   // First compute the sea level radius,
895   sgdVec3 pos, cpos;
896   sgGeodToCart(lat, lon, 0, pos);
897   double slr = sgdLengthVec3(pos);
898   // .. then the cartesian position of the given lat/lon/alt.
899   sgGeodToCart(lat, lon, alt, pos);
900
901   // FIXME: how to handle t - ref_time differences ???
902   double ref_time, radius;
903   // Prepare the ground cache for that position.
904   if (!is_valid_m(&ref_time, cpos, &radius))
905     prepare_ground_cache_m(ref_time, pos, 10);
906   else if (radius*radius <= sgdDistanceSquaredVec3(pos, cpos))
907     prepare_ground_cache_m(ref_time, pos, radius);
908   
909   double contact[3], normal[3], vel[3], lc, ff, agl;
910   int type;
911   get_agl_m(ref_time, pos, 2.0, contact, normal, vel, &type, &lc, &ff, &agl);
912
913   return sgdLengthVec3(contact) - slr;
914 }
915   
916 bool
917 FGInterface::caught_wire_m(double t, const double pt[4][3])
918 {
919   return ground_cache.caught_wire(t, pt);
920 }
921
922 bool
923 FGInterface::caught_wire_ft(double t, const double pt[4][3])
924 {
925   // Convert units and do the real work.
926   double pt_m[4][3];
927   for (int i=0; i<4; ++i)
928     sgdScaleVec3(pt_m[i], pt[i], SG_FEET_TO_METER);
929     
930   return ground_cache.caught_wire(t, pt_m);
931 }
932   
933 bool
934 FGInterface::get_wire_ends_m(double t, double end[2][3], double vel[2][3])
935 {
936   return ground_cache.get_wire_ends(t, end, vel);
937 }
938
939 bool
940 FGInterface::get_wire_ends_ft(double t, double end[2][3], double vel[2][3])
941 {
942   // Convert units and do the real work.
943   bool ret = ground_cache.get_wire_ends(t, end, vel);
944   for (int k=0; k<2; ++k) {
945     sgdScaleVec3( end[k], SG_METER_TO_FEET );
946     sgdScaleVec3( vel[k], SG_METER_TO_FEET );
947   }
948   return ret;
949 }
950
951 void
952 FGInterface::release_wire(void)
953 {
954   ground_cache.release_wire();
955 }
956
957 void fgToggleFDMdataLogging(void) {
958   cur_fdm_state->ToggleDataLogging();
959 }