]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/predictor.cxx
Positioned/Cache tweaks to support PoIs.
[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 using namespace FGXMLAutopilot;
27
28 using std::endl;
29 using std::cout;
30
31 Predictor::Predictor () :
32     AnalogComponent(),
33     _average(0.0)
34 {
35 }
36
37 bool Predictor::configure(const string& nodeName, SGPropertyNode_ptr configNode)
38 {
39   SG_LOG( SG_AUTOPILOT, SG_BULK, "Predictor::configure(" << nodeName << ")" << endl );
40
41   if( AnalogComponent::configure( nodeName, configNode ) )
42     return true;
43
44   if( nodeName == "config" ) {
45     Component::configure( configNode );
46     return true;
47   }
48   
49   if (nodeName == "seconds") {
50     _seconds.push_back( new InputValue( configNode, 0 ) );
51     return true;
52   } 
53
54   if (nodeName == "filter-gain") {
55     _filter_gain.push_back( new InputValue( configNode, 0 ) );
56     return true;
57   }
58
59   SG_LOG( SG_AUTOPILOT, SG_BULK, "Predictor::configure(" << nodeName << ") [unhandled]" << endl );
60   return false;
61 }
62
63 void Predictor::update( bool firstTime, double dt ) 
64 {
65     double ivalue = _valueInput.get_value();
66
67     if ( firstTime ) {
68         _last_value = ivalue;
69     }
70
71     double current = (ivalue - _last_value)/dt; // calculate current error change (per second)
72     _average = dt < 1.0 ? ((1.0 - dt) * _average + current * dt) : current;
73
74     // calculate output with filter gain adjustment
75     double output = ivalue + 
76            (1.0 - _filter_gain.get_value()) * (_average * _seconds.get_value()) + 
77            _filter_gain.get_value() * (current * _seconds.get_value());
78     output = clamp( output );
79     set_output_value( output );
80
81     _last_value = ivalue;
82 }
83
84