]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/xmlauto.hxx
Fix a numeric_limits problem for older stdc++ libraries.
[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  * Roy Ovesen's PID controller
87  */
88
89 class FGPIDController : public FGXMLAutoComponent {
90
91 private:
92
93     // debug flag
94     bool debug;
95
96     // Input values
97     double y_n;                 // measured process value
98     double r_n;                 // reference (set point) value
99     double y_scale;             // scale process input from property system
100     double r_scale;             // scale reference input from property system
101
102     // Configuration values
103     double Kp;                  // proportional gain
104
105     double alpha;               // low pass filter weighing factor (usually 0.1)
106     double beta;                // process value weighing factor for
107                                 // calculating proportional error
108                                 // (usually 1.0)
109     double gamma;               // process value weighing factor for
110                                 // calculating derivative error
111                                 // (usually 0.0)
112
113     double Ti;                  // Integrator time (sec)
114     double Td;                  // Derivator time (sec)
115
116     double u_min;               // Minimum output clamp
117     double u_max;               // Maximum output clamp
118
119     // Previous state tracking values
120     double ep_n_1;              // ep[n-1]  (prop error)
121     double edf_n_1;             // edf[n-1] (derivative error)
122     double edf_n_2;             // edf[n-2] (derivative error)
123     double u_n_1;               // u[n-1]   (output)
124     
125     
126     
127 public:
128
129     FGPIDController( SGPropertyNode *node );
130     FGPIDController( SGPropertyNode *node, bool old );
131     ~FGPIDController() {}
132
133     void update_old( double dt );
134     void update( double dt );
135 };
136
137
138 /**
139  * A simplistic P [ + I ] PID controller
140  */
141
142 class FGPISimpleController : public FGXMLAutoComponent {
143
144 private:
145
146     // proportional component data
147     bool proportional;
148     double Kp;
149     SGPropertyNode *offset_prop;
150     double offset_value;
151
152     // integral component data
153     bool integral;
154     double Ki;
155     double int_sum;
156
157     // post functions for output
158     bool clamp;
159
160     // debug flag
161     bool debug;
162
163     // Input values
164     double y_n;                 // measured process value
165     double r_n;                 // reference (set point) value
166     double y_scale;             // scale process input from property system
167     double r_scale;             // scale reference input from property system
168
169     double u_min;               // Minimum output clamp
170     double u_max;               // Maximum output clamp
171
172     
173 public:
174
175     FGPISimpleController( SGPropertyNode *node );
176     ~FGPISimpleController() {}
177
178     void update( double dt );
179 };
180
181
182 /**
183  * Predictor - calculates value in x seconds future.
184  */
185
186 class FGPredictor : public FGXMLAutoComponent {
187
188 private:
189
190     // proportional component data
191     double last_value;
192     double average;
193     double seconds;
194     double filter_gain;
195
196     // debug flag
197     bool debug;
198
199     // Input values
200     double ivalue;                 // input value
201     
202 public:
203
204     FGPredictor( SGPropertyNode *node );
205     ~FGPredictor() {}
206
207     void update( double dt );
208 };
209
210
211 /**
212  * Model an autopilot system.
213  * 
214  */
215
216 class FGXMLAutopilot : public SGSubsystem
217 {
218
219 public:
220
221     FGXMLAutopilot();
222     ~FGXMLAutopilot();
223
224     void init();
225     void reinit();
226     void bind();
227     void unbind();
228     void update( double dt );
229
230     bool build();
231
232 protected:
233
234     typedef vector<FGXMLAutoComponent *> comp_list;
235
236 private:
237
238     bool serviceable;
239     SGPropertyNode *config_props;
240     comp_list components;
241 };
242
243
244 #endif // _XMLAUTO_HXX