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
143 FG_LOG( FG_GENERAL, FG_INFO,
144 "Attempting to set starting position from airport code "
147 airports.load("apt_simple");
148 if ( ! airports.search( id, &a ) ) {
149 FG_LOG( FG_GENERAL, FG_ALERT,
150 "Failed to find " << id << " in database." );
153 f->set_Longitude( a.longitude * DEG_TO_RAD );
154 f->set_Latitude( a.latitude * DEG_TO_RAD );
157 // set initial position from default or command line coordinates
159 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
160 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
163 FG_LOG( FG_GENERAL, FG_INFO,
164 "starting altitude is = " << current_options.get_altitude() );
166 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
167 fgFDMSetGroundElevation( current_options.get_flight_model(),
168 (f->get_Altitude() - 3.758099) * FEET_TO_METER );
170 FG_LOG( FG_GENERAL, FG_INFO,
171 "Initial position is: ("
172 << (f->get_Longitude() * RAD_TO_DEG) << ", "
173 << (f->get_Latitude() * RAD_TO_DEG) << ", "
174 << (f->get_Altitude() * FEET_TO_METER) << ")" );
180 // General house keeping initializations
181 bool fgInitGeneral( void ) {
184 #if defined(FX) && defined(XMESA)
185 char *mesa_win_state;
188 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
189 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
191 root = current_options.get_fg_root();
192 if ( ! root.length() ) {
193 // No root path set? Then bail ...
194 FG_LOG( FG_GENERAL, FG_ALERT,
195 "Cannot continue without environment variable FG_ROOT"
196 << "being defined." );
199 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
201 #if defined(FX) && defined(XMESA)
202 // initialize full screen flag
203 global_fullscreen = false;
204 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
205 // Test for the MESA_GLX_FX env variable
206 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
207 // test if we are fullscreen mesa/glide
208 if ( (mesa_win_state[0] == 'f') ||
209 (mesa_win_state[0] == 'F') ) {
210 global_fullscreen = true;
220 // This is the top level init routine which calls all the other
221 // initialization routines. If you are adding a subsystem to flight
222 // gear, its initialization call should located in this routine.
223 // Returns non-zero if a problem encountered.
224 bool fgInitSubsystems( void ) {
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;
241 } else if ( current_options.get_flight_model() ==
242 FGInterface::FG_EXTERNAL ) {
243 cur_fdm_state = new FGExternal;
245 FG_LOG( FG_GENERAL, FG_ALERT,
246 "No flight model, can't init aircraft" );
250 // allocates structures so must happen before any of the flight
251 // model or control parameters are set
252 fgAircraftInit(); // In the future this might not be the case.
254 // set the initial position
257 // Initialize the Scenery Management subsystem
258 if ( fgSceneryInit() ) {
259 // Scenery initialized ok.
261 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
265 if( global_tile_mgr.init() ) {
266 // Load the local scenery data
267 global_tile_mgr.update();
269 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
273 FG_LOG( FG_GENERAL, FG_DEBUG,
274 "Current terrain elevation after tile mgr init " <<
277 // Calculate ground elevation at starting point (we didn't have
278 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
280 // calculalate a cartesian point somewhere along the line between
281 // the center of the earth and our view position. Doesn't have to
282 // be the exact elevation (this is good because we don't know it
285 // now handled inside of the fgTileMgrUpdate()
288 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
289 tmp_abs_view_pos = fgGeodToCart(geod_pos);
291 FG_LOG( FG_GENERAL, FG_DEBUG,
292 "Initial abs_view_pos = " << tmp_abs_view_pos );
294 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
296 FG_LOG( FG_GENERAL, FG_DEBUG,
297 "Altitude after update " << scenery.cur_elev );
300 fgFDMSetGroundElevation( current_options.get_flight_model(),
303 // Reset our altitude if we are below ground
304 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
305 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
306 cur_fdm_state->get_Runway_altitude() );
308 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
309 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
312 FG_LOG( FG_GENERAL, FG_INFO,
313 "Updated position (after elevation adj): ("
314 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
315 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
316 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
318 // We need to calculate a few more values here that would normally
319 // be calculated by the FDM so that the current_view.UpdateViewMath()
320 // routine doesn't get hosed.
322 double sea_level_radius_meters;
324 // Set the FG variables first
325 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
326 &sea_level_radius_meters, &lat_geoc);
327 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
328 cur_fdm_state->get_Altitude() +
329 (sea_level_radius_meters * METER_TO_FEET) );
330 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
332 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
333 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
335 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
336 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
338 // The following section sets up the flight model EOM parameters
339 // and should really be read in from one or more files.
342 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
343 current_options.get_vBody(),
344 current_options.get_wBody());
346 // Initial Orientation
347 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
348 current_options.get_pitch() * DEG_TO_RAD,
349 current_options.get_heading() * DEG_TO_RAD );
351 // Initial Angular Body rates
352 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
354 cur_fdm_state->set_Earth_position_angle( 0.0 );
356 // Mass properties and geometry values
357 cur_fdm_state->set_Inertias( 8.547270E+01,
358 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
360 // CG position w.r.t. ref. point
361 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
363 // Initialize the event manager
364 global_events.Init();
366 // Output event stats every 60 seconds
367 global_events.Register( "fgEVENT_MGR::PrintStats()",
368 fgMethodCallback<fgEVENT_MGR>( &global_events,
369 &fgEVENT_MGR::PrintStats),
370 fgEVENT::FG_EVENT_READY, 60000 );
372 // Initialize view parameters
373 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
376 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
377 current_view.UpdateViewMath(*cur_fdm_state);
378 pilot_view.UpdateViewMath(*cur_fdm_state);
379 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
380 // current_view.UpdateWorldToEye(f);
382 // Initialize the planetary subsystem
383 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
384 // fgEVENT::FG_EVENT_READY, 600000);
386 // Initialize the sun's position
387 // global_events.Register( "fgSunInit()", fgSunInit,
388 // fgEVENT::FG_EVENT_READY, 30000 );
390 // Intialize the moon's position
391 // global_events.Register( "fgMoonInit()", fgMoonInit,
392 // fgEVENT::FG_EVENT_READY, 600000 );
394 // fgUpdateSunPos() needs a few position and view parameters set
395 // so it can calculate local relative sun angle and a few other
396 // things for correctly orienting the sky.
399 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
400 fgEVENT::FG_EVENT_READY, 60000);
401 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
402 fgEVENT::FG_EVENT_READY, 60000);
404 // Initialize Lighting interpolation tables
407 // update the lighting parameters (based on sun angle)
408 global_events.Register( "fgLight::Update()",
409 fgMethodCallback<fgLIGHT>( &cur_light_params,
411 fgEVENT::FG_EVENT_READY, 30000 );
412 // update the current timezone each 30 minutes
413 global_events.Register( "fgTIME::updateLocal()",
414 fgMethodCallback<FGTime>(FGTime::cur_time_params,
415 &FGTime::updateLocal),
416 fgEVENT::FG_EVENT_READY, 1800000);
418 // Initialize the weather modeling subsystem
419 #ifndef FG_OLD_WEATHER
420 // Initialize the WeatherDatabase
421 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
423 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
424 current_aircraft.fdm_state->get_Longitude(),
425 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
426 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
427 new FGLocalWeatherDatabase( position );
428 // cout << theFGLocalWeatherDatabase << endl;
429 // cout << "visibility = "
430 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
432 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
434 // register the periodic update of the weather
435 global_events.Register( "weather update", fgUpdateWeatherDatabase,
436 fgEVENT::FG_EVENT_READY, 30000);
438 current_weather.Init();
441 // Initialize the Cockpit subsystem
442 if( fgCockpitInit( ¤t_aircraft )) {
443 // Cockpit initialized ok.
445 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
449 // Initialize the flight model subsystem data structures base on
452 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
453 // 1.0 / current_options.get_model_hz() );
454 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
456 // I'm just sticking this here for now, it should probably move
458 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
460 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
461 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
464 FG_LOG( FG_GENERAL, FG_INFO,
465 "Updated position (after elevation adj): ("
466 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
467 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
468 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
469 // end of thing that I just stuck in that I should probably move
472 if ( fgJoystickInit() ) {
473 // Joystick initialized ok.
475 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
478 // Autopilot init added here, by Jeff Goeke-Smith
479 fgAPInit(¤t_aircraft);
481 // Initialize I/O channels
482 #if ! defined( MACOS )
486 FG_LOG( FG_GENERAL, FG_INFO, endl);
492 void fgReInitSubsystems( void )
494 FGTime *t = FGTime::cur_time_params;
496 int toggle_pause = t->getPause();
499 t->togglePauseMode();
502 if( global_tile_mgr.init() ) {
503 // Load the local scenery data
504 global_tile_mgr.update();
506 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
509 fgFDMSetGroundElevation( current_options.get_flight_model(),
512 // Reset our altitude if we are below ground
513 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
514 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
515 cur_fdm_state->get_Runway_altitude() );
517 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
518 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
520 double sea_level_radius_meters;
522 // Set the FG variables first
523 fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
524 &sea_level_radius_meters, &lat_geoc);
525 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
526 cur_fdm_state->get_Altitude() +
527 (sea_level_radius_meters * METER_TO_FEET) );
528 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
530 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
531 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
533 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
534 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
536 // The following section sets up the flight model EOM parameters
537 // and should really be read in from one or more files.
540 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
541 current_options.get_vBody(),
542 current_options.get_wBody());
544 // Initial Orientation
545 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
546 current_options.get_pitch() * DEG_TO_RAD,
547 current_options.get_heading() * DEG_TO_RAD );
549 // Initial Angular Body rates
550 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
552 cur_fdm_state->set_Earth_position_angle( 0.0 );
554 // Mass properties and geometry values
555 cur_fdm_state->set_Inertias( 8.547270E+01,
556 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
558 // CG position w.r.t. ref. point
559 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
561 // Initialize view parameters
562 current_view.set_view_offset( 0.0 );
563 current_view.set_goal_view_offset( 0.0 );
564 pilot_view.set_view_offset( 0.0 );
565 pilot_view.set_goal_view_offset( 0.0 );
567 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
568 current_view.UpdateViewMath(*cur_fdm_state);
569 pilot_view.UpdateViewMath(*cur_fdm_state);
570 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
572 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
573 // 1.0 / current_options.get_model_hz() );
574 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
576 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
578 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
579 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
582 controls.reset_all();
586 t->togglePauseMode();