SCL代码:
(* ============================================================================= *)
(* PURPOSE : To filter a value by applying a first-order exponential filter *)
(* with a user-selectable time period, F-tau. The filter is calculated *)
(* using the following formula - *)
(* *)
(* Rate Rate *)
(* FiltOut := FiltOut * e + RawValue * (1 - e ) *)
(* *)
(* where Rate := (-0.1 / F_tau ) *)
(* *)
(* The Function Block is intended to be triggered every 100mS (hence *)
(* the 0.1 in the formula) from cyclic interrupt Organization Block *)
(* OB35. If a different resolution is required, use a different OB as *)
(* the trigger, and adjust the formula to reflect this. *)
(* *)
(* ============================================================================= *)
FUNCTION_BLOCK FB200
TITLE = 'Low Pass Filter'
VERSION : '1.0'
AUTHOR : KenMuir
NAME : LowPass
FAMILY : Filter
(* ----------------------------- *)
(* DEFINITION OF LOCAL VARIABLES *)
(* ----------------------------- *)
VAR_INPUT
RawValue : REAL; // Incoming unfiltered value
F_tau : REAL; // Time interval (in seconds) for filter period
END_VAR
VAR_OUTPUT
FiltOut : REAL; // Filtered output value
ZRError : BOOL; // Error flag if F_tau is set to zero (invalid rate)
END_VAR
VAR
LastFV : REAL; // Static variable holds value of FiltOut from previous
END_VAR // execution cycle.
VAR_TEMP
ExpRate : REAL; // Equals exp(-0.1 / F_tau)
END_VAR
(* ----------------------------- *)
(* START OF CODE SECTION *)
(* ----------------------------- *)
BEGIN
IF F_tau = 0.0 THEN // If the filter period is set to zero ...
ZRError := true; // .. . set the Zero Rate Error bit true
ELSE
ZRError := false; // Error bit reset to false
ExpRate := EXP(-0.1 / F_tau);
FiltOut := LastFV * ExpRate + (RawValue * (1 - ExpRate)); // Calculate new output
LastFV := FiltOut; // Store output value for next cycle of calculation
END_IF;
END_FUNCTION_BLOCK
注意使用VAR定义的变量,会储存stat中。
调用
CALL FB 200 , DB2
RawValue:=MD10
F_tau :=MD14
FiltOut :=MD18
ZRError :=M0.0