]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/predictor.cxx
eece5cf37e4960fa3cc3b4649f2a29f419f1e3ae
[flightgear.git] / src / Autopilot / predictor.cxx
1 // predictor.cxx - predict future values
2 //
3 // Written by Torsten Dreyer
4 // Based heavily on work created by Curtis Olson, started January 2004.
5 //
6 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
7 // Copyright (C) 2010  Torsten Dreyer - Torsten (at) t3r (dot) de
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23
24 #include "predictor.hxx"
25
26 #ifdef SG_BULK
27 #undef SG_BULK
28 #endif
29 #define SG_BULK SG_ALERT
30 #ifdef SG_INFO
31 #undef SG_INFO
32 #endif
33 #define SG_INFO SG_ALERT
34
35 using namespace FGXMLAutopilot;
36
37 Predictor::Predictor () :
38     AnalogComponent(),
39     _average(0.0)
40 {
41 }
42
43 bool Predictor::configure(const string& nodeName, SGPropertyNode_ptr configNode)
44 {
45   SG_LOG( SG_AUTOPILOT, SG_BULK, "Predictor::configure(" << nodeName << ")" << endl );
46
47   if( AnalogComponent::configure( nodeName, configNode ) )
48     return true;
49
50   if( nodeName == "config" ) {
51     Component::configure( configNode );
52     return true;
53   }
54   
55   if (nodeName == "seconds") {
56     _seconds.push_back( new InputValue( configNode, 0 ) );
57     return true;
58   } 
59
60   if (nodeName == "filter-gain") {
61     _filter_gain.push_back( new InputValue( configNode, 0 ) );
62     return true;
63   }
64
65   SG_LOG( SG_AUTOPILOT, SG_BULK, "Predictor::configure(" << nodeName << ") [unhandled]" << endl );
66   return false;
67 }
68
69 void Predictor::update( bool firstTime, double dt ) 
70 {
71     double ivalue = _valueInput.get_value();
72
73     if ( firstTime ) {
74         _last_value = ivalue;
75     }
76
77     double current = (ivalue - _last_value)/dt; // calculate current error change (per second)
78     _average = dt < 1.0 ? ((1.0 - dt) * _average + current * dt) : current;
79
80     // calculate output with filter gain adjustment
81     double output = ivalue + 
82            (1.0 - _filter_gain.get_value()) * (_average * _seconds.get_value()) + 
83            _filter_gain.get_value() * (current * _seconds.get_value());
84     output = clamp( output );
85     set_output_value( output );
86
87     _last_value = ivalue;
88 }
89
90