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/ADA.hxx>
68 #include <FDM/Balloon.h>
69 #include <FDM/External.hxx>
70 #include <FDM/JSBSim.hxx>
71 #include <FDM/LaRCsim.hxx>
72 #include <FDM/MagicCarpet.hxx>
73 #include <Include/general.hxx>
74 #include <Joystick/joystick.hxx>
75 #include <Objects/matlib.hxx>
76 #include <Navaids/fixlist.hxx>
77 #include <Navaids/ilslist.hxx>
78 #include <Navaids/navlist.hxx>
79 #include <Scenery/scenery.hxx>
80 #include <Scenery/tilemgr.hxx>
81 #include <Time/event.hxx>
82 #include <Time/light.hxx>
83 #include <Time/sunpos.hxx>
84 #include <Time/moonpos.hxx>
85 #include <Time/tmp.hxx>
87 #ifndef FG_OLD_WEATHER
88 # include <WeatherCM/FGLocalWeatherDatabase.h>
90 # include <Weather/weather.hxx>
93 #include "fg_init.hxx"
95 #include "globals.hxx"
98 #if defined(FX) && defined(XMESA)
102 FG_USING_STD(string);
104 extern const char *default_root;
107 // Read in configuration (file and command line) and just set fg_root
108 bool fgInitFGRoot ( int argc, char **argv ) {
109 // Attempt to locate and parse a config file
110 // First check fg_root
111 FGPath config( globals->get_options()->get_fg_root() );
112 config.append( "system.fgfsrc" );
113 globals->get_options()->scan_config_file_for_root( config.str() );
115 // Next check home directory
116 char* envp = ::getenv( "HOME" );
117 if ( envp != NULL ) {
119 config.append( ".fgfsrc" );
120 globals->get_options()->scan_config_file_for_root( config.str() );
123 // Parse remaining command line options
124 // These will override anything specified in a config file
125 globals->get_options()->scan_command_line_for_root(argc, argv);
131 // Read in configuration (file and command line)
132 bool fgInitConfig ( int argc, char **argv ) {
133 // Attempt to locate and parse a config file
134 // First check fg_root
135 FGPath config( globals->get_options()->get_fg_root() );
136 config.append( "system.fgfsrc" );
137 globals->get_options()->parse_config_file( config.str() );
139 // Next check home directory
140 char* envp = ::getenv( "HOME" );
141 if ( envp != NULL ) {
143 config.append( ".fgfsrc" );
144 globals->get_options()->parse_config_file( config.str() );
147 // Parse remaining command line options
148 // These will override anything specified in a config file
149 if ( globals->get_options()->parse_command_line(argc, argv) !=
150 FGOptions::FG_OPTIONS_OK )
152 // Something must have gone horribly wrong with the command
153 // line parsing or maybe the user just requested help ... :-)
154 globals->get_options()->usage();
155 FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
163 // find basic airport location info from airport database
164 bool fgFindAirportID( const string& id, FGAirport *a ) {
166 FGPath path( globals->get_options()->get_fg_root() );
167 path.append( "Airports" );
168 path.append( "simple.mk4" );
169 FGAirports airports( path.c_str() );
171 FG_LOG( FG_GENERAL, FG_INFO, "Searching for airport code = " << id );
173 if ( ! airports.search( id, a ) ) {
174 FG_LOG( FG_GENERAL, FG_ALERT,
175 "Failed to find " << id << " in " << path.str() );
182 FG_LOG( FG_GENERAL, FG_INFO,
183 "Position for " << id << " is ("
184 << a->longitude << ", "
185 << a->latitude << ")" );
191 // Set current_options lon/lat given an airport id
192 bool fgSetPosFromAirportID( const string& id ) {
196 FG_LOG( FG_GENERAL, FG_INFO,
197 "Attempting to set starting position from airport code " << id );
199 if ( fgFindAirportID( id, &a ) ) {
200 globals->get_options()->set_lon( a.longitude );
201 globals->get_options()->set_lat( a.latitude );
202 current_properties.setDoubleValue("/position/longitude",
204 current_properties.setDoubleValue("/position/latitude",
207 FG_LOG( FG_GENERAL, FG_INFO,
208 "Position for " << id << " is ("
209 << a.longitude << ", "
210 << a.latitude << ")" );
220 // Set current_options lon/lat given an airport id and heading (degrees)
221 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
227 // set initial position from runway and heading
229 FGPath path( globals->get_options()->get_fg_root() );
230 path.append( "Airports" );
231 path.append( "runways.mk4" );
232 FGRunways runways( path.c_str() );
234 FG_LOG( FG_GENERAL, FG_INFO,
235 "Attempting to set starting position from runway code "
236 << id << " heading " << tgt_hdg );
238 // FGPath inpath( globals->get_options()->get_fg_root() );
239 // inpath.append( "Airports" );
240 // inpath.append( "apt_simple" );
241 // airports.load( inpath.c_str() );
243 // FGPath outpath( globals->get_options()->get_fg_root() );
244 // outpath.append( "Airports" );
245 // outpath.append( "simple.gdbm" );
246 // airports.dump_gdbm( outpath.c_str() );
248 if ( ! runways.search( id, &r ) ) {
249 FG_LOG( FG_GENERAL, FG_ALERT,
250 "Failed to find " << id << " in database." );
255 double min_diff = 360.0;
257 while ( r.id == id ) {
259 diff = tgt_hdg - r.heading;
260 while ( diff < -180.0 ) { diff += 360.0; }
261 while ( diff > 180.0 ) { diff -= 360.0; }
263 FG_LOG( FG_GENERAL, FG_INFO,
264 "Runway " << r.rwy_no << " heading = " << r.heading <<
265 " diff = " << diff );
266 if ( diff < min_diff ) {
273 diff = tgt_hdg - r.heading - 180.0;
274 while ( diff < -180.0 ) { diff += 360.0; }
275 while ( diff > 180.0 ) { diff -= 360.0; }
277 FG_LOG( FG_GENERAL, FG_INFO,
278 "Runway -" << r.rwy_no << " heading = " <<
280 " diff = " << diff );
281 if ( diff < min_diff ) {
290 FG_LOG( FG_GENERAL, FG_INFO, "closest runway = " << found_r.rwy_no
291 << " + " << found_dir );
297 double heading = found_r.heading + found_dir;
298 while ( heading >= 360.0 ) { heading -= 360.0; }
300 double lat2, lon2, az2;
301 double azimuth = found_r.heading + found_dir + 180.0;
302 while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
304 FG_LOG( FG_GENERAL, FG_INFO,
305 "runway = " << found_r.lon << ", " << found_r.lat
306 << " length = " << found_r.length * FEET_TO_METER * 0.5
307 << " heading = " << azimuth );
308 geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon,
309 azimuth, found_r.length * FEET_TO_METER * 0.5 - 5.0,
310 &lat2, &lon2, &az2 );
311 globals->get_options()->set_lon( lon2 );
312 globals->get_options()->set_lat( lat2 );
313 globals->get_options()->set_heading( heading );
314 current_properties.setDoubleValue("/position/longitude", lon2);
315 current_properties.setDoubleValue("/position/latitude", lat2);
316 current_properties.setDoubleValue("/orientation/heading", heading);
318 FG_LOG( FG_GENERAL, FG_INFO,
319 "Position for " << id << " is ("
321 << lat2 << ") new heading is "
328 // Set initial position and orientation
329 bool fgInitPosition( void ) {
330 FGInterface *f = current_aircraft.fdm_state;
331 string id = globals->get_options()->get_airport_id();
333 // set initial position from default or command line coordinates
334 f->set_Longitude( globals->get_options()->get_lon() * DEG_TO_RAD );
335 f->set_Latitude( globals->get_options()->get_lat() * DEG_TO_RAD );
337 if ( scenery.cur_elev > globals->get_options()->get_altitude() - 1) {
338 globals->get_options()->set_altitude( scenery.cur_elev + 1 );
341 FG_LOG( FG_GENERAL, FG_INFO,
342 "starting altitude is = " << globals->get_options()->get_altitude() );
344 f->set_Altitude( globals->get_options()->get_altitude() * METER_TO_FEET );
345 // fgFDMSetGroundElevation( globals->get_options()->get_flight_model(),
346 // f->get_Altitude() * FEET_TO_METER );
349 current_properties.setDoubleValue("/position/longitude",
350 f->get_Longitude() * RAD_TO_DEG);
351 current_properties.setDoubleValue("/position/latitude",
352 f->get_Latitude() * RAD_TO_DEG);
353 current_properties.setDoubleValue("/position/altitude",
354 f->get_Altitude() * RAD_TO_DEG);
357 FG_LOG( FG_GENERAL, FG_INFO,
358 "Initial position is: ("
359 << (f->get_Longitude() * RAD_TO_DEG) << ", "
360 << (f->get_Latitude() * RAD_TO_DEG) << ", "
361 << (f->get_Altitude() * FEET_TO_METER) << ")" );
367 // General house keeping initializations
368 bool fgInitGeneral( void ) {
371 #if defined(FX) && defined(XMESA)
372 char *mesa_win_state;
375 FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
376 FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
378 root = globals->get_options()->get_fg_root();
379 if ( ! root.length() ) {
380 // No root path set? Then bail ...
381 FG_LOG( FG_GENERAL, FG_ALERT,
382 "Cannot continue without environment variable FG_ROOT"
383 << "being defined." );
386 FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
388 #if defined(FX) && defined(XMESA)
389 // initialize full screen flag
390 global_fullscreen = false;
391 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
392 // Test for the MESA_GLX_FX env variable
393 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
394 // test if we are fullscreen mesa/glide
395 if ( (mesa_win_state[0] == 'f') ||
396 (mesa_win_state[0] == 'F') ) {
397 global_fullscreen = true;
407 // set initial aircraft speed
408 bool fgVelocityInit( void ) {
409 switch(globals->get_options()->get_speedset()) {
411 current_aircraft.fdm_state->set_V_calibrated_kts(
412 globals->get_options()->get_vc() );
415 current_aircraft.fdm_state->set_Mach_number(
416 globals->get_options()->get_mach() );
419 current_aircraft.fdm_state->set_Velocities_Wind_Body(
420 globals->get_options()->get_uBody(),
421 globals->get_options()->get_vBody(),
422 globals->get_options()->get_wBody() );
425 current_aircraft.fdm_state->set_Velocities_Local(
426 globals->get_options()->get_vNorth(),
427 globals->get_options()->get_vEast(),
428 globals->get_options()->get_vDown() );
431 current_aircraft.fdm_state->set_V_calibrated_kts( 0.0 );
437 // This is the top level init routine which calls all the other
438 // initialization routines. If you are adding a subsystem to flight
439 // gear, its initialization call should located in this routine.
440 // Returns non-zero if a problem encountered.
441 bool fgInitSubsystems( void ) {
442 fgLIGHT *l = &cur_light_params;
444 FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
445 FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
447 // Initialize the material property lib
448 FGPath mpath( globals->get_options()->get_fg_root() );
449 mpath.append( "materials" );
450 if ( material_lib.load( mpath.str() ) ) {
452 FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
456 // Initialize the Scenery Management subsystem
457 if ( fgSceneryInit() ) {
458 // Material lib initialized ok.
460 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
464 if ( global_tile_mgr.init() ) {
465 // Load the local scenery data
466 global_tile_mgr.update( globals->get_options()->get_lon(),
467 globals->get_options()->get_lat() );
469 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
473 FG_LOG( FG_GENERAL, FG_DEBUG,
474 "Current terrain elevation after tile mgr init " <<
477 if ( globals->get_options()->get_flight_model() == FGInterface::FG_LARCSIM ) {
478 cur_fdm_state = new FGLaRCsim;
479 } else if ( globals->get_options()->get_flight_model() == FGInterface::FG_JSBSIM ) {
480 cur_fdm_state = new FGJSBsim;
481 } else if ( globals->get_options()->get_flight_model() == FGInterface::FG_ADA ) {
482 cur_fdm_state = new FGADA;
483 } else if ( globals->get_options()->get_flight_model() ==
484 FGInterface::FG_BALLOONSIM ) {
485 cur_fdm_state = new FGBalloonSim;
486 } else if ( globals->get_options()->get_flight_model() ==
487 FGInterface::FG_MAGICCARPET ) {
488 cur_fdm_state = new FGMagicCarpet;
489 } else if ( globals->get_options()->get_flight_model() ==
490 FGInterface::FG_EXTERNAL ) {
491 cur_fdm_state = new FGExternal;
493 FG_LOG( FG_GENERAL, FG_ALERT,
494 "No flight model, can't init aircraft" );
498 // allocates structures so must happen before any of the flight
499 // model or control parameters are set
500 fgAircraftInit(); // In the future this might not be the case.
502 fgFDMSetGroundElevation( globals->get_options()->get_flight_model(),
505 // set the initial position
508 // Calculate ground elevation at starting point (we didn't have
509 // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
511 // calculalate a cartesian point somewhere along the line between
512 // the center of the earth and our view position. Doesn't have to
513 // be the exact elevation (this is good because we don't know it
516 // now handled inside of the fgTileMgrUpdate()
519 geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
520 tmp_abs_view_pos = sgGeodToCart(geod_pos);
522 FG_LOG( FG_GENERAL, FG_DEBUG,
523 "Initial abs_view_pos = " << tmp_abs_view_pos );
525 fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
527 FG_LOG( FG_GENERAL, FG_DEBUG,
528 "Altitude after update " << scenery.cur_elev );
531 // Reset our altitude if we are below ground
532 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
533 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
534 cur_fdm_state->get_Runway_altitude() );
536 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
537 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
540 FG_LOG( FG_GENERAL, FG_INFO,
541 "Updated position (after elevation adj): ("
542 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
543 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
544 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
546 // We need to calculate a few more values here that would normally
547 // be calculated by the FDM so that the current_view.UpdateViewMath()
548 // routine doesn't get hosed.
550 double sea_level_radius_meters;
552 // Set the FG variables first
553 sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
554 &sea_level_radius_meters, &lat_geoc);
555 /* cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
556 cur_fdm_state->get_Altitude() +
557 (sea_level_radius_meters * METER_TO_FEET) );
559 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
561 /* cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
562 cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
564 cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
565 cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc)); */
568 // The following section sets up the flight model EOM parameters
569 // and should really be read in from one or more files.
572 //cur_fdm_state->set_Velocities_Local( globals->get_options()->get_uBody(),
573 // globals->get_options()->get_vBody(),
574 // globals->get_options()->get_wBody());
577 // Initial Orientation
578 cur_fdm_state->set_Euler_Angles( globals->get_options()->get_roll() * DEG_TO_RAD,
579 globals->get_options()->get_pitch() * DEG_TO_RAD,
580 globals->get_options()->get_heading() * DEG_TO_RAD );
582 // Initial Angular Body rates
583 //cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
585 //cur_fdm_state->set_Earth_position_angle( 0.0 );
587 // Mass properties and geometry values
588 //cur_fdm_state->set_Inertias( 8.547270E+01,
589 // 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
591 // CG position w.r.t. ref. point
592 //cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
594 // Initialize the event manager
595 global_events.Init();
597 // Output event stats every 60 seconds
598 global_events.Register( "fgEVENT_MGR::PrintStats()",
599 fgMethodCallback<fgEVENT_MGR>( &global_events,
600 &fgEVENT_MGR::PrintStats),
601 fgEVENT::FG_EVENT_READY, 60000 );
603 // Initialize view parameters
604 FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
605 globals->get_current_view()->init();
606 // globals->get_pilot_view()->Init();
607 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
609 globals->get_current_view()->
610 set_geod_view_pos( cur_fdm_state->get_Longitude(),
611 cur_fdm_state->get_Lat_geocentric(),
612 cur_fdm_state->get_Altitude() *
614 globals->get_current_view()->
615 set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
617 globals->get_current_view()->
618 set_hpr( cur_fdm_state->get_Psi(),
619 cur_fdm_state->get_Theta(),
620 cur_fdm_state->get_Phi() );
622 // globals->get_current_view()->UpdateViewMath();
623 // globals->get_pilot_view()->UpdateViewMath();
624 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = "
625 << globals->get_current_view()->get_abs_view_pos());
626 // current_view.UpdateWorldToEye(f);
628 // Initialize the planetary subsystem
629 // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
630 // fgEVENT::FG_EVENT_READY, 600000);
632 // Initialize the sun's position
633 // global_events.Register( "fgSunInit()", fgSunInit,
634 // fgEVENT::FG_EVENT_READY, 30000 );
636 // Intialize the moon's position
637 // global_events.Register( "fgMoonInit()", fgMoonInit,
638 // fgEVENT::FG_EVENT_READY, 600000 );
640 // fgUpdateSunPos() needs a few position and view parameters set
641 // so it can calculate local relative sun angle and a few other
642 // things for correctly orienting the sky.
645 global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
646 fgEVENT::FG_EVENT_READY, 60000);
647 global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
648 fgEVENT::FG_EVENT_READY, 60000);
650 // Initialize Lighting interpolation tables
653 // update the lighting parameters (based on sun angle)
654 global_events.Register( "fgLight::Update()",
655 fgMethodCallback<fgLIGHT>( &cur_light_params,
657 fgEVENT::FG_EVENT_READY, 30000 );
658 // update the current timezone each 30 minutes
659 global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
660 fgEVENT::FG_EVENT_READY, 1800000);
662 // Initialize the weather modeling subsystem
663 #ifndef FG_OLD_WEATHER
664 // Initialize the WeatherDatabase
665 FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
667 sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
668 current_aircraft.fdm_state->get_Longitude(),
669 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
670 FGLocalWeatherDatabase::theFGLocalWeatherDatabase =
671 new FGLocalWeatherDatabase( position, globals->get_options()->get_fg_root() );
672 // cout << theFGLocalWeatherDatabase << endl;
673 // cout << "visibility = "
674 // << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
676 WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
678 // register the periodic update of the weather
679 global_events.Register( "weather update", fgUpdateWeatherDatabase,
680 fgEVENT::FG_EVENT_READY, 30000);
682 current_weather.Init();
685 // Initialize vor/ndb/ils/fix list management and query systems
686 FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
688 FG_LOG(FG_GENERAL, FG_INFO, " VOR/NDB");
689 current_navlist = new FGNavList;
690 FGPath p_nav( globals->get_options()->get_fg_root() );
691 p_nav.append( "Navaids/default.nav" );
692 current_navlist->init( p_nav );
694 FG_LOG(FG_GENERAL, FG_INFO, " ILS");
695 current_ilslist = new FGILSList;
696 FGPath p_ils( globals->get_options()->get_fg_root() );
697 p_ils.append( "Navaids/default.ils" );
698 current_ilslist->init( p_ils );
700 FG_LOG(FG_GENERAL, FG_INFO, " Fixes");
701 current_fixlist = new FGFixList;
702 FGPath p_fix( globals->get_options()->get_fg_root() );
703 p_fix.append( "Navaids/default.fix" );
704 current_fixlist->init( p_fix );
706 // Initialize the underlying radio stack model
707 current_radiostack = new FGRadioStack;
709 // current_radiostack->set_nav1_freq( 117.30 );
710 // current_radiostack->set_nav1_alt_freq( 110.30 );
711 // current_radiostack->set_nav1_sel_radial( 119.0 );
713 // current_radiostack->set_nav2_freq( 111.80 );
714 // current_radiostack->set_nav2_alt_freq( 115.70 );
715 // current_radiostack->set_nav2_sel_radial( 029.0 );
717 // current_radiostack->set_adf_freq( 266.0 );
720 // This block of settings are Alex's defaults for San Diego
721 current_radiostack->set_nav1_freq( 111.70 );
722 current_radiostack->set_nav1_alt_freq( 115.30 );
723 current_radiostack->set_nav1_sel_radial( 280.0 );
724 current_radiostack->set_nav2_freq( 117.80 );
725 current_radiostack->set_nav2_alt_freq( 114.00 );
726 current_radiostack->set_nav2_sel_radial( 68.0 );
727 current_radiostack->set_adf_freq( 210.0 );
728 // End of Alex's custom settings
731 current_radiostack->search( cur_fdm_state->get_Longitude(),
732 cur_fdm_state->get_Latitude(),
733 cur_fdm_state->get_Altitude() * FEET_TO_METER );
735 current_radiostack->update( cur_fdm_state->get_Longitude(),
736 cur_fdm_state->get_Latitude(),
737 cur_fdm_state->get_Altitude() * FEET_TO_METER );
739 // Search radio database once per second
740 global_events.Register( "fgRadioSearch()", fgRadioSearch,
741 fgEVENT::FG_EVENT_READY, 1000);
744 // Initialize the Cockpit subsystem
745 if( fgCockpitInit( ¤t_aircraft )) {
746 // Cockpit initialized ok.
748 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
752 // Initialize the flight model subsystem data structures base on
755 // fgFDMInit( globals->get_options()->get_flight_model(), cur_fdm_state,
756 // 1.0 / globals->get_options()->get_model_hz() );
757 if ( cur_fdm_state->init( 1.0 / globals->get_options()->get_model_hz() ) ) {
758 // fdm init successful
760 FG_LOG( FG_GENERAL, FG_ALERT, "FDM init() failed! Cannot continue." );
764 // I'm just sticking this here for now, it should probably move
766 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
768 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
769 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
772 FG_LOG( FG_GENERAL, FG_INFO,
773 "Updated position (after elevation adj): ("
774 << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
775 << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
776 << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
777 // end of thing that I just stuck in that I should probably move
780 if ( fgJoystickInit() ) {
781 // Joystick initialized ok.
783 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
787 current_autopilot = new FGAutopilot;
788 current_autopilot->init();
790 // initialize the gui parts of the autopilot
796 // Initialize I/O channels
797 #if ! defined( macintosh )
801 // Initialize the 2D panel.
803 current_properties.getStringValue("/sim/panel/path",
804 "Panels/Default/default.xml");
805 current_panel = fgReadPanel(panel_path);
806 if (current_panel == 0) {
807 FG_LOG(FG_INPUT, FG_ALERT,
808 "Error reading new panel from " << panel_path);
810 FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
812 // Initialize the BFI
815 FG_LOG( FG_GENERAL, FG_INFO, endl);
821 void fgReInitSubsystems( void )
823 bool freeze = globals->get_freeze();
825 globals->set_freeze( true );
827 if( global_tile_mgr.init() ) {
828 // Load the local scenery data
829 global_tile_mgr.update( globals->get_options()->get_lon(),
830 globals->get_options()->get_lat() );
832 FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
836 // cout << "current scenery elev = " << scenery.cur_elev << endl;
839 fgFDMSetGroundElevation( globals->get_options()->get_flight_model(),
842 // Reset our altitude if we are below ground
843 FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
844 FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
845 cur_fdm_state->get_Runway_altitude() );
847 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
848 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
850 double sea_level_radius_meters;
852 // Set the FG variables first
853 sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
854 &sea_level_radius_meters, &lat_geoc);
855 /* cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
856 cur_fdm_state->get_Altitude() +
857 (sea_level_radius_meters * METER_TO_FEET) );
859 cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
861 //cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
862 //cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
864 //cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
865 //cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
867 // The following section sets up the flight model EOM parameters
868 // and should really be read in from one or more files.
871 //cur_fdm_state->set_Velocities_Local( globals->get_options()->get_uBody(),
872 // globals->get_options()->get_vBody(),
873 // globals->get_options()->get_wBody());
876 // Initial Orientation
877 cur_fdm_state->set_Euler_Angles( globals->get_options()->get_roll() * DEG_TO_RAD,
878 globals->get_options()->get_pitch() * DEG_TO_RAD,
879 globals->get_options()->get_heading() * DEG_TO_RAD );
881 // Initial Angular Body rates
882 //cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
884 //cur_fdm_state->set_Earth_position_angle( 0.0 );
886 // Mass properties and geometry values
887 //cur_fdm_state->set_Inertias( 8.547270E+01,
888 // 1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
890 // CG position w.r.t. ref. point
891 //cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
893 // Initialize view parameters
894 globals->get_current_view()->set_view_offset( 0.0 );
895 globals->get_current_view()->set_goal_view_offset( 0.0 );
896 // globals->get_pilot_view()->set_view_offset( 0.0 );
897 // globals->get_pilot_view()->set_goal_view_offset( 0.0 );
899 FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
901 globals->get_current_view()->
902 set_geod_view_pos( cur_fdm_state->get_Longitude(),
903 cur_fdm_state->get_Lat_geocentric(),
904 cur_fdm_state->get_Altitude() *
906 globals->get_current_view()->
907 set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
909 globals->get_current_view()->
910 set_hpr( cur_fdm_state->get_Psi(),
911 cur_fdm_state->get_Theta(),
912 cur_fdm_state->get_Phi() );
914 // globals->get_current_view()->UpdateViewMath();
915 // globals->get_pilot_view()->UpdateViewMath();
916 FG_LOG( FG_GENERAL, FG_DEBUG, " abs_view_pos = "
917 << globals->get_current_view()->get_abs_view_pos());
919 // fgFDMInit( globals->get_options()->get_flight_model(), cur_fdm_state,
920 // 1.0 / globals->get_options()->get_model_hz() );
921 cur_fdm_state->init( 1.0 / globals->get_options()->get_model_hz() );
923 scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
925 if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
926 cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
929 controls.reset_all();
930 current_autopilot->reset();
933 globals->set_freeze( false );