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>
52 class FGXMLAutoInput : public SGReferenced {
54 double value; // The value as a constant or initializer for the property
55 SGPropertyNode_ptr property; // The name of the property containing the value
56 SGSharedPtr<FGXMLAutoInput> offset; // A fixed offset, defaults to zero
57 SGSharedPtr<FGXMLAutoInput> scale; // A constant scaling factor defaults to one
58 SGSharedPtr<FGXMLAutoInput> min; // A minimum clip defaults to no clipping
59 SGSharedPtr<FGXMLAutoInput> max; // A maximum clip defaults to no clipping
60 SGSharedPtr<const SGCondition> _condition;
63 FGXMLAutoInput( SGPropertyNode_ptr node = NULL, double value = 0.0, double offset = 0.0, double scale = 1.0 ) :
71 parse( node, value, offset, scale );
74 void parse( SGPropertyNode_ptr, double value = 0.0, double offset = 0.0, double scale = 1.0 );
76 /* get the value of this input, apply scale and offset and clipping */
79 /* set the input value after applying offset and scale */
80 void set_value( double value );
82 inline double get_scale() {
83 return scale == NULL ? 1.0 : scale->get_value();
86 inline double get_offset() {
87 return offset == NULL ? 0.0 : offset->get_value();
90 inline bool is_enabled() {
91 return _condition == NULL ? true : _condition->test();
96 class FGXMLAutoInputList : public vector<SGSharedPtr<FGXMLAutoInput> > {
98 FGXMLAutoInput * get_active() {
99 for (iterator it = begin(); it != end(); ++it) {
100 if( (*it)->is_enabled() )
106 double get_value( double def = 0.0 ) {
107 FGXMLAutoInput * input = get_active();
108 return input == NULL ? def : input->get_value();
114 * Base class for other autopilot components
117 class FGXMLAutoComponent : public SGReferenced {
120 vector <SGPropertyNode_ptr> output_list;
122 SGSharedPtr<const SGCondition> _condition;
123 SGPropertyNode_ptr enable_prop;
124 string * enable_value;
126 SGPropertyNode_ptr passive_mode;
131 /* Feed back output property to input property if
132 this filter is disabled. This is for multi-stage
133 filter where one filter sits behind a pid-controller
134 to provide changes of the overall output to the pid-
136 feedback is disabled by default.
138 bool feedback_if_disabled;
139 void do_feedback_if_disabled();
143 FGXMLAutoInputList valueInput;
144 FGXMLAutoInputList referenceInput;
145 FGXMLAutoInputList uminInput;
146 FGXMLAutoInputList umaxInput;
152 inline void do_feedback() {
153 if( feedback_if_disabled ) do_feedback_if_disabled();
158 FGXMLAutoComponent( SGPropertyNode *node);
159 virtual ~FGXMLAutoComponent();
161 virtual void update (double dt)=0;
163 inline const string& get_name() { return name; }
165 double clamp( double value );
167 inline void set_output_value( double value ) {
168 // passive_ignore == true means that we go through all the
169 // motions, but drive the outputs. This is analogous to
170 // running the autopilot with the "servos" off. This is
171 // helpful for things like flight directors which position
172 // their vbars from the autopilot computations.
173 if ( honor_passive && passive_mode->getBoolValue() ) return;
174 for( vector <SGPropertyNode_ptr>::iterator it = output_list.begin(); it != output_list.end(); ++it)
175 (*it)->setDoubleValue( clamp( value ) );
178 inline double get_output_value() {
179 return output_list.size() == 0 ? 0.0 : clamp(output_list[0]->getDoubleValue());
183 Returns true if the enable-condition is true.
185 If a <condition> is defined, this condition is evaluated,
186 <prop> and <value> tags are ignored.
188 If a <prop> is defined and no <value> is defined, the property
189 named in the <prop></prop> tags is evaluated as boolean.
191 If a <prop> is defined a a <value> is defined, the property named
192 in <prop></prop> is compared (as a string) to the value defined in
195 Returns true, if neither <condition> nor <prop> exists
198 Using a <condition> tag
201 <!-- any legal condition goes here and is evaluated -->
203 <prop>This is ignored</prop>
204 <value>This is also ignored</value>
207 Using a single boolean property
209 <prop>/some/property/that/is/evaluated/as/boolean</prop>
212 Using <prop> == <value>
213 This is the old style behaviour
215 <prop>/only/true/if/this/equals/true</prop>
219 bool isPropertyEnabled();
224 * Roy Ovesen's PID controller
227 class FGPIDController : public FGXMLAutoComponent {
232 // Configuration values
233 FGXMLAutoInputList Kp; // proportional gain
234 FGXMLAutoInputList Ti; // Integrator time (sec)
235 FGXMLAutoInputList Td; // Derivator time (sec)
237 double alpha; // low pass filter weighing factor (usually 0.1)
238 double beta; // process value weighing factor for
239 // calculating proportional error
241 double gamma; // process value weighing factor for
242 // calculating derivative error
245 // Previous state tracking values
246 double ep_n_1; // ep[n-1] (prop error)
247 double edf_n_1; // edf[n-1] (derivative error)
248 double edf_n_2; // edf[n-2] (derivative error)
249 double u_n_1; // u[n-1] (output)
250 double desiredTs; // desired sampling interval (sec)
251 double elapsedTime; // elapsed time (sec)
257 FGPIDController( SGPropertyNode *node );
258 FGPIDController( SGPropertyNode *node, bool old );
259 ~FGPIDController() {}
261 void update( double dt );
266 * A simplistic P [ + I ] PID controller
269 class FGPISimpleController : public FGXMLAutoComponent {
273 // proportional component data
274 FGXMLAutoInputList Kp;
276 // integral component data
277 FGXMLAutoInputList Ki;
283 FGPISimpleController( SGPropertyNode *node );
284 ~FGPISimpleController() {}
286 void update( double dt );
291 * Predictor - calculates value in x seconds future.
294 class FGPredictor : public FGXMLAutoComponent {
298 FGXMLAutoInputList seconds;
299 FGXMLAutoInputList filter_gain;
302 FGPredictor( SGPropertyNode *node );
305 void update( double dt );
310 * FGDigitalFilter - a selection of digital filters
313 * Double exponential filter
314 * Moving average filter
317 * All these filters are low-pass filters.
321 class FGDigitalFilter : public FGXMLAutoComponent
324 FGXMLAutoInputList samplesInput; // Number of input samples to average
325 FGXMLAutoInputList rateOfChangeInput; // The maximum allowable rate of change [1/s]
326 FGXMLAutoInputList gainInput; //
327 FGXMLAutoInputList TfInput; // Filter time [s]
329 deque <double> output;
330 deque <double> input;
331 enum filterTypes { exponential, doubleExponential, movingAverage,
332 noiseSpike, gain, reciprocal, none };
333 filterTypes filterType;
336 FGDigitalFilter(SGPropertyNode *node);
337 ~FGDigitalFilter() {}
339 void update(double dt);
343 * Model an autopilot system.
347 class FGXMLAutopilot : public SGSubsystem
359 void update( double dt );
365 typedef vector<SGSharedPtr<FGXMLAutoComponent> > comp_list;
370 SGPropertyNode_ptr config_props;
371 comp_list components;
375 #endif // _XMLAUTO_HXX