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 <Scenery/scenery.hxx>
70 #include <Scenery/tilemgr.hxx>
71 #include <Time/event.hxx>
72 #include <Time/fg_time.hxx>
73 #include <Time/light.hxx>
74 #include <Time/sunpos.hxx>
75 #include <Time/moonpos.hxx>
77 #ifndef FG_OLD_WEATHER
78 # include <WeatherCM/FGLocalWeatherDatabase.h>
80 # include <Weather/weather.hxx>
83 #include "fg_init.hxx"
85 #include "options.hxx"
88 #if defined(FX) && defined(XMESA)
94 extern const char *default_root;
97 // Read in configuration (file and command line)
98 bool fgInitConfig ( int argc, char **argv ) {
99 // Attempt to locate and parse a config file
100 // First check fg_root
101 FGPath config( current_options.get_fg_root() );
102 config.append( "system.fgfsrc" );
103 current_options.parse_config_file( config.str() );
105 // Next check home directory
106 char* envp = ::getenv( "HOME" );
107 if ( envp != NULL ) {
109 config.append( ".fgfsrc" );
110 current_options.parse_config_file( config.str() );
113 // Parse remaining command line options
114 // These will override anything specified in a config file
115 if ( current_options.parse_command_line(argc, argv) !=
116 fgOPTIONS::FG_OPTIONS_OK )
118 // Something must have gone horribly wrong with the command
119 // line parsing or maybe the user just requested help ... :-)
120 current_options.usage();
121 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
129 // Set initial position and orientation
130 bool fgInitPosition( void ) {
134 f = current_aircraft.fdm_state;
136 id = current_options.get_airport_id();
138 // set initial position from airport id
140 FGPath path( current_options.get_fg_root() );
141 path.append( "Airports" );
142 path.append( "simple.gdbm" );
143 FGAirports airports( path.c_str() );
146 FG_LOG( FG_GENERAL, FG_INFO,
147 "Attempting to set starting position from airport code "
150 // FGPath inpath( current_options.get_fg_root() );
151 // inpath.append( "Airports" );
152 // inpath.append( "apt_simple" );
153 // airports.load( inpath.c_str() );
155 // FGPath outpath( current_options.get_fg_root() );
156 // outpath.append( "Airports" );
157 // outpath.append( "simple.gdbm" );
158 // airports.dump_gdbm( outpath.c_str() );
160 if ( ! airports.search( id, &a ) ) {
161 FG_LOG( FG_GENERAL, FG_ALERT,
162 "Failed to find " << id << " in database." );
165 f->set_Longitude( a.longitude * DEG_TO_RAD );
166 f->set_Latitude( a.latitude * DEG_TO_RAD );
169 // set initial position from default or command line coordinates
171 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
172 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
175 FG_LOG( FG_GENERAL, FG_INFO,
176 "starting altitude is = " << current_options.get_altitude() );
178 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
179 fgFDMSetGroundElevation( current_options.get_flight_model(),
180 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
182 FG_LOG( FG_GENERAL, FG_INFO,
183 "Initial position is: ("
184 << (f->get_Longitude() * RAD_TO_DEG) << ", "
185 << (f->get_Latitude() * RAD_TO_DEG) << ", "
186 << (f->get_Altitude() * FEET_TO_METER) << ")" );
192 // General house keeping initializations
193 bool fgInitGeneral( void ) {
196 #if defined(FX) && defined(XMESA)
197 char *mesa_win_state;
200 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
201 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
203 root = current_options.get_fg_root();
204 if ( ! root.length() ) {
205 // No root path set? Then bail ...
206 FG_LOG( FG_GENERAL, FG_ALERT,
207 "Cannot continue without environment variable FG_ROOT"
208 << "being defined." );
211 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
213 #if defined(FX) && defined(XMESA)
214 // initialize full screen flag
215 global_fullscreen = false;
216 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
217 // Test for the MESA_GLX_FX env variable
218 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
219 // test if we are fullscreen mesa/glide
220 if ( (mesa_win_state[0] == 'f') ||
221 (mesa_win_state[0] == 'F') ) {
222 global_fullscreen = true;
232 // This is the top level init routine which calls all the other
233 // initialization routines. If you are adding a subsystem to flight
234 // gear, its initialization call should located in this routine.
235 // Returns non-zero if a problem encountered.
236 bool fgInitSubsystems( void ) {
237 fgLIGHT *l = &cur_light_params;
238 FGTime *t = FGTime::cur_time_params;
240 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
241 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
243 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
244 cur_fdm_state = new FGLaRCsim;
245 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
246 cur_fdm_state = new FGJSBsim;
247 } else if ( current_options.get_flight_model() ==
248 FGInterface::FG_BALLOONSIM ) {
249 cur_fdm_state = new FGBalloonSim;
250 } else if ( current_options.get_flight_model() ==
251 FGInterface::FG_MAGICCARPET ) {
252 cur_fdm_state = new FGMagicCarpet;
253 } else if ( current_options.get_flight_model() ==
254 FGInterface::FG_EXTERNAL ) {
255 cur_fdm_state = new FGExternal;
257 FG_LOG( FG_GENERAL, FG_ALERT,
258 "No flight model, can't init aircraft" );
262 // allocates structures so must happen before any of the flight
263 // model or control parameters are set
264 fgAircraftInit(); // In the future this might not be the case.
266 // set the initial position
269 // Initialize the Scenery Management subsystem
270 if ( fgSceneryInit() ) {
271 // Scenery initialized ok.
273 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
277 if( global_tile_mgr.init() ) {
278 // Load the local scenery data
279 global_tile_mgr.update();
281 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
285 FG_LOG( FG_GENERAL, FG_DEBUG,
286 "Current terrain elevation after tile mgr init " <<
289 // Calculate ground elevation at starting point (we didn't have
290 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
292 // calculalate a cartesian point somewhere along the line between
293 // the center of the earth and our view position. Doesn't have to
294 // be the exact elevation (this is good because we don't know it
297 // now handled inside of the fgTileMgrUpdate()
300 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
301 tmp_abs_view_pos = fgGeodToCart(geod_pos);
303 FG_LOG( FG_GENERAL, FG_DEBUG,
304 "Initial abs_view_pos = " << tmp_abs_view_pos );
306 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
308 FG_LOG( FG_GENERAL, FG_DEBUG,
309 "Altitude after update " << scenery.cur_elev );
312 fgFDMSetGroundElevation( current_options.get_flight_model(),
315 // Reset our altitude if we are below ground
316 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
317 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
318 cur_fdm_state->get_Runway_altitude() );
320 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
321 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
324 FG_LOG( FG_GENERAL, FG_INFO,
325 "Updated position (after elevation adj): ("
326 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
327 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
328 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
330 // We need to calculate a few more values here that would normally
331 // be calculated by the FDM so that the current_view.UpdateViewMath()
332 // routine doesn't get hosed.
334 double sea_level_radius_meters;
336 // Set the FG variables first
337 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
338 &sea_level_radius_meters, &lat_geoc);
339 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
340 cur_fdm_state->get_Altitude() +
341 (sea_level_radius_meters * METER_TO_FEET) );
342 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
344 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
345 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
347 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
348 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
350 // The following section sets up the flight model EOM parameters
351 // and should really be read in from one or more files.
354 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
355 current_options.get_vBody(),
356 current_options.get_wBody());
358 // Initial Orientation
359 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
360 current_options.get_pitch() * DEG_TO_RAD,
361 current_options.get_heading() * DEG_TO_RAD );
363 // Initial Angular Body rates
364 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
366 cur_fdm_state->set_Earth_position_angle( 0.0 );
368 // Mass properties and geometry values
369 cur_fdm_state->set_Inertias( 8.547270E+01,
370 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
372 // CG position w.r.t. ref. point
373 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
375 // Initialize the event manager
376 global_events.Init();
378 // Output event stats every 60 seconds
379 global_events.Register( "fgEVENT_MGR::PrintStats()",
380 fgMethodCallback<fgEVENT_MGR>( &global_events,
381 &fgEVENT_MGR::PrintStats),
382 fgEVENT::FG_EVENT_READY, 60000 );
384 // Initialize view parameters
385 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
388 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
389 current_view.UpdateViewMath(*cur_fdm_state);
390 pilot_view.UpdateViewMath(*cur_fdm_state);
391 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
392 // current_view.UpdateWorldToEye(f);
394 // Initialize the planetary subsystem
395 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
396 // fgEVENT::FG_EVENT_READY, 600000);
398 // Initialize the sun's position
399 // global_events.Register( "fgSunInit()", fgSunInit,
400 // fgEVENT::FG_EVENT_READY, 30000 );
402 // Intialize the moon's position
403 // global_events.Register( "fgMoonInit()", fgMoonInit,
404 // fgEVENT::FG_EVENT_READY, 600000 );
406 // fgUpdateSunPos() needs a few position and view parameters set
407 // so it can calculate local relative sun angle and a few other
408 // things for correctly orienting the sky.
411 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
412 fgEVENT::FG_EVENT_READY, 60000);
413 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
414 fgEVENT::FG_EVENT_READY, 60000);
416 // Initialize Lighting interpolation tables
419 // update the lighting parameters (based on sun angle)
420 global_events.Register( "fgLight::Update()",
421 fgMethodCallback<fgLIGHT>( &cur_light_params,
423 fgEVENT::FG_EVENT_READY, 30000 );
424 // update the current timezone each 30 minutes
425 global_events.Register( "fgTIME::updateLocal()",
426 fgMethodCallback<FGTime>(FGTime::cur_time_params,
427 &FGTime::updateLocal),
428 fgEVENT::FG_EVENT_READY, 1800000);
430 // Initialize the weather modeling subsystem
431 #ifndef FG_OLD_WEATHER
432 // Initialize the WeatherDatabase
433 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
435 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
436 current_aircraft.fdm_state->get_Longitude(),
437 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
438 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
439 new FGLocalWeatherDatabase( position );
440 // cout << theFGLocalWeatherDatabase << endl;
441 // cout << "visibility = "
442 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
444 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
446 // register the periodic update of the weather
447 global_events.Register( "weather update", fgUpdateWeatherDatabase,
448 fgEVENT::FG_EVENT_READY, 30000);
450 current_weather.Init();
453 // Initialize the Cockpit subsystem
454 if( fgCockpitInit( ¤t_aircraft )) {
455 // Cockpit initialized ok.
457 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
461 // Initialize the flight model subsystem data structures base on
464 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
465 // 1.0 / current_options.get_model_hz() );
466 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
468 // I'm just sticking this here for now, it should probably move
470 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
472 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
473 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
476 FG_LOG( FG_GENERAL, FG_INFO,
477 "Updated position (after elevation adj): ("
478 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
479 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
480 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
481 // end of thing that I just stuck in that I should probably move
484 if ( fgJoystickInit() ) {
485 // Joystick initialized ok.
487 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
490 // Autopilot init added here, by Jeff Goeke-Smith
491 fgAPInit(¤t_aircraft);
493 // Initialize I/O channels
494 #if ! defined( MACOS )
498 FG_LOG( FG_GENERAL, FG_INFO, endl);
504 void fgReInitSubsystems( void )
506 FGTime *t = FGTime::cur_time_params;
508 int toggle_pause = t->getPause();
511 t->togglePauseMode();
514 if( global_tile_mgr.init() ) {
515 // Load the local scenery data
516 global_tile_mgr.update();
518 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
521 fgFDMSetGroundElevation( current_options.get_flight_model(),
524 // Reset our altitude if we are below ground
525 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
526 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
527 cur_fdm_state->get_Runway_altitude() );
529 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
530 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
532 double sea_level_radius_meters;
534 // Set the FG variables first
535 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
536 &sea_level_radius_meters, &lat_geoc);
537 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
538 cur_fdm_state->get_Altitude() +
539 (sea_level_radius_meters * METER_TO_FEET) );
540 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
542 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
543 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
545 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
546 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
548 // The following section sets up the flight model EOM parameters
549 // and should really be read in from one or more files.
552 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
553 current_options.get_vBody(),
554 current_options.get_wBody());
556 // Initial Orientation
557 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
558 current_options.get_pitch() * DEG_TO_RAD,
559 current_options.get_heading() * DEG_TO_RAD );
561 // Initial Angular Body rates
562 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
564 cur_fdm_state->set_Earth_position_angle( 0.0 );
566 // Mass properties and geometry values
567 cur_fdm_state->set_Inertias( 8.547270E+01,
568 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
570 // CG position w.r.t. ref. point
571 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
573 // Initialize view parameters
574 current_view.set_view_offset( 0.0 );
575 current_view.set_goal_view_offset( 0.0 );
576 pilot_view.set_view_offset( 0.0 );
577 pilot_view.set_goal_view_offset( 0.0 );
579 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
580 current_view.UpdateViewMath(*cur_fdm_state);
581 pilot_view.UpdateViewMath(*cur_fdm_state);
582 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
584 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
585 // 1.0 / current_options.get_model_hz() );
586 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
588 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
590 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
591 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
594 controls.reset_all();
598 t->togglePauseMode();