]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/autopilotgroup.cxx
Tweaks to fg-aircraft handling.
[flightgear.git] / src / Autopilot / autopilotgroup.cxx
1 // autopilotgroup.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 "autopilot.hxx"
29 #include "autopilotgroup.hxx"
30
31 #include <string>
32 #include <vector>
33
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/structure/subsystem_mgr.hxx>
36 #include <simgear/structure/exception.hxx>
37 #include <Main/fg_props.hxx>
38 #include <boost/foreach.hpp>
39
40 using std::vector;
41 using std::string;
42 using simgear::PropertyList;
43
44 class FGXMLAutopilotGroupImplementation : public FGXMLAutopilotGroup
45 {
46 public:
47     FGXMLAutopilotGroupImplementation(const std::string& nodeName) :
48         FGXMLAutopilotGroup(), _nodeName(nodeName) {}
49     virtual void addAutopilot( const std::string & name, SGPropertyNode_ptr apNode, SGPropertyNode_ptr config );
50     virtual void removeAutopilot( const std::string & name );
51     void init();
52     InitStatus incrementalInit();
53     void reinit();
54     void update( double dt );
55 private:
56     void initFrom( SGPropertyNode_ptr rootNode, const char * childName );
57     vector<string> _autopilotNames;
58     std::string _nodeName;
59
60 };
61
62 void FGXMLAutopilotGroupImplementation::addAutopilot( const std::string & name, SGPropertyNode_ptr apNode, SGPropertyNode_ptr config )
63 {
64     BOOST_FOREACH( std::string & n, _autopilotNames ) {
65         if( n == name ) {
66             SG_LOG(SG_ALL, SG_ALERT, "NOT adding duplicate property rule name " << name );
67             return;
68         }
69     }
70     FGXMLAutopilot::Autopilot * ap = new FGXMLAutopilot::Autopilot( apNode, config );
71     ap->set_name( name );
72
73     double updateInterval = config->getDoubleValue( "update-interval-secs", 0.0 );
74     set_subsystem( name, ap, updateInterval );
75     _autopilotNames.push_back( name );
76 }
77
78 void FGXMLAutopilotGroupImplementation::removeAutopilot( const std::string & name )
79 {
80     FGXMLAutopilot::Autopilot * ap = (FGXMLAutopilot::Autopilot*)get_subsystem( name );
81     if( ap == NULL ) return; // ?
82     remove_subsystem( name );
83 }
84
85
86
87 void FGXMLAutopilotGroupImplementation::update( double dt )
88 {
89     // update all configured autopilots
90     SGSubsystemGroup::update( dt );
91 }
92
93 void FGXMLAutopilotGroupImplementation::reinit()
94 {
95     SGSubsystemGroup::unbind();
96
97     for( vector<string>::size_type i = 0; i < _autopilotNames.size(); i++ ) {
98       removeAutopilot( _autopilotNames[i] );
99     }
100     _autopilotNames.clear();
101     init();
102 }
103
104 SGSubsystem::InitStatus FGXMLAutopilotGroupImplementation::incrementalInit()
105 {
106   init();
107   return INIT_DONE;
108 }
109
110 void FGXMLAutopilotGroupImplementation::init()
111 {
112     initFrom( fgGetNode( "/sim/systems" ), _nodeName.c_str() );
113
114     SGSubsystemGroup::bind();
115     SGSubsystemGroup::init();
116 }
117
118 void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, const char * childName )
119 {
120     if( rootNode == NULL )
121         return;
122
123     BOOST_FOREACH( SGPropertyNode_ptr autopilotNode, rootNode->getChildren(childName) ) {
124         SGPropertyNode_ptr pathNode = autopilotNode->getNode( "path" );
125         if( pathNode == NULL ) {
126             SG_LOG( SG_ALL, SG_WARN, "No configuration file specified for this property-rule!");
127             continue;
128         }
129
130         string apName;
131         SGPropertyNode_ptr nameNode = autopilotNode->getNode( "name" );
132         if( nameNode != NULL ) {
133             apName = nameNode->getStringValue();
134         } else {
135           std::ostringstream buf;
136           buf <<  "unnamed_autopilot_" << autopilotNode->getIndex();
137           apName = buf.str();
138         }
139
140         {
141           // check for duplicate names
142           string name = apName;
143           for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) {
144               std::ostringstream buf;
145               buf <<  name << "_" << i;
146               apName = buf.str();
147           }
148           if( apName != name )
149             SG_LOG( SG_ALL, SG_WARN, "Duplicate property-rule configuration name " << name << ", renamed to " << apName );
150         }
151
152         addAutopilotFromFile( apName, autopilotNode, pathNode->getStringValue() );
153     }
154 }
155
156 void FGXMLAutopilotGroup::addAutopilotFromFile( const std::string & name, SGPropertyNode_ptr apNode, const char * path )
157 {
158     SGPath config = globals->resolve_maybe_aircraft_path(path);
159     if (config.isNull())
160     {
161         SG_LOG( SG_ALL, SG_ALERT, "Cannot find property-rule configuration file '" << path << "'." );
162         return;
163     }
164     SG_LOG( SG_ALL, SG_INFO, "Reading property-rule configuration from " << config.str() );
165
166     try {
167         SGPropertyNode_ptr configNode = new SGPropertyNode();
168         readProperties( config.str(), configNode );
169
170         SG_LOG( SG_AUTOPILOT, SG_INFO, "adding  property-rule subsystem " << name );
171         addAutopilot( name, apNode, configNode );
172
173     } catch (const sg_exception& e) {
174         SG_LOG( SG_AUTOPILOT, SG_ALERT, "Failed to load property-rule configuration: "
175             << config.str() << ":" << e.getMessage() );
176         return;
177     }
178 }
179
180 FGXMLAutopilotGroup * FGXMLAutopilotGroup::createInstance(const std::string& nodeName)
181 {
182     return new FGXMLAutopilotGroupImplementation(nodeName);
183 }