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
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 <Include/compiler.h>
51 #include <Debug/logstream.hxx>
52 #include <Aircraft/aircraft.hxx>
53 #include <Airports/simple.hxx>
54 #include <Astro/sky.hxx>
55 #include <Astro/stars.hxx>
56 #include <Astro/solarsystem.hxx>
57 #include <Autopilot/autopilot.hxx>
58 #include <Cockpit/cockpit.hxx>
59 #include <FDM/Balloon.h>
60 #include <FDM/JSBsim.hxx>
61 #include <FDM/LaRCsim.hxx>
62 #include <FDM/MagicCarpet.hxx>
63 #include <Include/fg_constants.h>
64 #include <Include/general.hxx>
65 #include <Joystick/joystick.hxx>
66 #include <Math/fg_geodesy.hxx>
67 #include <Math/point3d.hxx>
68 #include <Math/polar3d.hxx>
69 #include <Misc/fgpath.hxx>
70 #include <Scenery/scenery.hxx>
71 #include <Scenery/tilemgr.hxx>
72 #include <Time/event.hxx>
73 #include <Time/fg_time.hxx>
74 #include <Time/light.hxx>
75 #include <Time/sunpos.hxx>
76 #include <Time/moonpos.hxx>
78 #ifndef FG_OLD_WEATHER
79 # include <WeatherCM/FGLocalWeatherDatabase.h>
81 # include <Weather/weather.hxx>
84 #include "fg_init.hxx"
85 #include "options.hxx"
87 #include "fg_serial.hxx"
89 #if defined(FX) && defined(XMESA)
95 extern const char *default_root;
98 // Read in configuration (file and command line)
99 bool fgInitConfig ( int argc, char **argv ) {
100 // Attempt to locate and parse a config file
101 // First check fg_root
102 FGPath config( current_options.get_fg_root() );
103 config.append( "system.fgfsrc" );
104 current_options.parse_config_file( config.str() );
106 // Next check home directory
107 char* envp = ::getenv( "HOME" );
108 if ( envp != NULL ) {
110 config.append( ".fgfsrc" );
111 current_options.parse_config_file( config.str() );
114 // Parse remaining command line options
115 // These will override anything specified in a config file
116 if ( current_options.parse_command_line(argc, argv) !=
117 fgOPTIONS::FG_OPTIONS_OK )
119 // Something must have gone horribly wrong with the command
120 // line parsing or maybe the user just requested help ... :-)
121 current_options.usage();
122 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
130 // Set initial position and orientation
131 bool fgInitPosition( void ) {
135 f = current_aircraft.fdm_state;
137 id = current_options.get_airport_id();
139 // set initial position from airport id
144 FG_LOG( FG_GENERAL, FG_INFO,
145 "Attempting to set starting position from airport code "
148 airports.load("apt_simple");
149 if ( ! airports.search( id, &a ) ) {
150 FG_LOG( FG_GENERAL, FG_ALERT,
151 "Failed to find " << id << " in database." );
154 f->set_Longitude( a.longitude * DEG_TO_RAD );
155 f->set_Latitude( a.latitude * DEG_TO_RAD );
158 // set initial position from default or command line coordinates
160 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
161 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
164 FG_LOG( FG_GENERAL, FG_INFO,
165 "starting altitude is = " << current_options.get_altitude() );
167 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
168 fgFDMSetGroundElevation( current_options.get_flight_model(),
169 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
171 FG_LOG( FG_GENERAL, FG_INFO,
172 "Initial position is: ("
173 << (f->get_Longitude() * RAD_TO_DEG) << ", "
174 << (f->get_Latitude() * RAD_TO_DEG) << ", "
175 << (f->get_Altitude() * FEET_TO_METER) << ")" );
181 // General house keeping initializations
182 bool fgInitGeneral( void ) {
184 char *mesa_win_state;
186 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
187 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
189 root = current_options.get_fg_root();
190 if ( ! root.length() ) {
191 // No root path set? Then bail ...
192 FG_LOG( FG_GENERAL, FG_ALERT,
193 "Cannot continue without environment variable FG_ROOT"
194 << "being defined." );
197 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
199 #if defined(FX) && defined(XMESA)
200 // initialize full screen flag
201 global_fullscreen = false;
202 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
203 // Test for the MESA_GLX_FX env variable
204 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
205 // test if we are fullscreen mesa/glide
206 if ( (mesa_win_state[0] == 'f') ||
207 (mesa_win_state[0] == 'F') ) {
208 global_fullscreen = true;
218 // This is the top level init routine which calls all the other
219 // initialization routines. If you are adding a subsystem to flight
220 // gear, its initialization call should located in this routine.
221 // Returns non-zero if a problem encountered.
222 bool fgInitSubsystems( void ) {
223 FGTime::cur_time_params = new FGTime();
225 fgLIGHT *l = &cur_light_params;
226 FGTime *t = FGTime::cur_time_params;
228 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
229 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
231 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
232 cur_fdm_state = new FGLaRCsim;
233 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
234 cur_fdm_state = new FGJSBsim;
235 } else if ( current_options.get_flight_model() ==
236 FGInterface::FG_BALLOONSIM ) {
237 cur_fdm_state = new FGBalloonSim;
238 } else if ( current_options.get_flight_model() ==
239 FGInterface::FG_MAGICCARPET ) {
240 cur_fdm_state = new FGMagicCarpet;
242 FG_LOG( FG_GENERAL, FG_ALERT,
243 "No flight model, can't init aircraft" );
247 // allocates structures so must happen before any of the flight
248 // model or control parameters are set
249 fgAircraftInit(); // In the future this might not be the case.
251 // set the initial position
254 // Initialize the Scenery Management subsystem
255 if ( fgSceneryInit() ) {
256 // Scenery initialized ok.
258 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
262 if( global_tile_mgr.init() ) {
263 // Load the local scenery data
264 global_tile_mgr.update();
266 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
270 FG_LOG( FG_GENERAL, FG_DEBUG,
271 "Current terrain elevation after tile mgr init " <<
274 // Calculate ground elevation at starting point (we didn't have
275 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
277 // calculalate a cartesian point somewhere along the line between
278 // the center of the earth and our view position. Doesn't have to
279 // be the exact elevation (this is good because we don't know it
282 // now handled inside of the fgTileMgrUpdate()
285 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
286 tmp_abs_view_pos = fgGeodToCart(geod_pos);
288 FG_LOG( FG_GENERAL, FG_DEBUG,
289 "Initial abs_view_pos = " << tmp_abs_view_pos );
291 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
293 FG_LOG( FG_GENERAL, FG_DEBUG,
294 "Altitude after update " << scenery.cur_elev );
297 fgFDMSetGroundElevation( current_options.get_flight_model(),
300 // Reset our altitude if we are below ground
301 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
302 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
303 cur_fdm_state->get_Runway_altitude() );
305 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
306 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
309 FG_LOG( FG_GENERAL, FG_INFO,
310 "Updated position (after elevation adj): ("
311 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
312 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
313 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
315 // We need to calculate a few more values here that would normally
316 // be calculated by the FDM so that the current_view.UpdateViewMath()
317 // routine doesn't get hosed.
319 double sea_level_radius_meters;
321 // Set the FG variables first
322 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
323 &sea_level_radius_meters, &lat_geoc);
324 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
325 cur_fdm_state->get_Altitude() +
326 (sea_level_radius_meters * METER_TO_FEET) );
327 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
329 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
330 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
332 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
333 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
335 // The following section sets up the flight model EOM parameters
336 // and should really be read in from one or more files.
339 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
340 current_options.get_vBody(),
341 current_options.get_wBody());
343 // Initial Orientation
344 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
345 current_options.get_pitch() * DEG_TO_RAD,
346 current_options.get_heading() * DEG_TO_RAD );
348 // Initial Angular Body rates
349 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
351 cur_fdm_state->set_Earth_position_angle( 0.0 );
353 // Mass properties and geometry values
354 cur_fdm_state->set_Inertias( 8.547270E+01,
355 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
357 // CG position w.r.t. ref. point
358 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
360 // Initialize the event manager
361 global_events.Init();
363 // Output event stats every 60 seconds
364 global_events.Register( "fgEVENT_MGR::PrintStats()",
365 fgMethodCallback<fgEVENT_MGR>( &global_events,
366 &fgEVENT_MGR::PrintStats),
367 fgEVENT::FG_EVENT_READY, 60000 );
369 // Initialize the time dependent variables
370 t->init(*cur_fdm_state);
371 t->update(*cur_fdm_state);
373 // Initialize view parameters
374 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
377 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
378 current_view.UpdateViewMath(*cur_fdm_state);
379 pilot_view.UpdateViewMath(*cur_fdm_state);
380 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
381 // current_view.UpdateWorldToEye(f);
383 // Build the solar system
384 //fgSolarSystemInit(*t);
385 FG_LOG(FG_GENERAL, FG_INFO, "Building SolarSystem");
386 SolarSystem::theSolarSystem = new SolarSystem(t);
388 // Initialize the Stars subsystem
389 if( fgStarsInit() ) {
390 // Stars initialized ok.
392 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Stars initialization!" );
396 // Initialize the planetary subsystem
397 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
398 // fgEVENT::FG_EVENT_READY, 600000);
400 // Initialize the sun's position
401 // global_events.Register( "fgSunInit()", fgSunInit,
402 // fgEVENT::FG_EVENT_READY, 30000 );
404 // Intialize the moon's position
405 // global_events.Register( "fgMoonInit()", fgMoonInit,
406 // fgEVENT::FG_EVENT_READY, 600000 );
408 // register the periodic update of Sun, moon, and planets
409 global_events.Register( "ssolsysUpdate", solarSystemRebuild,
410 fgEVENT::FG_EVENT_READY, 600000);
412 // fgUpdateSunPos() needs a few position and view parameters set
413 // so it can calculate local relative sun angle and a few other
414 // things for correctly orienting the sky.
417 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
418 fgEVENT::FG_EVENT_READY, 60000);
419 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
420 fgEVENT::FG_EVENT_READY, 60000);
422 // Initialize Lighting interpolation tables
425 // update the lighting parameters (based on sun angle)
426 global_events.Register( "fgLight::Update()",
427 fgMethodCallback<fgLIGHT>( &cur_light_params,
429 fgEVENT::FG_EVENT_READY, 30000 );
430 // update the current timezone each 30 minutes
431 global_events.Register( "fgTIME::updateLocal()",
432 fgMethodCallback<FGTime>(FGTime::cur_time_params,
433 &FGTime::updateLocal),
434 fgEVENT::FG_EVENT_READY, 1800000);
436 // Initialize the weather modeling subsystem
437 #ifndef FG_OLD_WEATHER
438 // Initialize the WeatherDatabase
439 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
441 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
442 current_aircraft.fdm_state->get_Longitude(),
443 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
444 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
445 new FGLocalWeatherDatabase( position );
446 // cout << theFGLocalWeatherDatabase << endl;
447 // cout << "visibility = "
448 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
450 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
452 // register the periodic update of the weather
453 global_events.Register( "weather update", fgUpdateWeatherDatabase,
454 fgEVENT::FG_EVENT_READY, 30000);
456 current_weather.Init();
459 // Initialize the Cockpit subsystem
460 if( fgCockpitInit( ¤t_aircraft )) {
461 // Cockpit initialized ok.
463 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
467 // Initialize the "sky"
470 // Initialize the flight model subsystem data structures base on
473 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
474 // 1.0 / current_options.get_model_hz() );
475 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
477 // I'm just sticking this here for now, it should probably move
479 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
481 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
482 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
485 FG_LOG( FG_GENERAL, FG_INFO,
486 "Updated position (after elevation adj): ("
487 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
488 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
489 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
490 // end of thing that I just stuck in that I should probably move
493 if ( fgJoystickInit() ) {
494 // Joystick initialized ok.
496 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
499 // Autopilot init added here, by Jeff Goeke-Smith
500 fgAPInit(¤t_aircraft);
502 // Initialize serial ports
503 #if ! defined( MACOS )
507 FG_LOG( FG_GENERAL, FG_INFO, endl);
513 void fgReInitSubsystems( void )
515 FGTime *t = FGTime::cur_time_params;
517 int toggle_pause = t->getPause();
520 t->togglePauseMode();
523 if( global_tile_mgr.init() ) {
524 // Load the local scenery data
525 global_tile_mgr.update();
527 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
530 fgFDMSetGroundElevation( current_options.get_flight_model(),
533 // Reset our altitude if we are below ground
534 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
535 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
536 cur_fdm_state->get_Runway_altitude() );
538 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
539 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
541 double sea_level_radius_meters;
543 // Set the FG variables first
544 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
545 &sea_level_radius_meters, &lat_geoc);
546 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
547 cur_fdm_state->get_Altitude() +
548 (sea_level_radius_meters * METER_TO_FEET) );
549 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
551 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
552 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
554 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
555 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
557 // The following section sets up the flight model EOM parameters
558 // and should really be read in from one or more files.
561 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
562 current_options.get_vBody(),
563 current_options.get_wBody());
565 // Initial Orientation
566 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
567 current_options.get_pitch() * DEG_TO_RAD,
568 current_options.get_heading() * DEG_TO_RAD );
570 // Initial Angular Body rates
571 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
573 cur_fdm_state->set_Earth_position_angle( 0.0 );
575 // Mass properties and geometry values
576 cur_fdm_state->set_Inertias( 8.547270E+01,
577 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
579 // CG position w.r.t. ref. point
580 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
582 // Initialize view parameters
583 current_view.set_view_offset( 0.0 );
584 current_view.set_goal_view_offset( 0.0 );
585 pilot_view.set_view_offset( 0.0 );
586 pilot_view.set_goal_view_offset( 0.0 );
588 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
589 current_view.UpdateViewMath(*cur_fdm_state);
590 pilot_view.UpdateViewMath(*cur_fdm_state);
591 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
593 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
594 // 1.0 / current_options.get_model_hz() );
595 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
597 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
599 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
600 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
603 controls.reset_all();
607 t->togglePauseMode();