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., 675 Mass Ave, Cambridge, MA 02139, 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>
50 * Base class for other autopilot components
53 class FGXMLAutoComponent {
59 SGPropertyNode *enable_prop;
63 SGPropertyNode *input_prop;
64 SGPropertyNode *r_n_prop;
66 vector <SGPropertyNode *> output_list;
70 FGXMLAutoComponent() :
79 virtual ~FGXMLAutoComponent() {}
81 virtual void update (double dt) {}
83 inline string get_name() { return name; }
88 * Roy Ovesen's PID controller
91 class FGPIDController : public FGXMLAutoComponent {
99 double y_n; // measured process value
100 double r_n; // reference (set point) value
101 double y_scale; // scale process input from property system
102 double r_scale; // scale reference input from property system
106 // Configuration values
107 double Kp; // proportional gain
109 double alpha; // low pass filter weighing factor (usually 0.1)
110 double beta; // process value weighing factor for
111 // calculating proportional error
113 double gamma; // process value weighing factor for
114 // calculating derivative error
117 double Ti; // Integrator time (sec)
118 double Td; // Derivator time (sec)
120 double u_min; // Minimum output clamp
121 double u_max; // Maximum output clamp
123 // Previous state tracking values
124 double ep_n_1; // ep[n-1] (prop error)
125 double edf_n_1; // edf[n-1] (derivative error)
126 double edf_n_2; // edf[n-2] (derivative error)
127 double u_n_1; // u[n-1] (output)
128 double desiredTs; // desired sampling interval (sec)
129 double elapsedTime; // elapsed time (sec)
135 FGPIDController( SGPropertyNode *node );
136 FGPIDController( SGPropertyNode *node, bool old );
137 ~FGPIDController() {}
139 void update_old( double dt );
140 void update( double dt );
145 * A simplistic P [ + I ] PID controller
148 class FGPISimpleController : public FGXMLAutoComponent {
152 // proportional component data
155 SGPropertyNode *offset_prop;
158 // integral component data
163 // post functions for output
170 double y_n; // measured process value
171 double r_n; // reference (set point) value
172 double y_scale; // scale process input from property system
173 double r_scale; // scale reference input from property system
175 double u_min; // Minimum output clamp
176 double u_max; // Maximum output clamp
181 FGPISimpleController( SGPropertyNode *node );
182 ~FGPISimpleController() {}
184 void update( double dt );
189 * Predictor - calculates value in x seconds future.
192 class FGPredictor : public FGXMLAutoComponent {
196 // proportional component data
206 double ivalue; // input value
210 FGPredictor( SGPropertyNode *node );
213 void update( double dt );
218 * FGDigitalFilter - a selection of digital filters
221 * Double exponential filter
222 * Moving average filter
225 * All these filters are low-pass filters.
229 class FGDigitalFilter : public FGXMLAutoComponent
232 double Tf; // Filter time [s]
233 unsigned int samples; // Number of input samples to average
234 double rateOfChange; // The maximum allowable rate of change [1/s]
235 deque <double> output;
236 deque <double> input;
237 enum filterTypes { exponential, doubleExponential, movingAverage, noiseSpike };
238 filterTypes filterType;
243 FGDigitalFilter(SGPropertyNode *node);
244 ~FGDigitalFilter() {}
246 void update(double dt);
250 * Model an autopilot system.
254 class FGXMLAutopilot : public SGSubsystem
266 void update( double dt );
272 typedef vector<FGXMLAutoComponent *> comp_list;
277 SGPropertyNode *config_props;
278 comp_list components;
282 #endif // _XMLAUTO_HXX