1 // fg_init.cxx -- Flight Gear top level initialization routines
3 // Written by Curtis Olson, started August 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 // For BC 5.01 this must be included before OpenGL includes.
30 #ifdef FG_MATH_EXCEPTION_CLASH
35 #include <simgear/xgl/xgl.h>
40 // work around a stdc++ lib bug in some versions of linux, but doesn't
41 // seem to hurt to have this here for all versions of Linux.
43 # define _G_NO_EXTERN_TEMPLATES
46 #include <simgear/compiler.h>
50 #include <simgear/constants.h>
51 #include <simgear/debug/logstream.hxx>
52 #include <simgear/math/point3d.hxx>
53 #include <simgear/math/polar3d.hxx>
54 #include <simgear/math/sg_geodesy.hxx>
55 #include <simgear/misc/fgpath.hxx>
56 #include <simgear/timing/sg_time.hxx>
58 #include <Aircraft/aircraft.hxx>
59 #include <Airports/runways.hxx>
60 #include <Airports/simple.hxx>
61 #include <Autopilot/auto_gui.hxx>
62 #include <Autopilot/newauto.hxx>
63 #include <Cockpit/cockpit.hxx>
64 #include <Cockpit/radiostack.hxx>
65 #include <Cockpit/panel.hxx>
66 #include <Cockpit/panel_io.hxx>
67 #include <FDM/Balloon.h>
68 #include <FDM/External.hxx>
69 #include <FDM/JSBSim.hxx>
70 #include <FDM/LaRCsim.hxx>
71 #include <FDM/MagicCarpet.hxx>
72 #include <Include/general.hxx>
73 #include <Joystick/joystick.hxx>
74 #include <Objects/matlib.hxx>
75 #include <Navaids/fixlist.hxx>
76 #include <Navaids/ilslist.hxx>
77 #include <Navaids/navlist.hxx>
78 #include <Scenery/scenery.hxx>
79 #include <Scenery/tilemgr.hxx>
80 #include <Time/event.hxx>
81 #include <Time/light.hxx>
82 #include <Time/sunpos.hxx>
83 #include <Time/moonpos.hxx>
84 #include <Time/tmp.hxx>
86 #ifndef FG_OLD_WEATHER
87 # include <WeatherCM/FGLocalWeatherDatabase.h>
89 # include <Weather/weather.hxx>
92 #include "fg_init.hxx"
94 #include "globals.hxx"
95 #include "options.hxx"
99 #if defined(FX) && defined(XMESA)
100 #include <GL/xmesa.h>
103 FG_USING_STD(string);
105 extern const char *default_root;
108 // Read in configuration (file and command line) and just set fg_root
109 bool fgInitFGRoot ( int argc, char **argv ) {
110 // Attempt to locate and parse a config file
111 // First check fg_root
112 FGPath config( current_options.get_fg_root() );
113 config.append( "system.fgfsrc" );
114 current_options.scan_config_file_for_root( config.str() );
116 // Next check home directory
117 char* envp = ::getenv( "HOME" );
118 if ( envp != NULL ) {
120 config.append( ".fgfsrc" );
121 current_options.scan_config_file_for_root( config.str() );
124 // Parse remaining command line options
125 // These will override anything specified in a config file
126 current_options.scan_command_line_for_root(argc, argv);
132 // Read in configuration (file and command line)
133 bool fgInitConfig ( int argc, char **argv ) {
134 // Attempt to locate and parse a config file
135 // First check fg_root
136 FGPath config( current_options.get_fg_root() );
137 config.append( "system.fgfsrc" );
138 current_options.parse_config_file( config.str() );
140 // Next check home directory
141 char* envp = ::getenv( "HOME" );
142 if ( envp != NULL ) {
144 config.append( ".fgfsrc" );
145 current_options.parse_config_file( config.str() );
148 // Parse remaining command line options
149 // These will override anything specified in a config file
150 if ( current_options.parse_command_line(argc, argv) !=
151 fgOPTIONS::FG_OPTIONS_OK )
153 // Something must have gone horribly wrong with the command
154 // line parsing or maybe the user just requested help ... :-)
155 current_options.usage();
156 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
164 // find basic airport location info from airport database
165 bool fgFindAirportID( const string& id, FGAirport *a ) {
167 FGPath path( current_options.get_fg_root() );
168 path.append( "Airports" );
169 path.append( "simple.mk4" );
170 FGAirports airports( path.c_str() );
172 FG_LOG( FG_GENERAL, FG_INFO, "Searching for airport code = " << id );
174 if ( ! airports.search( id, a ) ) {
175 FG_LOG( FG_GENERAL, FG_ALERT,
176 "Failed to find " << id << " in " << path.str() );
183 FG_LOG( FG_GENERAL, FG_INFO,
184 "Position for " << id << " is ("
185 << a->longitude << ", "
186 << a->latitude << ")" );
192 // Set current_options lon/lat given an airport id
193 bool fgSetPosFromAirportID( const string& id ) {
197 FG_LOG( FG_GENERAL, FG_INFO,
198 "Attempting to set starting position from airport code " << id );
200 if ( fgFindAirportID( id, &a ) ) {
201 current_options.set_lon( a.longitude );
202 current_options.set_lat( a.latitude );
203 current_properties.setDoubleValue("/position/longitude",
205 current_properties.setDoubleValue("/position/latitude",
208 FG_LOG( FG_GENERAL, FG_INFO,
209 "Position for " << id << " is ("
210 << a.longitude << ", "
211 << a.latitude << ")" );
221 // Set current_options lon/lat given an airport id and heading (degrees)
222 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
228 // set initial position from runway and heading
230 FGPath path( current_options.get_fg_root() );
231 path.append( "Airports" );
232 path.append( "runways.mk4" );
233 FGRunways runways( path.c_str() );
235 FG_LOG( FG_GENERAL, FG_INFO,
236 "Attempting to set starting position from runway code "
237 << id << " heading " << tgt_hdg );
239 // FGPath inpath( current_options.get_fg_root() );
240 // inpath.append( "Airports" );
241 // inpath.append( "apt_simple" );
242 // airports.load( inpath.c_str() );
244 // FGPath outpath( current_options.get_fg_root() );
245 // outpath.append( "Airports" );
246 // outpath.append( "simple.gdbm" );
247 // airports.dump_gdbm( outpath.c_str() );
249 if ( ! runways.search( id, &r ) ) {
250 FG_LOG( FG_GENERAL, FG_ALERT,
251 "Failed to find " << id << " in database." );
256 double min_diff = 360.0;
258 while ( r.id == id ) {
260 diff = tgt_hdg - r.heading;
261 while ( diff < -180.0 ) { diff += 360.0; }
262 while ( diff > 180.0 ) { diff -= 360.0; }
264 FG_LOG( FG_GENERAL, FG_INFO,
265 "Runway " << r.rwy_no << " heading = " << r.heading <<
266 " diff = " << diff );
267 if ( diff < min_diff ) {
274 diff = tgt_hdg - r.heading - 180.0;
275 while ( diff < -180.0 ) { diff += 360.0; }
276 while ( diff > 180.0 ) { diff -= 360.0; }
278 FG_LOG( FG_GENERAL, FG_INFO,
279 "Runway -" << r.rwy_no << " heading = " <<
281 " diff = " << diff );
282 if ( diff < min_diff ) {
291 FG_LOG( FG_GENERAL, FG_INFO, "closest runway = " << found_r.rwy_no
292 << " + " << found_dir );
298 double heading = found_r.heading + found_dir;
299 while ( heading >= 360.0 ) { heading -= 360.0; }
301 double lat2, lon2, az2;
302 double azimuth = found_r.heading + found_dir + 180.0;
303 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
305 FG_LOG( FG_GENERAL, FG_INFO,
306 "runway = " << found_r.lon << ", " << found_r.lat
307 << " length = " << found_r.length * FEET_TO_METER * 0.5
308 << " heading = " << azimuth );
309 geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon,
310 azimuth, found_r.length * FEET_TO_METER * 0.5 - 5.0,
311 &lat2, &lon2, &az2 );
312 current_options.set_lon( lon2 );
313 current_options.set_lat( lat2 );
314 current_options.set_heading( heading );
315 current_properties.setDoubleValue("/position/longitude", lon2);
316 current_properties.setDoubleValue("/position/latitude", lat2);
317 current_properties.setDoubleValue("/orientation/heading", heading);
319 FG_LOG( FG_GENERAL, FG_INFO,
320 "Position for " << id << " is ("
322 << lat2 << ") new heading is "
329 // Set initial position and orientation
330 bool fgInitPosition( void ) {
331 FGInterface *f = current_aircraft.fdm_state;
332 string id = current_options.get_airport_id();
334 // set initial position from default or command line coordinates
335 f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
336 f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
338 if ( scenery.cur_elev > current_options.get_altitude() - 1) {
339 current_options.set_altitude( scenery.cur_elev + 1 );
342 FG_LOG( FG_GENERAL, FG_INFO,
343 "starting altitude is = " << current_options.get_altitude() );
345 f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
346 fgFDMSetGroundElevation( current_options.get_flight_model(),
347 f->get_Altitude() * FEET_TO_METER );
350 current_properties.setDoubleValue("/position/longitude",
351 f->get_Longitude() * RAD_TO_DEG);
352 current_properties.setDoubleValue("/position/latitude",
353 f->get_Latitude() * RAD_TO_DEG);
354 current_properties.setDoubleValue("/position/altitude",
355 f->get_Altitude() * RAD_TO_DEG);
358 FG_LOG( FG_GENERAL, FG_INFO,
359 "Initial position is: ("
360 << (f->get_Longitude() * RAD_TO_DEG) << ", "
361 << (f->get_Latitude() * RAD_TO_DEG) << ", "
362 << (f->get_Altitude() * FEET_TO_METER) << ")" );
368 // General house keeping initializations
369 bool fgInitGeneral( void ) {
372 #if defined(FX) && defined(XMESA)
373 char *mesa_win_state;
376 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
377 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
379 root = current_options.get_fg_root();
380 if ( ! root.length() ) {
381 // No root path set? Then bail ...
382 FG_LOG( FG_GENERAL, FG_ALERT,
383 "Cannot continue without environment variable FG_ROOT"
384 << "being defined." );
387 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
389 #if defined(FX) && defined(XMESA)
390 // initialize full screen flag
391 global_fullscreen = false;
392 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
393 // Test for the MESA_GLX_FX env variable
394 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
395 // test if we are fullscreen mesa/glide
396 if ( (mesa_win_state[0] == 'f') ||
397 (mesa_win_state[0] == 'F') ) {
398 global_fullscreen = true;
408 // This is the top level init routine which calls all the other
409 // initialization routines. If you are adding a subsystem to flight
410 // gear, its initialization call should located in this routine.
411 // Returns non-zero if a problem encountered.
412 bool fgInitSubsystems( void ) {
413 fgLIGHT *l = &cur_light_params;
415 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
416 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
418 // Initialize the material property lib
419 FGPath mpath( current_options.get_fg_root() );
420 mpath.append( "materials" );
421 if ( material_lib.load( mpath.str() ) ) {
423 FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
427 // Initialize the Scenery Management subsystem
428 if ( fgSceneryInit() ) {
429 // Material lib initialized ok.
431 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
435 if ( global_tile_mgr.init() ) {
436 // Load the local scenery data
437 global_tile_mgr.update( current_options.get_lon(),
438 current_options.get_lat() );
440 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
444 FG_LOG( FG_GENERAL, FG_DEBUG,
445 "Current terrain elevation after tile mgr init " <<
448 if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
449 cur_fdm_state = new FGLaRCsim;
450 } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
451 cur_fdm_state = new FGJSBsim;
452 } else if ( current_options.get_flight_model() ==
453 FGInterface::FG_BALLOONSIM ) {
454 cur_fdm_state = new FGBalloonSim;
455 } else if ( current_options.get_flight_model() ==
456 FGInterface::FG_MAGICCARPET ) {
457 cur_fdm_state = new FGMagicCarpet;
458 } else if ( current_options.get_flight_model() ==
459 FGInterface::FG_EXTERNAL ) {
460 cur_fdm_state = new FGExternal;
462 FG_LOG( FG_GENERAL, FG_ALERT,
463 "No flight model, can't init aircraft" );
467 // allocates structures so must happen before any of the flight
468 // model or control parameters are set
469 fgAircraftInit(); // In the future this might not be the case.
471 // set the initial position
474 // Calculate ground elevation at starting point (we didn't have
475 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
477 // calculalate a cartesian point somewhere along the line between
478 // the center of the earth and our view position. Doesn't have to
479 // be the exact elevation (this is good because we don't know it
482 // now handled inside of the fgTileMgrUpdate()
485 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
486 tmp_abs_view_pos = sgGeodToCart(geod_pos);
488 FG_LOG( FG_GENERAL, FG_DEBUG,
489 "Initial abs_view_pos = " << tmp_abs_view_pos );
491 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
493 FG_LOG( FG_GENERAL, FG_DEBUG,
494 "Altitude after update " << scenery.cur_elev );
497 fgFDMSetGroundElevation( current_options.get_flight_model(),
500 // Reset our altitude if we are below ground
501 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
502 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
503 cur_fdm_state->get_Runway_altitude() );
505 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
506 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
509 FG_LOG( FG_GENERAL, FG_INFO,
510 "Updated position (after elevation adj): ("
511 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
512 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
513 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
515 // We need to calculate a few more values here that would normally
516 // be calculated by the FDM so that the current_view.UpdateViewMath()
517 // routine doesn't get hosed.
519 double sea_level_radius_meters;
521 // Set the FG variables first
522 sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
523 &sea_level_radius_meters, &lat_geoc);
524 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
525 cur_fdm_state->get_Altitude() +
526 (sea_level_radius_meters * METER_TO_FEET) );
527 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
529 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
530 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
532 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
533 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
535 // The following section sets up the flight model EOM parameters
536 // and should really be read in from one or more files.
539 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
540 current_options.get_vBody(),
541 current_options.get_wBody());
543 // Initial Orientation
544 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
545 current_options.get_pitch() * DEG_TO_RAD,
546 current_options.get_heading() * DEG_TO_RAD );
548 // Initial Angular Body rates
549 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
551 cur_fdm_state->set_Earth_position_angle( 0.0 );
553 // Mass properties and geometry values
554 cur_fdm_state->set_Inertias( 8.547270E+01,
555 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
557 // CG position w.r.t. ref. point
558 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
560 // Initialize the event manager
561 global_events.Init();
563 // Output event stats every 60 seconds
564 global_events.Register( "fgEVENT_MGR::PrintStats()",
565 fgMethodCallback<fgEVENT_MGR>( &global_events,
566 &fgEVENT_MGR::PrintStats),
567 fgEVENT::FG_EVENT_READY, 60000 );
569 // Initialize view parameters
570 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
573 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
574 current_view.UpdateViewMath(*cur_fdm_state);
575 pilot_view.UpdateViewMath(*cur_fdm_state);
576 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
577 // current_view.UpdateWorldToEye(f);
579 // Initialize the planetary subsystem
580 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
581 // fgEVENT::FG_EVENT_READY, 600000);
583 // Initialize the sun's position
584 // global_events.Register( "fgSunInit()", fgSunInit,
585 // fgEVENT::FG_EVENT_READY, 30000 );
587 // Intialize the moon's position
588 // global_events.Register( "fgMoonInit()", fgMoonInit,
589 // fgEVENT::FG_EVENT_READY, 600000 );
591 // fgUpdateSunPos() needs a few position and view parameters set
592 // so it can calculate local relative sun angle and a few other
593 // things for correctly orienting the sky.
596 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
597 fgEVENT::FG_EVENT_READY, 60000);
598 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
599 fgEVENT::FG_EVENT_READY, 60000);
601 // Initialize Lighting interpolation tables
604 // update the lighting parameters (based on sun angle)
605 global_events.Register( "fgLight::Update()",
606 fgMethodCallback<fgLIGHT>( &cur_light_params,
608 fgEVENT::FG_EVENT_READY, 30000 );
609 // update the current timezone each 30 minutes
610 global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
611 fgEVENT::FG_EVENT_READY, 1800000);
613 // Initialize the weather modeling subsystem
614 #ifndef FG_OLD_WEATHER
615 // Initialize the WeatherDatabase
616 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
618 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
619 current_aircraft.fdm_state->get_Longitude(),
620 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
621 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
622 new FGLocalWeatherDatabase( position, current_options.get_fg_root() );
623 // cout << theFGLocalWeatherDatabase << endl;
624 // cout << "visibility = "
625 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
627 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
629 // register the periodic update of the weather
630 global_events.Register( "weather update", fgUpdateWeatherDatabase,
631 fgEVENT::FG_EVENT_READY, 30000);
633 current_weather.Init();
636 // Initialize vor/ndb/ils/fix list management and query systems
637 FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
639 FG_LOG(FG_GENERAL, FG_INFO, " VOR/NDB");
640 current_navlist = new FGNavList;
641 FGPath p_nav( current_options.get_fg_root() );
642 p_nav.append( "Navaids/default.nav" );
643 current_navlist->init( p_nav );
645 FG_LOG(FG_GENERAL, FG_INFO, " ILS");
646 current_ilslist = new FGILSList;
647 FGPath p_ils( current_options.get_fg_root() );
648 p_ils.append( "Navaids/default.ils" );
649 current_ilslist->init( p_ils );
651 FG_LOG(FG_GENERAL, FG_INFO, " Fixes");
652 current_fixlist = new FGFixList;
653 FGPath p_fix( current_options.get_fg_root() );
654 p_fix.append( "Navaids/default.fix" );
655 current_fixlist->init( p_fix );
657 // Initialize the underlying radio stack model
658 current_radiostack = new FGRadioStack;
660 // current_radiostack->set_nav1_freq( 117.30 );
661 // current_radiostack->set_nav1_alt_freq( 110.30 );
662 // current_radiostack->set_nav1_sel_radial( 119.0 );
664 // current_radiostack->set_nav2_freq( 111.80 );
665 // current_radiostack->set_nav2_alt_freq( 115.70 );
666 // current_radiostack->set_nav2_sel_radial( 029.0 );
668 // current_radiostack->set_adf_freq( 266.0 );
671 // This block of settings are Alex's defaults for San Diego
672 current_radiostack->set_nav1_freq( 111.70 );
673 current_radiostack->set_nav1_alt_freq( 115.30 );
674 current_radiostack->set_nav1_sel_radial( 280.0 );
675 current_radiostack->set_nav2_freq( 117.80 );
676 current_radiostack->set_nav2_alt_freq( 114.00 );
677 current_radiostack->set_nav2_sel_radial( 68.0 );
678 current_radiostack->set_adf_freq( 210.0 );
679 // End of Alex's custom settings
682 current_radiostack->search( cur_fdm_state->get_Longitude(),
683 cur_fdm_state->get_Latitude(),
684 cur_fdm_state->get_Altitude() * FEET_TO_METER );
686 current_radiostack->update( cur_fdm_state->get_Longitude(),
687 cur_fdm_state->get_Latitude(),
688 cur_fdm_state->get_Altitude() * FEET_TO_METER );
690 // Search radio database once per second
691 global_events.Register( "fgRadioSearch()", fgRadioSearch,
692 fgEVENT::FG_EVENT_READY, 1000);
695 // Initialize the Cockpit subsystem
696 if( fgCockpitInit( ¤t_aircraft )) {
697 // Cockpit initialized ok.
699 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
703 // Initialize the flight model subsystem data structures base on
706 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
707 // 1.0 / current_options.get_model_hz() );
708 if ( cur_fdm_state->init( 1.0 / current_options.get_model_hz() ) ) {
709 // fdm init successful
711 FG_LOG( FG_GENERAL, FG_ALERT, "FDM init() failed! Cannot continue." );
715 // I'm just sticking this here for now, it should probably move
717 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
719 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
720 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
723 FG_LOG( FG_GENERAL, FG_INFO,
724 "Updated position (after elevation adj): ("
725 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
726 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
727 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
728 // end of thing that I just stuck in that I should probably move
731 if ( fgJoystickInit() ) {
732 // Joystick initialized ok.
734 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
738 current_autopilot = new FGAutopilot;
739 current_autopilot->init();
741 // initialize the gui parts of the autopilot
747 // Initialize I/O channels
748 #if ! defined( macintosh )
752 // Initialize the 2D panel.
754 current_properties.getStringValue("/sim/panel/path",
755 "Panels/Default/default.xml");
756 current_panel = fgReadPanel(panel_path);
757 if (current_panel == 0) {
758 FG_LOG(FG_INPUT, FG_ALERT,
759 "Error reading new panel from " << panel_path);
761 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
763 // Initialize the BFI
766 FG_LOG( FG_GENERAL, FG_INFO, endl);
772 void fgReInitSubsystems( void )
774 bool freeze = globals->get_freeze();
776 globals->set_freeze( true );
778 if( global_tile_mgr.init() ) {
779 // Load the local scenery data
780 global_tile_mgr.update( current_options.get_lon(),
781 current_options.get_lat() );
783 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
787 // cout << "current scenery elev = " << scenery.cur_elev << endl;
790 fgFDMSetGroundElevation( current_options.get_flight_model(),
793 // Reset our altitude if we are below ground
794 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
795 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
796 cur_fdm_state->get_Runway_altitude() );
798 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
799 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
801 double sea_level_radius_meters;
803 // Set the FG variables first
804 sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
805 &sea_level_radius_meters, &lat_geoc);
806 cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
807 cur_fdm_state->get_Altitude() +
808 (sea_level_radius_meters * METER_TO_FEET) );
809 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
811 cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
812 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
814 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
815 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
817 // The following section sets up the flight model EOM parameters
818 // and should really be read in from one or more files.
821 cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
822 current_options.get_vBody(),
823 current_options.get_wBody());
825 // Initial Orientation
826 cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
827 current_options.get_pitch() * DEG_TO_RAD,
828 current_options.get_heading() * DEG_TO_RAD );
830 // Initial Angular Body rates
831 cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
833 cur_fdm_state->set_Earth_position_angle( 0.0 );
835 // Mass properties and geometry values
836 cur_fdm_state->set_Inertias( 8.547270E+01,
837 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
839 // CG position w.r.t. ref. point
840 cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
842 // Initialize view parameters
843 current_view.set_view_offset( 0.0 );
844 current_view.set_goal_view_offset( 0.0 );
845 pilot_view.set_view_offset( 0.0 );
846 pilot_view.set_goal_view_offset( 0.0 );
848 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
849 current_view.UpdateViewMath(*cur_fdm_state);
850 pilot_view.UpdateViewMath(*cur_fdm_state);
851 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = " << current_view.get_abs_view_pos());
853 // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
854 // 1.0 / current_options.get_model_hz() );
855 cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
857 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
859 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
860 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
863 controls.reset_all();
864 current_autopilot->reset();
867 globals->set_freeze( false );