]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/autopilotgroup.cxx
Merge branch 'next' of http://git.gitorious.org/fg/flightgear into next
[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/structure/subsystem_mgr.hxx>
35 #include <simgear/structure/exception.hxx>
36 #include <Main/fg_props.hxx>
37
38
39 using simgear::PropertyList;
40
41 class FGXMLAutopilotGroupImplementation : public FGXMLAutopilotGroup
42 {
43 public:
44     void init();
45     void reinit();
46     void update( double dt );
47 private:
48     void initFrom( SGPropertyNode_ptr rootNode, const char * childName );
49     vector<string> _autopilotNames;
50
51 };
52
53 void FGXMLAutopilotGroupImplementation::update( double dt )
54 {
55     // update all configured autopilots
56     SGSubsystemGroup::update( dt );
57 }
58
59 void FGXMLAutopilotGroupImplementation::reinit()
60 {
61     SGSubsystemGroup::unbind();
62
63     for( vector<string>::size_type i = 0; i < _autopilotNames.size(); i++ ) {
64       FGXMLAutopilot::Autopilot * ap = (FGXMLAutopilot::Autopilot*)get_subsystem( _autopilotNames[i] );
65       if( ap == NULL ) continue; // ?
66       remove_subsystem( _autopilotNames[i] );
67       delete ap;
68     }
69     _autopilotNames.clear();
70     init();
71 }
72
73 void FGXMLAutopilotGroupImplementation::init()
74 {
75     static const char * nodeNames[] = {
76         "autopilot",
77         "property-rule"
78     };
79     for( unsigned i = 0; i < sizeof(nodeNames)/sizeof(nodeNames[0]); i++ )
80         initFrom( fgGetNode( "/sim/systems" ), nodeNames[i] );
81
82     SGSubsystemGroup::bind();
83     SGSubsystemGroup::init();
84 }
85
86 void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode, const char * childName )
87 {
88     if( rootNode == NULL )
89         return;
90
91     PropertyList autopilotNodes = rootNode->getChildren(childName);
92     for( PropertyList::size_type i = 0; i < autopilotNodes.size(); i++ ) {
93         SGPropertyNode_ptr pathNode = autopilotNodes[i]->getNode( "path" );
94         if( pathNode == NULL ) {
95             SG_LOG( SG_ALL, SG_WARN, "No configuration file specified for this property-rule!");
96             continue;
97         }
98
99         string apName;
100         SGPropertyNode_ptr nameNode = autopilotNodes[i]->getNode( "name" );
101         if( nameNode != NULL ) {
102             apName = nameNode->getStringValue();
103         } else {
104           std::ostringstream buf;
105           buf <<  "unnamed_autopilot_" << i;
106           apName = buf.str();
107         }
108
109         {
110           // check for duplicate names
111           string name = apName;
112           for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) {
113               ostringstream buf;
114               buf <<  name << "_" << i;
115               apName = buf.str();
116           }
117           if( apName != name )
118             SG_LOG( SG_ALL, SG_WARN, "Duplicate property-rule configuration name " << name << ", renamed to " << apName );
119         }
120
121         SGPath config = globals->resolve_maybe_aircraft_path(pathNode->getStringValue());
122
123         SG_LOG( SG_ALL, SG_INFO, "Reading property-rule configuration from " << config.str() );
124
125         try {
126             SGPropertyNode_ptr root = new SGPropertyNode();
127             readProperties( config.str(), root );
128
129             SG_LOG( SG_AUTOPILOT, SG_INFO, "adding  property-rule subsystem " << apName );
130             FGXMLAutopilot::Autopilot * ap = new FGXMLAutopilot::Autopilot( autopilotNodes[i], root );
131             ap->set_name( apName );
132             set_subsystem( apName, ap );
133             _autopilotNames.push_back( apName );
134
135         } catch (const sg_exception& e) {
136             SG_LOG( SG_AUTOPILOT, SG_ALERT, "Failed to load property-rule configuration: "
137                     << config.str() << ":" << e.getMessage() );
138             continue;
139         }
140     }
141 }
142
143 FGXMLAutopilotGroup * FGXMLAutopilotGroup::createInstance()
144 {
145     return new FGXMLAutopilotGroupImplementation();
146 }