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>
47 #include <simgear/props/condition.hxx>
49 #include <Main/fg_props.hxx>
53 * Base class for other autopilot components
56 class FGXMLAutoComponent : public SGReferenced {
62 SGPropertyNode_ptr enable_prop;
63 SGPropertyNode_ptr passive_mode;
68 SGPropertyNode_ptr input_prop;
69 SGPropertyNode_ptr r_n_prop;
71 vector <SGPropertyNode_ptr> output_list;
72 SGSharedPtr<const SGCondition> _condition;
76 FGXMLAutoComponent() :
78 passive_mode( fgGetNode("/autopilot/locks/passive-mode", true) ),
80 honor_passive( false ),
88 virtual ~FGXMLAutoComponent() {}
90 virtual void update (double dt)=0;
92 inline const string& get_name() { return name; }
97 * Roy Ovesen's PID controller
100 class FGPIDController : public FGXMLAutoComponent {
108 double y_n; // measured process value
109 double r_n; // reference (set point) value
110 double y_scale; // scale process input from property system
111 double r_scale; // scale reference input from property system
115 // Configuration values
116 double Kp; // proportional gain
117 SGPropertyNode_ptr Kp_prop;
119 double alpha; // low pass filter weighing factor (usually 0.1)
120 double beta; // process value weighing factor for
121 // calculating proportional error
123 double gamma; // process value weighing factor for
124 // calculating derivative error
127 double Ti; // Integrator time (sec)
128 SGPropertyNode_ptr Ti_prop;
129 double Td; // Derivator time (sec)
130 SGPropertyNode_ptr Td_prop;
131 double u_min; // Minimum output clamp
132 SGPropertyNode_ptr umin_prop;
133 double u_max; // Maximum output clamp
134 SGPropertyNode_ptr umax_prop;
136 // Previous state tracking values
137 double ep_n_1; // ep[n-1] (prop error)
138 double edf_n_1; // edf[n-1] (derivative error)
139 double edf_n_2; // edf[n-2] (derivative error)
140 double u_n_1; // u[n-1] (output)
141 double desiredTs; // desired sampling interval (sec)
142 double elapsedTime; // elapsed time (sec)
148 FGPIDController( SGPropertyNode *node );
149 FGPIDController( SGPropertyNode *node, bool old );
150 ~FGPIDController() {}
152 void update_old( double dt );
153 void update( double dt );
158 * A simplistic P [ + I ] PID controller
161 class FGPISimpleController : public FGXMLAutoComponent {
165 // proportional component data
168 SGPropertyNode_ptr Kp_prop;
169 SGPropertyNode_ptr offset_prop;
172 // integral component data
177 // post functions for output
184 double y_n; // measured process value
185 double r_n; // reference (set point) value
186 double y_scale; // scale process input from property system
187 double r_scale; // scale reference input from property system
189 double u_min; // Minimum output clamp
190 SGPropertyNode_ptr umin_prop;
191 double u_max; // Maximum output clamp
192 SGPropertyNode_ptr umax_prop;
197 FGPISimpleController( SGPropertyNode *node );
198 ~FGPISimpleController() {}
200 void update( double dt );
205 * Predictor - calculates value in x seconds future.
208 class FGPredictor : public FGXMLAutoComponent {
212 // proportional component data
222 double ivalue; // input value
226 FGPredictor( SGPropertyNode *node );
229 void update( double dt );
234 * FGDigitalFilter - a selection of digital filters
237 * Double exponential filter
238 * Moving average filter
241 * All these filters are low-pass filters.
245 class FGDigitalFilter : public FGXMLAutoComponent
248 double Tf; // Filter time [s]
249 unsigned int samples; // Number of input samples to average
250 double rateOfChange; // The maximum allowable rate of change [1/s]
252 double output_min_clamp;
253 double output_max_clamp;
254 SGPropertyNode_ptr gain_prop;
256 deque <double> output;
257 deque <double> input;
258 enum filterTypes { exponential, doubleExponential, movingAverage,
259 noiseSpike, gain, reciprocal };
260 filterTypes filterType;
265 FGDigitalFilter(SGPropertyNode *node);
266 ~FGDigitalFilter() {}
268 void update(double dt);
272 * Model an autopilot system.
276 class FGXMLAutopilot : public SGSubsystem
288 void update( double dt );
294 typedef vector<SGSharedPtr<FGXMLAutoComponent> > comp_list;
299 SGPropertyNode_ptr config_props;
300 comp_list components;
304 #endif // _XMLAUTO_HXX