]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/autopilot.cxx
Remove un-needed header.
[flightgear.git] / src / Autopilot / autopilot.cxx
index b68ea958ee2f601585b85144bd47ca9bece9b89f..7e5f67e6d6a302c789fb68f6bc2c059678573ba5 100644 (file)
 #  include <config.h>
 #endif
 
+#include "autopilot.hxx"
+
+#include <simgear/structure/StateMachine.hxx>
+#include <simgear/sg_inlines.h>
+
+#include "component.hxx"
 #include "functor.hxx"
 #include "predictor.hxx"
 #include "digitalfilter.hxx"
 #include "pisimplecontroller.hxx"
 #include "pidcontroller.hxx"
-#include "autopilot.hxx"
 #include "logic.hxx"
 #include "flipflop.hxx"
 
@@ -41,18 +46,68 @@ using std::string;
 
 using namespace FGXMLAutopilot;
 
+class StateMachineComponent : public Component
+{
+public:
+  StateMachineComponent(SGPropertyNode_ptr config)
+  {
+    inner = simgear::StateMachine::createFromPlist(config, globals->get_props());
+  }
+  
+  virtual bool configure( const std::string & nodeName, SGPropertyNode_ptr config)
+  {
+    return false;
+  }
+  
+  virtual void update( bool firstTime, double dt )
+  {
+    SG_UNUSED(firstTime);
+    inner->update(dt);
+  }
+  
+private:
+  simgear::StateMachine_ptr inner;
+};
+
+class StateMachineFunctor : public FunctorBase<Component>
+{
+public:
+  virtual ~StateMachineFunctor() {}
+  virtual Component* operator()( SGPropertyNode_ptr configNode )
+  {
+    return new StateMachineComponent(configNode);
+  }
+};
+
+
+class ComponentForge : public map<string,FunctorBase<Component> *> {
+public:
+    virtual ~ ComponentForge();
+};
+
+ComponentForge::~ComponentForge()
+{
+    for( iterator it = begin(); it != end(); ++it )
+        delete it->second;
+}
+
+static ComponentForge componentForge;
+
 Autopilot::Autopilot( SGPropertyNode_ptr rootNode, SGPropertyNode_ptr configNode ) :
   _name("unnamed autopilot"),
   _serviceable(true),
   _rootNode(rootNode)
 {
-  map<string,FunctorBase<Component> *> componentForge;
-  componentForge["pid-controller"]       = new CreateAndConfigureFunctor<PIDController,Component>();
-  componentForge["pi-simple-controller"] = new CreateAndConfigureFunctor<PISimpleController,Component>();
-  componentForge["predict-simple"]       = new CreateAndConfigureFunctor<Predictor,Component>();
-  componentForge["filter"]               = new CreateAndConfigureFunctor<DigitalFilter,Component>();
-  componentForge["logic"]                = new CreateAndConfigureFunctor<Logic,Component>();
-  componentForge["flipflop"]             = new CreateAndConfigureFunctor<FlipFlop,Component>();
+  if (componentForge.empty())
+  {
+      componentForge["pid-controller"]       = new CreateAndConfigureFunctor<PIDController,Component>();
+      componentForge["pi-simple-controller"] = new CreateAndConfigureFunctor<PISimpleController,Component>();
+      componentForge["predict-simple"]       = new CreateAndConfigureFunctor<Predictor,Component>();
+      componentForge["filter"]               = new CreateAndConfigureFunctor<DigitalFilter,Component>();
+      componentForge["logic"]                = new CreateAndConfigureFunctor<Logic,Component>();
+      componentForge["flipflop"]             = new CreateAndConfigureFunctor<FlipFlop,Component>();
+      componentForge["state-machine"]        = new StateMachineFunctor();
+  }
 
   if( configNode == NULL ) configNode = rootNode;
 
@@ -72,8 +127,10 @@ Autopilot::Autopilot( SGPropertyNode_ptr rootNode, SGPropertyNode_ptr configNode
       component->set_name( buf.str() );
     }
 
-    SG_LOG( SG_AUTOPILOT, SG_INFO, "adding  autopilot component \"" << childName << "\" as \"" << component->get_name() << "\"" );
-    add_component(component);
+    double updateInterval = node->getDoubleValue( "update-interval-secs", 0.0 );
+
+    SG_LOG( SG_AUTOPILOT, SG_DEBUG, "adding  autopilot component \"" << childName << "\" as \"" << component->get_name() << "\" with interval=" << updateInterval );
+    add_component(component,updateInterval);
   }
 }
 
@@ -92,7 +149,7 @@ void Autopilot::unbind()
   _rootNode->untie( "serviceable" );
 }
 
-void Autopilot::add_component( Component * component )
+void Autopilot::add_component( Component * component, double updateInterval )
 {
   if( component == NULL ) return;
 
@@ -106,7 +163,7 @@ void Autopilot::add_component( Component * component )
   if( name != component->get_name() )
     SG_LOG( SG_ALL, SG_WARN, "Duplicate autopilot component " << component->get_name() << ", renamed to " << name );
 
-  set_subsystem( name.c_str(), component );
+  set_subsystem( name.c_str(), component, updateInterval );
 }
 
 void Autopilot::update( double dt )