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