]> git.mxchange.org Git - flightgear.git/blob - src/FDM/flight.cxx
Improved tumbling behaviour -- the AI doesn't just freeze now, but
[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  - curt@infoplane.com
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/timing/timestamp.hxx>
32
33 #include <Scenery/scenery.hxx>
34 #include <FDM/LaRCsim/ls_interface.h>
35 #include <Main/globals.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/location.hxx>
38 #include <Model/model.hxx>
39 #include <Model/acmodel.hxx>
40
41 #include "flight.hxx"
42
43
44 // base_fdm_state is the internal state that is updated in integer
45 // multiples of "dt".  This leads to "jitter" with respect to the real
46 // world time, so we introduce cur_fdm_state which is extrapolated by
47 // the difference between sim time and real world time
48
49 FGInterface *cur_fdm_state = 0;
50 FGInterface base_fdm_state;
51
52 inline void init_vec(FG_VECTOR_3 vec) {
53     vec[0] = 0.0; vec[1] = 0.0; vec[2] = 0.0;
54 }
55
56 // Constructor
57 FGInterface::FGInterface()
58   : remainder(0)
59 {
60     _setup();
61 }
62
63 FGInterface::FGInterface( double dt )
64   : remainder(0)
65 {
66     _setup();
67 }
68
69 // Destructor
70 FGInterface::~FGInterface() {
71     // unbind();                   // FIXME: should be called explicitly
72 }
73
74
75 int
76 FGInterface::_calc_multiloop (double dt)
77 {
78   int hz = fgGetInt("/sim/model-hz");
79   int speedup = fgGetInt("/sim/speed-up");
80
81   dt += remainder;
82   remainder = 0;
83   double ml = dt * hz;
84   int multiloop = int(floor(ml));
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     _acmodel = 0;
167 }
168
169 void
170 FGInterface::init () {}
171
172 /**
173  * Initialize the state of the FDM.
174  *
175  * Subclasses of FGInterface may do their own, additional initialization,
176  * but there is some that is common to all.  Normally, they should call
177  * this before they begin their own init to make sure the basic structures
178  * are set up properly.
179  */
180 void
181 FGInterface::common_init ()
182 {
183     SG_LOG( SG_FLIGHT, SG_INFO, "Start common FDM init" );
184
185     set_inited( true );
186
187 //     stamp();
188 //     set_remainder( 0 );
189
190     // linking in FGAircraft instance...
191     // FIXME: when using multiple instances, then there will be more than
192     // one model so get_aircraft_model will have to be indexed to the correct
193     // model.
194     _acmodel = globals->get_aircraft_model();
195
196     // Set initial position
197     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing position..." );
198     set_Longitude( fgGetDouble("/sim/presets/longitude-deg")
199                    * SGD_DEGREES_TO_RADIANS );
200     set_Latitude( fgGetDouble("/sim/presets/latitude-deg")
201                   * SGD_DEGREES_TO_RADIANS );
202     double ground_elev_m = globals->get_scenery()->get_cur_elev();
203     double ground_elev_ft = ground_elev_m * SG_METER_TO_FEET;
204     _acmodel->get3DModel()->getFGLocation()->set_cur_elev_m( ground_elev_m );
205     _set_Runway_altitude ( ground_elev_ft );
206     if ( fgGetBool("/sim/presets/onground")
207          || fgGetDouble("/sim/presets/altitude-ft") < ground_elev_ft ) {
208         fgSetDouble("/sim/presets/altitude-ft", ground_elev_ft);
209         fgSetDouble("/position/altitude-ft", ground_elev_ft);
210     }
211     set_Altitude( fgGetDouble("/sim/presets/altitude-ft") );
212
213     // Set ground elevation
214     SG_LOG( SG_FLIGHT, SG_INFO,
215             "...initializing ground elevation to " << ground_elev_ft
216             << "ft..." );
217
218     // Set sea-level radius
219     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing sea-level radius..." );
220     SG_LOG( SG_FLIGHT, SG_INFO, " lat = "
221             << fgGetDouble("/sim/presets/latitude-deg")
222             << " alt = " << fgGetDouble("/sim/presets/altitude-ft") );
223     double sea_level_radius_meters;
224     double lat_geoc;
225     sgGeodToGeoc( fgGetDouble("/sim/presets/latitude-deg")
226                     * SGD_DEGREES_TO_RADIANS,
227                   fgGetDouble("/sim/presets/altitude-ft") * SG_FEET_TO_METER,
228                   &sea_level_radius_meters, &lat_geoc );
229     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET );
230
231     // Set initial velocities
232     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing velocities..." );
233     if ( !fgHasNode("/sim/presets/speed-set") ) {
234         set_V_calibrated_kts(0.0);
235     } else {
236         const string speedset = fgGetString("/sim/presets/speed-set");
237         if ( speedset == "knots" || speedset == "KNOTS" ) {
238             set_V_calibrated_kts( fgGetDouble("/sim/presets/airspeed-kt") );
239         } else if ( speedset == "mach" || speedset == "MACH" ) {
240             set_Mach_number( fgGetDouble("/sim/presets/mach") );
241         } else if ( speedset == "UVW" || speedset == "uvw" ) {
242             set_Velocities_Wind_Body(
243                                      fgGetDouble("/sim/presets/uBody-fps"),
244                                      fgGetDouble("/sim/presets/vBody-fps"),
245                                      fgGetDouble("/sim/presets/wBody-fps") );
246         } else if ( speedset == "NED" || speedset == "ned" ) {
247             set_Velocities_Local(
248                                  fgGetDouble("/sim/presets/speed-north-fps"),
249                                  fgGetDouble("/sim/presets/speed-east-fps"),
250                                  fgGetDouble("/sim/presets/speed-down-fps") );
251         } else {
252             SG_LOG( SG_FLIGHT, SG_ALERT,
253                     "Unrecognized value for /sim/presets/speed-set: "
254                     << speedset);
255             set_V_calibrated_kts( 0.0 );
256         }
257     }
258
259     // Set initial Euler angles
260     SG_LOG( SG_FLIGHT, SG_INFO, "...initializing Euler angles..." );
261     set_Euler_Angles( fgGetDouble("/sim/presets/roll-deg")
262                         * SGD_DEGREES_TO_RADIANS,
263                       fgGetDouble("/sim/presets/pitch-deg")
264                         * SGD_DEGREES_TO_RADIANS,
265                       fgGetDouble("/sim/presets/heading-deg")
266                         * SGD_DEGREES_TO_RADIANS );
267
268     SG_LOG( SG_FLIGHT, SG_INFO, "End common FDM init" );
269 }
270
271
272 /**
273  * Bind getters and setters to properties.
274  *
275  * The bind() method will be invoked after init().  Note that unlike
276  * the usual implementations of FGSubsystem::bind(), this method does
277  * not automatically pick up existing values for the properties at
278  * bind time; instead, all values are set explicitly in the init()
279  * method.
280  */
281 void
282 FGInterface::bind ()
283 {
284   bound = true;
285
286                                 // Time management (read-only)
287 //   fgTie("/fdm/time/delta_t", this,
288 //         &FGInterface::get_delta_t); // read-only
289 //   fgTie("/fdm/time/elapsed", this,
290 //         &FGInterface::get_elapsed); // read-only
291 //   fgTie("/fdm/time/remainder", this,
292 //         &FGInterface::get_remainder); // read-only
293 //   fgTie("/fdm/time/multi_loop", this,
294 //         &FGInterface::get_multi_loop); // read-only
295
296                         // Aircraft position
297   fgTie("/position/latitude-deg", this,
298         &FGInterface::get_Latitude_deg,
299         &FGInterface::set_Latitude_deg,
300         false);
301   fgSetArchivable("/position/latitude-deg");
302   fgTie("/position/longitude-deg", this,
303         &FGInterface::get_Longitude_deg,
304         &FGInterface::set_Longitude_deg,
305         false);
306   fgSetArchivable("/position/longitude-deg");
307   fgTie("/position/altitude-ft", this,
308         &FGInterface::get_Altitude,
309         &FGInterface::set_Altitude,
310         false);
311   fgSetArchivable("/position/altitude-ft");
312   fgTie("/position/altitude-agl-ft", this,
313         &FGInterface::get_Altitude_AGL); // read-only
314
315                                 // Orientation
316   fgTie("/orientation/roll-deg", this,
317         &FGInterface::get_Phi_deg,
318         &FGInterface::set_Phi_deg);
319   fgSetArchivable("/orientation/roll-deg");
320   fgTie("/orientation/pitch-deg", this,
321         &FGInterface::get_Theta_deg,
322         &FGInterface::set_Theta_deg);
323   fgSetArchivable("/orientation/pitch-deg");
324   fgTie("/orientation/heading-deg", this,
325         &FGInterface::get_Psi_deg,
326         &FGInterface::set_Psi_deg);
327   fgSetArchivable("/orientation/heading-deg");
328
329   // Body-axis "euler rates" (rotation speed, but in a funny
330   // representation).
331   fgTie("/orientation/roll-rate-degps", this,
332         &FGInterface::get_Phi_dot_degps);
333   fgTie("/orientation/pitch-rate-degps", this,
334         &FGInterface::get_Theta_dot_degps);
335   fgTie("/orientation/yaw-rate-degps", this,
336         &FGInterface::get_Psi_dot_degps);
337
338                                 // Calibrated airspeed
339   fgTie("/velocities/airspeed-kt", this,
340         &FGInterface::get_V_calibrated_kts,
341         &FGInterface::set_V_calibrated_kts,
342         false);
343
344                                 // Mach number
345   fgTie("/velocities/mach", this,
346         &FGInterface::get_Mach_number,
347         &FGInterface::set_Mach_number,
348         false);
349
350                                 // Local velocities
351 //   fgTie("/velocities/speed-north-fps", this,
352 //      &FGInterface::get_V_north,
353 //      &FGInterface::set_V_north);
354 //   fgSetArchivable("/velocities/speed-north-fps");
355 //   fgTie("/velocities/speed-east-fps", this,
356 //      &FGInterface::get_V_east,
357 //      &FGInterface::set_V_east);
358 //   fgSetArchivable("/velocities/speed-east-fps");
359 //   fgTie("/velocities/speed-down-fps", this,
360 //      &FGInterface::get_V_down,
361 //      &FGInterface::set_V_down);
362 //   fgSetArchivable("/velocities/speed-down-fps");
363                                 // FIXME: Temporarily read-only, until the
364                                 // incompatibilities between JSBSim and
365                                 // LaRCSim are fixed (LaRCSim adds the
366                                 // earth's rotation to the east velocity).
367   fgTie("/velocities/speed-north-fps", this,
368         &FGInterface::get_V_north);
369   fgTie("/velocities/speed-east-fps", this,
370         &FGInterface::get_V_east);
371   fgTie("/velocities/speed-down-fps", this,
372         &FGInterface::get_V_down);
373
374                                 // Relative wind
375                                 // FIXME: temporarily archivable, until
376                                 // the NED problem is fixed.
377   fgTie("/velocities/uBody-fps", this,
378         &FGInterface::get_uBody,
379         &FGInterface::set_uBody,
380         false);
381   fgSetArchivable("/velocities/uBody-fps");
382   fgTie("/velocities/vBody-fps", this,
383         &FGInterface::get_vBody,
384         &FGInterface::set_vBody,
385         false);
386   fgSetArchivable("/velocities/vBody-fps");
387   fgTie("/velocities/wBody-fps", this,
388         &FGInterface::get_wBody,
389         &FGInterface::set_wBody,
390         false);
391   fgSetArchivable("/velocities/wBody-fps");
392
393                                 // Climb and slip (read-only)
394   fgTie("/velocities/vertical-speed-fps", this,
395         &FGInterface::get_Climb_Rate,
396   &FGInterface::set_Climb_Rate ); 
397   fgTie("/velocities/glideslope", this,
398   &FGInterface::get_Gamma_vert_rad,
399   &FGInterface::set_Gamma_vert_rad );
400   fgTie("/orientation/side-slip-rad", this,
401         &FGInterface::get_Beta); // read-only
402   fgTie("/orientation/side-slip-deg", this,
403   &FGInterface::get_Beta_deg); // read-only
404   fgTie("/orientation/alpha-deg", this,
405   &FGInterface::get_Alpha_deg); // read-only
406   fgTie("/accelerations/nlf", this,
407   &FGInterface::get_Nlf); // read-only
408
409                                 // NED accelerations
410   fgTie("/accelerations/ned/north-accel-fps_sec",
411         this, &FGInterface::get_V_dot_north);
412   fgTie("/accelerations/ned/east-accel-fps_sec",
413         this, &FGInterface::get_V_dot_east);
414   fgTie("/accelerations/ned/down-accel-fps_sec",
415         this, &FGInterface::get_V_dot_down);
416
417                                 // Pilot accelerations
418   fgTie("/accelerations/pilot/x-accel-fps_sec",
419         this, &FGInterface::get_A_X_pilot);
420   fgTie("/accelerations/pilot/y-accel-fps_sec",
421         this, &FGInterface::get_A_Y_pilot);
422   fgTie("/accelerations/pilot/z-accel-fps_sec",
423         this, &FGInterface::get_A_Z_pilot);
424
425 }
426
427
428 /**
429  * Unbind any properties bound to this FDM.
430  *
431  * This method allows the FDM to release properties so that a new
432  * FDM can bind them instead.
433  */
434 void
435 FGInterface::unbind ()
436 {
437   bound = false;
438
439   // fgUntie("/fdm/time/delta_t");
440   // fgUntie("/fdm/time/elapsed");
441   // fgUntie("/fdm/time/remainder");
442   // fgUntie("/fdm/time/multi_loop");
443   fgUntie("/position/latitude-deg");
444   fgUntie("/position/longitude-deg");
445   fgUntie("/position/altitude-ft");
446   fgUntie("/position/altitude-agl-ft");
447   fgUntie("/orientation/roll-deg");
448   fgUntie("/orientation/pitch-deg");
449   fgUntie("/orientation/heading-deg");
450   fgUntie("/orientation/roll-rate-degps");
451   fgUntie("/orientation/pitch-rate-degps");
452   fgUntie("/orientation/yaw-rate-degps");
453   fgUntie("/orientation/side-slip-rad");
454   fgUntie("/orientation/side-slip-deg");
455   fgUntie("/orientation/alpha-deg");
456   fgUntie("/velocities/airspeed-kt");
457   fgUntie("/velocities/mach");
458   fgUntie("/velocities/speed-north-fps");
459   fgUntie("/velocities/speed-east-fps");
460   fgUntie("/velocities/speed-down-fps");
461   fgUntie("/velocities/uBody-fps");
462   fgUntie("/velocities/vBody-fps");
463   fgUntie("/velocities/wBody-fps");
464   fgUntie("/velocities/vertical-speed-fps");
465   fgUntie("/velocities/glideslope");
466   fgUntie("/accelerations/nlf");
467   fgUntie("/accelerations/pilot/x-accel-fps_sec");
468   fgUntie("/accelerations/pilot/y-accel-fps_sec");
469   fgUntie("/accelerations/pilot/z-accel-fps_sec");
470   fgUntie("/accelerations/ned/north-accel-fps_sec");
471   fgUntie("/accelerations/ned/east-accel-fps_sec");
472   fgUntie("/accelerations/ned/down-accel-fps_sec");
473 }
474
475 /**
476  * Update the state of the FDM (i.e. run the equations of motion).
477  */
478 void
479 FGInterface::update (double dt)
480 {
481     SG_LOG(SG_FLIGHT, SG_ALERT, "dummy update() ... SHOULDN'T BE CALLED!");
482 }
483
484
485 void FGInterface::_updateGeodeticPosition( double lat, double lon, double alt )
486 {
487     double lat_geoc, sl_radius;
488
489     // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
490
491     sgGeodToGeoc( lat, alt * SG_FEET_TO_METER, &sl_radius, &lat_geoc );
492
493     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon 
494             << " lat_geod = " << lat
495             << " lat_geoc = " << lat_geoc
496             << " alt = " << alt 
497             << " sl_radius = " << sl_radius * SG_METER_TO_FEET
498             << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
499
500     _set_Geocentric_Position( lat_geoc, lon, 
501                               sl_radius * SG_METER_TO_FEET + alt );
502         
503     _set_Geodetic_Position( lat, lon, alt );
504
505     _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET );
506     if ( getACModel() != NULL ) {
507         _set_Runway_altitude( getACModel()->get3DModel()->getFGLocation()->get_cur_elev_m() * SG_METER_TO_FEET );
508     }
509
510     _set_sin_lat_geocentric( lat_geoc );
511     _set_cos_lat_geocentric( lat_geoc );
512
513     _set_sin_cos_longitude( lon );
514
515     _set_sin_cos_latitude( lat );
516
517     /* Norman's code for slope of the terrain */
518     /* needs to be tested -- get it on the HUD and taxi around */
519     /* double *tnorm = scenery.cur_normal;
520
521        double sy = sin ( -get_Psi() ) ;
522        double cy = cos ( -get_Psi() ) ;
523
524        double phitb, thetatb, psitb;
525        if ( tnorm[1] != 0.0 ) {
526            psitb = -atan2 ( tnorm[0], tnorm[1] );
527        }
528        if ( tnorm[2] != 0.0 ) { 
529            thetatb =  atan2 ( tnorm[0] * cy - tnorm[1] * sy, tnorm[2] );
530            phitb = -atan2 ( tnorm[1] * cy + tnorm[0] * sy, tnorm[2] );  
531        }        
532         
533        _set_terrain_slope(phitb, thetatb, psitb) 
534      */
535 }
536
537
538 void FGInterface::_updateGeocentricPosition( double lat_geoc, double lon,
539                                              double alt )
540 {
541     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
542
543     // cout << "starting sea level rad = " << get_Sea_level_radius() << endl;
544
545     sgGeocToGeod( lat_geoc, ( get_Sea_level_radius() + alt ) * SG_FEET_TO_METER,
546                   &lat_geod, &tmp_alt, &sl_radius1 );
547     sgGeodToGeoc( lat_geod, alt * SG_FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
548
549     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << lon 
550             << " lat_geod = " << lat_geod
551             << " lat_geoc = " << lat_geoc
552             << " alt = " << alt 
553             << " tmp_alt = " << tmp_alt * SG_METER_TO_FEET
554             << " sl_radius1 = " << sl_radius1 * SG_METER_TO_FEET
555             << " sl_radius2 = " << sl_radius2 * SG_METER_TO_FEET
556             << " Equator = " << SG_EQUATORIAL_RADIUS_FT );
557
558     _set_Geocentric_Position( lat_geoc, lon, 
559                               sl_radius2 * SG_METER_TO_FEET + alt );
560         
561     _set_Geodetic_Position( lat_geod, lon, alt );
562
563     _set_Sea_level_radius( sl_radius2 * SG_METER_TO_FEET );
564     _set_Runway_altitude( getACModel()->get3DModel()->getFGLocation()->get_cur_elev_m() * SG_METER_TO_FEET );
565
566     _set_sin_lat_geocentric( lat_geoc );
567     _set_cos_lat_geocentric( lat_geoc );
568
569     _set_sin_cos_longitude( lon );
570
571     _set_sin_cos_latitude( lat_geod );
572
573     /* Norman's code for slope of the terrain */
574     /* needs to be tested -- get it on the HUD and taxi around */
575     /* double *tnorm = scenery.cur_normal;
576
577        double sy = sin ( -get_Psi() ) ;
578        double cy = cos ( -get_Psi() ) ;
579
580        double phitb, thetatb, psitb;
581        if ( tnorm[1] != 0.0 ) {
582            psitb = -atan2 ( tnorm[0], tnorm[1] );
583        }
584        if ( tnorm[2] != 0.0 ) { 
585            thetatb =  atan2 ( tnorm[0] * cy - tnorm[1] * sy, tnorm[2] );
586            phitb = -atan2 ( tnorm[1] * cy + tnorm[0] * sy, tnorm[2] );  
587        }        
588         
589        _set_terrain_slope(phitb, thetatb, psitb) 
590      */
591 }
592
593
594 // Extrapolate fdm based on time_offset (in usec)
595 void FGInterface::extrapolate( int time_offset ) {
596     double dt = time_offset / 1000000.0;
597
598     // -dw- metrowerks complains about ambiguous access, not critical
599     // to keep this ;)
600 #ifndef __MWERKS__
601     SG_LOG(SG_FLIGHT, SG_INFO, "extrapolating FDM by dt = " << dt);
602 #endif
603
604     double lat = geodetic_position_v[0] + geocentric_rates_v[0] * dt;
605     double lat_geoc = geocentric_position_v[0] + geocentric_rates_v[0] * dt;
606
607     double lon = geodetic_position_v[1] + geocentric_rates_v[1] * dt;
608     double lon_geoc = geocentric_position_v[1] + geocentric_rates_v[1] * dt;
609
610     double alt = geodetic_position_v[2] + geocentric_rates_v[2] * dt;
611     double radius = geocentric_position_v[2] + geocentric_rates_v[2] * dt;
612
613     geodetic_position_v[0] = lat;
614     geocentric_position_v[0] = lat_geoc;
615
616     geodetic_position_v[1] = lon;
617     geocentric_position_v[1] = lon_geoc;
618
619     geodetic_position_v[2] = alt;
620     geocentric_position_v[2] = radius;
621 }
622
623 // Positions
624 void FGInterface::set_Latitude(double lat) {
625     geodetic_position_v[0] = lat;
626 }
627
628 void FGInterface::set_Longitude(double lon) {
629     geodetic_position_v[1] = lon;
630 }
631
632 void FGInterface::set_Altitude(double alt) {
633     geodetic_position_v[2] = alt;
634 }
635
636 void FGInterface::set_AltitudeAGL(double altagl) {
637     altitude_agl=altagl;
638 }
639
640 // Velocities
641 void FGInterface::set_V_calibrated_kts(double vc) {
642     v_calibrated_kts = vc;
643 }
644
645 void FGInterface::set_Mach_number(double mach) {
646     mach_number = mach;
647 }
648
649 void FGInterface::set_Velocities_Local( double north, 
650                                         double east, 
651                                         double down ){
652     v_local_v[0] = north;
653     v_local_v[1] = east;
654     v_local_v[2] = down;
655 }
656
657 void FGInterface::set_Velocities_Wind_Body( double u, 
658                                             double v, 
659                                             double w){
660     v_wind_body_v[0] = u;
661     v_wind_body_v[1] = v;
662     v_wind_body_v[2] = w;
663 }
664
665 // Euler angles 
666 void FGInterface::set_Euler_Angles( double phi, 
667                                     double theta, 
668                                     double psi ) {
669     euler_angles_v[0] = phi;
670     euler_angles_v[1] = theta;
671     euler_angles_v[2] = psi;                                            
672 }  
673
674 // Flight Path
675 void FGInterface::set_Climb_Rate( double roc) {
676     climb_rate = roc;
677 }
678
679 void FGInterface::set_Gamma_vert_rad( double gamma) {
680     gamma_vert_rad = gamma;
681 }
682
683 void FGInterface::set_Static_pressure(double p) { static_pressure = p; }
684 void FGInterface::set_Static_temperature(double T) { static_temperature = T; }
685 void FGInterface::set_Density(double rho) { density = rho; }
686
687 void FGInterface::set_Velocities_Local_Airmass (double wnorth, 
688                                                 double weast, 
689                                                 double wdown ) {
690     v_local_airmass_v[0] = wnorth;
691     v_local_airmass_v[1] = weast;
692     v_local_airmass_v[2] = wdown;
693 }
694
695
696 void FGInterface::_busdump(void) {
697
698     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]);
699     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]);
700     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]);
701     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]);
702     SG_LOG(SG_FLIGHT,SG_INFO,"f_aero_v[3]: " << f_aero_v[0] << ", " << f_aero_v[1] << ", " << f_aero_v[2]);
703     SG_LOG(SG_FLIGHT,SG_INFO,"f_engine_v[3]: " << f_engine_v[0] << ", " << f_engine_v[1] << ", " << f_engine_v[2]);
704     SG_LOG(SG_FLIGHT,SG_INFO,"f_gear_v[3]: " << f_gear_v[0] << ", " << f_gear_v[1] << ", " << f_gear_v[2]);
705     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]);
706     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]);
707     SG_LOG(SG_FLIGHT,SG_INFO,"m_aero_v[3]: " << m_aero_v[0] << ", " << m_aero_v[1] << ", " << m_aero_v[2]);
708     SG_LOG(SG_FLIGHT,SG_INFO,"m_engine_v[3]: " << m_engine_v[0] << ", " << m_engine_v[1] << ", " << m_engine_v[2]);
709     SG_LOG(SG_FLIGHT,SG_INFO,"m_gear_v[3]: " << m_gear_v[0] << ", " << m_gear_v[1] << ", " << m_gear_v[2]);
710     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]);
711     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]);
712     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]);
713     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]);
714     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]);
715     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]);
716     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]);
717     SG_LOG(SG_FLIGHT,SG_INFO,"v_local_v[3]: " << v_local_v[0] << ", " << v_local_v[1] << ", " << v_local_v[2]);
718     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]);
719     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]);
720     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]);
721     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]);
722     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]);
723     SG_LOG(SG_FLIGHT,SG_INFO,"omega_body_v[3]: " << omega_body_v[0] << ", " << omega_body_v[1] << ", " << omega_body_v[2]);
724     SG_LOG(SG_FLIGHT,SG_INFO,"omega_local_v[3]: " << omega_local_v[0] << ", " << omega_local_v[1] << ", " << omega_local_v[2]);
725     SG_LOG(SG_FLIGHT,SG_INFO,"omega_total_v[3]: " << omega_total_v[0] << ", " << omega_total_v[1] << ", " << omega_total_v[2]);
726     SG_LOG(SG_FLIGHT,SG_INFO,"euler_rates_v[3]: " << euler_rates_v[0] << ", " << euler_rates_v[1] << ", " << euler_rates_v[2]);
727     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_rates_v[3]: " << geocentric_rates_v[0] << ", " << geocentric_rates_v[1] << ", " << geocentric_rates_v[2]);
728     SG_LOG(SG_FLIGHT,SG_INFO,"geocentric_position_v[3]: " << geocentric_position_v[0] << ", " << geocentric_position_v[1] << ", " << geocentric_position_v[2]);
729     SG_LOG(SG_FLIGHT,SG_INFO,"geodetic_position_v[3]: " << geodetic_position_v[0] << ", " << geodetic_position_v[1] << ", " << geodetic_position_v[2]);
730     SG_LOG(SG_FLIGHT,SG_INFO,"euler_angles_v[3]: " << euler_angles_v[0] << ", " << euler_angles_v[1] << ", " << euler_angles_v[2]);
731     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]);
732     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]);
733     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]);
734     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]);
735
736     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]);
737     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]);
738     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]);
739
740     SG_LOG(SG_FLIGHT,SG_INFO,"mass: " << mass );
741     SG_LOG(SG_FLIGHT,SG_INFO,"i_xx: " << i_xx );
742     SG_LOG(SG_FLIGHT,SG_INFO,"i_yy: " << i_yy );
743     SG_LOG(SG_FLIGHT,SG_INFO,"i_zz: " << i_zz );
744     SG_LOG(SG_FLIGHT,SG_INFO,"i_xz: " << i_xz );
745     SG_LOG(SG_FLIGHT,SG_INFO,"nlf: " << nlf );
746     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_wind: " << v_rel_wind );
747     SG_LOG(SG_FLIGHT,SG_INFO,"v_true_kts: " << v_true_kts );
748     SG_LOG(SG_FLIGHT,SG_INFO,"v_rel_ground: " << v_rel_ground );
749     SG_LOG(SG_FLIGHT,SG_INFO,"v_inertial: " << v_inertial );
750     SG_LOG(SG_FLIGHT,SG_INFO,"v_ground_speed: " << v_ground_speed );
751     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv: " << v_equiv );
752     SG_LOG(SG_FLIGHT,SG_INFO,"v_equiv_kts: " << v_equiv_kts );
753     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated: " << v_calibrated );
754     SG_LOG(SG_FLIGHT,SG_INFO,"v_calibrated_kts: " << v_calibrated_kts );
755     SG_LOG(SG_FLIGHT,SG_INFO,"gravity: " << gravity );
756     SG_LOG(SG_FLIGHT,SG_INFO,"centrifugal_relief: " << centrifugal_relief );
757     SG_LOG(SG_FLIGHT,SG_INFO,"alpha: " << alpha );
758     SG_LOG(SG_FLIGHT,SG_INFO,"beta: " << beta );
759     SG_LOG(SG_FLIGHT,SG_INFO,"alpha_dot: " << alpha_dot );
760     SG_LOG(SG_FLIGHT,SG_INFO,"beta_dot: " << beta_dot );
761     SG_LOG(SG_FLIGHT,SG_INFO,"cos_alpha: " << cos_alpha );
762     SG_LOG(SG_FLIGHT,SG_INFO,"sin_alpha: " << sin_alpha );
763     SG_LOG(SG_FLIGHT,SG_INFO,"cos_beta: " << cos_beta );
764     SG_LOG(SG_FLIGHT,SG_INFO,"sin_beta: " << sin_beta );
765     SG_LOG(SG_FLIGHT,SG_INFO,"cos_phi: " << cos_phi );
766     SG_LOG(SG_FLIGHT,SG_INFO,"sin_phi: " << sin_phi );
767     SG_LOG(SG_FLIGHT,SG_INFO,"cos_theta: " << cos_theta );
768     SG_LOG(SG_FLIGHT,SG_INFO,"sin_theta: " << sin_theta );
769     SG_LOG(SG_FLIGHT,SG_INFO,"cos_psi: " << cos_psi );
770     SG_LOG(SG_FLIGHT,SG_INFO,"sin_psi: " << sin_psi );
771     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_vert_rad: " << gamma_vert_rad );
772     SG_LOG(SG_FLIGHT,SG_INFO,"gamma_horiz_rad: " << gamma_horiz_rad );
773     SG_LOG(SG_FLIGHT,SG_INFO,"sigma: " << sigma );
774     SG_LOG(SG_FLIGHT,SG_INFO,"density: " << density );
775     SG_LOG(SG_FLIGHT,SG_INFO,"v_sound: " << v_sound );
776     SG_LOG(SG_FLIGHT,SG_INFO,"mach_number: " << mach_number );
777     SG_LOG(SG_FLIGHT,SG_INFO,"static_pressure: " << static_pressure );
778     SG_LOG(SG_FLIGHT,SG_INFO,"total_pressure: " << total_pressure );
779     SG_LOG(SG_FLIGHT,SG_INFO,"impact_pressure: " << impact_pressure );
780     SG_LOG(SG_FLIGHT,SG_INFO,"dynamic_pressure: " << dynamic_pressure );
781     SG_LOG(SG_FLIGHT,SG_INFO,"static_temperature: " << static_temperature );
782     SG_LOG(SG_FLIGHT,SG_INFO,"total_temperature: " << total_temperature );
783     SG_LOG(SG_FLIGHT,SG_INFO,"sea_level_radius: " << sea_level_radius );
784     SG_LOG(SG_FLIGHT,SG_INFO,"earth_position_angle: " << earth_position_angle );
785     SG_LOG(SG_FLIGHT,SG_INFO,"runway_altitude: " << runway_altitude );
786     SG_LOG(SG_FLIGHT,SG_INFO,"runway_latitude: " << runway_latitude );
787     SG_LOG(SG_FLIGHT,SG_INFO,"runway_longitude: " << runway_longitude );
788     SG_LOG(SG_FLIGHT,SG_INFO,"runway_heading: " << runway_heading );
789     SG_LOG(SG_FLIGHT,SG_INFO,"radius_to_rwy: " << radius_to_rwy );
790     SG_LOG(SG_FLIGHT,SG_INFO,"climb_rate: " << climb_rate );
791     SG_LOG(SG_FLIGHT,SG_INFO,"sin_lat_geocentric: " << sin_lat_geocentric );
792     SG_LOG(SG_FLIGHT,SG_INFO,"cos_lat_geocentric: " << cos_lat_geocentric );
793     SG_LOG(SG_FLIGHT,SG_INFO,"sin_longitude: " << sin_longitude );
794     SG_LOG(SG_FLIGHT,SG_INFO,"cos_longitude: " << cos_longitude );
795     SG_LOG(SG_FLIGHT,SG_INFO,"sin_latitude: " << sin_latitude );
796     SG_LOG(SG_FLIGHT,SG_INFO,"cos_latitude: " << cos_latitude );
797     SG_LOG(SG_FLIGHT,SG_INFO,"altitude_agl: " << altitude_agl );
798 }
799
800
801 void fgToggleFDMdataLogging(void) {
802   cur_fdm_state->ToggleDataLogging();
803 }