// Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de
//
// Washout/high-pass filter, lead-lag filter and integrator added.
-// low-pass and lag aliases added to Exponential filter,
+// low-pass and lag aliases added to Exponential filter,
// rate-limit added. A J Teeder 2013
//
// This program is free software; you can redistribute it and/or
//
#include "digitalfilter.hxx"
-#include "functor.hxx"
#include <deque>
-using std::map;
-using std::string;
-using std::endl;
-using std::cout;
-
-namespace FGXMLAutopilot {
+namespace FGXMLAutopilot
+{
/**
*
*
*/
-class DigitalFilterImplementation : public SGReferenced {
-protected:
- virtual bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode) = 0;
-public:
- virtual ~DigitalFilterImplementation() {}
- DigitalFilterImplementation();
- virtual void initialize( double initvalue ) {}
- virtual double compute( double dt, double input ) = 0;
- bool configure( SGPropertyNode_ptr configNode );
-
- void setDigitalFilter( DigitalFilter * digitalFilter ) { _digitalFilter = digitalFilter; }
-
-protected:
- DigitalFilter * _digitalFilter;
+class DigitalFilterImplementation:
+ public SGReferenced
+{
+ public:
+ virtual ~DigitalFilterImplementation() {}
+ DigitalFilterImplementation();
+ virtual void initialize( double initvalue ) {}
+ virtual double compute( double dt, double input ) = 0;
+ virtual bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root ) = 0;
+
+ void setDigitalFilter( DigitalFilter * digitalFilter ) { _digitalFilter = digitalFilter; }
+
+ protected:
+ DigitalFilter * _digitalFilter;
};
/* --------------------------------------------------------------------------------- */
class GainFilterImplementation : public DigitalFilterImplementation {
protected:
InputValueList _gainInput;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
public:
GainFilterImplementation() : _gainInput(1.0) {}
double compute( double dt, double input );
class DerivativeFilterImplementation : public GainFilterImplementation {
InputValueList _TfInput;
double _input_1;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
public:
DerivativeFilterImplementation();
double compute( double dt, double input );
class ExponentialFilterImplementation : public GainFilterImplementation {
protected:
InputValueList _TfInput;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
bool _isSecondOrder;
double _output_1, _output_2;
public:
InputValueList _samplesInput;
double _output_1;
std::deque <double> _inputQueue;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
public:
MovingAverageFilterImplementation();
double compute( double dt, double input );
protected:
double _output_1;
InputValueList _rateOfChangeInput;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
public:
NoiseSpikeFilterImplementation();
double compute( double dt, double input );
double _output_1;
InputValueList _rateOfChangeMax;
InputValueList _rateOfChangeMin ;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
public:
RateLimitFilterImplementation();
double compute( double dt, double input );
InputValueList _maxInput;
double _input_1;
double _output_1;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
public:
IntegratorFilterImplementation();
double compute( double dt, double input );
virtual void initialize( double initvalue );
};
+// integrates x" + ax' + bx + c = 0
+class DampedOsciFilterImplementation : public GainFilterImplementation {
+protected:
+ InputValueList _aInput;
+ InputValueList _bInput;
+ InputValueList _cInput;
+ double _x2;
+ double _x1;
+ double _x0;
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
+public:
+ DampedOsciFilterImplementation();
+ double compute( double dt, double input );
+ virtual void initialize( double initvalue );
+};
+
class HighPassFilterImplementation : public GainFilterImplementation {
protected:
InputValueList _TfInput;
double _input_1;
double _output_1;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
public:
HighPassFilterImplementation();
double compute( double dt, double input );
InputValueList _TfbInput;
double _input_1;
double _output_1;
- bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
+ bool configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root );
public:
LeadLagFilterImplementation();
double compute( double dt, double input );
using namespace FGXMLAutopilot;
-/* --------------------------------------------------------------------------------- */
-/* --------------------------------------------------------------------------------- */
+//------------------------------------------------------------------------------
DigitalFilterImplementation::DigitalFilterImplementation() :
_digitalFilter(NULL)
{
-}
-
-bool DigitalFilterImplementation::configure( SGPropertyNode_ptr configNode )
-{
- for (int i = 0; i < configNode->nChildren(); ++i ) {
- SGPropertyNode_ptr prop;
-
- SGPropertyNode_ptr child = configNode->getChild(i);
- string cname(child->getName());
-
- if( configure( cname, child ) )
- continue;
- } // for configNode->nChildren()
-
- return true;
}
-/* --------------------------------------------------------------------------------- */
-/* --------------------------------------------------------------------------------- */
-
+//------------------------------------------------------------------------------
double GainFilterImplementation::compute( double dt, double input )
{
return _gainInput.get_value() * input;
}
-bool GainFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+bool GainFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if (nodeName == "gain" ) {
- _gainInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "gain" ) {
+ _gainInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
_input_1 = initvalue;
}
-
-bool DerivativeFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+//------------------------------------------------------------------------------
+bool DerivativeFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if( GainFilterImplementation::configure( nodeName, configNode ) )
+ if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) )
return true;
- if (nodeName == "filter-time" ) {
- _TfInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "filter-time" ) {
+ _TfInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
return output_0;
}
-bool MovingAverageFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+bool MovingAverageFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if (nodeName == "samples" ) {
- _samplesInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "samples" ) {
+ _samplesInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
return (_output_1 = _output_1 + copysign( maxChange, delta ));
}
-bool NoiseSpikeFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+//------------------------------------------------------------------------------
+bool NoiseSpikeFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if (nodeName == "max-rate-of-change" ) {
- _rateOfChangeInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "max-rate-of-change" ) {
+ _rateOfChangeInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
return (output);
}
-bool RateLimitFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+bool RateLimitFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if (nodeName == "max-rate-of-change" ) {
- _rateOfChangeMax.push_back( new InputValue( configNode, 1 ) );
+// std::cout << "RateLimitFilterImplementation " << cfg_name << std::endl;
+ if (cfg_name == "max-rate-of-change" ) {
+ _rateOfChangeMax.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
- if (nodeName == "min-rate-of-change" ) {
- _rateOfChangeMin.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "min-rate-of-change" ) {
+ _rateOfChangeMin.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
double output_0;
- // avoid negative filter times
+ // avoid negative filter times
// and div by zero if -tf == dt
double alpha = tf > 0.0 ? 1 / ((tf/dt) + 1) : 1.0;
-
+
if(_isSecondOrder) {
- output_0 = alpha * alpha * input +
+ output_0 = alpha * alpha * input +
2 * (1 - alpha) * _output_1 -
(1 - alpha) * (1 - alpha) * _output_2;
} else {
return (_output_1 = output_0);
}
-bool ExponentialFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+//------------------------------------------------------------------------------
+bool ExponentialFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if( GainFilterImplementation::configure( nodeName, configNode ) )
+ if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) )
return true;
- if (nodeName == "filter-time" ) {
- _TfInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "filter-time" ) {
+ _TfInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
- if (nodeName == "type" ) {
- string type(configNode->getStringValue());
+ if (cfg_name == "type" ) {
+ std::string type(cfg_node.getStringValue());
_isSecondOrder = type == "double-exponential";
}
_input_1 = _output_1 = initvalue;
}
-
-bool IntegratorFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+//------------------------------------------------------------------------------
+bool IntegratorFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if( GainFilterImplementation::configure( nodeName, configNode ) )
+ if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) )
return true;
- if (nodeName == "u_min" ) {
- _minInput.push_back( new InputValue( configNode, 1 ) );
+
+ if (cfg_name == "u_min" ) {
+ _minInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
- if (nodeName == "u_max" ) {
- _maxInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "u_max" ) {
+ _maxInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
return false;
}
+/* --------------------------------------------------------------------------------- */
+DampedOsciFilterImplementation::DampedOsciFilterImplementation() :
+ _x0(0.0)
+{
+}
+
+void DampedOsciFilterImplementation::initialize( double initvalue )
+{
+ _x2 = _x1 = _x0 = initvalue;
+}
+
+bool DampedOsciFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
+{
+ if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) )
+ return true;
+
+ if (cfg_name == "a" ) {
+ _aInput.push_back( new InputValue(prop_root, cfg_node, 1) );
+ return true;
+ }
+ if (cfg_name == "b" ) {
+ _bInput.push_back( new InputValue(prop_root, cfg_node, 1) );
+ return true;
+ }
+ if (cfg_name == "c" ) {
+ _cInput.push_back( new InputValue(prop_root, cfg_node, 1) );
+ return true;
+ }
+ return false;
+}
+
+double DampedOsciFilterImplementation::compute( double dt, double input )
+{
+ if (fabs(input) > 1e-15) {
+ double dz = dt * input;
+ _x0 = _x1 - dz;
+ _x2 = _x1 + dz;
+ } else {
+ double a = _aInput.get_value();
+ double b = _bInput.get_value();
+ double c = _cInput.get_value();
+ _x0 = (_x1 * (2. + dt * (a - b * dt)) - _x2 - c * dt * dt) / (1. + a * dt);
+ _x2 = _x1;
+ _x1 = _x0;
+ }
+ return _x0;
+}
+
/* --------------------------------------------------------------------------------- */
HighPassFilterImplementation::HighPassFilterImplementation() :
double output;
- // avoid negative filter times
+ // avoid negative filter times
// and div by zero if -tf == dt
double alpha = tf > 0.0 ? 1 / ((tf/dt) + 1) : 1.0;
return output;
}
-bool HighPassFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+//------------------------------------------------------------------------------
+bool HighPassFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if( GainFilterImplementation::configure( nodeName, configNode ) )
+ if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) )
return true;
- if (nodeName == "filter-time" ) {
- _TfInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "filter-time" ) {
+ _TfInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
-
+
return false;
}
double output;
- // avoid negative filter times
+ // avoid negative filter times
// and div by zero if -tf == dt
double alpha = tfa > 0.0 ? 1 / ((tfa/dt) + 1) : 1.0;
return output;
}
-bool LeadLagFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
+//------------------------------------------------------------------------------
+bool LeadLagFilterImplementation::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
{
- if( GainFilterImplementation::configure( nodeName, configNode ) )
+ if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) )
return true;
- if (nodeName == "filter-time-a" ) {
- _TfaInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "filter-time-a" ) {
+ _TfaInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
- if (nodeName == "filter-time-b" ) {
- _TfbInput.push_back( new InputValue( configNode, 1 ) );
+ if (cfg_name == "filter-time-b" ) {
+ _TfbInput.push_back( new InputValue(prop_root, cfg_node, 1) );
return true;
}
return false;
}
-/* --------------------------------------------------------------------------------- */
-/* Digital Filter Component Implementation */
-/* --------------------------------------------------------------------------------- */
+/* -------------------------------------------------------------------------- */
+/* Digital Filter Component Implementation */
+/* -------------------------------------------------------------------------- */
DigitalFilter::DigitalFilter() :
AnalogComponent(),
{
}
+//------------------------------------------------------------------------------
+template<class DigitalFilterType>
+DigitalFilterImplementation* digitalFilterFactory()
+{
+ return new DigitalFilterType();
+}
-static map<string,FunctorBase<DigitalFilterImplementation> *> componentForge;
+typedef std::map<std::string, DigitalFilterImplementation*(*)()>
+DigitalFilterMap;
+static DigitalFilterMap componentForge;
-bool DigitalFilter::configure(const string& nodeName, SGPropertyNode_ptr configNode)
+//------------------------------------------------------------------------------
+bool DigitalFilter::configure( SGPropertyNode& prop_root,
+ SGPropertyNode& cfg )
{
- if( componentForge.empty() ) {
- componentForge["gain"] = new CreateAndConfigureFunctor<GainFilterImplementation,DigitalFilterImplementation>();
- componentForge["exponential"] = new CreateAndConfigureFunctor<ExponentialFilterImplementation,DigitalFilterImplementation>();
- componentForge["double-exponential"] = new CreateAndConfigureFunctor<ExponentialFilterImplementation,DigitalFilterImplementation>();
- componentForge["moving-average"] = new CreateAndConfigureFunctor<MovingAverageFilterImplementation,DigitalFilterImplementation>();
- componentForge["noise-spike"] = new CreateAndConfigureFunctor<NoiseSpikeFilterImplementation,DigitalFilterImplementation>();
- componentForge["reciprocal"] = new CreateAndConfigureFunctor<ReciprocalFilterImplementation,DigitalFilterImplementation>();
- componentForge["derivative"] = new CreateAndConfigureFunctor<DerivativeFilterImplementation,DigitalFilterImplementation>();
- componentForge["high-pass"] = new CreateAndConfigureFunctor<HighPassFilterImplementation,DigitalFilterImplementation>();
- componentForge["lead-lag"] = new CreateAndConfigureFunctor<LeadLagFilterImplementation,DigitalFilterImplementation>();
- componentForge["integrator"] = new CreateAndConfigureFunctor<IntegratorFilterImplementation,DigitalFilterImplementation>();
+ if( componentForge.empty() )
+ {
+ componentForge["gain" ] = digitalFilterFactory<GainFilterImplementation>;
+ componentForge["exponential" ] = digitalFilterFactory<ExponentialFilterImplementation>;
+ componentForge["double-exponential" ] = digitalFilterFactory<ExponentialFilterImplementation>;
+ componentForge["moving-average" ] = digitalFilterFactory<MovingAverageFilterImplementation>;
+ componentForge["noise-spike" ] = digitalFilterFactory<NoiseSpikeFilterImplementation>;
+ componentForge["rate-limit" ] = digitalFilterFactory<RateLimitFilterImplementation>;
+ componentForge["reciprocal" ] = digitalFilterFactory<ReciprocalFilterImplementation>;
+ componentForge["derivative" ] = digitalFilterFactory<DerivativeFilterImplementation>;
+ componentForge["high-pass" ] = digitalFilterFactory<HighPassFilterImplementation>;
+ componentForge["lead-lag" ] = digitalFilterFactory<LeadLagFilterImplementation>;
+ componentForge["integrator" ] = digitalFilterFactory<IntegratorFilterImplementation>;
+ componentForge["damped-osci" ] = digitalFilterFactory<DampedOsciFilterImplementation>;
}
- SG_LOG( SG_AUTOPILOT, SG_BULK, "DigitalFilter::configure(" << nodeName << ")" << endl );
- if( AnalogComponent::configure( nodeName, configNode ) )
- return true;
+ const std::string type = cfg.getStringValue("type");
+ DigitalFilterMap::iterator component_factory = componentForge.find(type);
+ if( component_factory == componentForge.end() )
+ {
+ SG_LOG(SG_AUTOPILOT, SG_WARN, "unhandled filter type '" << type << "'");
+ return false;
+ }
- if (nodeName == "type" ) {
- string type( configNode->getStringValue() );
- if( componentForge.count(type) == 0 ) {
- SG_LOG( SG_AUTOPILOT, SG_BULK, "unhandled filter type <" << type << ">" << endl );
- return true;
- }
- _implementation = (*componentForge[type])( configNode->getParent() );
- _implementation->setDigitalFilter( this );
- return true;
+ _implementation = (*component_factory->second)();
+ _implementation->setDigitalFilter( this );
+
+ for( int i = 0; i < cfg.nChildren(); ++i )
+ {
+ SGPropertyNode_ptr child = cfg.getChild(i);
+ std::string cname(child->getName());
+
+ if( !_implementation->configure(*child, cname, prop_root)
+ && !configure(*child, cname, prop_root)
+ && cname != "type"
+ && cname != "params" ) // 'params' is usually used to specify parameters
+ // in PropertList files.
+ SG_LOG
+ (
+ SG_AUTOPILOT,
+ SG_WARN,
+ "DigitalFilter: unknown config node: " << cname
+ );
}
- if( nodeName == "initialize-to" ) {
- string s( configNode->getStringValue() );
- if( s == "input" ) {
+ return true;
+}
+
+//------------------------------------------------------------------------------
+bool DigitalFilter::configure( SGPropertyNode& cfg_node,
+ const std::string& cfg_name,
+ SGPropertyNode& prop_root )
+{
+ if( cfg_name == "initialize-to" )
+ {
+ std::string s( cfg_node.getStringValue() );
+ if( s == "input" )
_initializeTo = INITIALIZE_INPUT;
- } else if( s == "output" ) {
+ else if( s == "output" )
_initializeTo = INITIALIZE_OUTPUT;
- } else if( s == "none" ) {
+ else if( s == "none" )
_initializeTo = INITIALIZE_NONE;
- } else {
- SG_LOG( SG_AUTOPILOT, SG_WARN, "unhandled initialize-to value '" << s << "' ignored" );
- }
+ else
+ SG_LOG
+ (
+ SG_AUTOPILOT,
+ SG_WARN, "DigitalFilter: initialize-to (" << s << ") ignored"
+ );
+
return true;
}
- SG_LOG( SG_AUTOPILOT, SG_BULK, "DigitalFilter::configure(" << nodeName << ") [unhandled]" << endl );
- return false; // not handled by us, let the base class try
+ return AnalogComponent::configure(cfg_node, cfg_name, prop_root);
}
+//------------------------------------------------------------------------------
void DigitalFilter::update( bool firstTime, double dt)
{
if( _implementation == NULL ) return;
set_output_value( output );
if(_debug) {
- cout << "input:" << input
- << "\toutput:" << output << endl;
+ std::cout << "input:" << input
+ << "\toutput:" << output << std::endl;
}
}