FlightGear next
pisimplecontroller.cxx
Go to the documentation of this file.
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
25
26using namespace FGXMLAutopilot;
27
28//------------------------------------------------------------------------------
31 _int_sum( 0.0 )
32{
33}
34
35//------------------------------------------------------------------------------
36bool PISimpleController::configure( SGPropertyNode& cfg_node,
37 const std::string& cfg_name,
38 SGPropertyNode& prop_root )
39{
40 if( cfg_name == "config" ) {
41 Component::configure(prop_root, cfg_node);
42 return true;
43 }
44
45 if (cfg_name == "Kp") {
46 _Kp.push_back(new simgear::Value(prop_root, cfg_node));
47 return true;
48 }
49
50 if (cfg_name == "Ki") {
51 _Ki.push_back(new simgear::Value(prop_root, cfg_node));
52 return true;
53 }
54
55 return AnalogComponent::configure(cfg_node, cfg_name, prop_root);
56}
57
58void PISimpleController::update( bool firstTime, double dt )
59{
60 if ( firstTime ) {
61 // we have just been enabled, zero out int_sum
62 _int_sum = 0.0;
63 }
64
65 if ( _debug ) std::cout << "Updating " << subsystemId() << std::endl;
66 double y_n = _valueInput.get_value();
67 double r_n = _referenceInput.get_value();
68
69 double error = r_n - y_n;
70 if ( _debug ) std::cout << "input = " << y_n
71 << " reference = " << r_n
72 << " error = " << error
73 << std::endl;
74
75 double prop_comp = clamp(error * _Kp.get_value());
76 _int_sum += error * _Ki.get_value() * dt;
77
78
79 double output = prop_comp + _int_sum;
80 double clamped_output = clamp( output );
81 if( output != clamped_output ) // anti-windup
82 _int_sum = clamped_output - prop_comp;
83
84 if ( _debug ) std::cout << "prop_comp = " << prop_comp
85 << " int_sum = " << _int_sum << std::endl;
86
87 set_output_value( clamped_output );
88 if ( _debug ) std::cout << "output = " << clamped_output << std::endl;
89}
90
91
92// Register the subsystem.
93SGSubsystemMgr::Registrant<PISimpleController> registrantPISimpleController;
simgear::ValueList _valueInput
the value input
bool configure(SGPropertyNode &cfg_node, const std::string &cfg_name, SGPropertyNode &prop_root) override
This method configures this analog component from a property node.
AnalogComponent()
A constructor for an analog component.
simgear::ValueList _referenceInput
the reference input
double clamp(double value) const
clamp the given value if <min> and/or <max> inputs were given
virtual bool configure(SGPropertyNode &cfg_node, const std::string &cfg_name, SGPropertyNode &prop_root)
Definition component.cxx:67
bool _debug
debug flag, true if this component should generate some useful output on every iteration
Definition component.hxx:66
void update(bool firstTime, double dt)
pure virtual function to be implemented by the derived classes.
virtual bool configure(SGPropertyNode &cfg_node, const std::string &cfg_name, SGPropertyNode &prop_root)
This method configures this analog component from a property node.
SGSubsystemMgr::Registrant< PISimpleController > registrantPISimpleController