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 : public SGReferenced {
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
114 SGPropertyNode_ptr Kp_prop;
116 double alpha; // low pass filter weighing factor (usually 0.1)
117 double beta; // process value weighing factor for
118 // calculating proportional error
120 double gamma; // process value weighing factor for
121 // calculating derivative error
124 double Ti; // Integrator time (sec)
125 double Td; // Derivator time (sec)
127 double u_min; // Minimum output clamp
128 double u_max; // Maximum output clamp
130 // Previous state tracking values
131 double ep_n_1; // ep[n-1] (prop error)
132 double edf_n_1; // edf[n-1] (derivative error)
133 double edf_n_2; // edf[n-2] (derivative error)
134 double u_n_1; // u[n-1] (output)
135 double desiredTs; // desired sampling interval (sec)
136 double elapsedTime; // elapsed time (sec)
142 FGPIDController( SGPropertyNode *node );
143 FGPIDController( SGPropertyNode *node, bool old );
144 ~FGPIDController() {}
146 void update_old( double dt );
147 void update( double dt );
152 * A simplistic P [ + I ] PID controller
155 class FGPISimpleController : public FGXMLAutoComponent {
159 // proportional component data
162 SGPropertyNode_ptr offset_prop;
165 // integral component data
170 // post functions for output
177 double y_n; // measured process value
178 double r_n; // reference (set point) value
179 double y_scale; // scale process input from property system
180 double r_scale; // scale reference input from property system
182 double u_min; // Minimum output clamp
183 double u_max; // Maximum output clamp
188 FGPISimpleController( SGPropertyNode *node );
189 ~FGPISimpleController() {}
191 void update( double dt );
196 * Predictor - calculates value in x seconds future.
199 class FGPredictor : public FGXMLAutoComponent {
203 // proportional component data
213 double ivalue; // input value
217 FGPredictor( SGPropertyNode *node );
220 void update( double dt );
225 * FGDigitalFilter - a selection of digital filters
228 * Double exponential filter
229 * Moving average filter
232 * All these filters are low-pass filters.
236 class FGDigitalFilter : public FGXMLAutoComponent
239 double Tf; // Filter time [s]
240 unsigned int samples; // Number of input samples to average
241 double rateOfChange; // The maximum allowable rate of change [1/s]
243 double output_min_clamp;
244 double output_max_clamp;
245 SGPropertyNode_ptr gain_prop;
247 deque <double> output;
248 deque <double> input;
249 enum filterTypes { exponential, doubleExponential, movingAverage,
250 noiseSpike, gain, reciprocal };
251 filterTypes filterType;
256 FGDigitalFilter(SGPropertyNode *node);
257 ~FGDigitalFilter() {}
259 void update(double dt);
263 * Model an autopilot system.
267 class FGXMLAutopilot : public SGSubsystem
279 void update( double dt );
285 typedef vector<SGSharedPtr<FGXMLAutoComponent> > comp_list;
290 SGPropertyNode_ptr config_props;
291 comp_list components;
295 #endif // _XMLAUTO_HXX