1 // xmlauto.hxx - a more flexible, generic way to build autopilots
3 // Written by Curtis Olson, started January 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #define _XMLAUTO_HXX 1
28 # error This library requires C++
35 #include <simgear/compiler.h>
45 #include <simgear/props/props.hxx>
46 #include <simgear/structure/subsystem_mgr.hxx>
48 #include <Main/fg_props.hxx>
52 * Base class for other autopilot components
55 class FGXMLAutoComponent {
61 SGPropertyNode_ptr enable_prop;
62 SGPropertyNode_ptr passive_mode;
67 SGPropertyNode_ptr input_prop;
68 SGPropertyNode_ptr r_n_prop;
70 vector <SGPropertyNode_ptr> output_list;
74 FGXMLAutoComponent() :
76 passive_mode( fgGetNode("/autopilot/locks/passive-mode", true) ),
78 honor_passive( false ),
85 virtual ~FGXMLAutoComponent() {}
87 virtual void update (double dt)=0;
89 inline const string& get_name() { return name; }
94 * Roy Ovesen's PID controller
97 class FGPIDController : public FGXMLAutoComponent {
105 double y_n; // measured process value
106 double r_n; // reference (set point) value
107 double y_scale; // scale process input from property system
108 double r_scale; // scale reference input from property system
112 // Configuration values
113 double Kp; // proportional gain
115 double alpha; // low pass filter weighing factor (usually 0.1)
116 double beta; // process value weighing factor for
117 // calculating proportional error
119 double gamma; // process value weighing factor for
120 // calculating derivative error
123 double Ti; // Integrator time (sec)
124 double Td; // Derivator time (sec)
126 double u_min; // Minimum output clamp
127 double u_max; // Maximum output clamp
129 // Previous state tracking values
130 double ep_n_1; // ep[n-1] (prop error)
131 double edf_n_1; // edf[n-1] (derivative error)
132 double edf_n_2; // edf[n-2] (derivative error)
133 double u_n_1; // u[n-1] (output)
134 double desiredTs; // desired sampling interval (sec)
135 double elapsedTime; // elapsed time (sec)
141 FGPIDController( SGPropertyNode *node );
142 FGPIDController( SGPropertyNode *node, bool old );
143 ~FGPIDController() {}
145 void update_old( double dt );
146 void update( double dt );
151 * A simplistic P [ + I ] PID controller
154 class FGPISimpleController : public FGXMLAutoComponent {
158 // proportional component data
161 SGPropertyNode_ptr offset_prop;
164 // integral component data
169 // post functions for output
176 double y_n; // measured process value
177 double r_n; // reference (set point) value
178 double y_scale; // scale process input from property system
179 double r_scale; // scale reference input from property system
181 double u_min; // Minimum output clamp
182 double u_max; // Maximum output clamp
187 FGPISimpleController( SGPropertyNode *node );
188 ~FGPISimpleController() {}
190 void update( double dt );
195 * Predictor - calculates value in x seconds future.
198 class FGPredictor : public FGXMLAutoComponent {
202 // proportional component data
212 double ivalue; // input value
216 FGPredictor( SGPropertyNode *node );
219 void update( double dt );
224 * FGDigitalFilter - a selection of digital filters
227 * Double exponential filter
228 * Moving average filter
231 * All these filters are low-pass filters.
235 class FGDigitalFilter : public FGXMLAutoComponent
238 double Tf; // Filter time [s]
239 unsigned int samples; // Number of input samples to average
240 double rateOfChange; // The maximum allowable rate of change [1/s]
241 deque <double> output;
242 deque <double> input;
243 enum filterTypes { exponential, doubleExponential, movingAverage, noiseSpike };
244 filterTypes filterType;
249 FGDigitalFilter(SGPropertyNode *node);
250 ~FGDigitalFilter() {}
252 void update(double dt);
256 * Model an autopilot system.
260 class FGXMLAutopilot : public SGSubsystem
272 void update( double dt );
278 typedef vector<FGXMLAutoComponent *> comp_list;
283 SGPropertyNode_ptr config_props;
284 comp_list components;
288 #endif // _XMLAUTO_HXX