41 #include "math/FGParameterValue.h"
51 FGFilter::FGFilter(FGFCS* fcs, Element* element)
52 : FGFCSComponent(fcs, element), DynamicFilter(false), Initialize(true)
54 C[1] = C[2] = C[3] = C[4] = C[5] = C[6] =
nullptr;
56 CheckInputNodes(1, 1, element);
58 for (
int i=1; i<7; i++)
59 ReadFilterCoefficients(element, i);
61 if (Type ==
"LAG_FILTER") FilterType = eLag ;
62 else if (Type ==
"LEAD_LAG_FILTER") FilterType = eLeadLag ;
63 else if (Type ==
"SECOND_ORDER_FILTER") FilterType = eOrder2 ;
64 else if (Type ==
"WASHOUT_FILTER") FilterType = eWashout ;
65 else FilterType = eUnknown ;
67 CalculateDynamicFilters();
83 void FGFilter::ResetPastStates(
void)
85 FGFCSComponent::ResetPastStates();
87 Input = 0.0; Initialize =
true;
92 void FGFilter::ReadFilterCoefficients(Element* element,
int index)
96 string coefficient =
"c0";
97 coefficient[1] += index;
99 if ( element->FindElement(coefficient) ) {
100 C[index] =
new FGParameterValue(element->FindElement(coefficient),
102 DynamicFilter |= !C[index]->IsConstant();
108 void FGFilter::CalculateDynamicFilters(
void)
112 switch (FilterType) {
114 denom = 2.0 + dt*C[1];
115 ca = dt*C[1] / denom;
116 cb = (2.0 - dt*C[1]) / denom;
120 denom = 2.0*C[3] + dt*C[4];
121 ca = (2.0*C[1] + dt*C[2]) / denom;
122 cb = (dt*C[2] - 2.0*C[1]) / denom;
123 cc = (2.0*C[3] - dt*C[4]) / denom;
126 denom = 4.0*C[4] + 2.0*C[5]*dt + C[6]*dt*dt;
127 ca = (4.0*C[1] + 2.0*C[2]*dt + C[3]*dt*dt) / denom;
128 cb = (2.0*C[3]*dt*dt - 8.0*C[1]) / denom;
129 cc = (4.0*C[1] - 2.0*C[2]*dt + C[3]*dt*dt) / denom;
130 cd = (2.0*C[6]*dt*dt - 8.0*C[4]) / denom;
131 ce = (4.0*C[4] - 2.0*C[5]*dt + C[6]*dt*dt) / denom;
134 denom = 2.0 + dt*C[1];
136 cb = (2.0 - dt*C[1]) / denom;
139 cerr <<
"Unknown filter type" << endl;
147 bool FGFilter::Run(
void)
151 PreviousOutput2 = PreviousInput2 = PreviousOutput1 = PreviousInput1 = Output = Input;
156 Input = InputNodes[0]->getDoubleValue();
158 if (DynamicFilter) CalculateDynamicFilters();
160 switch (FilterType) {
162 Output = (Input + PreviousInput1) * ca + PreviousOutput1 * cb;
165 Output = Input * ca + PreviousInput1 * cb + PreviousOutput1 * cc;
168 Output = Input * ca + PreviousInput1 * cb + PreviousInput2 * cc
169 - PreviousOutput1 * cd - PreviousOutput2 * ce;
172 Output = Input * ca - PreviousInput1 * ca + PreviousOutput1 * cb;
180 PreviousOutput2 = PreviousOutput1;
181 PreviousOutput1 = Output;
182 PreviousInput2 = PreviousInput1;
183 PreviousInput1 = Input;
210 void FGFilter::Debug(
int from)
212 if (debug_lvl <= 0)
return;
216 cout <<
" INPUT: " << InputNodes[0]->GetName() << endl;
218 for (
int i=1; i < 7; i++) {
221 cout <<
" C[" << i <<
"]";
222 if (!C[i]->IsConstant()) cout <<
" is the value of property";
223 cout <<
": "<< C[i]->GetName() << endl;
226 for (
auto node: OutputNodes)
227 cout <<
" OUTPUT: " << node->getNameString() << endl;
230 if (debug_lvl & 2 ) {
231 if (from == 0) cout <<
"Instantiated: FGFilter" << endl;
232 if (from == 1) cout <<
"Destroyed: FGFilter" << endl;
234 if (debug_lvl & 4 ) {
236 if (debug_lvl & 8 ) {
238 if (debug_lvl & 16) {
240 if (debug_lvl & 64) {