From 505796e349db709c7fbcebf069bf5ddea4fa79bf Mon Sep 17 00:00:00 2001 From: James Turner Date: Tue, 18 Sep 2012 00:58:27 +0100 Subject: [PATCH] Make magavr a regular subsystem. Remove various hacks and make magvar work like a normal subsystem, as part of the environment manager. Fix the remaining users of the globals->get_mag accessor, and hence kill off the global pointer. --- src/Environment/CMakeLists.txt | 2 + src/Environment/environment_mgr.cxx | 7 ++ src/Environment/magvarmanager.cxx | 70 ++++++++++++++++++++ src/Environment/magvarmanager.hxx | 49 ++++++++++++++ src/Instrumentation/heading_indicator.cxx | 6 +- src/Instrumentation/heading_indicator_fg.cxx | 3 +- src/Main/fg_props.cxx | 30 ++------- src/Main/globals.cxx | 23 ++++--- src/Main/globals.hxx | 9 +-- src/Main/main.cxx | 21 ------ 10 files changed, 149 insertions(+), 71 deletions(-) create mode 100644 src/Environment/magvarmanager.cxx create mode 100644 src/Environment/magvarmanager.hxx diff --git a/src/Environment/CMakeLists.txt b/src/Environment/CMakeLists.txt index 3007eb781..e75dde258 100644 --- a/src/Environment/CMakeLists.txt +++ b/src/Environment/CMakeLists.txt @@ -17,6 +17,7 @@ set(SOURCES terrainsampler.cxx presets.cxx gravity.cxx + magvarmanager.cxx ) set(HEADERS @@ -36,6 +37,7 @@ set(HEADERS terrainsampler.hxx presets.hxx gravity.hxx + magvarmanager.hxx ) flightgear_component(Environment "${SOURCES}" "${HEADERS}") diff --git a/src/Environment/environment_mgr.cxx b/src/Environment/environment_mgr.cxx index f13d9e88f..a1e22555d 100644 --- a/src/Environment/environment_mgr.cxx +++ b/src/Environment/environment_mgr.cxx @@ -45,6 +45,7 @@ #include "terrainsampler.hxx" #include "Airports/simple.hxx" #include "gravity.hxx" +#include "magvarmanager.hxx" class FG3DCloudsListener : public SGPropertyChangeListener { public: @@ -93,6 +94,8 @@ FGEnvironmentMgr::FGEnvironmentMgr () : set_subsystem("precipitation", new FGPrecipitationMgr); set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) )); set_subsystem("ridgelift", new FGRidgeLift); + + set_subsystem("magvar", new FGMagVarManager); } FGEnvironmentMgr::~FGEnvironmentMgr () @@ -119,6 +122,10 @@ FGEnvironmentMgr::~FGEnvironmentMgr () remove_subsystem("controller"); delete subsys; + subsys = get_subsystem("magvar"); + remove_subsystem("magvar"); + delete subsys; + delete fgClouds; delete _environment; diff --git a/src/Environment/magvarmanager.cxx b/src/Environment/magvarmanager.cxx new file mode 100644 index 000000000..726ba4983 --- /dev/null +++ b/src/Environment/magvarmanager.cxx @@ -0,0 +1,70 @@ +// magvarmanager.cxx -- Wraps the SimGear SGMagVar in a subsystem +// +// Copyright (C) 2012 James Turner - zakalawe@mac.com +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "magvarmanager.hxx" + +#include +#include +#include + +#include
+#include
+ +FGMagVarManager::FGMagVarManager() : + _magVar(new SGMagVar) +{ +} + +FGMagVarManager::~FGMagVarManager() +{ +} + +void FGMagVarManager::init() +{ + update(0.0); // force an immediate update +} + +void FGMagVarManager::bind() +{ + _magVarNode = fgGetNode("/environment/magnetic-variation-deg", true); + _magDipNode = fgGetNode("/environment/magnetic-dip-deg", true); +} + +void FGMagVarManager::unbind() +{ + _magVarNode = SGPropertyNode_ptr(); + _magDipNode = SGPropertyNode_ptr(); +} + +void FGMagVarManager::update(double dt) +{ + SG_UNUSED(dt); + + // update magvar model + _magVar->update( globals->get_aircraft_position(), + globals->get_time_params()->getJD() ); + + _magVarNode->setDoubleValue(_magVar->get_magvar() * SG_RADIANS_TO_DEGREES); + _magDipNode->setDoubleValue(_magVar->get_magdip() * SG_RADIANS_TO_DEGREES); + +} diff --git a/src/Environment/magvarmanager.hxx b/src/Environment/magvarmanager.hxx new file mode 100644 index 000000000..9aa6f76ad --- /dev/null +++ b/src/Environment/magvarmanager.hxx @@ -0,0 +1,49 @@ +// magvarmanager.hxx -- Wraps the SimGear SGMagVar in a subsystem +// +// Copyright (C) 2012 James Turner - zakalawe@mac.com +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifndef FG_MAGVAR_MANAGER +#define FG_MAGVAR_MANAGER 1 + +#include + +#include +#include + +// forward decls +class SGMagVar; + +class FGMagVarManager : public SGSubsystem +{ +public: + FGMagVarManager(); + virtual ~FGMagVarManager(); + + virtual void init(); + virtual void bind(); + virtual void unbind(); + + virtual void update(double dt); + +private: + std::auto_ptr _magVar; + + SGPropertyNode_ptr _magVarNode, _magDipNode; +}; + +#endif // of FG_MAGVAR_MANAGER diff --git a/src/Instrumentation/heading_indicator.cxx b/src/Instrumentation/heading_indicator.cxx index 12daafb8a..d95a1a084 100644 --- a/src/Instrumentation/heading_indicator.cxx +++ b/src/Instrumentation/heading_indicator.cxx @@ -18,8 +18,6 @@ #include
#include
-#include - HeadingIndicator::HeadingIndicator ( SGPropertyNode *node ) : _name(node->getStringValue("name", "heading-indicator")), @@ -41,14 +39,14 @@ HeadingIndicator::init () SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true ); if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) { _offset_node = node->getChild("offset-deg", 0, true); - _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES ); + _offset_node->setDoubleValue( -fgGetDouble("/environment/magnetic-variation-deg") ); } _heading_in_node = fgGetNode("/orientation/heading-deg", true); _suction_node = fgGetNode(_suction.c_str(), true); _heading_out_node = node->getChild("indicated-heading-deg", 0, true); _heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true); _heading_bug_node = node->getChild("heading-bug-deg", 0, true); - + reinit(); } diff --git a/src/Instrumentation/heading_indicator_fg.cxx b/src/Instrumentation/heading_indicator_fg.cxx index eeeaf1bd4..fc7bff442 100644 --- a/src/Instrumentation/heading_indicator_fg.cxx +++ b/src/Instrumentation/heading_indicator_fg.cxx @@ -14,7 +14,6 @@ #include #include -#include #include #include
@@ -66,7 +65,7 @@ HeadingIndicatorFG::init () SGPropertyNode *node = fgGetNode(branch.c_str(), num, true ); if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) { _offset_node = node->getChild("offset-deg", 0, true); - _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES ); + _offset_node->setDoubleValue( -fgGetDouble("/environment/magnetic-variation-deg") ); } _serviceable_node = node->getChild("serviceable", 0, true); _error_node = node->getChild("heading-bug-error-deg", 0, true); diff --git a/src/Main/fg_props.cxx b/src/Main/fg_props.cxx index 807c92118..8eefb8c49 100644 --- a/src/Main/fg_props.cxx +++ b/src/Main/fg_props.cxx @@ -28,7 +28,6 @@ #include #include -#include #include #include #include @@ -339,33 +338,14 @@ getGMTString () return buf; } -/** - * Return the magnetic variation - */ -static double -getMagVar () -{ - return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES; -} - - -/** - * Return the magnetic dip - */ -static double -getMagDip () -{ - return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES; -} - - /** * Return the current heading in degrees. */ static double getHeadingMag () { - double magheading = fgGetDouble("/orientation/heading-deg") - getMagVar(); + double magheading = fgGetDouble("/orientation/heading-deg") - + fgGetDouble("/environment/magnetic-variation-deg"); return SGMiscd::normalizePeriodic(0, 360, magheading ); } @@ -375,7 +355,8 @@ getHeadingMag () static double getTrackMag () { - double magtrack = fgGetDouble("/orientation/track-deg") - getMagVar(); + double magtrack = fgGetDouble("/orientation/track-deg") - + fgGetDouble("/environment/magnetic-variation-deg"); return SGMiscd::normalizePeriodic(0, 360, magtrack ); } @@ -502,9 +483,6 @@ FGProperties::bind () _tiedProperties.Tie("/orientation/heading-magnetic-deg", getHeadingMag); _tiedProperties.Tie("/orientation/track-magnetic-deg", getTrackMag); - _tiedProperties.Tie("/environment/magnetic-variation-deg", getMagVar); - _tiedProperties.Tie("/environment/magnetic-dip-deg", getMagDip); - // Misc. Temporary junk. _tiedProperties.Tie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false); } diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index a8df39d03..c63129866 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -131,7 +130,6 @@ FGGlobals::FGGlobals() : fg_home( "" ), time_params( NULL ), ephem( NULL ), - mag( NULL ), matlib( NULL ), route_mgr( NULL ), ATIS_mgr( NULL ), @@ -185,7 +183,6 @@ FGGlobals::~FGGlobals() renderer = NULL; delete time_params; - delete mag; delete matlib; delete route_mgr; @@ -389,15 +386,21 @@ FGGlobals::get_event_mgr () const return event_mgr; } -const SGGeod & +SGGeod FGGlobals::get_aircraft_position() const { - if( acmodel != NULL ) { - SGModelPlacement * mp = acmodel->get3DModel(); - if( mp != NULL ) - return mp->getPosition(); - } - throw sg_exception("Can't get aircraft position", "FGGlobals::get_aircraft_position()" ); + if( acmodel != NULL ) { + SGModelPlacement * mp = acmodel->get3DModel(); + if( mp != NULL ) + return mp->getPosition(); + } + + // fall back to reading the property tree. this can occur during + // startup before the acmodel is initialised + + return SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"), + fgGetDouble("/position/latitude-deg"), + fgGetDouble("/position/altitude-ft")); } SGVec3d diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index 4079a4a9a..7cc1813fc 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -46,7 +46,6 @@ typedef std::vector string_list; class SGEphemeris; class SGCommandMgr; -class SGMagVar; class SGMaterialLib; class SGPropertyNode; class SGTime; @@ -112,9 +111,6 @@ private: // Sky structures SGEphemeris *ephem; - // Magnetic Variation - SGMagVar *mag; - // Material properties library SGMaterialLib *matlib; @@ -245,9 +241,6 @@ public: inline SGEphemeris *get_ephem() const { return ephem; } inline void set_ephem( SGEphemeris *e ) { ephem = e; } - inline SGMagVar *get_mag() const { return mag; } - inline void set_mag( SGMagVar *m ) { mag = m; } - inline SGMaterialLib *get_matlib() const { return matlib; } inline void set_matlib( SGMaterialLib *m ) { matlib = m; } @@ -275,7 +268,7 @@ public: acmodel = model; } - const SGGeod & get_aircraft_position() const; + SGGeod get_aircraft_position() const; SGVec3d get_aircraft_positon_cart() const; diff --git a/src/Main/main.cxx b/src/Main/main.cxx index 8d65c1b69..2e7f47bd7 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -44,7 +44,6 @@ #include #include #include -#include #include #include #include @@ -122,10 +121,6 @@ static void fgMainLoop( void ) static TimeManager* timeMgr = (TimeManager*) globals->get_subsystem("time"); timeMgr->computeTimeDeltas(sim_dt, real_dt); - // update magvar model - globals->get_mag()->update( globals->get_aircraft_position(), - globals->get_time_params()->getJD() ); - // update all subsystems globals->get_subsystem_mgr()->update(sim_dt); @@ -267,22 +262,6 @@ static void fgIdleFunction ( void ) { } else if ( idle_state == 6 ) { idle_state++; - - // Initialize MagVar model - SGMagVar *magvar = new SGMagVar(); - globals->set_mag( magvar ); - - // kludge to initialize mag compass - // (should only be done for in-flight startup) - // update magvar model - globals->get_mag()->update( fgGetDouble("/position/longitude-deg") - * SGD_DEGREES_TO_RADIANS, - fgGetDouble("/position/latitude-deg") - * SGD_DEGREES_TO_RADIANS, - fgGetDouble("/position/altitude-ft") - * SG_FEET_TO_METER, - globals->get_time_params()->getJD() ); - fgSplashProgress("initializing subsystems"); } else if ( idle_state == 7 ) { -- 2.39.5