]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/autopilot.cxx
Fixed some uninitialized member vars and a mem leak.
[flightgear.git] / src / Autopilot / autopilot.cxx
1 // autopilot.cxx - an even more flexible, generic way to build autopilots
2 //
3 // Written by Torsten Dreyer
4 // Based heavily on work created by Curtis Olson, started January 2004.
5 //
6 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
7 // Copyright (C) 2010  Torsten Dreyer - Torsten (at) t3r (dot) de
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include "functor.hxx"
29 #include "predictor.hxx"
30 #include "digitalfilter.hxx"
31 #include "pisimplecontroller.hxx"
32 #include "pidcontroller.hxx"
33 #include "autopilot.hxx"
34 #include "logic.hxx"
35 #include "flipflop.hxx"
36
37 #include "Main/fg_props.hxx"
38
39 using std::map;
40 using std::string;
41
42 using namespace FGXMLAutopilot;
43
44 class ComponentForge : public map<string,FunctorBase<Component> *> {
45 public:
46     virtual ~ ComponentForge();
47 };
48
49 ComponentForge::~ComponentForge()
50 {
51     for( iterator it = begin(); it != end(); ++it )
52         delete it->second;
53 }
54
55 static ComponentForge componentForge;
56
57 Autopilot::Autopilot( SGPropertyNode_ptr rootNode, SGPropertyNode_ptr configNode ) :
58   _name("unnamed autopilot"),
59   _serviceable(true),
60   _rootNode(rootNode)
61 {
62   if (componentForge.empty())
63   {
64       componentForge["pid-controller"]       = new CreateAndConfigureFunctor<PIDController,Component>();
65       componentForge["pi-simple-controller"] = new CreateAndConfigureFunctor<PISimpleController,Component>();
66       componentForge["predict-simple"]       = new CreateAndConfigureFunctor<Predictor,Component>();
67       componentForge["filter"]               = new CreateAndConfigureFunctor<DigitalFilter,Component>();
68       componentForge["logic"]                = new CreateAndConfigureFunctor<Logic,Component>();
69       componentForge["flipflop"]             = new CreateAndConfigureFunctor<FlipFlop,Component>();
70   }
71
72   if( configNode == NULL ) configNode = rootNode;
73
74   int count = configNode->nChildren();
75   for ( int i = 0; i < count; ++i ) {
76     SGPropertyNode_ptr node = configNode->getChild(i);
77     string childName = node->getName();
78     if( componentForge.count(childName) == 0 ) {
79       SG_LOG( SG_AUTOPILOT, SG_BULK, "unhandled element <" << childName << ">" << std::endl );
80       continue;
81     }
82
83     Component * component = (*componentForge[childName])(node);
84     if( component->get_name().length() == 0 ) {
85       std::ostringstream buf;
86       buf <<  "unnamed_component_" << i;
87       component->set_name( buf.str() );
88     }
89
90     double updateInterval = node->getDoubleValue( "update-interval-secs", 0.0 );
91
92     SG_LOG( SG_AUTOPILOT, SG_DEBUG, "adding  autopilot component \"" << childName << "\" as \"" << component->get_name() << "\" with interval=" << updateInterval );
93     add_component(component,updateInterval);
94   }
95 }
96
97 Autopilot::~Autopilot() 
98 {
99 }
100
101 void Autopilot::bind() 
102 {
103   fgTie( _rootNode->getNode("serviceable", true)->getPath().c_str(), this, 
104     &Autopilot::is_serviceable, &Autopilot::set_serviceable );
105 }
106
107 void Autopilot::unbind() 
108 {
109   _rootNode->untie( "serviceable" );
110 }
111
112 void Autopilot::add_component( Component * component, double updateInterval )
113 {
114   if( component == NULL ) return;
115
116   // check for duplicate name
117   std::string name = component->get_name();
118   for( unsigned i = 0; get_subsystem( name.c_str() ) != NULL; i++ ) {
119       std::ostringstream buf;
120       buf <<  component->get_name() << "_" << i;
121       name = buf.str();
122   }
123   if( name != component->get_name() )
124     SG_LOG( SG_ALL, SG_WARN, "Duplicate autopilot component " << component->get_name() << ", renamed to " << name );
125
126   set_subsystem( name.c_str(), component, updateInterval );
127 }
128
129 void Autopilot::update( double dt ) 
130 {
131   if( !_serviceable || dt <= SGLimitsd::min() )
132     return;
133   SGSubsystemGroup::update( dt );
134 }