Reconfiguration with initial values
This sample shows how a filter can be reconfigured or dynamically adapted during runtime. It also describes how previous knowledge of a signal can be used to shorten the settling time of a filter after reconfiguration.
Download: Tc3_Filter_InitVals.zip (*.tnzip)
Description:
- The sample project consists of a TwinCAT PLC project and a measurement project.
- Two output signals and a counter variable are configured in the measurement project.
- The input signal is generated synthetically via a function generator that is called in the PLC. The signal is processed by two Butterworth low-pass filters that are configured in different ways.
- Each filter is described in a separate function block, which is instantiated in the MAIN PLC program.
- The MAIN PLC program is called by a task with a cycle time of 1 ms.
Observation:
The recording of the TwinCAT 3 Scope shows a signal curve that is very noisy and jumps between two levels.

The first filter described in the function block FB_ContinousFilter
remains unchanged during runtime. The output signal of this filter is limited in its dynamics due to the low-pass effect.
The second filter, which is described in the function block FB_DynamicFiltering
, responds dynamically to the variations of the input signal during runtime.

The bTrigger
input informs the fbDynamicLowPass
instance of the FB_DynamicFiltering
function block that there is a jump in the signal. In practice, this can be indicated by the signal itself or by an action initiated by the PLC.
If bTrigger
is set to TRUE
, the Configure()
method is called, which sets the low-pass cut-off frequency to the maximum permitted value. This means that the low-pass effect is lost the next time the Call()
method is called. However, the output signal can quickly follow the jump in the input signal.
Finally, the filter effect is reactivated by calling the Configure()
method and greatly reducing the cut-off frequency.
In order to reduce the settling time of the filter considerably, previous knowledge about the two plateaus is used. To this end, the parameter pInitialValues
of the configuration structure stParams
(type ST_FTR_IIRSpec
) is assigned the initial value 1 or 5. The values are defined in the MAIN program (fPreKnowledgeHigh
, fPreKnowledgeLow
) and reflect the user's knowledge that the signal to be evaluated will jump approximately between 1 and 5. The filter is thus already in the steady state around this value when the Call()
method is called for the first time.
stParams.fCutoff := Main.fCutOff; // reduce cutoff freq.
IF bRisingEdge THEN
// on rising edge apply preknowledge (Signal around 5)
bRisingEdge := FALSE;
stParams.pInitialValues := ADR(fInitValHighLevel);
stParams.nInitialValuesSize := SIZEOF(fInitValHighLevel);
ELSE
// on falling edge
stParams.pInitialValues := ADR(fInitValBaseLevel);
stParams.nInitialValuesSize := SIZEOF(fInitValBaseLevel);
END_IF;
fbFilter.Configure(stConfig := stParams);
The time it takes to settle to the new plateau can be reduced significantly by event-based switching of the filter characteristics.
See also: