1 // fg_init.cxx -- Flight Gear top level initialization routines
3 // Written by Curtis Olson, started August 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
29 // For BC 5.01 this must be included before OpenGL includes.
30 #ifdef FG_MATH_EXCEPTION_CLASH
35 #include <simgear/xgl/xgl.h>
40 // work around a stdc++ lib bug in some versions of linux, but doesn't
41 // seem to hurt to have this here for all versions of Linux.
43 # define _G_NO_EXTERN_TEMPLATES
46 #include <simgear/compiler.h>
50 #include <simgear/constants.h>
51 #include <simgear/debug/logstream.hxx>
52 #include <simgear/math/point3d.hxx>
53 #include <simgear/math/polar3d.hxx>
54 #include <simgear/math/sg_geodesy.hxx>
55 #include <simgear/misc/fgpath.hxx>
56 #include <simgear/timing/sg_time.hxx>
58 #include <Aircraft/aircraft.hxx>
59 #include <Airports/runways.hxx>
60 #include <Airports/simple.hxx>
61 #include <Autopilot/auto_gui.hxx>
62 #include <Autopilot/newauto.hxx>
63 #include <Cockpit/cockpit.hxx>
64 #include <Cockpit/radiostack.hxx>
65 #include <Cockpit/panel.hxx>
66 #include <Cockpit/panel_io.hxx>
67 #include <FDM/ADA.hxx>
68 #include <FDM/Balloon.h>
69 #include <FDM/External.hxx>
70 #include <FDM/JSBSim.hxx>
71 #include <FDM/LaRCsim.hxx>
72 #include <FDM/MagicCarpet.hxx>
73 #include <Include/general.hxx>
74 #include <Joystick/joystick.hxx>
75 #include <Objects/matlib.hxx>
76 #include <Navaids/fixlist.hxx>
77 #include <Navaids/ilslist.hxx>
78 #include <Navaids/navlist.hxx>
79 #include <Scenery/scenery.hxx>
80 #include <Scenery/tilemgr.hxx>
81 #include <Time/event.hxx>
82 #include <Time/light.hxx>
83 #include <Time/sunpos.hxx>
84 #include <Time/moonpos.hxx>
85 #include <Time/tmp.hxx>
87 #ifndef FG_OLD_WEATHER
88 # include <WeatherCM/FGLocalWeatherDatabase.h>
90 # include <Weather/weather.hxx>
93 #include "fg_init.hxx"
95 #include "options.hxx"
96 #include "globals.hxx"
99 #if defined(FX) && defined(XMESA)
100 #include <GL/xmesa.h>
103 FG_USING_STD(string);
105 extern const char *default_root;
108 // Read in configuration (file and command line) and just set fg_root
109 bool fgInitFGRoot ( int argc, char **argv ) {
113 // First parse command line options looking for fg-root, this will
114 // override anything specified in a config file
115 root = fgScanForRoot(argc, argv);
117 // Next check home directory for .fgfsrc file
119 envp = ::getenv( "HOME" );
120 if ( envp != NULL ) {
121 FGPath config( envp );
122 config.append( ".fgfsrc" );
123 root = fgScanForRoot(config.str());
127 // Next check if fg-root is set as an env variable
129 envp = ::getenv( "FG_ROOT" );
130 if ( envp != NULL ) {
135 // Otherwise, default to a random compiled-in location if we can't
136 // find fg-root any other way.
139 root = "\\FlightGear";
140 #elif defined( macintosh )
147 FG_LOG(FG_INPUT, FG_INFO, "fg_root = " << root );
148 globals->set_fg_root(root);
154 // Read in configuration (file and command line)
155 bool fgInitConfig ( int argc, char **argv ) {
157 // First, set some sane default values
160 // Read global preferences from $FG_ROOT/preferences.xml
161 FGPath props_path(globals->get_fg_root());
162 props_path.append("preferences.xml");
163 FG_LOG(FG_INPUT, FG_INFO, "Reading global preferences");
164 if (!readProperties(props_path.str(), globals->get_props())) {
165 FG_LOG(FG_INPUT, FG_ALERT, "Failed to read global preferences from "
166 << props_path.str());
168 FG_LOG(FG_INPUT, FG_INFO, "Finished Reading global preferences");
171 // Attempt to locate and parse a config file
172 // First check fg_root
173 FGPath config( globals->get_fg_root() );
174 config.append( "system.fgfsrc" );
175 fgParseOptions(config.str());
177 // Next check home directory
178 char* envp = ::getenv( "HOME" );
179 if ( envp != NULL ) {
181 config.append( ".fgfsrc" );
182 fgParseOptions(config.str());
185 // Parse remaining command line options
186 // These will override anything specified in a config file
187 fgParseOptions(argc, argv);
193 // find basic airport location info from airport database
194 bool fgFindAirportID( const string& id, FGAirport *a ) {
196 FGPath path( globals->get_fg_root() );
197 path.append( "Airports" );
198 path.append( "simple.mk4" );
199 FGAirports airports( path.c_str() );
201 FG_LOG( FG_GENERAL, FG_INFO, "Searching for airport code = " << id );
203 if ( ! airports.search( id, a ) ) {
204 FG_LOG( FG_GENERAL, FG_ALERT,
205 "Failed to find " << id << " in " << path.str() );
212 FG_LOG( FG_GENERAL, FG_INFO,
213 "Position for " << id << " is ("
214 << a->longitude << ", "
215 << a->latitude << ")" );
221 // Set current_options lon/lat given an airport id
222 bool fgSetPosFromAirportID( const string& id ) {
226 FG_LOG( FG_GENERAL, FG_INFO,
227 "Attempting to set starting position from airport code " << id );
229 if ( fgFindAirportID( id, &a ) ) {
230 fgSetDouble("/position/longitude", a.longitude );
231 fgSetDouble("/position/latitude", a.latitude );
232 FG_LOG( FG_GENERAL, FG_INFO,
233 "Position for " << id << " is ("
234 << a.longitude << ", "
235 << a.latitude << ")" );
245 // Set current_options lon/lat given an airport id and heading (degrees)
246 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
249 double found_dir = 0.0;
252 // set initial position from runway and heading
254 FGPath path( globals->get_fg_root() );
255 path.append( "Airports" );
256 path.append( "runways.mk4" );
257 FGRunways runways( path.c_str() );
259 FG_LOG( FG_GENERAL, FG_INFO,
260 "Attempting to set starting position from runway code "
261 << id << " heading " << tgt_hdg );
263 // FGPath inpath( globals->get_fg_root() );
264 // inpath.append( "Airports" );
265 // inpath.append( "apt_simple" );
266 // airports.load( inpath.c_str() );
268 // FGPath outpath( globals->get_fg_root() );
269 // outpath.append( "Airports" );
270 // outpath.append( "simple.gdbm" );
271 // airports.dump_gdbm( outpath.c_str() );
273 if ( ! runways.search( id, &r ) ) {
274 FG_LOG( FG_GENERAL, FG_ALERT,
275 "Failed to find " << id << " in database." );
280 double min_diff = 360.0;
282 while ( r.id == id ) {
284 diff = tgt_hdg - r.heading;
285 while ( diff < -180.0 ) { diff += 360.0; }
286 while ( diff > 180.0 ) { diff -= 360.0; }
288 FG_LOG( FG_GENERAL, FG_INFO,
289 "Runway " << r.rwy_no << " heading = " << r.heading <<
290 " diff = " << diff );
291 if ( diff < min_diff ) {
298 diff = tgt_hdg - r.heading - 180.0;
299 while ( diff < -180.0 ) { diff += 360.0; }
300 while ( diff > 180.0 ) { diff -= 360.0; }
302 FG_LOG( FG_GENERAL, FG_INFO,
303 "Runway -" << r.rwy_no << " heading = " <<
305 " diff = " << diff );
306 if ( diff < min_diff ) {
315 FG_LOG( FG_GENERAL, FG_INFO, "closest runway = " << found_r.rwy_no
316 << " + " << found_dir );
322 double heading = found_r.heading + found_dir;
323 while ( heading >= 360.0 ) { heading -= 360.0; }
325 double lat2, lon2, az2;
326 double azimuth = found_r.heading + found_dir + 180.0;
327 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
329 FG_LOG( FG_GENERAL, FG_INFO,
330 "runway = " << found_r.lon << ", " << found_r.lat
331 << " length = " << found_r.length * FEET_TO_METER * 0.5
332 << " heading = " << azimuth );
334 geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon,
335 azimuth, found_r.length * FEET_TO_METER * 0.5 - 5.0,
336 &lat2, &lon2, &az2 );
338 if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > FG_EPSILON ) {
340 double odist = fgGetDouble("/sim/startup/offset-distance");
341 odist *= NM_TO_METER;
342 double oaz = azimuth;
343 if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > FG_EPSILON ) {
344 oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
346 while ( oaz >= 360.0 ) { oaz -= 360.0; }
347 geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
351 fgSetDouble("/position/longitude", lon2 );
352 fgSetDouble("/position/latitude", lat2 );
353 fgSetDouble("/orientation/heading", heading );
355 FG_LOG( FG_GENERAL, FG_INFO,
356 "Position for " << id << " is ("
358 << lat2 << ") new heading is "
365 // Set initial position and orientation
366 bool fgInitPosition( void ) {
367 FGInterface *f = current_aircraft.fdm_state;
368 string id = fgGetString("/sim/startup/airport-id");
370 // set initial position from default or command line coordinates
371 f->set_Longitude( fgGetDouble("/position/longitude") * DEG_TO_RAD );
372 f->set_Latitude( fgGetDouble("/position/latitude") * DEG_TO_RAD );
374 FG_LOG( FG_GENERAL, FG_INFO,
375 "scenery.cur_elev = " << scenery.cur_elev );
376 FG_LOG( FG_GENERAL, FG_INFO,
377 "/position/altitude = " << fgGetDouble("/position/altitude") );
379 // if we requested on ground startups
380 if ( fgGetBool( "/sim/startup/onground" ) ) {
381 fgSetDouble("/position/altitude", scenery.cur_elev + 1 );
384 // if requested altitude is below ground level
385 if ( scenery.cur_elev > fgGetDouble("/position/altitude") - 1) {
386 fgSetDouble("/position/altitude", scenery.cur_elev + 1 );
389 FG_LOG( FG_GENERAL, FG_INFO,
390 "starting altitude is = " <<
391 fgGetDouble("/position/altitude") );
393 f->set_Altitude( fgGetDouble("/position/altitude") );
394 FG_LOG( FG_GENERAL, FG_INFO,
395 "Initial position is: ("
396 << (f->get_Longitude() * RAD_TO_DEG) << ", "
397 << (f->get_Latitude() * RAD_TO_DEG) << ", "
398 << (f->get_Altitude() * FEET_TO_METER) << ")" );
404 // General house keeping initializations
405 bool fgInitGeneral( void ) {
408 #if defined(FX) && defined(XMESA)
409 char *mesa_win_state;
412 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
413 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
415 root = globals->get_fg_root();
416 if ( ! root.length() ) {
417 // No root path set? Then bail ...
418 FG_LOG( FG_GENERAL, FG_ALERT,
419 "Cannot continue without environment variable FG_ROOT"
420 << "being defined." );
423 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
425 #if defined(FX) && defined(XMESA)
426 // initialize full screen flag
427 global_fullscreen = false;
428 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
429 // Test for the MESA_GLX_FX env variable
430 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
431 // test if we are fullscreen mesa/glide
432 if ( (mesa_win_state[0] == 'f') ||
433 (mesa_win_state[0] == 'F') ) {
434 global_fullscreen = true;
444 // set initial aircraft speed
446 fgVelocityInit( void )
448 if (!fgHasValue("/sim/startup/speed-set")) {
449 current_aircraft.fdm_state->set_V_calibrated_kts(0.0);
453 const string speedset = fgGetString("/sim/startup/speed-set");
454 if (speedset == "knots" || speedset == "KNOTS") {
455 current_aircraft.fdm_state
456 ->set_V_calibrated_kts(fgGetDouble("/velocities/airspeed"));
457 } else if (speedset == "mach" || speedset == "MACH") {
458 current_aircraft.fdm_state
459 ->set_Mach_number(fgGetDouble("/velocities/mach"));
460 } else if (speedset == "UVW" || speedset == "uvw") {
461 current_aircraft.fdm_state
462 ->set_Velocities_Wind_Body(fgGetDouble("/velocities/uBody"),
463 fgGetDouble("/velocities/vBody"),
464 fgGetDouble("/velocities/wBody"));
465 } else if (speedset == "NED" || speedset == "ned") {
466 current_aircraft.fdm_state
467 ->set_Velocities_Local(fgGetDouble("/velocities/speed-north"),
468 fgGetDouble("/velocities/speed-east"),
469 fgGetDouble("/velocities/speed-down"));
471 FG_LOG(FG_GENERAL, FG_ALERT,
472 "Unrecognized value for /sim/startup/speed-set: " << speedset);
473 current_aircraft.fdm_state->set_V_calibrated_kts(0.0);
478 // This is the top level init routine which calls all the other
479 // initialization routines. If you are adding a subsystem to flight
480 // gear, its initialization call should located in this routine.
481 // Returns non-zero if a problem encountered.
482 bool fgInitSubsystems( void ) {
483 fgLIGHT *l = &cur_light_params;
485 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
486 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
488 // Initialize the material property lib
489 FGPath mpath( globals->get_fg_root() );
490 mpath.append( "materials" );
491 if ( material_lib.load( mpath.str() ) ) {
493 FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
497 // Initialize the Scenery Management subsystem
498 if ( fgSceneryInit() ) {
499 // Material lib initialized ok.
501 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
505 if ( global_tile_mgr.init() ) {
506 // Load the local scenery data
507 global_tile_mgr.update( fgGetDouble("/position/longitude"),
508 fgGetDouble("/position/latitude") );
510 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
514 FG_LOG( FG_GENERAL, FG_DEBUG,
515 "Current terrain elevation after tile mgr init " <<
518 double dt = 1.0 / fgGetInt("/sim/model-hz");
519 // cout << "dt = " << dt << endl;
521 const string &model = fgGetString("/sim/flight-model");
522 if (model == "larcsim") {
523 cur_fdm_state = new FGLaRCsim( dt );
524 } else if (model == "jsb") {
525 cur_fdm_state = new FGJSBsim( dt );
526 } else if (model == "ada") {
527 cur_fdm_state = new FGADA( dt );
528 } else if (model == "balloon") {
529 cur_fdm_state = new FGBalloonSim( dt );
530 } else if (model == "magic") {
531 cur_fdm_state = new FGMagicCarpet( dt );
532 } else if (model == "external") {
533 cur_fdm_state = new FGExternal( dt );
535 FG_LOG(FG_GENERAL, FG_ALERT,
536 "Unrecognized flight model '" << model
537 << ", can't init aircraft");
540 cur_fdm_state->stamp();
541 cur_fdm_state->set_remainder( 0 );
543 // allocates structures so must happen before any of the flight
544 // model or control parameters are set
545 fgAircraftInit(); // In the future this might not be the case.
547 fgFDMSetGroundElevation( fgGetString("/sim/flight-model"),
550 // set the initial position
553 // Calculate ground elevation at starting point (we didn't have
554 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
556 // calculalate a cartesian point somewhere along the line between
557 // the center of the earth and our view position. Doesn't have to
558 // be the exact elevation (this is good because we don't know it
561 // now handled inside of the fgTileMgrUpdate()
563 // Reset our altitude if we are below ground
564 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = "
565 << cur_fdm_state->get_Altitude() );
566 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
567 cur_fdm_state->get_Runway_altitude() );
569 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() +
571 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() +
575 FG_LOG( FG_GENERAL, FG_INFO,
576 "Updated position (after elevation adj): ("
577 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
578 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
579 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
581 // We need to calculate a few sea_level_radius here so we can pass
582 // the correct value to the view class
583 double sea_level_radius_meters;
585 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
586 cur_fdm_state->get_Altitude(),
587 &sea_level_radius_meters, &lat_geoc);
588 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters *
591 // The following section sets up the flight model EOM parameters
592 // and should really be read in from one or more files.
597 // Initial Orientation
599 // set_Euler_Angles( fgGetDouble("/orientation/roll") * DEG_TO_RAD,
600 // fgGetDouble("/orientation/pitch") * DEG_TO_RAD,
601 // fgGetDouble("/orientation/heading") * DEG_TO_RAD );
603 // Initialize the event manager
604 global_events.Init();
606 // Output event stats every 60 seconds
607 global_events.Register( "fgEVENT_MGR::PrintStats()",
608 fgMethodCallback<fgEVENT_MGR>( &global_events,
609 &fgEVENT_MGR::PrintStats),
610 fgEVENT::FG_EVENT_READY, 60000 );
612 // Initialize win_ratio parameters
613 for ( int i = 0; i < globals->get_viewmgr()->size(); ++i ) {
614 globals->get_viewmgr()->get_view(i)->
615 set_win_ratio( fgGetInt("/sim/startup/xsize") /
616 fgGetInt("/sim/startup/ysize") );
619 // Initialize pilot view
620 FGViewerRPH *pilot_view =
621 (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
623 pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(),
624 cur_fdm_state->get_Lat_geocentric(),
625 cur_fdm_state->get_Altitude() *
627 pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
629 pilot_view->set_rph( cur_fdm_state->get_Phi(),
630 cur_fdm_state->get_Theta(),
631 cur_fdm_state->get_Psi() );
633 // set current view to 0 (first) which is our main pilot view
634 globals->set_current_view( pilot_view );
636 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = "
637 << globals->get_current_view()->get_abs_view_pos());
639 // fgUpdateSunPos() needs a few position and view parameters set
640 // so it can calculate local relative sun angle and a few other
641 // things for correctly orienting the sky.
644 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
645 fgEVENT::FG_EVENT_READY, 60000);
646 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
647 fgEVENT::FG_EVENT_READY, 60000);
649 // Initialize Lighting interpolation tables
652 // update the lighting parameters (based on sun angle)
653 global_events.Register( "fgLight::Update()",
654 fgMethodCallback<fgLIGHT>( &cur_light_params,
656 fgEVENT::FG_EVENT_READY, 30000 );
657 // update the current timezone each 30 minutes
658 global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
659 fgEVENT::FG_EVENT_READY, 1800000);
661 // Initialize the weather modeling subsystem
662 #ifndef FG_OLD_WEATHER
663 // Initialize the WeatherDatabase
664 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
666 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
667 current_aircraft.fdm_state->get_Longitude(),
668 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
669 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
670 new FGLocalWeatherDatabase( position,
671 globals->get_fg_root() );
672 // cout << theFGLocalWeatherDatabase << endl;
673 // cout << "visibility = "
674 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
676 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
678 double init_vis = fgGetDouble("/environment/visibility");
679 if ( init_vis > 0 ) {
680 WeatherDatabase->setWeatherVisibility( init_vis );
683 // register the periodic update of the weather
684 global_events.Register( "weather update", fgUpdateWeatherDatabase,
685 fgEVENT::FG_EVENT_READY, 30000);
687 current_weather.Init();
690 // Initialize vor/ndb/ils/fix list management and query systems
691 FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
693 FG_LOG(FG_GENERAL, FG_INFO, " VOR/NDB");
694 current_navlist = new FGNavList;
695 FGPath p_nav( globals->get_fg_root() );
696 p_nav.append( "Navaids/default.nav" );
697 current_navlist->init( p_nav );
699 FG_LOG(FG_GENERAL, FG_INFO, " ILS");
700 current_ilslist = new FGILSList;
701 FGPath p_ils( globals->get_fg_root() );
702 p_ils.append( "Navaids/default.ils" );
703 current_ilslist->init( p_ils );
705 FG_LOG(FG_GENERAL, FG_INFO, " Fixes");
706 current_fixlist = new FGFixList;
707 FGPath p_fix( globals->get_fg_root() );
708 p_fix.append( "Navaids/default.fix" );
709 current_fixlist->init( p_fix );
711 // Radio stack subsystem.
712 current_radiostack = new FGRadioStack;
713 current_radiostack->init();
714 current_radiostack->bind();
716 // Initialize the Cockpit subsystem
717 if( fgCockpitInit( ¤t_aircraft )) {
718 // Cockpit initialized ok.
720 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
724 // Initialize the flight model subsystem data structures base on
727 cur_fdm_state->init();
728 cur_fdm_state->bind();
729 // if ( cur_fdm_state->init( 1.0 / fgGetInt("/sim/model-hz") ) ) {
730 // // fdm init successful
732 // FG_LOG( FG_GENERAL, FG_ALERT, "FDM init() failed! Cannot continue." );
736 // *ABCD* I'm just sticking this here for now, it should probably
738 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
740 if ( cur_fdm_state->get_Altitude() <
741 cur_fdm_state->get_Runway_altitude() + 3.758099)
743 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() +
747 FG_LOG( FG_GENERAL, FG_INFO,
748 "Updated position (after elevation adj): ("
749 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
750 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
751 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
752 // *ABCD* end of thing that I just stuck in that I should probably
756 if ( ! fgJoystickInit() ) {
757 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
761 current_autopilot = new FGAutopilot;
762 current_autopilot->init();
764 // initialize the gui parts of the autopilot
770 // Initialize I/O channels
771 #if ! defined( macintosh )
775 // Initialize the 2D panel.
776 string panel_path = fgGetString("/sim/panel/path",
777 "Panels/Default/default.xml");
778 current_panel = fgReadPanel(panel_path);
779 if (current_panel == 0) {
780 FG_LOG( FG_INPUT, FG_ALERT,
781 "Error reading new panel from " << panel_path );
783 FG_LOG( FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path );
784 current_panel->init();
785 current_panel->bind();
788 // Initialize the BFI
794 FG_LOG( FG_GENERAL, FG_INFO, endl);
796 // Save the initial state for future
798 globals->saveInitialState();
804 void fgReInitSubsystems( void )
806 FG_LOG( FG_GENERAL, FG_INFO,
807 "/position/altitude = " << fgGetDouble("/position/altitude") );
809 bool freeze = globals->get_freeze();
811 globals->set_freeze( true );
813 // Initialize the Scenery Management subsystem
814 if ( ! fgSceneryInit() ) {
815 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
819 if( global_tile_mgr.init() ) {
820 // Load the local scenery data
821 global_tile_mgr.update( fgGetDouble("/position/longitude"),
822 fgGetDouble("/position/latitude") );
824 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
828 // cout << "current scenery elev = " << scenery.cur_elev << endl;
830 fgFDMSetGroundElevation( fgGetString("/sim/flight-model"),
834 // Reset our altitude if we are below ground
835 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = "
836 << cur_fdm_state->get_Altitude() );
837 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = "
838 << cur_fdm_state->get_Runway_altitude() );
840 if ( cur_fdm_state->get_Altitude() <
841 cur_fdm_state->get_Runway_altitude() + 3.758099)
843 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() +
846 double sea_level_radius_meters;
848 sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
849 &sea_level_radius_meters, &lat_geoc);
850 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters *
853 // The following section sets up the flight model EOM parameters
854 // and should really be read in from one or more files.
859 // Initial Orientation
861 // set_Euler_Angles( fgGetDouble("/orientation/roll") * DEG_TO_RAD,
862 // fgGetDouble("/orientation/pitch") * DEG_TO_RAD,
863 // fgGetDouble("/orientation/heading") * DEG_TO_RAD );
865 // Initialize view parameters
866 FGViewerRPH *pilot_view =
867 (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
869 pilot_view->set_view_offset( 0.0 );
870 pilot_view->set_goal_view_offset( 0.0 );
872 pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(),
873 cur_fdm_state->get_Lat_geocentric(),
874 cur_fdm_state->get_Altitude() *
876 pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
878 pilot_view->set_rph( cur_fdm_state->get_Phi(),
879 cur_fdm_state->get_Theta(),
880 cur_fdm_state->get_Psi() );
882 // set current view to 0 (first) which is our main pilot view
883 globals->set_current_view( pilot_view );
885 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = "
886 << globals->get_current_view()->get_abs_view_pos());
888 cur_fdm_state->init();
889 // cur_fdm_state->bind();
890 // cur_fdm_state->init( 1.0 / fgGetInt("/sim/model-hz") );
892 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
894 if ( cur_fdm_state->get_Altitude() <
895 cur_fdm_state->get_Runway_altitude() + 3.758099)
897 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() +
901 controls.reset_all();
902 current_autopilot->reset();
905 globals->set_freeze( false );