]> git.mxchange.org Git - flightgear.git/commitdiff
Make magavr a regular subsystem.
authorJames Turner <zakalawe@mac.com>
Mon, 17 Sep 2012 23:58:27 +0000 (00:58 +0100)
committerJames Turner <zakalawe@mac.com>
Mon, 17 Sep 2012 23:58:27 +0000 (00:58 +0100)
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
src/Environment/environment_mgr.cxx
src/Environment/magvarmanager.cxx [new file with mode: 0644]
src/Environment/magvarmanager.hxx [new file with mode: 0644]
src/Instrumentation/heading_indicator.cxx
src/Instrumentation/heading_indicator_fg.cxx
src/Main/fg_props.cxx
src/Main/globals.cxx
src/Main/globals.hxx
src/Main/main.cxx

index 3007eb7814c234372b15da0be349dccec3ab9c28..e75dde258693cba999603796cdef644c91c531b5 100644 (file)
@@ -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}")
index f13d9e88f87e61444d85eb4cd6fe5581c2264870..a1e22555d6fc72fa00899e6aab19a9f9f7f1ebc9 100644 (file)
@@ -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 (file)
index 0000000..726ba49
--- /dev/null
@@ -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 <config.h>
+#endif
+
+#include "magvarmanager.hxx"
+
+#include <simgear/sg_inlines.h>
+#include <simgear/magvar/magvar.hxx>
+#include <simgear/timing/sg_time.hxx>
+
+#include <Main/globals.hxx>
+#include <Main/fg_props.hxx>
+
+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 (file)
index 0000000..9aa6f76
--- /dev/null
@@ -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 <memory>
+
+#include <simgear/structure/subsystem_mgr.hxx>
+#include <simgear/props/propsfwd.hxx>
+
+// 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<SGMagVar> _magVar;
+  
+  SGPropertyNode_ptr _magVarNode, _magDipNode;
+};
+
+#endif // of FG_MAGVAR_MANAGER
index 12daafb8a6288734e712fa789455f25730795fde..d95a1a0844488106d0b5cf42a3f49b905793cd98 100644 (file)
@@ -18,8 +18,6 @@
 #include <Main/fg_props.hxx>
 #include <Main/util.hxx>
 
-#include <simgear/magvar/magvar.hxx>
-
 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();
 }
 
index eeeaf1bd4570a58f5272327f63de6d858c6a27e3..fc7bff442bf03bd803f2a9e0aa6443e6deeae6af 100644 (file)
@@ -14,7 +14,6 @@
 #include <string>
 #include <sstream>
 
-#include <simgear/magvar/magvar.hxx>
 #include <simgear/math/SGMath.hxx>
 
 #include <Main/fg_props.hxx>
@@ -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);
index 807c92118990a972504bdbc36d020b845ec5196d..8eefb8c4981f2aa6d272a112a6dc7bdaecb96cce 100644 (file)
@@ -28,7 +28,6 @@
 #include <simgear/structure/exception.hxx>
 #include <simgear/props/props_io.hxx>
 
-#include <simgear/magvar/magvar.hxx>
 #include <simgear/timing/sg_time.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/scene/model/particles.hxx>
@@ -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);
 }
index a8df39d03403afbceff5917c6b2433d8d344e18f..c63129866c95da9758c0e788f66e8d12f70b608a 100644 (file)
@@ -32,7 +32,6 @@
 #include <simgear/misc/sg_dir.hxx>
 #include <simgear/timing/sg_time.hxx>
 #include <simgear/ephemeris/ephemeris.hxx>
-#include <simgear/magvar/magvar.hxx>
 #include <simgear/scene/material/matlib.hxx>
 #include <simgear/structure/subsystem_mgr.hxx>
 #include <simgear/structure/event_mgr.hxx>
@@ -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
index 4079a4a9a27f64a21fe6f0bc63aea74477189e40..7cc1813fc2ff546bed5b2e7206b3574a98165516 100644 (file)
@@ -46,7 +46,6 @@ typedef std::vector<std::string> 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;
     
index 8d65c1b690b03cee1e0ee31963ddb67d67f58d52..2e7f47bd757507e38243715402609ff06ecbdcea 100644 (file)
@@ -44,7 +44,6 @@
 #include <simgear/props/AtomicChangeListener.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/timing/sg_time.hxx>
-#include <simgear/magvar/magvar.hxx>
 #include <simgear/io/raw_socket.hxx>
 #include <simgear/scene/tsync/terrasync.hxx>
 #include <simgear/math/SGMath.hxx>
@@ -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 ) {