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