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 <FDM/Balloon.h>
65 #include <FDM/External.hxx>
66 #include <FDM/JSBsim.hxx>
67 #include <FDM/LaRCsim.hxx>
68 #include <FDM/MagicCarpet.hxx>
69 #include <Include/general.hxx>
70 #include <Joystick/joystick.hxx>
71 #include <Navaids/fixlist.hxx>
72 #include <Navaids/ilslist.hxx>
73 #include <Navaids/navlist.hxx>
74 #include <Scenery/scenery.hxx>
75 #include <Scenery/tilemgr.hxx>
76 #include <Time/event.hxx>
77 #include <Time/fg_time.hxx>
78 #include <Time/light.hxx>
79 #include <Time/sunpos.hxx>
80 #include <Time/moonpos.hxx>
82 #ifndef FG_OLD_WEATHER
83 # include <WeatherCM/FGLocalWeatherDatabase.h>
85 # include <Weather/weather.hxx>
88 #include "fg_init.hxx"
90 #include "options.hxx"
93 #if defined(FX) && defined(XMESA)
99 extern const char *default_root;
102 // Read in configuration (file and command line)
103 bool fgInitConfig ( int argc, char **argv ) {
104 // Attempt to locate and parse a config file
105 // First check fg_root
106 FGPath config( current_options.get_fg_root() );
107 config.append( "system.fgfsrc" );
108 current_options.parse_config_file( config.str() );
110 // Next check home directory
111 char* envp = ::getenv( "HOME" );
112 if ( envp != NULL ) {
114 config.append( ".fgfsrc" );
115 current_options.parse_config_file( config.str() );
118 // Parse remaining command line options
119 // These will override anything specified in a config file
120 if ( current_options.parse_command_line(argc, argv) !=
121 fgOPTIONS::FG_OPTIONS_OK )
123 // Something must have gone horribly wrong with the command
124 // line parsing or maybe the user just requested help ... :-)
125 current_options.usage();
126 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
134 // Set initial position and orientation
135 bool fgInitPosition( void ) {
139 f = current_aircraft.fdm_state;
141 id = current_options.get_airport_id();
143 // set initial position from airport id
145 FGPath path( current_options.get_fg_root() );
146 path.append( "Airports" );
147 path.append( "simple.gdbm" );
148 FGAirports airports( path.c_str() );
151 FG_LOG( FG_GENERAL, FG_INFO,
152 "Attempting to set starting position from airport code "
155 // FGPath inpath( current_options.get_fg_root() );
156 // inpath.append( "Airports" );
157 // inpath.append( "apt_simple" );
158 // airports.load( inpath.c_str() );
160 // FGPath outpath( current_options.get_fg_root() );
161 // outpath.append( "Airports" );
162 // outpath.append( "simple.gdbm" );
163 // airports.dump_gdbm( outpath.c_str() );
165 if ( ! airports.search( id, &a ) ) {
166 FG_LOG( FG_GENERAL, FG_ALERT,
167 "Failed to find " << id << " in database." );
170 f->set_Longitude( a.longitude * DEG_TO_RAD );
171 f->set_Latitude( a.latitude * DEG_TO_RAD );
174 // set initial position from default or command line coordinates
176 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
177 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
180 FG_LOG( FG_GENERAL, FG_INFO,
181 "starting altitude is = " << current_options.get_altitude() );
183 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
184 fgFDMSetGroundElevation( current_options.get_flight_model(),
185 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
187 FG_LOG( FG_GENERAL, FG_INFO,
188 "Initial position is: ("
189 << (f->get_Longitude() * RAD_TO_DEG) << ", "
190 << (f->get_Latitude() * RAD_TO_DEG) << ", "
191 << (f->get_Altitude() * FEET_TO_METER) << ")" );
197 // General house keeping initializations
198 bool fgInitGeneral( void ) {
201 #if defined(FX) && defined(XMESA)
202 char *mesa_win_state;
205 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
206 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
208 root = current_options.get_fg_root();
209 if ( ! root.length() ) {
210 // No root path set? Then bail ...
211 FG_LOG( FG_GENERAL, FG_ALERT,
212 "Cannot continue without environment variable FG_ROOT"
213 << "being defined." );
216 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
218 #if defined(FX) && defined(XMESA)
219 // initialize full screen flag
220 global_fullscreen = false;
221 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
222 // Test for the MESA_GLX_FX env variable
223 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
224 // test if we are fullscreen mesa/glide
225 if ( (mesa_win_state[0] == 'f') ||
226 (mesa_win_state[0] == 'F') ) {
227 global_fullscreen = true;
237 // This is the top level init routine which calls all the other
238 // initialization routines. If you are adding a subsystem to flight
239 // gear, its initialization call should located in this routine.
240 // Returns non-zero if a problem encountered.
241 bool fgInitSubsystems( void ) {
242 fgLIGHT *l = &cur_light_params;
243 FGTime *t = FGTime::cur_time_params;
245 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
246 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
248 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
249 cur_fdm_state = new FGLaRCsim;
250 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
251 cur_fdm_state = new FGJSBsim;
252 } else if ( current_options.get_flight_model() ==
253 FGInterface::FG_BALLOONSIM ) {
254 cur_fdm_state = new FGBalloonSim;
255 } else if ( current_options.get_flight_model() ==
256 FGInterface::FG_MAGICCARPET ) {
257 cur_fdm_state = new FGMagicCarpet;
258 } else if ( current_options.get_flight_model() ==
259 FGInterface::FG_EXTERNAL ) {
260 cur_fdm_state = new FGExternal;
262 FG_LOG( FG_GENERAL, FG_ALERT,
263 "No flight model, can't init aircraft" );
267 // allocates structures so must happen before any of the flight
268 // model or control parameters are set
269 fgAircraftInit(); // In the future this might not be the case.
271 // set the initial position
274 // Initialize the Scenery Management subsystem
275 if ( fgSceneryInit() ) {
276 // Scenery initialized ok.
278 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
282 if( global_tile_mgr.init() ) {
283 // Load the local scenery data
284 global_tile_mgr.update();
286 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
290 FG_LOG( FG_GENERAL, FG_DEBUG,
291 "Current terrain elevation after tile mgr init " <<
294 // Calculate ground elevation at starting point (we didn't have
295 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
297 // calculalate a cartesian point somewhere along the line between
298 // the center of the earth and our view position. Doesn't have to
299 // be the exact elevation (this is good because we don't know it
302 // now handled inside of the fgTileMgrUpdate()
305 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
306 tmp_abs_view_pos = fgGeodToCart(geod_pos);
308 FG_LOG( FG_GENERAL, FG_DEBUG,
309 "Initial abs_view_pos = " << tmp_abs_view_pos );
311 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
313 FG_LOG( FG_GENERAL, FG_DEBUG,
314 "Altitude after update " << scenery.cur_elev );
317 fgFDMSetGroundElevation( current_options.get_flight_model(),
320 // Reset our altitude if we are below ground
321 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
322 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
323 cur_fdm_state->get_Runway_altitude() );
325 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
326 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
329 FG_LOG( FG_GENERAL, FG_INFO,
330 "Updated position (after elevation adj): ("
331 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
332 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
333 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
335 // We need to calculate a few more values here that would normally
336 // be calculated by the FDM so that the current_view.UpdateViewMath()
337 // routine doesn't get hosed.
339 double sea_level_radius_meters;
341 // Set the FG variables first
342 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
343 &sea_level_radius_meters, &lat_geoc);
344 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
345 cur_fdm_state->get_Altitude() +
346 (sea_level_radius_meters * METER_TO_FEET) );
347 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
349 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
350 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
352 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
353 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
355 // The following section sets up the flight model EOM parameters
356 // and should really be read in from one or more files.
359 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
360 current_options.get_vBody(),
361 current_options.get_wBody());
363 // Initial Orientation
364 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
365 current_options.get_pitch() * DEG_TO_RAD,
366 current_options.get_heading() * DEG_TO_RAD );
368 // Initial Angular Body rates
369 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
371 cur_fdm_state->set_Earth_position_angle( 0.0 );
373 // Mass properties and geometry values
374 cur_fdm_state->set_Inertias( 8.547270E+01,
375 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
377 // CG position w.r.t. ref. point
378 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
380 // Initialize the event manager
381 global_events.Init();
383 // Output event stats every 60 seconds
384 global_events.Register( "fgEVENT_MGR::PrintStats()",
385 fgMethodCallback<fgEVENT_MGR>( &global_events,
386 &fgEVENT_MGR::PrintStats),
387 fgEVENT::FG_EVENT_READY, 60000 );
389 // Initialize view parameters
390 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
393 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
394 current_view.UpdateViewMath(*cur_fdm_state);
395 pilot_view.UpdateViewMath(*cur_fdm_state);
396 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
397 // current_view.UpdateWorldToEye(f);
399 // Initialize the planetary subsystem
400 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
401 // fgEVENT::FG_EVENT_READY, 600000);
403 // Initialize the sun's position
404 // global_events.Register( "fgSunInit()", fgSunInit,
405 // fgEVENT::FG_EVENT_READY, 30000 );
407 // Intialize the moon's position
408 // global_events.Register( "fgMoonInit()", fgMoonInit,
409 // fgEVENT::FG_EVENT_READY, 600000 );
411 // fgUpdateSunPos() needs a few position and view parameters set
412 // so it can calculate local relative sun angle and a few other
413 // things for correctly orienting the sky.
416 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
417 fgEVENT::FG_EVENT_READY, 60000);
418 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
419 fgEVENT::FG_EVENT_READY, 60000);
421 // Initialize Lighting interpolation tables
424 // update the lighting parameters (based on sun angle)
425 global_events.Register( "fgLight::Update()",
426 fgMethodCallback<fgLIGHT>( &cur_light_params,
428 fgEVENT::FG_EVENT_READY, 30000 );
429 // update the current timezone each 30 minutes
430 global_events.Register( "fgTIME::updateLocal()",
431 fgMethodCallback<FGTime>(FGTime::cur_time_params,
432 &FGTime::updateLocal),
433 fgEVENT::FG_EVENT_READY, 1800000);
435 // Initialize the weather modeling subsystem
436 #ifndef FG_OLD_WEATHER
437 // Initialize the WeatherDatabase
438 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
440 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
441 current_aircraft.fdm_state->get_Longitude(),
442 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
443 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
444 new FGLocalWeatherDatabase( position );
445 // cout << theFGLocalWeatherDatabase << endl;
446 // cout << "visibility = "
447 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
449 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
451 // register the periodic update of the weather
452 global_events.Register( "weather update", fgUpdateWeatherDatabase,
453 fgEVENT::FG_EVENT_READY, 30000);
455 current_weather.Init();
458 // Initialize vor/ndb/ils/fix list management and query systems
459 FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
461 current_navlist = new FGNavList;
462 FGPath p_nav( current_options.get_fg_root() );
463 p_nav.append( "Navaids/default.nav" );
464 current_navlist->init( p_nav );
466 current_ilslist = new FGILSList;
467 FGPath p_ils( current_options.get_fg_root() );
468 p_ils.append( "Navaids/default.ils" );
469 current_ilslist->init( p_ils );
471 current_fixlist = new FGFixList;
472 FGPath p_fix( current_options.get_fg_root() );
473 p_fix.append( "Navaids/default.fix" );
474 current_fixlist->init( p_fix );
476 // Initialize the underlying radio stack model
477 current_radiostack = new FGRadioStack;
479 current_radiostack->set_nav1_freq( 110.30 );
480 current_radiostack->set_nav1_sel_radial( 299.0 );
482 current_radiostack->set_nav2_freq( 115.70 );
483 current_radiostack->set_nav2_sel_radial( 45.0 );
485 current_radiostack->set_adf_freq( 266.0 );
487 current_radiostack->update( cur_fdm_state->get_Longitude(),
488 cur_fdm_state->get_Latitude(),
489 cur_fdm_state->get_Altitude() * FEET_TO_METER );
491 // Initialize the Cockpit subsystem
492 if( fgCockpitInit( ¤t_aircraft )) {
493 // Cockpit initialized ok.
495 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
499 // Initialize the 2D panel.
500 current_panel.init(0, 0, 1024, 768);
502 // Initialize the flight model subsystem data structures base on
505 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
506 // 1.0 / current_options.get_model_hz() );
507 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
509 // I'm just sticking this here for now, it should probably move
511 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
513 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
514 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
517 FG_LOG( FG_GENERAL, FG_INFO,
518 "Updated position (after elevation adj): ("
519 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
520 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
521 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
522 // end of thing that I just stuck in that I should probably move
525 if ( fgJoystickInit() ) {
526 // Joystick initialized ok.
528 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
532 current_autopilot = new FGAutopilot;
533 current_autopilot->init();
535 // initialize the gui parts of the autopilot
541 // Initialize I/O channels
542 #if ! defined( MACOS )
546 FG_LOG( FG_GENERAL, FG_INFO, endl);
552 void fgReInitSubsystems( void )
554 FGTime *t = FGTime::cur_time_params;
556 int toggle_pause = t->getPause();
559 t->togglePauseMode();
562 if( global_tile_mgr.init() ) {
563 // Load the local scenery data
564 global_tile_mgr.update();
566 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
569 fgFDMSetGroundElevation( current_options.get_flight_model(),
572 // Reset our altitude if we are below ground
573 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
574 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
575 cur_fdm_state->get_Runway_altitude() );
577 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
578 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
580 double sea_level_radius_meters;
582 // Set the FG variables first
583 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
584 &sea_level_radius_meters, &lat_geoc);
585 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
586 cur_fdm_state->get_Altitude() +
587 (sea_level_radius_meters * METER_TO_FEET) );
588 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
590 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
591 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
593 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
594 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
596 // The following section sets up the flight model EOM parameters
597 // and should really be read in from one or more files.
600 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
601 current_options.get_vBody(),
602 current_options.get_wBody());
604 // Initial Orientation
605 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
606 current_options.get_pitch() * DEG_TO_RAD,
607 current_options.get_heading() * DEG_TO_RAD );
609 // Initial Angular Body rates
610 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
612 cur_fdm_state->set_Earth_position_angle( 0.0 );
614 // Mass properties and geometry values
615 cur_fdm_state->set_Inertias( 8.547270E+01,
616 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
618 // CG position w.r.t. ref. point
619 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
621 // Initialize view parameters
622 current_view.set_view_offset( 0.0 );
623 current_view.set_goal_view_offset( 0.0 );
624 pilot_view.set_view_offset( 0.0 );
625 pilot_view.set_goal_view_offset( 0.0 );
627 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
628 current_view.UpdateViewMath(*cur_fdm_state);
629 pilot_view.UpdateViewMath(*cur_fdm_state);
630 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
632 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
633 // 1.0 / current_options.get_model_hz() );
634 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
636 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
638 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
639 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
642 controls.reset_all();
643 current_autopilot->reset();
646 t->togglePauseMode();