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 <Astro/skydome.hxx>
61 #include <Astro/stars.hxx>
62 #include <Astro/solarsystem.hxx>
63 #include <Autopilot/autopilot.hxx>
64 #include <Cockpit/cockpit.hxx>
65 #include <FDM/Balloon.h>
66 #include <FDM/External.hxx>
67 #include <FDM/JSBsim.hxx>
68 #include <FDM/LaRCsim.hxx>
69 #include <FDM/MagicCarpet.hxx>
70 #include <Include/general.hxx>
71 #include <Joystick/joystick.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
146 FG_LOG( FG_GENERAL, FG_INFO,
147 "Attempting to set starting position from airport code "
150 airports.load("apt_simple");
151 if ( ! airports.search( id, &a ) ) {
152 FG_LOG( FG_GENERAL, FG_ALERT,
153 "Failed to find " << id << " in database." );
156 f->set_Longitude( a.longitude * DEG_TO_RAD );
157 f->set_Latitude( a.latitude * DEG_TO_RAD );
160 // set initial position from default or command line coordinates
162 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
163 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
166 FG_LOG( FG_GENERAL, FG_INFO,
167 "starting altitude is = " << current_options.get_altitude() );
169 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
170 fgFDMSetGroundElevation( current_options.get_flight_model(),
171 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
173 FG_LOG( FG_GENERAL, FG_INFO,
174 "Initial position is: ("
175 << (f->get_Longitude() * RAD_TO_DEG) << ", "
176 << (f->get_Latitude() * RAD_TO_DEG) << ", "
177 << (f->get_Altitude() * FEET_TO_METER) << ")" );
183 // General house keeping initializations
184 bool fgInitGeneral( void ) {
187 #if defined(FX) && defined(XMESA)
188 char *mesa_win_state;
191 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
192 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
194 root = current_options.get_fg_root();
195 if ( ! root.length() ) {
196 // No root path set? Then bail ...
197 FG_LOG( FG_GENERAL, FG_ALERT,
198 "Cannot continue without environment variable FG_ROOT"
199 << "being defined." );
202 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
204 #if defined(FX) && defined(XMESA)
205 // initialize full screen flag
206 global_fullscreen = false;
207 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
208 // Test for the MESA_GLX_FX env variable
209 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
210 // test if we are fullscreen mesa/glide
211 if ( (mesa_win_state[0] == 'f') ||
212 (mesa_win_state[0] == 'F') ) {
213 global_fullscreen = true;
223 // This is the top level init routine which calls all the other
224 // initialization routines. If you are adding a subsystem to flight
225 // gear, its initialization call should located in this routine.
226 // Returns non-zero if a problem encountered.
227 bool fgInitSubsystems( void ) {
228 FGTime::cur_time_params = new FGTime();
230 fgLIGHT *l = &cur_light_params;
231 FGTime *t = FGTime::cur_time_params;
233 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
234 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
236 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
237 cur_fdm_state = new FGLaRCsim;
238 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
239 cur_fdm_state = new FGJSBsim;
240 } else if ( current_options.get_flight_model() ==
241 FGInterface::FG_BALLOONSIM ) {
242 cur_fdm_state = new FGBalloonSim;
243 } else if ( current_options.get_flight_model() ==
244 FGInterface::FG_MAGICCARPET ) {
245 cur_fdm_state = new FGMagicCarpet;
246 } else if ( current_options.get_flight_model() ==
247 FGInterface::FG_EXTERNAL ) {
248 cur_fdm_state = new FGExternal;
250 FG_LOG( FG_GENERAL, FG_ALERT,
251 "No flight model, can't init aircraft" );
255 // allocates structures so must happen before any of the flight
256 // model or control parameters are set
257 fgAircraftInit(); // In the future this might not be the case.
259 // set the initial position
262 // Initialize the Scenery Management subsystem
263 if ( fgSceneryInit() ) {
264 // Scenery initialized ok.
266 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
270 if( global_tile_mgr.init() ) {
271 // Load the local scenery data
272 global_tile_mgr.update();
274 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
278 FG_LOG( FG_GENERAL, FG_DEBUG,
279 "Current terrain elevation after tile mgr init " <<
282 // Calculate ground elevation at starting point (we didn't have
283 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
285 // calculalate a cartesian point somewhere along the line between
286 // the center of the earth and our view position. Doesn't have to
287 // be the exact elevation (this is good because we don't know it
290 // now handled inside of the fgTileMgrUpdate()
293 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
294 tmp_abs_view_pos = fgGeodToCart(geod_pos);
296 FG_LOG( FG_GENERAL, FG_DEBUG,
297 "Initial abs_view_pos = " << tmp_abs_view_pos );
299 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
301 FG_LOG( FG_GENERAL, FG_DEBUG,
302 "Altitude after update " << scenery.cur_elev );
305 fgFDMSetGroundElevation( current_options.get_flight_model(),
308 // Reset our altitude if we are below ground
309 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
310 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
311 cur_fdm_state->get_Runway_altitude() );
313 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
314 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
317 FG_LOG( FG_GENERAL, FG_INFO,
318 "Updated position (after elevation adj): ("
319 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
320 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
321 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
323 // We need to calculate a few more values here that would normally
324 // be calculated by the FDM so that the current_view.UpdateViewMath()
325 // routine doesn't get hosed.
327 double sea_level_radius_meters;
329 // Set the FG variables first
330 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
331 &sea_level_radius_meters, &lat_geoc);
332 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
333 cur_fdm_state->get_Altitude() +
334 (sea_level_radius_meters * METER_TO_FEET) );
335 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
337 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
338 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
340 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
341 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
343 // The following section sets up the flight model EOM parameters
344 // and should really be read in from one or more files.
347 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
348 current_options.get_vBody(),
349 current_options.get_wBody());
351 // Initial Orientation
352 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
353 current_options.get_pitch() * DEG_TO_RAD,
354 current_options.get_heading() * DEG_TO_RAD );
356 // Initial Angular Body rates
357 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
359 cur_fdm_state->set_Earth_position_angle( 0.0 );
361 // Mass properties and geometry values
362 cur_fdm_state->set_Inertias( 8.547270E+01,
363 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
365 // CG position w.r.t. ref. point
366 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
368 // Initialize the event manager
369 global_events.Init();
371 // Output event stats every 60 seconds
372 global_events.Register( "fgEVENT_MGR::PrintStats()",
373 fgMethodCallback<fgEVENT_MGR>( &global_events,
374 &fgEVENT_MGR::PrintStats),
375 fgEVENT::FG_EVENT_READY, 60000 );
377 // Initialize the time dependent variables
378 t->init(*cur_fdm_state);
379 t->update(*cur_fdm_state);
381 // Initialize view parameters
382 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
385 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
386 current_view.UpdateViewMath(*cur_fdm_state);
387 pilot_view.UpdateViewMath(*cur_fdm_state);
388 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
389 // current_view.UpdateWorldToEye(f);
391 // Build the solar system
392 //fgSolarSystemInit(*t);
393 FG_LOG(FG_GENERAL, FG_INFO, "Building SolarSystem");
394 SolarSystem::theSolarSystem = new SolarSystem(t);
396 // Initialize the Stars subsystem
397 if( fgStarsInit() ) {
398 // Stars initialized ok.
400 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Stars initialization!" );
404 // Initialize the planetary subsystem
405 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
406 // fgEVENT::FG_EVENT_READY, 600000);
408 // Initialize the sun's position
409 // global_events.Register( "fgSunInit()", fgSunInit,
410 // fgEVENT::FG_EVENT_READY, 30000 );
412 // Intialize the moon's position
413 // global_events.Register( "fgMoonInit()", fgMoonInit,
414 // fgEVENT::FG_EVENT_READY, 600000 );
416 // register the periodic update of Sun, moon, and planets
417 global_events.Register( "ssolsysUpdate", solarSystemRebuild,
418 fgEVENT::FG_EVENT_READY, 600000);
420 // fgUpdateSunPos() needs a few position and view parameters set
421 // so it can calculate local relative sun angle and a few other
422 // things for correctly orienting the sky.
425 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
426 fgEVENT::FG_EVENT_READY, 60000);
427 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
428 fgEVENT::FG_EVENT_READY, 60000);
430 // Initialize Lighting interpolation tables
433 // update the lighting parameters (based on sun angle)
434 global_events.Register( "fgLight::Update()",
435 fgMethodCallback<fgLIGHT>( &cur_light_params,
437 fgEVENT::FG_EVENT_READY, 30000 );
438 // update the current timezone each 30 minutes
439 global_events.Register( "fgTIME::updateLocal()",
440 fgMethodCallback<FGTime>(FGTime::cur_time_params,
441 &FGTime::updateLocal),
442 fgEVENT::FG_EVENT_READY, 1800000);
444 // Initialize the weather modeling subsystem
445 #ifndef FG_OLD_WEATHER
446 // Initialize the WeatherDatabase
447 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
449 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
450 current_aircraft.fdm_state->get_Longitude(),
451 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
452 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
453 new FGLocalWeatherDatabase( position );
454 // cout << theFGLocalWeatherDatabase << endl;
455 // cout << "visibility = "
456 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
458 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
460 // register the periodic update of the weather
461 global_events.Register( "weather update", fgUpdateWeatherDatabase,
462 fgEVENT::FG_EVENT_READY, 30000);
464 current_weather.Init();
467 // Initialize the Cockpit subsystem
468 if( fgCockpitInit( ¤t_aircraft )) {
469 // Cockpit initialized ok.
471 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
475 // Initialize the flight model subsystem data structures base on
478 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
479 // 1.0 / current_options.get_model_hz() );
480 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
482 // I'm just sticking this here for now, it should probably move
484 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
486 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
487 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
490 FG_LOG( FG_GENERAL, FG_INFO,
491 "Updated position (after elevation adj): ("
492 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
493 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
494 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
495 // end of thing that I just stuck in that I should probably move
498 if ( fgJoystickInit() ) {
499 // Joystick initialized ok.
501 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
504 // Autopilot init added here, by Jeff Goeke-Smith
505 fgAPInit(¤t_aircraft);
507 // Initialize I/O channels
508 #if ! defined( MACOS )
512 FG_LOG( FG_GENERAL, FG_INFO, endl);
518 void fgReInitSubsystems( void )
520 FGTime *t = FGTime::cur_time_params;
522 int toggle_pause = t->getPause();
525 t->togglePauseMode();
528 if( global_tile_mgr.init() ) {
529 // Load the local scenery data
530 global_tile_mgr.update();
532 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
535 fgFDMSetGroundElevation( current_options.get_flight_model(),
538 // Reset our altitude if we are below ground
539 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
540 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
541 cur_fdm_state->get_Runway_altitude() );
543 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
544 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
546 double sea_level_radius_meters;
548 // Set the FG variables first
549 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
550 &sea_level_radius_meters, &lat_geoc);
551 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
552 cur_fdm_state->get_Altitude() +
553 (sea_level_radius_meters * METER_TO_FEET) );
554 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
556 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
557 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
559 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
560 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
562 // The following section sets up the flight model EOM parameters
563 // and should really be read in from one or more files.
566 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
567 current_options.get_vBody(),
568 current_options.get_wBody());
570 // Initial Orientation
571 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
572 current_options.get_pitch() * DEG_TO_RAD,
573 current_options.get_heading() * DEG_TO_RAD );
575 // Initial Angular Body rates
576 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
578 cur_fdm_state->set_Earth_position_angle( 0.0 );
580 // Mass properties and geometry values
581 cur_fdm_state->set_Inertias( 8.547270E+01,
582 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
584 // CG position w.r.t. ref. point
585 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
587 // Initialize view parameters
588 current_view.set_view_offset( 0.0 );
589 current_view.set_goal_view_offset( 0.0 );
590 pilot_view.set_view_offset( 0.0 );
591 pilot_view.set_goal_view_offset( 0.0 );
593 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
594 current_view.UpdateViewMath(*cur_fdm_state);
595 pilot_view.UpdateViewMath(*cur_fdm_state);
596 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
598 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
599 // 1.0 / current_options.get_model_hz() );
600 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
602 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
604 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
605 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
608 controls.reset_all();
612 t->togglePauseMode();