1 // autopilot.cxx - an even more flexible, generic way to build autopilots
3 // Written by Torsten Dreyer
4 // Based heavily on work created by Curtis Olson, started January 2004.
6 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de
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.
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.
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.
28 #include "functor.hxx"
29 #include "predictor.hxx"
30 #include "digitalfilter.hxx"
31 #include "pisimplecontroller.hxx"
32 #include "pidcontroller.hxx"
33 #include "autopilot.hxx"
35 #include "flipflop.hxx"
37 #include "Main/fg_props.hxx"
42 using namespace FGXMLAutopilot;
44 Autopilot::Autopilot( SGPropertyNode_ptr rootNode, SGPropertyNode_ptr configNode ) :
45 _name("unnamed autopilot"),
49 map<string,FunctorBase<Component> *> componentForge;
50 componentForge["pid-controller"] = new CreateAndConfigureFunctor<PIDController,Component>();
51 componentForge["pi-simple-controller"] = new CreateAndConfigureFunctor<PISimpleController,Component>();
52 componentForge["predict-simple"] = new CreateAndConfigureFunctor<Predictor,Component>();
53 componentForge["filter"] = new CreateAndConfigureFunctor<DigitalFilter,Component>();
54 componentForge["logic"] = new CreateAndConfigureFunctor<Logic,Component>();
55 componentForge["flipflop"] = new CreateAndConfigureFunctor<FlipFlop,Component>();
57 if( configNode == NULL ) configNode = rootNode;
59 int count = configNode->nChildren();
60 for ( int i = 0; i < count; ++i ) {
61 SGPropertyNode_ptr node = configNode->getChild(i);
62 string childName = node->getName();
63 if( componentForge.count(childName) == 0 ) {
64 SG_LOG( SG_AUTOPILOT, SG_BULK, "unhandled element <" << childName << ">" << std::endl );
68 Component * component = (*componentForge[childName])(node);
69 if( component->get_name().length() == 0 ) {
70 std::ostringstream buf;
71 buf << "unnamed_component_" << i;
72 component->set_name( buf.str() );
75 SG_LOG( SG_AUTOPILOT, SG_INFO, "adding autopilot component \"" << childName << "\" as \"" << component->get_name() << "\"" );
76 add_component(component);
80 Autopilot::~Autopilot()
84 void Autopilot::bind()
86 fgTie( _rootNode->getNode("serviceable", true)->getPath().c_str(), this,
87 &Autopilot::is_serviceable, &Autopilot::set_serviceable );
90 void Autopilot::unbind()
92 _rootNode->untie( "serviceable" );
95 void Autopilot::add_component( Component * component )
97 if( component == NULL ) return;
99 // check for duplicate name
100 std::string name = component->get_name();
101 for( unsigned i = 0; get_subsystem( name.c_str() ) != NULL; i++ ) {
102 std::ostringstream buf;
103 buf << component->get_name() << "_" << i;
106 if( name != component->get_name() )
107 SG_LOG( SG_ALL, SG_WARN, "Duplicate autopilot component " << component->get_name() << ", renamed to " << name );
109 set_subsystem( name.c_str(), component );
112 void Autopilot::update( double dt )
114 if( !_serviceable || dt <= SGLimitsd::min() )
116 SGSubsystemGroup::update( dt );