急求西门子PLC大神帮忙解读下这段程序,尤其是cycletime这个变量名引导的程序不是很明白,相互学习,不胜感激!!!
FUNCTION_BLOCK "PositionXYFB"
TITLE = 'Position'
VERSION : '2.0'
KNOW_HOW_PROTECT
AUTHOR : MHK
// Block Parameters
VAR_INPUT
bDirect: BOOL; // Direction (Note! nOffset have to set also)
nIn: DWORD; // Signal from absolute encoder(来自绝对值编码器的信号)
nOffset: DINT; // Position offset value (note! Can be used for direction change)(位置偏移值,可用于方向的改变)
nMax: DINT; // Position max value for direction change(方向改变的位置最大值)
fFactor: REAL; // Position scale factor(位置比例系数)
END_VAR
VAR_IN_OUT
nEncPls: DINT; // Position pulses from encoder(来自编码器的位置脉冲)
END_VAR
VAR_OUTPUT
bEncErrSt: BOOL; // Encoder error status(编码器错误标志位)
nActPos: DINT; // Actual position by scaled unit i.e.(mm)(实际位置单位转换为mm)
fSpeed: REAL; // Calculated speed (m/min)(计算速度m/min)
END_VAR
VAR
CycleTimeAct: DINT; // One program cycle (s)(一个程序循环周期)
CycleTime: TON; // Timer for measure cycle time(测量循环时间的计时器)
fPrevPosit: REAL; // Previous position without direction and offset (mm)(没有方向和偏移的之前位置)
END_VAR
VAR_TEMP
nPosErr: DINT; // Encoder error code(编码器错误码)
nPosition: DINT; // Position witout direction and offset (mm)(没有方向和偏移量位置值)
fPosition: REAL; // Position without direction and offset (mm)
END_VAR
BEGIN
//***********************************************************************
// Encoder signal handling(编码器信号处理)
//***********************************************************************
// Only error code ( 00000000 00000000 00000000 0000X000 )
nPosErr := DWORD_TO_DINT( SHR( IN:=SHL( IN:=nIn, N:=28 ), N:=31 ));
// Only position signal ( 00000XXX XXXXXXXX XXXXXXXX 00000000 )
nEncPls := DWORD_TO_DINT( SHR( IN:=SHL( IN:=nIn, N:=5 ), N:=13 ));
//***********************************************************************
//***********************************************************************
// Scale pulses to units (mm)
//***********************************************************************
IF fFactor <> 0 THEN
fPosition := nEncPls / fFactor;
ELSE
fPosition := nEncPls;
END_IF;
nPosition := REAL_TO_DINT(fPosition);
//***********************************************************************
//***********************************************************************
// Direction change
//***********************************************************************
IF NOT bDirect THEN
nActPos := nPosition + nOffset;
ELSE
nActPos := nMax - nPosition + nOffset;
END_IF;
//***********************************************************************
//***********************************************************************
// Encoder error status
//***********************************************************************
bEncErrSt := nPosErr <> 0 OR nPosition < 0;
//***********************************************************************
//***********************************************************************
// Speed calculation
//***********************************************************************
// Measure current cycletime in milli second(测量当前的以毫秒为单位的循环时间)
CycleTime(IN:=TRUE, PT:=T#1h);
CycleTimeAct := TIME_TO_DINT(CycleTime.ET);
// Cycle time at least
IF CycleTimeAct >= 200 THEN
CycleTime(IN:=FALSE, PT:=T#1h);
CycleTime(IN:=TRUE, PT:=T#1h);
// Speed (m/min)
fSpeed := ( fPosition - fPrevPosit ) / ( CycleTimeAct * 0.001 ) * 0.06;
// Update previous position
fPrevPosit := fPosition;
END_IF;
//***********************************************************************
END_FUNCTION_BLOCK
CycleTime(IN:=TRUE, PT:=T#1h); //建立一个定时器,定时时间为1小时
CycleTimeAct := TIME_TO_DINT(CycleTime.ET); //将已经过去的定时时间给变量CycleTimeAct
// Cycle time at least
IF CycleTimeAct >= 200 THEN
CycleTime(IN:=FALSE, PT:=T#1h);
CycleTime(IN:=TRUE, PT:=T#1h); //定时时间200MS复位一下定时器
// Speed (m/min)
fSpeed := ( fPosition - fPrevPosit ) / ( CycleTimeAct * 0.001 ) * 0.06;
// Update previous position//速度等于位移/时间
fPrevPosit := fPosition; //当前位置保存
END_IF;
位移:当前位置-上一次位置
时间:200MS*0.001得到秒,然后乘以0.06得到分