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 SGPropertyNode_ptr Ti_prop;
126 double Td; // Derivator time (sec)
127 SGPropertyNode_ptr Td_prop;
128 double u_min; // Minimum output clamp
129 SGPropertyNode_ptr umin_prop;
130 double u_max; // Maximum output clamp
131 SGPropertyNode_ptr umax_prop;
133 // Previous state tracking values
134 double ep_n_1; // ep[n-1] (prop error)
135 double edf_n_1; // edf[n-1] (derivative error)
136 double edf_n_2; // edf[n-2] (derivative error)
137 double u_n_1; // u[n-1] (output)
138 double desiredTs; // desired sampling interval (sec)
139 double elapsedTime; // elapsed time (sec)
145 FGPIDController( SGPropertyNode *node );
146 FGPIDController( SGPropertyNode *node, bool old );
147 ~FGPIDController() {}
149 void update_old( double dt );
150 void update( double dt );
155 * A simplistic P [ + I ] PID controller
158 class FGPISimpleController : public FGXMLAutoComponent {
162 // proportional component data
165 SGPropertyNode_ptr Kp_prop;
166 SGPropertyNode_ptr offset_prop;
169 // integral component data
174 // post functions for output
181 double y_n; // measured process value
182 double r_n; // reference (set point) value
183 double y_scale; // scale process input from property system
184 double r_scale; // scale reference input from property system
186 double u_min; // Minimum output clamp
187 SGPropertyNode_ptr umin_prop;
188 double u_max; // Maximum output clamp
189 SGPropertyNode_ptr umax_prop;
194 FGPISimpleController( SGPropertyNode *node );
195 ~FGPISimpleController() {}
197 void update( double dt );
202 * Predictor - calculates value in x seconds future.
205 class FGPredictor : public FGXMLAutoComponent {
209 // proportional component data
219 double ivalue; // input value
223 FGPredictor( SGPropertyNode *node );
226 void update( double dt );
231 * FGDigitalFilter - a selection of digital filters
234 * Double exponential filter
235 * Moving average filter
238 * All these filters are low-pass filters.
242 class FGDigitalFilter : public FGXMLAutoComponent
245 double Tf; // Filter time [s]
246 unsigned int samples; // Number of input samples to average
247 double rateOfChange; // The maximum allowable rate of change [1/s]
249 double output_min_clamp;
250 double output_max_clamp;
251 SGPropertyNode_ptr gain_prop;
253 deque <double> output;
254 deque <double> input;
255 enum filterTypes { exponential, doubleExponential, movingAverage,
256 noiseSpike, gain, reciprocal };
257 filterTypes filterType;
262 FGDigitalFilter(SGPropertyNode *node);
263 ~FGDigitalFilter() {}
265 void update(double dt);
269 * Model an autopilot system.
273 class FGXMLAutopilot : public SGSubsystem
285 void update( double dt );
291 typedef vector<SGSharedPtr<FGXMLAutoComponent> > comp_list;
296 SGPropertyNode_ptr config_props;
297 comp_list components;
301 #endif // _XMLAUTO_HXX