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/autopilot.hxx>
61 #include <Cockpit/cockpit.hxx>
62 #include <FDM/Balloon.h>
63 #include <FDM/External.hxx>
64 #include <FDM/JSBsim.hxx>
65 #include <FDM/LaRCsim.hxx>
66 #include <FDM/MagicCarpet.hxx>
67 #include <Include/general.hxx>
68 #include <Joystick/joystick.hxx>
69 #include <Navaids/fixlist.hxx>
70 #include <Navaids/ilslist.hxx>
71 #include <Navaids/navlist.hxx>
72 #include <Scenery/scenery.hxx>
73 #include <Scenery/tilemgr.hxx>
74 #include <Time/event.hxx>
75 #include <Time/fg_time.hxx>
76 #include <Time/light.hxx>
77 #include <Time/sunpos.hxx>
78 #include <Time/moonpos.hxx>
80 #ifndef FG_OLD_WEATHER
81 # include <WeatherCM/FGLocalWeatherDatabase.h>
83 # include <Weather/weather.hxx>
86 #include "fg_init.hxx"
88 #include "options.hxx"
91 #if defined(FX) && defined(XMESA)
97 extern const char *default_root;
100 // Read in configuration (file and command line)
101 bool fgInitConfig ( int argc, char **argv ) {
102 // Attempt to locate and parse a config file
103 // First check fg_root
104 FGPath config( current_options.get_fg_root() );
105 config.append( "system.fgfsrc" );
106 current_options.parse_config_file( config.str() );
108 // Next check home directory
109 char* envp = ::getenv( "HOME" );
110 if ( envp != NULL ) {
112 config.append( ".fgfsrc" );
113 current_options.parse_config_file( config.str() );
116 // Parse remaining command line options
117 // These will override anything specified in a config file
118 if ( current_options.parse_command_line(argc, argv) !=
119 fgOPTIONS::FG_OPTIONS_OK )
121 // Something must have gone horribly wrong with the command
122 // line parsing or maybe the user just requested help ... :-)
123 current_options.usage();
124 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
132 // Set initial position and orientation
133 bool fgInitPosition( void ) {
137 f = current_aircraft.fdm_state;
139 id = current_options.get_airport_id();
141 // set initial position from airport id
143 FGPath path( current_options.get_fg_root() );
144 path.append( "Airports" );
145 path.append( "simple.gdbm" );
146 FGAirports airports( path.c_str() );
149 FG_LOG( FG_GENERAL, FG_INFO,
150 "Attempting to set starting position from airport code "
153 // FGPath inpath( current_options.get_fg_root() );
154 // inpath.append( "Airports" );
155 // inpath.append( "apt_simple" );
156 // airports.load( inpath.c_str() );
158 // FGPath outpath( current_options.get_fg_root() );
159 // outpath.append( "Airports" );
160 // outpath.append( "simple.gdbm" );
161 // airports.dump_gdbm( outpath.c_str() );
163 if ( ! airports.search( id, &a ) ) {
164 FG_LOG( FG_GENERAL, FG_ALERT,
165 "Failed to find " << id << " in database." );
168 f->set_Longitude( a.longitude * DEG_TO_RAD );
169 f->set_Latitude( a.latitude * DEG_TO_RAD );
172 // set initial position from default or command line coordinates
174 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
175 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
178 FG_LOG( FG_GENERAL, FG_INFO,
179 "starting altitude is = " << current_options.get_altitude() );
181 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
182 fgFDMSetGroundElevation( current_options.get_flight_model(),
183 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
185 FG_LOG( FG_GENERAL, FG_INFO,
186 "Initial position is: ("
187 << (f->get_Longitude() * RAD_TO_DEG) << ", "
188 << (f->get_Latitude() * RAD_TO_DEG) << ", "
189 << (f->get_Altitude() * FEET_TO_METER) << ")" );
195 // General house keeping initializations
196 bool fgInitGeneral( void ) {
199 #if defined(FX) && defined(XMESA)
200 char *mesa_win_state;
203 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
204 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
206 root = current_options.get_fg_root();
207 if ( ! root.length() ) {
208 // No root path set? Then bail ...
209 FG_LOG( FG_GENERAL, FG_ALERT,
210 "Cannot continue without environment variable FG_ROOT"
211 << "being defined." );
214 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
216 #if defined(FX) && defined(XMESA)
217 // initialize full screen flag
218 global_fullscreen = false;
219 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
220 // Test for the MESA_GLX_FX env variable
221 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
222 // test if we are fullscreen mesa/glide
223 if ( (mesa_win_state[0] == 'f') ||
224 (mesa_win_state[0] == 'F') ) {
225 global_fullscreen = true;
235 // This is the top level init routine which calls all the other
236 // initialization routines. If you are adding a subsystem to flight
237 // gear, its initialization call should located in this routine.
238 // Returns non-zero if a problem encountered.
239 bool fgInitSubsystems( void ) {
240 fgLIGHT *l = &cur_light_params;
241 FGTime *t = FGTime::cur_time_params;
243 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
244 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
246 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
247 cur_fdm_state = new FGLaRCsim;
248 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
249 cur_fdm_state = new FGJSBsim;
250 } else if ( current_options.get_flight_model() ==
251 FGInterface::FG_BALLOONSIM ) {
252 cur_fdm_state = new FGBalloonSim;
253 } else if ( current_options.get_flight_model() ==
254 FGInterface::FG_MAGICCARPET ) {
255 cur_fdm_state = new FGMagicCarpet;
256 } else if ( current_options.get_flight_model() ==
257 FGInterface::FG_EXTERNAL ) {
258 cur_fdm_state = new FGExternal;
260 FG_LOG( FG_GENERAL, FG_ALERT,
261 "No flight model, can't init aircraft" );
265 // allocates structures so must happen before any of the flight
266 // model or control parameters are set
267 fgAircraftInit(); // In the future this might not be the case.
269 // set the initial position
272 // Initialize the Scenery Management subsystem
273 if ( fgSceneryInit() ) {
274 // Scenery initialized ok.
276 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
280 if( global_tile_mgr.init() ) {
281 // Load the local scenery data
282 global_tile_mgr.update();
284 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
288 FG_LOG( FG_GENERAL, FG_DEBUG,
289 "Current terrain elevation after tile mgr init " <<
292 // Calculate ground elevation at starting point (we didn't have
293 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
295 // calculalate a cartesian point somewhere along the line between
296 // the center of the earth and our view position. Doesn't have to
297 // be the exact elevation (this is good because we don't know it
300 // now handled inside of the fgTileMgrUpdate()
303 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
304 tmp_abs_view_pos = fgGeodToCart(geod_pos);
306 FG_LOG( FG_GENERAL, FG_DEBUG,
307 "Initial abs_view_pos = " << tmp_abs_view_pos );
309 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
311 FG_LOG( FG_GENERAL, FG_DEBUG,
312 "Altitude after update " << scenery.cur_elev );
315 fgFDMSetGroundElevation( current_options.get_flight_model(),
318 // Reset our altitude if we are below ground
319 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
320 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
321 cur_fdm_state->get_Runway_altitude() );
323 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
324 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
327 FG_LOG( FG_GENERAL, FG_INFO,
328 "Updated position (after elevation adj): ("
329 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
330 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
331 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
333 // We need to calculate a few more values here that would normally
334 // be calculated by the FDM so that the current_view.UpdateViewMath()
335 // routine doesn't get hosed.
337 double sea_level_radius_meters;
339 // Set the FG variables first
340 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
341 &sea_level_radius_meters, &lat_geoc);
342 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
343 cur_fdm_state->get_Altitude() +
344 (sea_level_radius_meters * METER_TO_FEET) );
345 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
347 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
348 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
350 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
351 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
353 // The following section sets up the flight model EOM parameters
354 // and should really be read in from one or more files.
357 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
358 current_options.get_vBody(),
359 current_options.get_wBody());
361 // Initial Orientation
362 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
363 current_options.get_pitch() * DEG_TO_RAD,
364 current_options.get_heading() * DEG_TO_RAD );
366 // Initial Angular Body rates
367 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
369 cur_fdm_state->set_Earth_position_angle( 0.0 );
371 // Mass properties and geometry values
372 cur_fdm_state->set_Inertias( 8.547270E+01,
373 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
375 // CG position w.r.t. ref. point
376 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
378 // Initialize the event manager
379 global_events.Init();
381 // Output event stats every 60 seconds
382 global_events.Register( "fgEVENT_MGR::PrintStats()",
383 fgMethodCallback<fgEVENT_MGR>( &global_events,
384 &fgEVENT_MGR::PrintStats),
385 fgEVENT::FG_EVENT_READY, 60000 );
387 // Initialize view parameters
388 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
391 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
392 current_view.UpdateViewMath(*cur_fdm_state);
393 pilot_view.UpdateViewMath(*cur_fdm_state);
394 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
395 // current_view.UpdateWorldToEye(f);
397 // Initialize the planetary subsystem
398 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
399 // fgEVENT::FG_EVENT_READY, 600000);
401 // Initialize the sun's position
402 // global_events.Register( "fgSunInit()", fgSunInit,
403 // fgEVENT::FG_EVENT_READY, 30000 );
405 // Intialize the moon's position
406 // global_events.Register( "fgMoonInit()", fgMoonInit,
407 // fgEVENT::FG_EVENT_READY, 600000 );
409 // fgUpdateSunPos() needs a few position and view parameters set
410 // so it can calculate local relative sun angle and a few other
411 // things for correctly orienting the sky.
414 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
415 fgEVENT::FG_EVENT_READY, 60000);
416 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
417 fgEVENT::FG_EVENT_READY, 60000);
419 // Initialize Lighting interpolation tables
422 // update the lighting parameters (based on sun angle)
423 global_events.Register( "fgLight::Update()",
424 fgMethodCallback<fgLIGHT>( &cur_light_params,
426 fgEVENT::FG_EVENT_READY, 30000 );
427 // update the current timezone each 30 minutes
428 global_events.Register( "fgTIME::updateLocal()",
429 fgMethodCallback<FGTime>(FGTime::cur_time_params,
430 &FGTime::updateLocal),
431 fgEVENT::FG_EVENT_READY, 1800000);
433 // Initialize the weather modeling subsystem
434 #ifndef FG_OLD_WEATHER
435 // Initialize the WeatherDatabase
436 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
438 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
439 current_aircraft.fdm_state->get_Longitude(),
440 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
441 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
442 new FGLocalWeatherDatabase( position );
443 // cout << theFGLocalWeatherDatabase << endl;
444 // cout << "visibility = "
445 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
447 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
449 // register the periodic update of the weather
450 global_events.Register( "weather update", fgUpdateWeatherDatabase,
451 fgEVENT::FG_EVENT_READY, 30000);
453 current_weather.Init();
456 // Initialize vor/ndb/ils/fix list management and query systems
457 current_navlist = new FGNavList;
458 FGPath p_nav( current_options.get_fg_root() );
459 p_nav.append( "Navaids/default.nav" );
460 current_navlist->init( p_nav );
462 current_ilslist = new FGILSList;
463 FGPath p_ils( current_options.get_fg_root() );
464 p_ils.append( "Navaids/default.ils" );
465 current_ilslist->init( p_ils );
467 current_fixlist = new FGFixList;
468 FGPath p_fix( current_options.get_fg_root() );
469 p_fix.append( "Navaids/default.fix" );
470 current_fixlist->init( p_fix );
472 // Initialize the Cockpit subsystem
473 if( fgCockpitInit( ¤t_aircraft )) {
474 // Cockpit initialized ok.
476 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
480 // Initialize the flight model subsystem data structures base on
483 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
484 // 1.0 / current_options.get_model_hz() );
485 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
487 // I'm just sticking this here for now, it should probably move
489 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
491 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
492 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
495 FG_LOG( FG_GENERAL, FG_INFO,
496 "Updated position (after elevation adj): ("
497 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
498 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
499 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
500 // end of thing that I just stuck in that I should probably move
503 if ( fgJoystickInit() ) {
504 // Joystick initialized ok.
506 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
509 // Autopilot init added here, by Jeff Goeke-Smith
510 fgAPInit(¤t_aircraft);
512 // Initialize I/O channels
513 #if ! defined( MACOS )
517 FG_LOG( FG_GENERAL, FG_INFO, endl);
523 void fgReInitSubsystems( void )
525 FGTime *t = FGTime::cur_time_params;
527 int toggle_pause = t->getPause();
530 t->togglePauseMode();
533 if( global_tile_mgr.init() ) {
534 // Load the local scenery data
535 global_tile_mgr.update();
537 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
540 fgFDMSetGroundElevation( current_options.get_flight_model(),
543 // Reset our altitude if we are below ground
544 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
545 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
546 cur_fdm_state->get_Runway_altitude() );
548 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
549 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
551 double sea_level_radius_meters;
553 // Set the FG variables first
554 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
555 &sea_level_radius_meters, &lat_geoc);
556 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
557 cur_fdm_state->get_Altitude() +
558 (sea_level_radius_meters * METER_TO_FEET) );
559 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
561 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
562 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
564 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
565 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
567 // The following section sets up the flight model EOM parameters
568 // and should really be read in from one or more files.
571 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
572 current_options.get_vBody(),
573 current_options.get_wBody());
575 // Initial Orientation
576 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
577 current_options.get_pitch() * DEG_TO_RAD,
578 current_options.get_heading() * DEG_TO_RAD );
580 // Initial Angular Body rates
581 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
583 cur_fdm_state->set_Earth_position_angle( 0.0 );
585 // Mass properties and geometry values
586 cur_fdm_state->set_Inertias( 8.547270E+01,
587 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
589 // CG position w.r.t. ref. point
590 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
592 // Initialize view parameters
593 current_view.set_view_offset( 0.0 );
594 current_view.set_goal_view_offset( 0.0 );
595 pilot_view.set_view_offset( 0.0 );
596 pilot_view.set_goal_view_offset( 0.0 );
598 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
599 current_view.UpdateViewMath(*cur_fdm_state);
600 pilot_view.UpdateViewMath(*cur_fdm_state);
601 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
603 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
604 // 1.0 / current_options.get_model_hz() );
605 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
607 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
609 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
610 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
613 controls.reset_all();
617 t->togglePauseMode();