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 <Cockpit/radiostack.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 <Navaids/fixlist.hxx>
71 #include <Navaids/ilslist.hxx>
72 #include <Navaids/navlist.hxx>
73 #include <Scenery/scenery.hxx>
74 #include <Scenery/tilemgr.hxx>
75 #include <Time/event.hxx>
76 #include <Time/fg_time.hxx>
77 #include <Time/light.hxx>
78 #include <Time/sunpos.hxx>
79 #include <Time/moonpos.hxx>
81 #ifndef FG_OLD_WEATHER
82 # include <WeatherCM/FGLocalWeatherDatabase.h>
84 # include <Weather/weather.hxx>
87 #include "fg_init.hxx"
89 #include "options.hxx"
92 #if defined(FX) && defined(XMESA)
98 extern const char *default_root;
101 // Read in configuration (file and command line)
102 bool fgInitConfig ( int argc, char **argv ) {
103 // Attempt to locate and parse a config file
104 // First check fg_root
105 FGPath config( current_options.get_fg_root() );
106 config.append( "system.fgfsrc" );
107 current_options.parse_config_file( config.str() );
109 // Next check home directory
110 char* envp = ::getenv( "HOME" );
111 if ( envp != NULL ) {
113 config.append( ".fgfsrc" );
114 current_options.parse_config_file( config.str() );
117 // Parse remaining command line options
118 // These will override anything specified in a config file
119 if ( current_options.parse_command_line(argc, argv) !=
120 fgOPTIONS::FG_OPTIONS_OK )
122 // Something must have gone horribly wrong with the command
123 // line parsing or maybe the user just requested help ... :-)
124 current_options.usage();
125 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
133 // Set initial position and orientation
134 bool fgInitPosition( void ) {
138 f = current_aircraft.fdm_state;
140 id = current_options.get_airport_id();
142 // set initial position from airport id
144 FGPath path( current_options.get_fg_root() );
145 path.append( "Airports" );
146 path.append( "simple.gdbm" );
147 FGAirports airports( path.c_str() );
150 FG_LOG( FG_GENERAL, FG_INFO,
151 "Attempting to set starting position from airport code "
154 // FGPath inpath( current_options.get_fg_root() );
155 // inpath.append( "Airports" );
156 // inpath.append( "apt_simple" );
157 // airports.load( inpath.c_str() );
159 // FGPath outpath( current_options.get_fg_root() );
160 // outpath.append( "Airports" );
161 // outpath.append( "simple.gdbm" );
162 // airports.dump_gdbm( outpath.c_str() );
164 if ( ! airports.search( id, &a ) ) {
165 FG_LOG( FG_GENERAL, FG_ALERT,
166 "Failed to find " << id << " in database." );
169 f->set_Longitude( a.longitude * DEG_TO_RAD );
170 f->set_Latitude( a.latitude * DEG_TO_RAD );
173 // set initial position from default or command line coordinates
175 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
176 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
179 FG_LOG( FG_GENERAL, FG_INFO,
180 "starting altitude is = " << current_options.get_altitude() );
182 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
183 fgFDMSetGroundElevation( current_options.get_flight_model(),
184 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
186 FG_LOG( FG_GENERAL, FG_INFO,
187 "Initial position is: ("
188 << (f->get_Longitude() * RAD_TO_DEG) << ", "
189 << (f->get_Latitude() * RAD_TO_DEG) << ", "
190 << (f->get_Altitude() * FEET_TO_METER) << ")" );
196 // General house keeping initializations
197 bool fgInitGeneral( void ) {
200 #if defined(FX) && defined(XMESA)
201 char *mesa_win_state;
204 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
205 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
207 root = current_options.get_fg_root();
208 if ( ! root.length() ) {
209 // No root path set? Then bail ...
210 FG_LOG( FG_GENERAL, FG_ALERT,
211 "Cannot continue without environment variable FG_ROOT"
212 << "being defined." );
215 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
217 #if defined(FX) && defined(XMESA)
218 // initialize full screen flag
219 global_fullscreen = false;
220 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
221 // Test for the MESA_GLX_FX env variable
222 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
223 // test if we are fullscreen mesa/glide
224 if ( (mesa_win_state[0] == 'f') ||
225 (mesa_win_state[0] == 'F') ) {
226 global_fullscreen = true;
236 // This is the top level init routine which calls all the other
237 // initialization routines. If you are adding a subsystem to flight
238 // gear, its initialization call should located in this routine.
239 // Returns non-zero if a problem encountered.
240 bool fgInitSubsystems( void ) {
241 fgLIGHT *l = &cur_light_params;
242 FGTime *t = FGTime::cur_time_params;
244 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
245 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
247 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
248 cur_fdm_state = new FGLaRCsim;
249 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
250 cur_fdm_state = new FGJSBsim;
251 } else if ( current_options.get_flight_model() ==
252 FGInterface::FG_BALLOONSIM ) {
253 cur_fdm_state = new FGBalloonSim;
254 } else if ( current_options.get_flight_model() ==
255 FGInterface::FG_MAGICCARPET ) {
256 cur_fdm_state = new FGMagicCarpet;
257 } else if ( current_options.get_flight_model() ==
258 FGInterface::FG_EXTERNAL ) {
259 cur_fdm_state = new FGExternal;
261 FG_LOG( FG_GENERAL, FG_ALERT,
262 "No flight model, can't init aircraft" );
266 // allocates structures so must happen before any of the flight
267 // model or control parameters are set
268 fgAircraftInit(); // In the future this might not be the case.
270 // set the initial position
273 // Initialize the Scenery Management subsystem
274 if ( fgSceneryInit() ) {
275 // Scenery initialized ok.
277 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
281 if( global_tile_mgr.init() ) {
282 // Load the local scenery data
283 global_tile_mgr.update();
285 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
289 FG_LOG( FG_GENERAL, FG_DEBUG,
290 "Current terrain elevation after tile mgr init " <<
293 // Calculate ground elevation at starting point (we didn't have
294 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
296 // calculalate a cartesian point somewhere along the line between
297 // the center of the earth and our view position. Doesn't have to
298 // be the exact elevation (this is good because we don't know it
301 // now handled inside of the fgTileMgrUpdate()
304 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
305 tmp_abs_view_pos = fgGeodToCart(geod_pos);
307 FG_LOG( FG_GENERAL, FG_DEBUG,
308 "Initial abs_view_pos = " << tmp_abs_view_pos );
310 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
312 FG_LOG( FG_GENERAL, FG_DEBUG,
313 "Altitude after update " << scenery.cur_elev );
316 fgFDMSetGroundElevation( current_options.get_flight_model(),
319 // Reset our altitude if we are below ground
320 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
321 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
322 cur_fdm_state->get_Runway_altitude() );
324 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
325 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
328 FG_LOG( FG_GENERAL, FG_INFO,
329 "Updated position (after elevation adj): ("
330 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
331 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
332 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
334 // We need to calculate a few more values here that would normally
335 // be calculated by the FDM so that the current_view.UpdateViewMath()
336 // routine doesn't get hosed.
338 double sea_level_radius_meters;
340 // Set the FG variables first
341 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
342 &sea_level_radius_meters, &lat_geoc);
343 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
344 cur_fdm_state->get_Altitude() +
345 (sea_level_radius_meters * METER_TO_FEET) );
346 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
348 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
349 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
351 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
352 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
354 // The following section sets up the flight model EOM parameters
355 // and should really be read in from one or more files.
358 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
359 current_options.get_vBody(),
360 current_options.get_wBody());
362 // Initial Orientation
363 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
364 current_options.get_pitch() * DEG_TO_RAD,
365 current_options.get_heading() * DEG_TO_RAD );
367 // Initial Angular Body rates
368 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
370 cur_fdm_state->set_Earth_position_angle( 0.0 );
372 // Mass properties and geometry values
373 cur_fdm_state->set_Inertias( 8.547270E+01,
374 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
376 // CG position w.r.t. ref. point
377 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
379 // Initialize the event manager
380 global_events.Init();
382 // Output event stats every 60 seconds
383 global_events.Register( "fgEVENT_MGR::PrintStats()",
384 fgMethodCallback<fgEVENT_MGR>( &global_events,
385 &fgEVENT_MGR::PrintStats),
386 fgEVENT::FG_EVENT_READY, 60000 );
388 // Initialize view parameters
389 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
392 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
393 current_view.UpdateViewMath(*cur_fdm_state);
394 pilot_view.UpdateViewMath(*cur_fdm_state);
395 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
396 // current_view.UpdateWorldToEye(f);
398 // Initialize the planetary subsystem
399 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
400 // fgEVENT::FG_EVENT_READY, 600000);
402 // Initialize the sun's position
403 // global_events.Register( "fgSunInit()", fgSunInit,
404 // fgEVENT::FG_EVENT_READY, 30000 );
406 // Intialize the moon's position
407 // global_events.Register( "fgMoonInit()", fgMoonInit,
408 // fgEVENT::FG_EVENT_READY, 600000 );
410 // fgUpdateSunPos() needs a few position and view parameters set
411 // so it can calculate local relative sun angle and a few other
412 // things for correctly orienting the sky.
415 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
416 fgEVENT::FG_EVENT_READY, 60000);
417 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
418 fgEVENT::FG_EVENT_READY, 60000);
420 // Initialize Lighting interpolation tables
423 // update the lighting parameters (based on sun angle)
424 global_events.Register( "fgLight::Update()",
425 fgMethodCallback<fgLIGHT>( &cur_light_params,
427 fgEVENT::FG_EVENT_READY, 30000 );
428 // update the current timezone each 30 minutes
429 global_events.Register( "fgTIME::updateLocal()",
430 fgMethodCallback<FGTime>(FGTime::cur_time_params,
431 &FGTime::updateLocal),
432 fgEVENT::FG_EVENT_READY, 1800000);
434 // Initialize the weather modeling subsystem
435 #ifndef FG_OLD_WEATHER
436 // Initialize the WeatherDatabase
437 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
439 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
440 current_aircraft.fdm_state->get_Longitude(),
441 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
442 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
443 new FGLocalWeatherDatabase( position );
444 // cout << theFGLocalWeatherDatabase << endl;
445 // cout << "visibility = "
446 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
448 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
450 // register the periodic update of the weather
451 global_events.Register( "weather update", fgUpdateWeatherDatabase,
452 fgEVENT::FG_EVENT_READY, 30000);
454 current_weather.Init();
457 // Initialize vor/ndb/ils/fix list management and query systems
458 current_navlist = new FGNavList;
459 FGPath p_nav( current_options.get_fg_root() );
460 p_nav.append( "Navaids/default.nav" );
461 current_navlist->init( p_nav );
463 current_ilslist = new FGILSList;
464 FGPath p_ils( current_options.get_fg_root() );
465 p_ils.append( "Navaids/default.ils" );
466 current_ilslist->init( p_ils );
468 current_fixlist = new FGFixList;
469 FGPath p_fix( current_options.get_fg_root() );
470 p_fix.append( "Navaids/default.fix" );
471 current_fixlist->init( p_fix );
473 // Initialize the underlying radio stack model
474 current_radiostack = new FGRadioStack;
476 current_radiostack->set_nav1_freq( 110.30 );
477 current_radiostack->set_nav1_radial( 299.0 );
479 current_radiostack->set_nav2_freq( 115.70 );
480 current_radiostack->set_nav2_radial( 45.0 );
482 current_radiostack->set_adf_freq( 266.0 );
484 current_radiostack->update( cur_fdm_state->get_Longitude(),
485 cur_fdm_state->get_Latitude(),
486 cur_fdm_state->get_Altitude() * FEET_TO_METER );
488 // Initialize the Cockpit subsystem
489 if( fgCockpitInit( ¤t_aircraft )) {
490 // Cockpit initialized ok.
492 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
496 // Initialize the flight model subsystem data structures base on
499 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
500 // 1.0 / current_options.get_model_hz() );
501 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
503 // I'm just sticking this here for now, it should probably move
505 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
507 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
508 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
511 FG_LOG( FG_GENERAL, FG_INFO,
512 "Updated position (after elevation adj): ("
513 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
514 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
515 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
516 // end of thing that I just stuck in that I should probably move
519 if ( fgJoystickInit() ) {
520 // Joystick initialized ok.
522 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
525 // Autopilot init added here, by Jeff Goeke-Smith
526 fgAPInit(¤t_aircraft);
528 // Initialize I/O channels
529 #if ! defined( MACOS )
533 FG_LOG( FG_GENERAL, FG_INFO, endl);
539 void fgReInitSubsystems( void )
541 FGTime *t = FGTime::cur_time_params;
543 int toggle_pause = t->getPause();
546 t->togglePauseMode();
549 if( global_tile_mgr.init() ) {
550 // Load the local scenery data
551 global_tile_mgr.update();
553 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
556 fgFDMSetGroundElevation( current_options.get_flight_model(),
559 // Reset our altitude if we are below ground
560 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
561 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
562 cur_fdm_state->get_Runway_altitude() );
564 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
565 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
567 double sea_level_radius_meters;
569 // Set the FG variables first
570 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
571 &sea_level_radius_meters, &lat_geoc);
572 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
573 cur_fdm_state->get_Altitude() +
574 (sea_level_radius_meters * METER_TO_FEET) );
575 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
577 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
578 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
580 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
581 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
583 // The following section sets up the flight model EOM parameters
584 // and should really be read in from one or more files.
587 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
588 current_options.get_vBody(),
589 current_options.get_wBody());
591 // Initial Orientation
592 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
593 current_options.get_pitch() * DEG_TO_RAD,
594 current_options.get_heading() * DEG_TO_RAD );
596 // Initial Angular Body rates
597 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
599 cur_fdm_state->set_Earth_position_angle( 0.0 );
601 // Mass properties and geometry values
602 cur_fdm_state->set_Inertias( 8.547270E+01,
603 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
605 // CG position w.r.t. ref. point
606 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
608 // Initialize view parameters
609 current_view.set_view_offset( 0.0 );
610 current_view.set_goal_view_offset( 0.0 );
611 pilot_view.set_view_offset( 0.0 );
612 pilot_view.set_goal_view_offset( 0.0 );
614 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
615 current_view.UpdateViewMath(*cur_fdm_state);
616 pilot_view.UpdateViewMath(*cur_fdm_state);
617 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
619 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
620 // 1.0 / current_options.get_model_hz() );
621 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
623 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
625 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
626 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
629 controls.reset_all();
633 t->togglePauseMode();