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/stars.hxx>
61 #include <Autopilot/autopilot.hxx>
62 #include <Cockpit/cockpit.hxx>
63 #include <FDM/Balloon.h>
64 #include <FDM/External.hxx>
65 #include <FDM/JSBsim.hxx>
66 #include <FDM/LaRCsim.hxx>
67 #include <FDM/MagicCarpet.hxx>
68 #include <Include/general.hxx>
69 #include <Joystick/joystick.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"
86 #include "options.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 ) {
185 #if defined(FX) && defined(XMESA)
186 char *mesa_win_state;
189 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
190 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
192 root = current_options.get_fg_root();
193 if ( ! root.length() ) {
194 // No root path set? Then bail ...
195 FG_LOG( FG_GENERAL, FG_ALERT,
196 "Cannot continue without environment variable FG_ROOT"
197 << "being defined." );
200 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
202 #if defined(FX) && defined(XMESA)
203 // initialize full screen flag
204 global_fullscreen = false;
205 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
206 // Test for the MESA_GLX_FX env variable
207 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
208 // test if we are fullscreen mesa/glide
209 if ( (mesa_win_state[0] == 'f') ||
210 (mesa_win_state[0] == 'F') ) {
211 global_fullscreen = true;
221 // This is the top level init routine which calls all the other
222 // initialization routines. If you are adding a subsystem to flight
223 // gear, its initialization call should located in this routine.
224 // Returns non-zero if a problem encountered.
225 bool fgInitSubsystems( void ) {
226 fgLIGHT *l = &cur_light_params;
227 FGTime *t = FGTime::cur_time_params;
229 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
230 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
232 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
233 cur_fdm_state = new FGLaRCsim;
234 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
235 cur_fdm_state = new FGJSBsim;
236 } else if ( current_options.get_flight_model() ==
237 FGInterface::FG_BALLOONSIM ) {
238 cur_fdm_state = new FGBalloonSim;
239 } else if ( current_options.get_flight_model() ==
240 FGInterface::FG_MAGICCARPET ) {
241 cur_fdm_state = new FGMagicCarpet;
242 } else if ( current_options.get_flight_model() ==
243 FGInterface::FG_EXTERNAL ) {
244 cur_fdm_state = new FGExternal;
246 FG_LOG( FG_GENERAL, FG_ALERT,
247 "No flight model, can't init aircraft" );
251 // allocates structures so must happen before any of the flight
252 // model or control parameters are set
253 fgAircraftInit(); // In the future this might not be the case.
255 // set the initial position
258 // Initialize the Scenery Management subsystem
259 if ( fgSceneryInit() ) {
260 // Scenery initialized ok.
262 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
266 if( global_tile_mgr.init() ) {
267 // Load the local scenery data
268 global_tile_mgr.update();
270 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
274 FG_LOG( FG_GENERAL, FG_DEBUG,
275 "Current terrain elevation after tile mgr init " <<
278 // Calculate ground elevation at starting point (we didn't have
279 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
281 // calculalate a cartesian point somewhere along the line between
282 // the center of the earth and our view position. Doesn't have to
283 // be the exact elevation (this is good because we don't know it
286 // now handled inside of the fgTileMgrUpdate()
289 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
290 tmp_abs_view_pos = fgGeodToCart(geod_pos);
292 FG_LOG( FG_GENERAL, FG_DEBUG,
293 "Initial abs_view_pos = " << tmp_abs_view_pos );
295 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
297 FG_LOG( FG_GENERAL, FG_DEBUG,
298 "Altitude after update " << scenery.cur_elev );
301 fgFDMSetGroundElevation( current_options.get_flight_model(),
304 // Reset our altitude if we are below ground
305 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
306 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
307 cur_fdm_state->get_Runway_altitude() );
309 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
310 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
313 FG_LOG( FG_GENERAL, FG_INFO,
314 "Updated position (after elevation adj): ("
315 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
316 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
317 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
319 // We need to calculate a few more values here that would normally
320 // be calculated by the FDM so that the current_view.UpdateViewMath()
321 // routine doesn't get hosed.
323 double sea_level_radius_meters;
325 // Set the FG variables first
326 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
327 &sea_level_radius_meters, &lat_geoc);
328 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
329 cur_fdm_state->get_Altitude() +
330 (sea_level_radius_meters * METER_TO_FEET) );
331 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
333 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
334 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
336 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
337 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
339 // The following section sets up the flight model EOM parameters
340 // and should really be read in from one or more files.
343 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
344 current_options.get_vBody(),
345 current_options.get_wBody());
347 // Initial Orientation
348 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
349 current_options.get_pitch() * DEG_TO_RAD,
350 current_options.get_heading() * DEG_TO_RAD );
352 // Initial Angular Body rates
353 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
355 cur_fdm_state->set_Earth_position_angle( 0.0 );
357 // Mass properties and geometry values
358 cur_fdm_state->set_Inertias( 8.547270E+01,
359 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
361 // CG position w.r.t. ref. point
362 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
364 // Initialize the event manager
365 global_events.Init();
367 // Output event stats every 60 seconds
368 global_events.Register( "fgEVENT_MGR::PrintStats()",
369 fgMethodCallback<fgEVENT_MGR>( &global_events,
370 &fgEVENT_MGR::PrintStats),
371 fgEVENT::FG_EVENT_READY, 60000 );
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 // Initialize the planetary subsystem
384 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
385 // fgEVENT::FG_EVENT_READY, 600000);
387 // Initialize the sun's position
388 // global_events.Register( "fgSunInit()", fgSunInit,
389 // fgEVENT::FG_EVENT_READY, 30000 );
391 // Intialize the moon's position
392 // global_events.Register( "fgMoonInit()", fgMoonInit,
393 // fgEVENT::FG_EVENT_READY, 600000 );
395 // fgUpdateSunPos() needs a few position and view parameters set
396 // so it can calculate local relative sun angle and a few other
397 // things for correctly orienting the sky.
400 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
401 fgEVENT::FG_EVENT_READY, 60000);
402 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
403 fgEVENT::FG_EVENT_READY, 60000);
405 // Initialize Lighting interpolation tables
408 // update the lighting parameters (based on sun angle)
409 global_events.Register( "fgLight::Update()",
410 fgMethodCallback<fgLIGHT>( &cur_light_params,
412 fgEVENT::FG_EVENT_READY, 30000 );
413 // update the current timezone each 30 minutes
414 global_events.Register( "fgTIME::updateLocal()",
415 fgMethodCallback<FGTime>(FGTime::cur_time_params,
416 &FGTime::updateLocal),
417 fgEVENT::FG_EVENT_READY, 1800000);
419 // Initialize the weather modeling subsystem
420 #ifndef FG_OLD_WEATHER
421 // Initialize the WeatherDatabase
422 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
424 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
425 current_aircraft.fdm_state->get_Longitude(),
426 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
427 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
428 new FGLocalWeatherDatabase( position );
429 // cout << theFGLocalWeatherDatabase << endl;
430 // cout << "visibility = "
431 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
433 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
435 // register the periodic update of the weather
436 global_events.Register( "weather update", fgUpdateWeatherDatabase,
437 fgEVENT::FG_EVENT_READY, 30000);
439 current_weather.Init();
442 // Initialize the Cockpit subsystem
443 if( fgCockpitInit( ¤t_aircraft )) {
444 // Cockpit initialized ok.
446 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
450 // Initialize the flight model subsystem data structures base on
453 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
454 // 1.0 / current_options.get_model_hz() );
455 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
457 // I'm just sticking this here for now, it should probably move
459 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
461 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
462 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
465 FG_LOG( FG_GENERAL, FG_INFO,
466 "Updated position (after elevation adj): ("
467 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
468 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
469 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
470 // end of thing that I just stuck in that I should probably move
473 if ( fgJoystickInit() ) {
474 // Joystick initialized ok.
476 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
479 // Autopilot init added here, by Jeff Goeke-Smith
480 fgAPInit(¤t_aircraft);
482 // Initialize I/O channels
483 #if ! defined( MACOS )
487 FG_LOG( FG_GENERAL, FG_INFO, endl);
493 void fgReInitSubsystems( void )
495 FGTime *t = FGTime::cur_time_params;
497 int toggle_pause = t->getPause();
500 t->togglePauseMode();
503 if( global_tile_mgr.init() ) {
504 // Load the local scenery data
505 global_tile_mgr.update();
507 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
510 fgFDMSetGroundElevation( current_options.get_flight_model(),
513 // Reset our altitude if we are below ground
514 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
515 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
516 cur_fdm_state->get_Runway_altitude() );
518 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
519 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
521 double sea_level_radius_meters;
523 // Set the FG variables first
524 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
525 &sea_level_radius_meters, &lat_geoc);
526 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
527 cur_fdm_state->get_Altitude() +
528 (sea_level_radius_meters * METER_TO_FEET) );
529 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
531 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
532 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
534 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
535 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
537 // The following section sets up the flight model EOM parameters
538 // and should really be read in from one or more files.
541 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
542 current_options.get_vBody(),
543 current_options.get_wBody());
545 // Initial Orientation
546 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
547 current_options.get_pitch() * DEG_TO_RAD,
548 current_options.get_heading() * DEG_TO_RAD );
550 // Initial Angular Body rates
551 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
553 cur_fdm_state->set_Earth_position_angle( 0.0 );
555 // Mass properties and geometry values
556 cur_fdm_state->set_Inertias( 8.547270E+01,
557 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
559 // CG position w.r.t. ref. point
560 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
562 // Initialize view parameters
563 current_view.set_view_offset( 0.0 );
564 current_view.set_goal_view_offset( 0.0 );
565 pilot_view.set_view_offset( 0.0 );
566 pilot_view.set_goal_view_offset( 0.0 );
568 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
569 current_view.UpdateViewMath(*cur_fdm_state);
570 pilot_view.UpdateViewMath(*cur_fdm_state);
571 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
573 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
574 // 1.0 / current_options.get_model_hz() );
575 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
577 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
579 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
580 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
583 controls.reset_all();
587 t->togglePauseMode();