]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/xmlauto.hxx
1e840d82446d65d8e90d148120d6cb0273e82996
[flightgear.git] / src / Autopilot / xmlauto.hxx
1 // xmlauto.hxx - a more flexible, generic way to build autopilots
2 //
3 // Written by Curtis Olson, started January 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _XMLAUTO_HXX
25 #define _XMLAUTO_HXX 1
26
27 #ifndef __cplusplus
28 # error This library requires C++
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 #  include <config.h>
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include STL_STRING
38 #include <vector>
39
40 SG_USING_STD(string);
41 SG_USING_STD(vector);
42
43 #include <simgear/props/props.hxx>
44 #include <simgear/structure/subsystem_mgr.hxx>
45
46
47 /**
48  * Base class for other autopilot components
49  */
50
51 class FGXMLAutoComponent {
52
53 protected:
54
55     string name;
56
57     SGPropertyNode *enable_prop;
58     string enable_value;
59     bool enabled;
60
61     SGPropertyNode *input_prop;
62     SGPropertyNode *r_n_prop;
63     double r_n_value;
64     vector <SGPropertyNode *> output_list;
65
66 public:
67
68     FGXMLAutoComponent() :
69       enable_prop( NULL ),
70       enable_value( "" ),
71       enabled( false ),
72       input_prop( NULL ),
73       r_n_prop( NULL ),
74       r_n_value( 0.0 )
75     { }
76
77     virtual ~FGXMLAutoComponent() {}
78
79     virtual void update (double dt) {}
80     
81     inline string get_name() { return name; }
82 };
83
84
85 /**
86  * A simple proportional controler
87  */
88
89 class FGPIDController : public FGXMLAutoComponent {
90
91 private:
92
93     // proportional component data
94     bool proportional;
95     double factor;
96     SGPropertyNode *offset_prop;
97     double offset_value;
98
99     // integral component data
100     bool integral;
101     double gain;
102     double int_sum;
103
104     // prep functions for error term
105     bool one_eighty;
106
107     // post functions for output
108     bool clamp;
109
110     // debug flag
111     bool debug;
112
113     // Input values
114     double y_n;                 // measured process value
115     double r_n;                 // reference (set point) value
116
117     // Configuration values
118     double Kp;                  // proportional gain
119
120     double alpha;               // low pass filter weighing factor (usually 0.1)
121     double beta;                // process value weighing factor for
122                                 // calculating proportional error
123                                 // (usually 1.0)
124     double gamma;               // process value weighing factor for
125                                 // calculating derivative error
126                                 // (usually 0.0)
127
128     double Ti;                  // Integrator time (sec)
129     double Td;                  // Derivator time (sec)
130
131     double u_min;               // Minimum output clamp
132     double u_max;               // Maximum output clamp
133
134     // Previous state tracking values
135     double ep_n_1;              // ep[n-1]  (prop error)
136     double edf_n_1;             // edf[n-1] (derivative error)
137     double edf_n_2;             // edf[n-2] (derivative error)
138     double u_n_1;               // u[n-1]   (output)
139     
140     
141     
142 public:
143
144     FGPIDController( SGPropertyNode *node );
145     FGPIDController( SGPropertyNode *node, bool old );
146     ~FGPIDController() {}
147
148     void update_old( double dt );
149     void update( double dt );
150 };
151
152
153 /**
154  * Model an autopilot system.
155  * 
156  */
157
158 class FGXMLAutopilot : public SGSubsystem
159 {
160
161 public:
162
163     FGXMLAutopilot();
164     ~FGXMLAutopilot();
165
166     void init();
167     void reinit();
168     void bind();
169     void unbind();
170     void update( double dt );
171
172     bool build();
173
174 protected:
175
176     typedef vector<FGXMLAutoComponent *> comp_list;
177
178 private:
179
180     bool serviceable;
181     SGPropertyNode *config_props;
182     comp_list components;
183 };
184
185
186 #endif // _XMLAUTO_HXX