]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/pisimplecontroller.cxx
Update VS2008 projects : use Boost 1.44.0 available in last 3rd Party archive
[flightgear.git] / src / Autopilot / pisimplecontroller.cxx
1 // pisimplecontroller.cxx - implementation of a simple PI controller
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 "pisimplecontroller.hxx"
25
26 using namespace FGXMLAutopilot;
27
28 PISimpleController::PISimpleController() :
29     AnalogComponent(),
30     _int_sum( 0.0 )
31 {
32 }
33
34 bool PISimpleController::configure( const string& nodeName, SGPropertyNode_ptr configNode)
35 {
36   if( AnalogComponent::configure( nodeName, configNode ) )
37     return true;
38
39   if( nodeName == "config" ) {
40     Component::configure( configNode );
41     return true;
42   }
43
44   if (nodeName == "Kp") {
45     _Kp.push_back( new InputValue(configNode) );
46     return true;
47   } 
48
49   if (nodeName == "Ki") {
50     _Ki.push_back( new InputValue(configNode) );
51     return true;
52   }
53
54   return false;
55 }
56
57 void PISimpleController::update( bool firstTime, double dt )
58 {
59     if ( firstTime ) {
60         // we have just been enabled, zero out int_sum
61         _int_sum = 0.0;
62     }
63
64     if ( _debug ) cout << "Updating " << get_name() << endl;
65     double y_n = _valueInput.get_value();
66     double r_n = _referenceInput.get_value();
67                   
68     double error = r_n - y_n;
69     if ( _debug ) cout << "input = " << y_n
70                       << " reference = " << r_n
71                       << " error = " << error
72                       << endl;
73
74     double prop_comp = clamp(error * _Kp.get_value());
75     _int_sum += error * _Ki.get_value() * dt;
76
77
78     double output = prop_comp + _int_sum;
79     double clamped_output = clamp( output );
80     if( output != clamped_output ) // anti-windup
81       _int_sum = clamped_output - prop_comp;
82
83     if ( _debug ) cout << "prop_comp = " << prop_comp
84                       << " int_sum = " << _int_sum << endl;
85
86     set_output_value( clamped_output );
87     if ( _debug ) cout << "output = " << clamped_output << endl;
88 }