2 // fg_init.cxx -- Flight Gear top level initialization routines
4 // Written by Curtis Olson, started August 1997.
6 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 // For BC 5.01 this must be included before OpenGL includes.
31 #ifdef FG_MATH_EXCEPTION_CLASH
36 #include <simgear/xgl/xgl.h>
41 // work around a stdc++ lib bug in some versions of linux, but doesn't
42 // seem to hurt to have this here for all versions of Linux.
44 # define _G_NO_EXTERN_TEMPLATES
47 #include <simgear/compiler.h>
51 #include <simgear/constants.h>
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/math/fg_geodesy.hxx>
54 #include <simgear/math/point3d.hxx>
55 #include <simgear/math/polar3d.hxx>
56 #include <simgear/misc/fgpath.hxx>
58 #include <Aircraft/aircraft.hxx>
59 #include <Airports/simple.hxx>
60 #include <Autopilot/auto_gui.hxx>
61 #include <Autopilot/newauto.hxx>
62 #include <Cockpit/cockpit.hxx>
63 #include <Cockpit/radiostack.hxx>
64 #include <Cockpit/panel.hxx>
65 #include <Cockpit/sp_panel.hxx>
66 #include <FDM/Balloon.h>
67 #include <FDM/External.hxx>
68 #include <FDM/JSBsim.hxx>
69 #include <FDM/LaRCsim.hxx>
70 #include <FDM/MagicCarpet.hxx>
71 #include <Include/general.hxx>
72 #include <Joystick/joystick.hxx>
73 #include <Navaids/fixlist.hxx>
74 #include <Navaids/ilslist.hxx>
75 #include <Navaids/navlist.hxx>
76 #include <Scenery/scenery.hxx>
77 #include <Scenery/tilemgr.hxx>
78 #include <Time/event.hxx>
79 #include <Time/fg_time.hxx>
80 #include <Time/light.hxx>
81 #include <Time/sunpos.hxx>
82 #include <Time/moonpos.hxx>
84 #ifndef FG_OLD_WEATHER
85 # include <WeatherCM/FGLocalWeatherDatabase.h>
87 # include <Weather/weather.hxx>
90 #include "fg_init.hxx"
92 #include "options.hxx"
95 #if defined(FX) && defined(XMESA)
101 extern const char *default_root;
104 // Read in configuration (file and command line)
105 bool fgInitConfig ( int argc, char **argv ) {
106 // Attempt to locate and parse a config file
107 // First check fg_root
108 FGPath config( current_options.get_fg_root() );
109 config.append( "system.fgfsrc" );
110 current_options.parse_config_file( config.str() );
112 // Next check home directory
113 char* envp = ::getenv( "HOME" );
114 if ( envp != NULL ) {
116 config.append( ".fgfsrc" );
117 current_options.parse_config_file( config.str() );
120 // Parse remaining command line options
121 // These will override anything specified in a config file
122 if ( current_options.parse_command_line(argc, argv) !=
123 fgOPTIONS::FG_OPTIONS_OK )
125 // Something must have gone horribly wrong with the command
126 // line parsing or maybe the user just requested help ... :-)
127 current_options.usage();
128 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
136 // Set initial position and orientation
137 bool fgInitPosition( void ) {
141 f = current_aircraft.fdm_state;
143 id = current_options.get_airport_id();
145 // set initial position from airport id
147 FGPath path( current_options.get_fg_root() );
148 path.append( "Airports" );
149 path.append( "simple.gdbm" );
150 FGAirports airports( path.c_str() );
153 FG_LOG( FG_GENERAL, FG_INFO,
154 "Attempting to set starting position from airport code "
157 // FGPath inpath( current_options.get_fg_root() );
158 // inpath.append( "Airports" );
159 // inpath.append( "apt_simple" );
160 // airports.load( inpath.c_str() );
162 // FGPath outpath( current_options.get_fg_root() );
163 // outpath.append( "Airports" );
164 // outpath.append( "simple.gdbm" );
165 // airports.dump_gdbm( outpath.c_str() );
167 if ( ! airports.search( id, &a ) ) {
168 FG_LOG( FG_GENERAL, FG_ALERT,
169 "Failed to find " << id << " in database." );
172 f->set_Longitude( a.longitude * DEG_TO_RAD );
173 f->set_Latitude( a.latitude * DEG_TO_RAD );
176 // set initial position from default or command line coordinates
178 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
179 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
182 FG_LOG( FG_GENERAL, FG_INFO,
183 "starting altitude is = " << current_options.get_altitude() );
185 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
186 fgFDMSetGroundElevation( current_options.get_flight_model(),
187 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
189 FG_LOG( FG_GENERAL, FG_INFO,
190 "Initial position is: ("
191 << (f->get_Longitude() * RAD_TO_DEG) << ", "
192 << (f->get_Latitude() * RAD_TO_DEG) << ", "
193 << (f->get_Altitude() * FEET_TO_METER) << ")" );
199 // General house keeping initializations
200 bool fgInitGeneral( void ) {
203 #if defined(FX) && defined(XMESA)
204 char *mesa_win_state;
207 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
208 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
210 root = current_options.get_fg_root();
211 if ( ! root.length() ) {
212 // No root path set? Then bail ...
213 FG_LOG( FG_GENERAL, FG_ALERT,
214 "Cannot continue without environment variable FG_ROOT"
215 << "being defined." );
218 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
220 #if defined(FX) && defined(XMESA)
221 // initialize full screen flag
222 global_fullscreen = false;
223 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
224 // Test for the MESA_GLX_FX env variable
225 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
226 // test if we are fullscreen mesa/glide
227 if ( (mesa_win_state[0] == 'f') ||
228 (mesa_win_state[0] == 'F') ) {
229 global_fullscreen = true;
239 // This is the top level init routine which calls all the other
240 // initialization routines. If you are adding a subsystem to flight
241 // gear, its initialization call should located in this routine.
242 // Returns non-zero if a problem encountered.
243 bool fgInitSubsystems( void ) {
244 fgLIGHT *l = &cur_light_params;
245 FGTime *t = FGTime::cur_time_params;
247 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
248 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
250 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
251 cur_fdm_state = new FGLaRCsim;
252 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
253 cur_fdm_state = new FGJSBsim;
254 } else if ( current_options.get_flight_model() ==
255 FGInterface::FG_BALLOONSIM ) {
256 cur_fdm_state = new FGBalloonSim;
257 } else if ( current_options.get_flight_model() ==
258 FGInterface::FG_MAGICCARPET ) {
259 cur_fdm_state = new FGMagicCarpet;
260 } else if ( current_options.get_flight_model() ==
261 FGInterface::FG_EXTERNAL ) {
262 cur_fdm_state = new FGExternal;
264 FG_LOG( FG_GENERAL, FG_ALERT,
265 "No flight model, can't init aircraft" );
269 // allocates structures so must happen before any of the flight
270 // model or control parameters are set
271 fgAircraftInit(); // In the future this might not be the case.
273 // set the initial position
276 // Initialize the Scenery Management subsystem
277 if ( fgSceneryInit() ) {
278 // Scenery initialized ok.
280 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
284 if( global_tile_mgr.init() ) {
285 // Load the local scenery data
286 global_tile_mgr.update();
288 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
292 FG_LOG( FG_GENERAL, FG_DEBUG,
293 "Current terrain elevation after tile mgr init " <<
296 // Calculate ground elevation at starting point (we didn't have
297 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
299 // calculalate a cartesian point somewhere along the line between
300 // the center of the earth and our view position. Doesn't have to
301 // be the exact elevation (this is good because we don't know it
304 // now handled inside of the fgTileMgrUpdate()
307 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
308 tmp_abs_view_pos = fgGeodToCart(geod_pos);
310 FG_LOG( FG_GENERAL, FG_DEBUG,
311 "Initial abs_view_pos = " << tmp_abs_view_pos );
313 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
315 FG_LOG( FG_GENERAL, FG_DEBUG,
316 "Altitude after update " << scenery.cur_elev );
319 fgFDMSetGroundElevation( current_options.get_flight_model(),
322 // Reset our altitude if we are below ground
323 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
324 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
325 cur_fdm_state->get_Runway_altitude() );
327 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
328 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
331 FG_LOG( FG_GENERAL, FG_INFO,
332 "Updated position (after elevation adj): ("
333 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
334 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
335 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
337 // We need to calculate a few more values here that would normally
338 // be calculated by the FDM so that the current_view.UpdateViewMath()
339 // routine doesn't get hosed.
341 double sea_level_radius_meters;
343 // Set the FG variables first
344 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
345 &sea_level_radius_meters, &lat_geoc);
346 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
347 cur_fdm_state->get_Altitude() +
348 (sea_level_radius_meters * METER_TO_FEET) );
349 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
351 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
352 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
354 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
355 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
357 // The following section sets up the flight model EOM parameters
358 // and should really be read in from one or more files.
361 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
362 current_options.get_vBody(),
363 current_options.get_wBody());
365 // Initial Orientation
366 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
367 current_options.get_pitch() * DEG_TO_RAD,
368 current_options.get_heading() * DEG_TO_RAD );
370 // Initial Angular Body rates
371 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
373 cur_fdm_state->set_Earth_position_angle( 0.0 );
375 // Mass properties and geometry values
376 cur_fdm_state->set_Inertias( 8.547270E+01,
377 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
379 // CG position w.r.t. ref. point
380 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
382 // Initialize the event manager
383 global_events.Init();
385 // Output event stats every 60 seconds
386 global_events.Register( "fgEVENT_MGR::PrintStats()",
387 fgMethodCallback<fgEVENT_MGR>( &global_events,
388 &fgEVENT_MGR::PrintStats),
389 fgEVENT::FG_EVENT_READY, 60000 );
391 // Initialize view parameters
392 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
395 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
396 current_view.UpdateViewMath(*cur_fdm_state);
397 pilot_view.UpdateViewMath(*cur_fdm_state);
398 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
399 // current_view.UpdateWorldToEye(f);
401 // Initialize the planetary subsystem
402 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
403 // fgEVENT::FG_EVENT_READY, 600000);
405 // Initialize the sun's position
406 // global_events.Register( "fgSunInit()", fgSunInit,
407 // fgEVENT::FG_EVENT_READY, 30000 );
409 // Intialize the moon's position
410 // global_events.Register( "fgMoonInit()", fgMoonInit,
411 // fgEVENT::FG_EVENT_READY, 600000 );
413 // fgUpdateSunPos() needs a few position and view parameters set
414 // so it can calculate local relative sun angle and a few other
415 // things for correctly orienting the sky.
418 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
419 fgEVENT::FG_EVENT_READY, 60000);
420 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
421 fgEVENT::FG_EVENT_READY, 60000);
423 // Initialize Lighting interpolation tables
426 // update the lighting parameters (based on sun angle)
427 global_events.Register( "fgLight::Update()",
428 fgMethodCallback<fgLIGHT>( &cur_light_params,
430 fgEVENT::FG_EVENT_READY, 30000 );
431 // update the current timezone each 30 minutes
432 global_events.Register( "fgTIME::updateLocal()",
433 fgMethodCallback<FGTime>(FGTime::cur_time_params,
434 &FGTime::updateLocal),
435 fgEVENT::FG_EVENT_READY, 1800000);
437 // Initialize the weather modeling subsystem
438 #ifndef FG_OLD_WEATHER
439 // Initialize the WeatherDatabase
440 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
442 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
443 current_aircraft.fdm_state->get_Longitude(),
444 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
445 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
446 new FGLocalWeatherDatabase( position );
447 // cout << theFGLocalWeatherDatabase << endl;
448 // cout << "visibility = "
449 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
451 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
453 // register the periodic update of the weather
454 global_events.Register( "weather update", fgUpdateWeatherDatabase,
455 fgEVENT::FG_EVENT_READY, 30000);
457 current_weather.Init();
460 // Initialize vor/ndb/ils/fix list management and query systems
461 FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
463 FG_LOG(FG_GENERAL, FG_INFO, " VOR/NDB");
464 current_navlist = new FGNavList;
465 FGPath p_nav( current_options.get_fg_root() );
466 p_nav.append( "Navaids/default.nav" );
467 current_navlist->init( p_nav );
469 FG_LOG(FG_GENERAL, FG_INFO, " ILS");
470 current_ilslist = new FGILSList;
471 FGPath p_ils( current_options.get_fg_root() );
472 p_ils.append( "Navaids/default.ils" );
473 current_ilslist->init( p_ils );
475 FG_LOG(FG_GENERAL, FG_INFO, " Fixes");
476 current_fixlist = new FGFixList;
477 FGPath p_fix( current_options.get_fg_root() );
478 p_fix.append( "Navaids/default.fix" );
479 current_fixlist->init( p_fix );
481 // Initialize the underlying radio stack model
482 current_radiostack = new FGRadioStack;
484 current_radiostack->set_nav1_freq( 117.30 );
485 current_radiostack->set_nav1_alt_freq( 110.30 );
486 current_radiostack->set_nav1_sel_radial( 299.0 );
488 current_radiostack->set_nav2_freq( 115.70 );
489 current_radiostack->set_nav2_alt_freq( 115.70 );
490 current_radiostack->set_nav2_sel_radial( 45.0 );
492 current_radiostack->set_adf_freq( 266.0 );
494 current_radiostack->search( cur_fdm_state->get_Longitude(),
495 cur_fdm_state->get_Latitude(),
496 cur_fdm_state->get_Altitude() * FEET_TO_METER );
498 current_radiostack->update( cur_fdm_state->get_Longitude(),
499 cur_fdm_state->get_Latitude(),
500 cur_fdm_state->get_Altitude() * FEET_TO_METER );
502 // Search radio database once per second
503 global_events.Register( "fgRadioSearch()", fgRadioSearch,
504 fgEVENT::FG_EVENT_READY, 1000);
507 // Initialize the Cockpit subsystem
508 if( fgCockpitInit( ¤t_aircraft )) {
509 // Cockpit initialized ok.
511 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
515 // Initialize the flight model subsystem data structures base on
518 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
519 // 1.0 / current_options.get_model_hz() );
520 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
522 // I'm just sticking this here for now, it should probably move
524 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
526 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
527 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
530 FG_LOG( FG_GENERAL, FG_INFO,
531 "Updated position (after elevation adj): ("
532 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
533 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
534 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
535 // end of thing that I just stuck in that I should probably move
538 if ( fgJoystickInit() ) {
539 // Joystick initialized ok.
541 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
545 current_autopilot = new FGAutopilot;
546 current_autopilot->init();
548 // initialize the gui parts of the autopilot
554 // Initialize I/O channels
555 #if ! defined( MACOS )
559 // Initialize the 2D panel.
560 current_panel = fgCreateSmallSinglePropPanel(0, 0, 1024, 768);
562 FG_LOG( FG_GENERAL, FG_INFO, endl);
568 void fgReInitSubsystems( void )
570 FGTime *t = FGTime::cur_time_params;
572 int toggle_pause = t->getPause();
575 t->togglePauseMode();
578 if( global_tile_mgr.init() ) {
579 // Load the local scenery data
580 global_tile_mgr.update();
582 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
585 fgFDMSetGroundElevation( current_options.get_flight_model(),
588 // Reset our altitude if we are below ground
589 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
590 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
591 cur_fdm_state->get_Runway_altitude() );
593 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
594 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
596 double sea_level_radius_meters;
598 // Set the FG variables first
599 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
600 &sea_level_radius_meters, &lat_geoc);
601 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
602 cur_fdm_state->get_Altitude() +
603 (sea_level_radius_meters * METER_TO_FEET) );
604 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
606 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
607 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
609 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
610 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
612 // The following section sets up the flight model EOM parameters
613 // and should really be read in from one or more files.
616 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
617 current_options.get_vBody(),
618 current_options.get_wBody());
620 // Initial Orientation
621 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
622 current_options.get_pitch() * DEG_TO_RAD,
623 current_options.get_heading() * DEG_TO_RAD );
625 // Initial Angular Body rates
626 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
628 cur_fdm_state->set_Earth_position_angle( 0.0 );
630 // Mass properties and geometry values
631 cur_fdm_state->set_Inertias( 8.547270E+01,
632 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
634 // CG position w.r.t. ref. point
635 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
637 // Initialize view parameters
638 current_view.set_view_offset( 0.0 );
639 current_view.set_goal_view_offset( 0.0 );
640 pilot_view.set_view_offset( 0.0 );
641 pilot_view.set_goal_view_offset( 0.0 );
643 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
644 current_view.UpdateViewMath(*cur_fdm_state);
645 pilot_view.UpdateViewMath(*cur_fdm_state);
646 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
648 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
649 // 1.0 / current_options.get_model_hz() );
650 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
652 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
654 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
655 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
658 controls.reset_all();
659 current_autopilot->reset();
662 t->togglePauseMode();