]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/xmlauto.hxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[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
100     // Configuration values
101     double Kp;                  // proportional gain
102
103     double alpha;               // low pass filter weighing factor (usually 0.1)
104     double beta;                // process value weighing factor for
105                                 // calculating proportional error
106                                 // (usually 1.0)
107     double gamma;               // process value weighing factor for
108                                 // calculating derivative error
109                                 // (usually 0.0)
110
111     double Ti;                  // Integrator time (sec)
112     double Td;                  // Derivator time (sec)
113
114     double u_min;               // Minimum output clamp
115     double u_max;               // Maximum output clamp
116
117     // Previous state tracking values
118     double ep_n_1;              // ep[n-1]  (prop error)
119     double edf_n_1;             // edf[n-1] (derivative error)
120     double edf_n_2;             // edf[n-2] (derivative error)
121     double u_n_1;               // u[n-1]   (output)
122     
123     
124     
125 public:
126
127     FGPIDController( SGPropertyNode *node );
128     FGPIDController( SGPropertyNode *node, bool old );
129     ~FGPIDController() {}
130
131     void update_old( double dt );
132     void update( double dt );
133 };
134
135
136 /**
137  * A simplistic P [ + I ] PID controller
138  */
139
140 class FGPISimpleController : public FGXMLAutoComponent {
141
142 private:
143
144     // proportional component data
145     bool proportional;
146     double Kp;
147     SGPropertyNode *offset_prop;
148     double offset_value;
149
150     // integral component data
151     bool integral;
152     double Ki;
153     double int_sum;
154
155     // post functions for output
156     bool clamp;
157
158     // debug flag
159     bool debug;
160
161     // Input values
162     double y_n;                 // measured process value
163     double r_n;                 // reference (set point) value
164
165     double u_min;               // Minimum output clamp
166     double u_max;               // Maximum output clamp
167
168     
169 public:
170
171     FGPISimpleController( SGPropertyNode *node );
172     ~FGPISimpleController() {}
173
174     void update( double dt );
175 };
176
177
178 /**
179  * Model an autopilot system.
180  * 
181  */
182
183 class FGXMLAutopilot : public SGSubsystem
184 {
185
186 public:
187
188     FGXMLAutopilot();
189     ~FGXMLAutopilot();
190
191     void init();
192     void reinit();
193     void bind();
194     void unbind();
195     void update( double dt );
196
197     bool build();
198
199 protected:
200
201     typedef vector<FGXMLAutoComponent *> comp_list;
202
203 private:
204
205     bool serviceable;
206     SGPropertyNode *config_props;
207     comp_list components;
208 };
209
210
211 #endif // _XMLAUTO_HXX