void PIDController::update( bool firstTime, double dt )
{
- double edf_n = 0.0;
- double u_n = 0.0; // absolute output
+ if( firstTime ) {
+ ep_n_1 = 0.0;
+ edf_n_2 = edf_n_1 = 0.0;
+
+ // first time being enabled, seed with current property tree value
+ u_n_1 = get_output_value();
+ }
double u_min = _minInput.get_value();
double u_max = _maxInput.get_value();
double Ts = elapsedTime; // sampling interval (sec)
elapsedTime = 0.0;
- if( firstTime ) {
- // first time being enabled, seed u_n with current
- // property tree value
- ep_n_1 = 0.0;
- edf_n_2 = edf_n_1 = edf_n = 0.0;
- u_n = get_output_value();
- u_n_1 = u_n;
- }
-
if( Ts > SGLimitsd::min()) {
if( _debug ) cout << "Updating " << get_name()
<< " Ts " << Ts << endl;
double e_n = r_n - y_n;
if ( _debug ) cout << " e_n = " << e_n;
- if( _debug_node )
- {
- _debug_node->setDoubleValue("Ts", Ts);
- _debug_node->setDoubleValue("y_n", y_n);
- _debug_node->setDoubleValue("r_n", r_n);
- _debug_node->setDoubleValue("ep_n", ep_n);
- _debug_node->setDoubleValue("ep_n_1", ep_n_1);
- _debug_node->setDoubleValue("e_n", e_n);
- }
-
+ double edf_n = 0.0;
double td = Td.get_value();
if ( td > 0.0 ) { // do we need to calcluate derivative error?
edf_n = edf_n_1 / (Ts/Tf + 1)
+ ed_n * (Ts/Tf) / (Ts/Tf + 1);
if ( _debug ) cout << " edf_n = " << edf_n;
-
- if( _debug_node )
- {
- _debug_node->setDoubleValue("ed_n", ed_n);
- _debug_node->setDoubleValue("Tf", Tf);
- _debug_node->setDoubleValue("edf_n", edf_n);
- }
} else {
edf_n_2 = edf_n_1 = edf_n = 0.0;
}
+ ((Ts/ti) * e_n)
+ ((td/Ts) * (edf_n - 2*edf_n_1 + edf_n_2)) );
- double Pn = Kp.get_value() * (ep_n - ep_n_1),
- In = Kp.get_value() * ((Ts/ti) * e_n),
- Dn = Kp.get_value() * ((td/Ts) * (edf_n - 2*edf_n_1 + edf_n_2));
- if ( _debug )
- {
+ if ( _debug ) {
cout << " delta_u_n = " << delta_u_n << endl;
- cout << "P:" << Pn << " I:" << In << " D:" << Dn << endl;
- }
- if( _debug_node )
- {
- _debug_node->setDoubleValue("Pn", Pn);
- _debug_node->setDoubleValue("In", In);
- _debug_node->setDoubleValue("Dn", Dn);
- }
+ cout << "P:" << Kp.get_value() * (ep_n - ep_n_1)
+ << " I:" << Kp.get_value() * ((Ts/ti) * e_n)
+ << " D:" << Kp.get_value() * ((td/Ts) * (edf_n - 2*edf_n_1 + edf_n_2))
+ << endl;
+ }
}
// Integrator anti-windup logic:
}
// Calculates absolute output:
- u_n = u_n_1 + delta_u_n;
+ double u_n = u_n_1 + delta_u_n;
if ( _debug ) cout << " output = " << u_n << endl;
- if( _debug_node )
- _debug_node->setDoubleValue("dU", delta_u_n);
-
// Updates indexed values;
u_n_1 = u_n;
ep_n_1 = ep_n;
}
}
-bool PIDController::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+//------------------------------------------------------------------------------
+bool PIDController::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- SG_LOG( SG_AUTOPILOT, SG_BULK, "PIDController::configure(" << nodeName << ")" << endl );
-
- if( AnalogComponent::configure( nodeName, configNode ) )
- return true;
-
- if( nodeName == "config" ) {
- Component::configure( configNode );
+ if( cfg_name == "config" ) {
+ Component::configure(prop_root, cfg_node);
return true;
}
- if (nodeName == "Ts") {
- desiredTs = configNode->getDoubleValue();
+ if (cfg_name == "Ts") {
+ desiredTs = cfg_node.getDoubleValue();
return true;
}
- if (nodeName == "Kp") {
- Kp.push_back( new InputValue(configNode) );
+ if (cfg_name == "Kp") {
+ Kp.push_back( new InputValue(prop_root, cfg_node) );
return true;
}
- if (nodeName == "Ti") {
- Ti.push_back( new InputValue(configNode) );
+ if (cfg_name == "Ti") {
+ Ti.push_back( new InputValue(prop_root, cfg_node) );
return true;
}
- if (nodeName == "Td") {
- Td.push_back( new InputValue(configNode) );
+ if (cfg_name == "Td") {
+ Td.push_back( new InputValue(prop_root, cfg_node) );
return true;
}
- if (nodeName == "beta") {
- beta = configNode->getDoubleValue();
+ if (cfg_name == "beta") {
+ beta = cfg_node.getDoubleValue();
return true;
}
- if (nodeName == "alpha") {
- alpha = configNode->getDoubleValue();
+ if (cfg_name == "alpha") {
+ alpha = cfg_node.getDoubleValue();
return true;
}
- if (nodeName == "gamma") {
- gamma = configNode->getDoubleValue();
+ if (cfg_name == "gamma") {
+ gamma = cfg_node.getDoubleValue();
return true;
- }
+ }
- SG_LOG( SG_AUTOPILOT, SG_BULK, "PIDController::configure(" << nodeName << ") [unhandled]" << endl );
- return false;
+ return AnalogComponent::configure(cfg_node, cfg_name, prop_root);
}
-