Configuration of a filter with FB_FTR_IIRSpec
This sample shows how to configure a filter of type Butterworth with the function block FB_FTR_IIRSpec.
Download: Tc3_Filter_SimpleIIRSpec.zip (*.tnzip)
Description:
- The sample project consists of a TwinCAT PLC project and a measurement project.
- In the measurement project, two input signals and two output signals are configured in two axes.
- The two input signals are generated synthetically via a function generator called in the PLC. They are harmonic signals (sine) with a frequency of 250 Hz for the first channel and 300 Hz for the second channel. The signals are processed by a Butterworth type filter. The implemented filter has a cut-off frequency of 250 Hz.
- The MAIN PLC program is called by a task with a cycle time of 1 ms.
Implementation:
- First, a structure
stParams
of typeST_FTR_IIRSpec
is declared and initialized. The structure is used for parameterizing the function blockFB_FTR_IIRSpec
or for configuring the filter.
stParams : ST_FTR_IIRSpec := ( … )
- An instance
fbFilter
of the function blockFB_FTR_IIRSpec
is then created. The structurestParams
is transferred during instantiation.
fbFilter : FB_FTR_IIRSpec := (stConfig := stParams);
- In the source code, the
Call()
method is called to execute the filter, and error checking variants are listed.
IF NOT fbFilter.bError THEN
fbFilter.Call(ADR(aSignalBuffer), SIZEOF(aSignalBuffer),
ADR(aFilteredSignal), SIZEOF(aFilteredSignal));
ELSE
// if on error state, do something
nErrorCount := nErrorCount + 1;
// e.g. check if filter is configured
// IF fbFilter.bConfigured THEN
// or use ipResultMessage to check root cause
// fbFilter.ipResultMessage
END_IF;
- The
Configure()
and/orReset()
methods are also called to change the filter configuration during runtime (fbFilter.Configure
) or to perform a reset (fbFilter.Reset
). The call of the methods is controlled by the flagsbReconfigure
andbReset
. They can be manually set toTRUE
orFALSE
in the Online Watch.
Observation:
Recording with TwinCAT 3 Scope shows that the amplitude of the output signals is attenuated and phase-shifted by the filter. The signal in the second channel (green) is attenuated more strongly than that in the first channel (blue, attenuation by -3 dB).
If the flag bReconfigure
is set, the cut-off frequency is shifted to 50 Hz, which makes the amplitude of both output signals even smaller.
If the flag bReset
is set, the internal state of the filter is deleted, i.e. past input and output values are no longer taken into account. The continuous signal curve is interrupted (at approx. 27 ms), and the filter settles again.
See also: