前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >S7-SCL 4个线性缩放功能块 <copy代码即可使用>

S7-SCL 4个线性缩放功能块 <copy代码即可使用>

作者头像
科控物联
发布2022-03-29 21:33:42
3320
发布2022-03-29 21:33:42
举报
文章被收录于专栏:科控自动化科控自动化

Y值根据一般线性方程计算:y = a x + b.

由此引出以下关系:

( y1 -y0 )

y = -------------- * ( x - x0 ) + y0

( x1 - x0 )

线性缩放功能块 “ SclScaleLinearIntToReal ” 。

代码语言:javascript
复制
FUNCTION "SclScaleLinearInt" : Int
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      x : Int;
      yMin : Int;
      yMax : Int;
      x0 : Int;
      y0 : Int;
      x1 : Int;
      y1 : Int;
   END_VAR

   VAR_TEMP 
      tempYwert : Real;
      tempX0real : Real;
      tempX1real : Real;
      tempY0real : Real;
      tempY1real : Real;
      tempXreal : Real;
      tempYminReal : Real;
      tempYmaxReal : Real;
   END_VAR


BEGIN
  #tempX0real := INT_TO_REAL(#x0);
  #tempX1real := INT_TO_REAL(#x1);
  #tempY0real := INT_TO_REAL(#y0);
  #tempY1real := INT_TO_REAL(#y1);
  #tempXreal := INT_TO_REAL(#x);
  #tempYminReal := INT_TO_REAL(#yMin);
  #tempYmaxReal := INT_TO_REAL(#yMax);
  
  IF (#x1 - #x0) = 0 OR (#x - #x0) = 0 OR (#y1 - #y0) = 0 OR #yMin > #yMax
  THEN
      #tempYwert := 0;
  ELSE
      #tempYwert := (#tempY1real - #tempY0real) / (#tempX1real - #tempX0real) * (#tempXreal - #tempX0real) + #tempY0real;
  END_IF;
  
  IF #tempYwert < #tempYminReal THEN
      #SclScaleLinearInt := 0;
  ELSIF #tempYwert > #tempYmaxReal THEN
      #SclScaleLinearInt := #yMax;
  ELSE
      #SclScaleLinearInt := REAL_TO_INT(#tempYwert);
  END_IF;
END_FUNCTION
代码语言:javascript
复制
FUNCTION "SclScaleLinearIntToReal" : Real
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      x : Int;
      yMin : Real;
      yMax : Real;
      x0 : Int;
      y0 : Real;
      x1 : Int;
      y1 : Real;
   END_VAR

   VAR_TEMP 
      tempXreal : Real;
      tempX1real : Real;
      tempX0real : Real;
      tempYwert : Real;
   END_VAR


BEGIN
  #tempXreal := INT_TO_REAL(#x);
  #tempX0real := INT_TO_REAL(#x0);
  #tempX1real := INT_TO_REAL(#x1);
  
  IF (#x1 - #x0) = 0 OR (#x - #x0) = 0 OR (#y1 - #y0) = 0 OR #yMin > #yMax
  THEN
      #tempYwert := 0;
  ELSE
      #tempYwert := (#y1 - #y0) / (#tempX1real - #tempX0real) * (#tempXreal - #tempX0real) + #y0;
  END_IF;
  IF #tempYwert < #yMin THEN
      #SclScaleLinearIntToReal := 0;
  ELSIF #tempYwert > #yMax THEN
      #SclScaleLinearIntToReal := #yMax;
  ELSE
      #SclScaleLinearIntToReal := #tempYwert;
  END_IF;
END_FUNCTION

代码语言:javascript
复制
FUNCTION "SclScaleLinearReal" : Real
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      x : Real;
      yMin : Real;
      yMax : Real;
      x0 : Real;
      y0 : Real;
      x1 : Real;
      y1 : Real;
   END_VAR

   VAR_TEMP 
      tempYtemp : Real;
   END_VAR


BEGIN
  IF (#y1 - #y0) = 0 OR (#x1 - #x0) = 0 OR (#x - #x0) = 0 OR #yMin > #yMax
  THEN
      #tempYtemp := 0;
  ELSE
      #tempYtemp := (#y1 - #y0) / (#x1 - #x0) * (#x - #x0) + #y0;
  END_IF;
  
  IF #tempYtemp < #yMin THEN
      #SclScaleLinearReal := 0;
  ELSIF #tempYtemp > #yMax THEN
      #SclScaleLinearReal := #yMax;
  ELSE
      #SclScaleLinearReal := #tempYtemp;
  END_IF;
END_FUNCTION

代码语言:javascript
复制
FUNCTION "SclScaleLinearRealToInt" : Int
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      x : Real;
      yMin : Int;
      yMax : Int;
      x0 : Real;
      y0 : Int;
      x1 : Real;
      y1 : Int;
   END_VAR

   VAR_TEMP 
      tempY0real : Real;
      tempY1real : Real;
      tempYreal : Real;
      tempYminReal : Real;
      tempYmaxReal : Real;
   END_VAR


BEGIN
  #tempY0real := INT_TO_REAL(#y0);
  #tempY1real := INT_TO_REAL(#y1);
  #tempYminReal := INT_TO_REAL(#yMin);
  #tempYmaxReal := INT_TO_REAL(#yMax);
  
  IF (#y1 - #y0) = 0 OR (#x1 - #x0) = 0 OR (#x - #x0) = 0 OR #yMin > #yMax
  THEN
      #SclScaleLinearRealToInt := 0;
  ELSE
      #tempYreal := (#tempY1real - #tempY0real) / (#x1 - #x0) * (#x - #x0) + #tempY0real;
  END_IF;
  
  IF #tempYreal < #tempYminReal THEN
      #SclScaleLinearRealToInt := 0;
  ELSIF #tempYreal > #tempYmaxReal THEN
      #SclScaleLinearRealToInt := #yMax;
  ELSE
      #SclScaleLinearRealToInt := REAL_TO_INT(#tempYreal);
  END_IF;
END_FUNCTION

例子

模拟量输入模块用来测量一个4mA 至 20mA 的电流信号。此信号在 CPU 内部被转换为 0 至27648。液位用此计算值来测量。

由此可知 :

4mA 对应 0.0m 液位,

而 20mA 对应 1.7m 液位。

按照如下确定参数:

  • P0 ( x0=0; y0=0.0 )
  • P1 ( x1=+27648; y1=1.7 )

图4“SclScaleLinearIntToReal”函数的调用和参数。

( y1 -y0 )

y = -------------- * ( x - x0 ) + y0

( x1 - x0 )

( 1.7-0.0)

= -------------- * ( 12556 - 0 ) + 0.0

( 27648- 0)

=0.7720341...

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 科控物联 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档