STEP 7 V5.x 软件低通滤波

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

原文地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容沁风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值