移模功能整理文档
1. 概述
本文档详细描述注塑机移模功能的实现,包括左移模、右移模动作的控制逻辑、参数配置和MODBUS通信适配。移模功能用于调整模具位置,支持单移、左移、右移、双移等多种工作模式。
2. 移模功能块定义 (FB_MoldTransfer)
2.1 变量定义
pascal
FUNCTION_BLOCK FB_MoldTransfer
VAR_INPUT
Execute : BOOL; // 执行触发信号
Mode : INT; // 移模模式 (0: 停止, 1: 单移, 2: 左移, 3: 右移, 4: 双移, 5: 三工正转)
SimSpeed : INT := 5; // 仿真速度 (1-10, 10最快)
UseDIPositionSensor : BOOL := FALSE; // 是否使用DI输入信号作为到位检测
LeftPositionSensorInput : BOOL := FALSE; // 左移到位传感器
RightPositionSensorInput : BOOL := FALSE; // 右移到位传感器
UseRealADInput : BOOL := FALSE; // 是否使用实际电子尺AD输入
LeftRealADInput : INT := 0; // 左移模实际电子尺AD输入值
RightRealADInput : INT := 0; // 右移模实际电子尺AD输入值
// 可配置的参数(使用INT类型,符合MODBUS协议要求)
LeftFastPressure : INT := 440; // 左移快速压力(bar*10)
LeftFastFlow : INT := 440; // 左移快速流量(%*10)
LeftSlowPressure : INT := 550; // 左移慢速压力(bar*10)
LeftSlowFlow : INT := 550; // 左移慢速流量(%*10)
RightFastPressure : INT := 660; // 右移快速压力(bar*10)
RightFastFlow : INT := 660; // 右移快速流量(%*10)
RightSlowPressure : INT := 770; // 右移慢速压力(bar*10)
RightSlowFlow : INT := 770; // 右移慢速流量(%*10)
PositionPressure : INT := 440; // 定位进压力(bar*10)
PositionFlow : INT := 440; // 定位进流量(%*10)
RetractPressure : INT := 550; // 定位退压力(bar*10)
RetractFlow : INT := 550; // 定位退流量(%*10)
// 延时参数
LeftMoveDelay : INT := 5; // 左移延时(0.01s*100,即500ms)
RightMoveDelay : INT := 2; // 右移延时(0.01s*100,即200ms)
END_VAR
VAR_OUTPUT
Done : BOOL; // 完成信号
Error : BOOL; // 错误信号
CurrentStep : INT; // 当前执行步骤
StepName : STRING; // 当前步骤名称
LeftCurrentPosition : INT; // 左移模当前位置
RightCurrentPosition : INT; // 右移模当前位置
// 输出控制信号(BOOL类型)
LeftMoveOut : BOOL; // 左移模输出
RightMoveOut : BOOL; // 右移模输出
PositionInOut : BOOL; // 定位进输出
PositionOutOut : BOOL; // 定位退输出
FastTransferOut : BOOL; // 移模快速输出
END_VAR
VAR
// 内部变量
Execute_prev : BOOL := FALSE;
IsRunning : BOOL := FALSE;
CurrentStepInternal : INT := 0;
StepStartTime : INT := 0;
DelayTimer : INT := 0;
// 位置模拟相关变量
LeftPosition : INT := 0;
RightPosition : INT := 0;
PositionIncrement : INT := 0;
// 目标位置
LeftTargetPosition : INT := 100;
RightTargetPosition : INT := 100;
// 速度配置
FastSpeed : INT := 3;
SlowSpeed : INT := 1;
// 状态标志
IsLeftMoving : BOOL := FALSE;
IsRightMoving : BOOL := FALSE;
IsPositioning : BOOL := FALSE;
END_VAR2.2 接口设计
| 输入/输出 | 变量名 | 类型 | 功能说明 |
|---|---|---|---|
| 输入 | Execute | BOOL | 执行触发信号,上升沿启动 |
| 输入 | Mode | INT | 移模模式选择 |
| 输入 | SimSpeed | INT | 仿真速度调节 |
| 输入 | LeftFastPressure | INT | 左移快速压力(bar*10) |
| 输入 | LeftFastFlow | INT | 左移快速流量(%*10) |
| 输出 | Done | BOOL | 动作完成信号 |
| 输出 | Error | BOOL | 错误信号 |
| 输出 | LeftMoveOut | BOOL | 左移模输出控制 |
| 输出 | RightMoveOut | BOOL | 右移模输出控制 |
2.3 方法实现
pascal
// 首先检查Execute状态,如果为FALSE则立即复位所有状态(最高优先级)
IF Execute = FALSE THEN
IsRunning := FALSE;
CurrentStepInternal := 0;
StepStartTime := 0;
Done := FALSE;
Error := FALSE;
StepName := '';
CurrentStep := 0;
LeftMoveOut := FALSE;
RightMoveOut := FALSE;
PositionInOut := FALSE;
PositionOutOut := FALSE;
FastTransferOut := FALSE;
IsLeftMoving := FALSE;
IsRightMoving := FALSE;
IsPositioning := FALSE;
// 更新Execute_prev为当前值,确保下次调用时能够正确检测上升沿
Execute_prev := FALSE;
RETURN; // 直接返回,不执行后续逻辑
END_IF;
// 检测Execute信号上升沿或Execute为TRUE但需要重新初始化
IF (Execute_prev = FALSE AND Execute = TRUE) OR
(Execute = TRUE AND IsRunning = FALSE) OR
(Execute = TRUE AND CurrentStepInternal = 9) THEN
// 新的执行周期开始或需要重新启动,强制重置所有状态
IsRunning := TRUE;
CurrentStepInternal := 1;
StepStartTime := 0;
Done := FALSE;
Error := FALSE;
LeftMoveOut := FALSE;
RightMoveOut := FALSE;
PositionInOut := FALSE;
PositionOutOut := FALSE;
FastTransferOut := FALSE;
IsLeftMoving := FALSE;
IsRightMoving := FALSE;
IsPositioning := FALSE;
END_IF;
// 在所有逻辑处理完成后,再保存当前的Execute状态用于下一个扫描周期
Execute_prev := Execute;
// 限制SimSpeed范围
IF SimSpeed < 1 THEN
SimSpeed := 1;
END_IF;
IF SimSpeed > 10 THEN
SimSpeed := 10;
END_IF;
// 主执行逻辑 - 只有当IsRunning为TRUE时执行
IF IsRunning = TRUE THEN
StepStartTime := StepStartTime + 1;
CurrentStep := CurrentStepInternal;
// 基于速度调整位置增量
PositionIncrement := 1 + (SimSpeed - 1) / 2; // 根据SimSpeed调整速度
// 步骤1: 移模准备
IF CurrentStepInternal = 1 THEN
StepName := '准备移模';
IF StepStartTime >= 10 THEN // 简单的准备时间
// 根据移模模式决定下一步操作
CASE Mode OF
0: // 停止模式
CurrentStepInternal := 9;
1: // 单移模式
CurrentStepInternal := 2;
2: // 左移模式
CurrentStepInternal := 3;
3: // 右移模式
CurrentStepInternal := 5;
4: // 双移模式
CurrentStepInternal := 7;
5: // 三工正转模式
CurrentStepInternal := 2;
END_CASE;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤2: 单移模式(此处简化为左移)
IF CurrentStepInternal = 2 THEN
StepName := '单移模式';
LeftMoveOut := TRUE;
FastTransferOut := TRUE;
IsLeftMoving := TRUE;
// 模拟位置更新
IF NOT UseRealADInput THEN
LeftPosition := LeftPosition + (FastSpeed * PositionIncrement);
IF LeftPosition > LeftTargetPosition THEN
LeftPosition := LeftTargetPosition;
END_IF;
END_IF;
// 检查是否到位
IF (UseDIPositionSensor AND LeftPositionSensorInput) OR
(NOT UseDIPositionSensor AND ((UseRealADInput AND LeftRealADInput >= LeftTargetPosition) OR
(NOT UseRealADInput AND LeftPosition >= LeftTargetPosition))) THEN
LeftMoveOut := FALSE;
FastTransferOut := FALSE;
IsLeftMoving := FALSE;
CurrentStepInternal := 9;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤3: 左移模式 - 快速
IF CurrentStepInternal = 3 THEN
StepName := '左移快速';
LeftMoveOut := TRUE;
FastTransferOut := TRUE;
IsLeftMoving := TRUE;
// 左移延时
IF DelayTimer < LeftMoveDelay THEN
DelayTimer := DelayTimer + 1;
RETURN;
END_IF;
// 模拟位置更新
IF NOT UseRealADInput THEN
LeftPosition := LeftPosition + (FastSpeed * PositionIncrement);
IF LeftPosition > LeftTargetPosition THEN
LeftPosition := LeftTargetPosition;
END_IF;
END_IF;
// 检查是否到达减速点(此处简化)
IF LeftPosition >= LeftTargetPosition * 0.7 THEN
CurrentStepInternal := 4;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤4: 左移模式 - 慢速
IF CurrentStepInternal = 4 THEN
StepName := '左移慢速';
LeftMoveOut := TRUE;
FastTransferOut := FALSE; // 关闭快速
IsLeftMoving := TRUE;
// 模拟位置更新
IF NOT UseRealADInput THEN
LeftPosition := LeftPosition + (SlowSpeed * PositionIncrement);
IF LeftPosition >= LeftTargetPosition THEN
LeftPosition := LeftTargetPosition;
LeftMoveOut := FALSE;
IsLeftMoving := FALSE;
CurrentStepInternal := 9;
StepStartTime := 0;
END_IF;
END_IF;
// 检查是否到位
IF (UseDIPositionSensor AND LeftPositionSensorInput) OR
(NOT UseDIPositionSensor AND ((UseRealADInput AND LeftRealADInput >= LeftTargetPosition) OR
(NOT UseRealADInput AND LeftPosition >= LeftTargetPosition))) THEN
LeftMoveOut := FALSE;
IsLeftMoving := FALSE;
CurrentStepInternal := 9;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤5: 右移模式 - 快速
IF CurrentStepInternal = 5 THEN
StepName := '右移快速';
RightMoveOut := TRUE;
FastTransferOut := TRUE;
IsRightMoving := TRUE;
// 右移延时
IF DelayTimer < RightMoveDelay THEN
DelayTimer := DelayTimer + 1;
RETURN;
END_IF;
// 模拟位置更新
IF NOT UseRealADInput THEN
RightPosition := RightPosition + (FastSpeed * PositionIncrement);
IF RightPosition > RightTargetPosition THEN
RightPosition := RightTargetPosition;
END_IF;
END_IF;
// 检查是否到达减速点(此处简化)
IF RightPosition >= RightTargetPosition * 0.7 THEN
CurrentStepInternal := 6;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤6: 右移模式 - 慢速
IF CurrentStepInternal = 6 THEN
StepName := '右移慢速';
RightMoveOut := TRUE;
FastTransferOut := FALSE; // 关闭快速
IsRightMoving := TRUE;
// 模拟位置更新
IF NOT UseRealADInput THEN
RightPosition := RightPosition + (SlowSpeed * PositionIncrement);
IF RightPosition >= RightTargetPosition THEN
RightPosition := RightTargetPosition;
RightMoveOut := FALSE;
IsRightMoving := FALSE;
CurrentStepInternal := 9;
StepStartTime := 0;
END_IF;
END_IF;
// 检查是否到位
IF (UseDIPositionSensor AND RightPositionSensorInput) OR
(NOT UseDIPositionSensor AND ((UseRealADInput AND RightRealADInput >= RightTargetPosition) OR
(NOT UseRealADInput AND RightPosition >= RightTargetPosition))) THEN
RightMoveOut := FALSE;
IsRightMoving := FALSE;
CurrentStepInternal := 9;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤7: 双移模式 - 左移
IF CurrentStepInternal = 7 THEN
StepName := '双移模式-左移';
LeftMoveOut := TRUE;
FastTransferOut := TRUE;
IsLeftMoving := TRUE;
// 模拟位置更新
IF NOT UseRealADInput THEN
LeftPosition := LeftPosition + (FastSpeed * PositionIncrement);
IF LeftPosition >= LeftTargetPosition THEN
LeftPosition := LeftTargetPosition;
LeftMoveOut := FALSE;
IsLeftMoving := FALSE;
CurrentStepInternal := 8;
StepStartTime := 0;
END_IF;
END_IF;
// 检查是否到位
IF (UseDIPositionSensor AND LeftPositionSensorInput) OR
(NOT UseDIPositionSensor AND ((UseRealADInput AND LeftRealADInput >= LeftTargetPosition) OR
(NOT UseRealADInput AND LeftPosition >= LeftTargetPosition))) THEN
LeftMoveOut := FALSE;
IsLeftMoving := FALSE;
CurrentStepInternal := 8;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤8: 双移模式 - 右移
IF CurrentStepInternal = 8 THEN
StepName := '双移模式-右移';
RightMoveOut := TRUE;
FastTransferOut := TRUE;
IsRightMoving := TRUE;
// 模拟位置更新
IF NOT UseRealADInput THEN
RightPosition := RightPosition + (FastSpeed * PositionIncrement);
IF RightPosition >= RightTargetPosition THEN
RightPosition := RightTargetPosition;
RightMoveOut := FALSE;
IsRightMoving := FALSE;
CurrentStepInternal := 9;
StepStartTime := 0;
END_IF;
END_IF;
// 检查是否到位
IF (UseDIPositionSensor AND RightPositionSensorInput) OR
(NOT UseDIPositionSensor AND ((UseRealADInput AND RightRealADInput >= RightTargetPosition) OR
(NOT UseRealADInput AND RightPosition >= RightTargetPosition))) THEN
RightMoveOut := FALSE;
IsRightMoving := FALSE;
CurrentStepInternal := 9;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤9: 完成
IF CurrentStepInternal = 9 THEN
StepName := '移模完成';
Done := TRUE;
LeftMoveOut := FALSE;
RightMoveOut := FALSE;
FastTransferOut := FALSE;
IsLeftMoving := FALSE;
IsRightMoving := FALSE;
// 保持完成状态一段时间
IF StepStartTime >= 5 THEN
// 可以选择在此处自动重置状态或等待Execute变为FALSE
// IsRunning := FALSE;
// CurrentStepInternal := 0;
END_IF;
END_IF;
// 更新输出位置
IF UseRealADInput THEN
LeftCurrentPosition := LeftRealADInput;
RightCurrentPosition := RightRealADInput;
ELSE
LeftCurrentPosition := LeftPosition;
RightCurrentPosition := RightPosition;
END_IF;
END_IF;3. 中子功能块定义 (FB_CorePull)
3.1 变量定义
pascal
FUNCTION_BLOCK FB_CorePull
VAR_INPUT
Execute : BOOL; // 执行触发信号
Mode : INT; // 中子模式 (0: 停止, 1: 中子进, 2: 中子退)
SimSpeed : INT := 5; // 仿真速度 (1-10, 10最快)
UseDIPositionSensor : BOOL := FALSE; // 是否使用DI输入信号作为到位检测
ForwardPositionSensorInput : BOOL := FALSE; // 前进到位传感器
BackwardPositionSensorInput : BOOL := FALSE; // 后退到位传感器
UseRealADInput : BOOL := FALSE; // 是否使用实际电子尺AD输入
RealADInput : INT := 0; // 实际电子尺AD输入值
// 可配置的参数(使用INT类型,符合MODBUS协议要求)
ForwardPressure : INT := 350; // 前进压力(bar*10)
ForwardFlow : INT := 300; // 前进流量(%*10)
ForwardPos : INT := 600; // 前进到位位置(mm*10)
ForwardTimeLimit : INT := 15; // 前进时间限制(0.1s*10,即1.5s)
BackwardPressure : INT := 400; // 后退压力(bar*10)
BackwardFlow : INT := 350; // 后退流量(%*10)
BackwardPos : INT := 0; // 后退到位位置(mm*10)
BackwardTimeLimit : INT := 15; // 后退时间限制(0.1s*10,即1.5s)
// 延时参数
ForwardDelay : INT := 0; // 前进延时(0.01s*100)
BackwardDelay : INT := 0; // 后退延时(0.01s*100)
END_VAR
VAR_OUTPUT
Done : BOOL; // 完成信号
Error : BOOL; // 错误信号
CurrentStep : INT; // 当前执行步骤
StepName : STRING; // 当前步骤名称
CurrentPosition : INT; // 当前位置
// 输出控制信号(BOOL类型)
CoreInOut : BOOL; // 中子进输出
CoreOutOut : BOOL; // 中子退输出
END_VAR
VAR
// 内部变量
Execute_prev : BOOL := FALSE;
IsRunning : BOOL := FALSE;
CurrentStepInternal : INT := 0;
StepStartTime : INT := 0;
DelayTimer : INT := 0;
ActionTimer : INT := 0;
// 位置模拟相关变量
Position : INT := 0;
PositionIncrement : INT := 0;
// 速度配置
ForwardSpeed : INT := 2;
BackwardSpeed : INT := 2;
END_VAR3.2 方法实现
pascal
// 首先检查Execute状态,如果为FALSE则立即复位所有状态(最高优先级)
IF Execute = FALSE THEN
IsRunning := FALSE;
CurrentStepInternal := 0;
StepStartTime := 0;
DelayTimer := 0;
ActionTimer := 0;
Done := FALSE;
Error := FALSE;
StepName := '';
CurrentStep := 0;
CoreInOut := FALSE;
CoreOutOut := FALSE;
// 更新Execute_prev为当前值,确保下次调用时能够正确检测上升沿
Execute_prev := FALSE;
RETURN; // 直接返回,不执行后续逻辑
END_IF;
// 检测Execute信号上升沿或Execute为TRUE但需要重新初始化
IF (Execute_prev = FALSE AND Execute = TRUE) OR
(Execute = TRUE AND IsRunning = FALSE) OR
(Execute = TRUE AND CurrentStepInternal = 6) THEN
// 新的执行周期开始或需要重新启动,强制重置所有状态
IsRunning := TRUE;
CurrentStepInternal := 1;
StepStartTime := 0;
DelayTimer := 0;
ActionTimer := 0;
Done := FALSE;
Error := FALSE;
CoreInOut := FALSE;
CoreOutOut := FALSE;
END_IF;
// 在所有逻辑处理完成后,再保存当前的Execute状态用于下一个扫描周期
Execute_prev := Execute;
// 限制SimSpeed范围
IF SimSpeed < 1 THEN
SimSpeed := 1;
END_IF;
IF SimSpeed > 10 THEN
SimSpeed := 10;
END_IF;
// 主执行逻辑 - 只有当IsRunning为TRUE时执行
IF IsRunning = TRUE THEN
StepStartTime := StepStartTime + 1;
CurrentStep := CurrentStepInternal;
// 基于速度调整位置增量
PositionIncrement := 1 + (SimSpeed - 1) / 2; // 根据SimSpeed调整速度
// 步骤1: 中子准备
IF CurrentStepInternal = 1 THEN
StepName := '准备中子动作';
IF StepStartTime >= 5 THEN // 简单的准备时间
// 根据中子模式决定下一步操作
CASE Mode OF
0: // 停止模式
CurrentStepInternal := 6;
1: // 中子进
CurrentStepInternal := 2;
2: // 中子退
CurrentStepInternal := 4;
END_CASE;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤2: 中子进延时
IF CurrentStepInternal = 2 THEN
StepName := '中子进延时';
IF DelayTimer < ForwardDelay THEN
DelayTimer := DelayTimer + 1;
ELSE
CurrentStepInternal := 3;
StepStartTime := 0;
ActionTimer := 0;
END_IF;
END_IF;
// 步骤3: 中子进动作
IF CurrentStepInternal = 3 THEN
StepName := '中子进动作';
CoreInOut := TRUE;
ActionTimer := ActionTimer + 1;
// 模拟位置更新
IF NOT UseRealADInput THEN
Position := Position + (ForwardSpeed * PositionIncrement);
IF Position > ForwardPos THEN
Position := ForwardPos;
END_IF;
END_IF;
// 检查是否到位
IF (UseDIPositionSensor AND ForwardPositionSensorInput) OR
(NOT UseDIPositionSensor AND ((UseRealADInput AND RealADInput >= ForwardPos) OR
(NOT UseRealADInput AND Position >= ForwardPos))) THEN
CoreInOut := FALSE;
CurrentStepInternal := 6;
StepStartTime := 0;
END_IF;
// 检查超时
IF ActionTimer > ForwardTimeLimit THEN
CoreInOut := FALSE;
Error := TRUE;
CurrentStepInternal := 6;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤4: 中子退延时
IF CurrentStepInternal = 4 THEN
StepName := '中子退延时';
IF DelayTimer < BackwardDelay THEN
DelayTimer := DelayTimer + 1;
ELSE
CurrentStepInternal := 5;
StepStartTime := 0;
ActionTimer := 0;
END_IF;
END_IF;
// 步骤5: 中子退动作
IF CurrentStepInternal = 5 THEN
StepName := '中子退动作';
CoreOutOut := TRUE;
ActionTimer := ActionTimer + 1;
// 模拟位置更新
IF NOT UseRealADInput THEN
Position := Position - (BackwardSpeed * PositionIncrement);
IF Position < BackwardPos THEN
Position := BackwardPos;
END_IF;
END_IF;
// 检查是否到位
IF (UseDIPositionSensor AND BackwardPositionSensorInput) OR
(NOT UseDIPositionSensor AND ((UseRealADInput AND RealADInput <= BackwardPos) OR
(NOT UseRealADInput AND Position <= BackwardPos))) THEN
CoreOutOut := FALSE;
CurrentStepInternal := 6;
StepStartTime := 0;
END_IF;
// 检查超时
IF ActionTimer > BackwardTimeLimit THEN
CoreOutOut := FALSE;
Error := TRUE;
CurrentStepInternal := 6;
StepStartTime := 0;
END_IF;
END_IF;
// 步骤6: 完成
IF CurrentStepInternal = 6 THEN
StepName := '中子动作完成';
Done := TRUE;
CoreInOut := FALSE;
CoreOutOut := FALSE;
// 保持完成状态一段时间
IF StepStartTime >= 5 THEN
// 可以选择在此处自动重置状态或等待Execute变为FALSE
// IsRunning := FALSE;
// CurrentStepInternal := 0;
END_IF;
END_IF;
// 更新输出位置
IF UseRealADInput THEN
CurrentPosition := RealADInput;
ELSE
CurrentPosition := Position;
END_IF;
END_IF;4. MODBUS通信适配指南
4.1 数据类型说明
为确保MODBUS通信的一致性,所有参数均使用INT类型存储,并遵循以下原则:
压力参数:以bar为单位,实际存储值 = 显示值 * 10
- 例如:显示35.0bar,实际存储350
流量参数:以%为单位,实际存储值 = 显示值 * 10
- 例如:显示30.0%,实际存储300
位置参数:以mm为单位,实际存储值 = 显示值 * 10
- 例如:显示60.0mm,实际存储600
时间参数:以秒为单位,根据精度要求选择合适的放大倍数
- 延时参数:显示值*100(0.01s精度)
- 时间限制:显示值*10(0.1s精度)
4.2 HMI与PLC数据交互
HMI读取参数:从PLC读取INT值,显示时除以相应的倍数
- 例如:PLC值350 → HMI显示35.0bar
HMI写入参数:用户输入显示值,HMI乘以相应的倍数后写入PLC
- 例如:用户输入35.0bar → HMI写入350到PLC
4.3 程序内数据处理
在程序内部需要进行计算时,应将INT类型参数转换为REAL类型:
pascal
// 例如:计算实际延时时间(秒)
ActualDelayTime := TO_REAL(LeftMoveDelay) / 100.0;5. 使用注意事项
互锁保护:移模动作必须与开模/合模动作互锁,避免干涉
位置检测:确保位置传感器安装正确,信号可靠
参数调整:压力和流量参数应根据实际负载情况合理调整
超时保护:所有动作都设置了超时保护,超时将触发错误信号
时序协调:中子动作应与开模、顶出等动作协调,避免干涉
模拟与实际:功能块支持模拟模式和实际输入模式,可通过参数切换
MODBUS一致性:确保所有与HMI交互的参数都使用INT类型,遵循数据放大规则
