Skip to content

更新: 3/24/2026, 2:52:03 PM 字数: 0 字 时长: 0 分钟

转盘功能整理文档

1. 概述

本文档详细描述注塑机转盘功能的实现,包括转盘旋转控制逻辑、工位定位、速度控制和MODBUS通信适配。转盘功能用于多工位注塑生产,支持正反转换位和多工位定位功能。

2. 转盘功能块定义 (FB_Turntable)

2.1 变量定义

pascal
FUNCTION_BLOCK FB_Turntable
VAR_INPUT
  Execute : BOOL;                 // 执行触发信号
  Mode : INT;                     // 转盘模式 (0: 停止, 1: 正转, 2: 反转, 3: 回零)
  SimSpeed : INT := 5;            // 仿真速度 (1-10, 10最快)
  UsePhotoelectricSensor : BOOL := FALSE; // 是否使用光电眼
  PhotoelectricSensorInput : BOOL := FALSE; // 光电眼输入
  UseRealADInput : BOOL := FALSE;    // 是否使用实际编码器AD输入
  RealEncoderInput : INT := 0;       // 实际编码器AD输入值
  
  // 可配置的参数(使用INT类型,符合MODBUS协议要求)
  Position1Degree : INT := 1800;    // 工位一位置(度*10)
  Position2Degree : INT := 0;       // 工位二位置(度*10)
  Position3Degree : INT := 3600;    // 工位三位置(度*10)
  RotateSpeed : INT := 0;          // 转盘运行速度(%*10)
  ManualSpeed : INT := 100;        // 手动运行速度(%*10)
  PositiveDelay : INT := 20;       // 正转延时(0.01s*100,即0.2s)
  ReverseDelay : INT := 20;        // 反转延时(0.01s*100,即0.2s)
  PositionPressure : INT := 1000;  // 定位进压力(bar*10)
  PositionFlow : INT := 990;       // 定位进流量(%*10)
  RetractPressure : INT := 1000;   // 定位退压力(bar*10)
  RetractFlow : INT := 990;        // 定位退流量(%*10)
END_VAR
VAR_OUTPUT
  Done : BOOL;                      // 完成信号
  Error : BOOL;                     // 错误信号
  CurrentStep : INT;                // 当前执行步骤
  StepName : STRING;                // 当前步骤名称
  CurrentPosition : INT;            // 当前位置(度*10)
  CurrentRPM : INT;                 // 当前转速(*10)
  
  // 输出控制信号(BOOL类型)
  TurntableRunOut : BOOL;           // 转盘运行输出
  TurntableForwardOut : BOOL;       // 转盘正转输出
  TurntableReverseOut : BOOL;       // 转盘反转输出
  PositionInOut : BOOL;             // 定位进输出
  PositionOutOut : 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;
  RotationSpeed : INT := 0;
  TargetPosition : INT := 0;
  
  // 状态标志
  IsRotating : BOOL := FALSE;
  IsPositioning : BOOL := FALSE;
  IsHoming : BOOL := FALSE;
END_VAR

2.2 接口设计

输入/输出变量名类型功能说明
输入ExecuteBOOL执行触发信号,上升沿启动
输入ModeINT转盘模式选择
输入SimSpeedINT仿真速度调节
输入Position1DegreeINT工位一位置(度*10)
输入RotateSpeedINT转盘运行速度(%*10)
输出DoneBOOL动作完成信号
输出ErrorBOOL错误信号
输出CurrentPositionINT当前位置(度*10)
输出TurntableForwardOutBOOL转盘正转输出控制
输出TurntableReverseOutBOOL转盘反转输出控制

2.3 方法实现

pascal
// 首先检查Execute状态,如果为FALSE则立即复位所有状态(最高优先级)
IF Execute = FALSE THEN
  IsRunning := FALSE;
  CurrentStepInternal := 0;
  StepStartTime := 0;
  DelayTimer := 0;
  ActionTimer := 0;
  Done := FALSE;
  Error := FALSE;
  StepName := '';
  CurrentStep := 0;
  TurntableRunOut := FALSE;
  TurntableForwardOut := FALSE;
  TurntableReverseOut := FALSE;
  PositionInOut := FALSE;
  PositionOutOut := FALSE;
  IsRotating := FALSE;
  IsPositioning := FALSE;
  IsHoming := 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;
  DelayTimer := 0;
  ActionTimer := 0;
  Done := FALSE;
  Error := FALSE;
  TurntableRunOut := FALSE;
  TurntableForwardOut := FALSE;
  TurntableReverseOut := FALSE;
  PositionInOut := FALSE;
  PositionOutOut := FALSE;
  IsRotating := FALSE;
  IsPositioning := FALSE;
  IsHoming := 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 := 9;
        1: // 正转
          CurrentStepInternal := 2;
        2: // 反转
          CurrentStepInternal := 4;
        3: // 回零
          CurrentStepInternal := 7;
      END_CASE;
      StepStartTime := 0;
    END_IF;
  END_IF;
  
  // 步骤2: 正转延时
  IF CurrentStepInternal = 2 THEN
    StepName := '正转延时';
    IF DelayTimer < PositiveDelay THEN
      DelayTimer := DelayTimer + 1;
    ELSE
      CurrentStepInternal := 3;
      StepStartTime := 0;
      ActionTimer := 0;
      
      // 设置目标位置(根据工位一位置)
      TargetPosition := Position1Degree;
    END_IF;
  END_IF;
  
  // 步骤3: 正转动作
  IF CurrentStepInternal = 3 THEN
    StepName := '正转动作';
    TurntableRunOut := TRUE;
    TurntableForwardOut := TRUE;
    IsRotating := TRUE;
    ActionTimer := ActionTimer + 1;
    
    // 计算旋转速度
    RotationSpeed := (RotateSpeed / 10) * PositionIncrement;
    IF RotationSpeed < 1 THEN
      RotationSpeed := 1;
    END_IF;
    
    // 模拟位置更新
    IF NOT UseRealADInput THEN
      Position := Position + RotationSpeed;
      // 处理360度循环
      IF Position >= 3600 THEN // 360度*10
        Position := Position - 3600;
      END_IF;
    END_IF;
    
    // 检查是否到达目标位置
    IF (UseRealADInput AND RealEncoderInput >= TargetPosition - 5 AND RealEncoderInput <= TargetPosition + 5) OR 
       (NOT UseRealADInput AND Position >= TargetPosition - 5 AND Position <= TargetPosition + 5) THEN
      TurntableRunOut := FALSE;
      TurntableForwardOut := FALSE;
      IsRotating := FALSE;
      CurrentStepInternal := 5;
      StepStartTime := 0;
    END_IF;
    
    // 光电眼检查
    IF UsePhotoelectricSensor AND NOT PhotoelectricSensorInput THEN
      TurntableRunOut := FALSE;
      TurntableForwardOut := FALSE;
      IsRotating := FALSE;
      Error := TRUE;
      CurrentStepInternal := 9;
      StepStartTime := 0;
    END_IF;
  END_IF;
  
  // 步骤4: 反转延时
  IF CurrentStepInternal = 4 THEN
    StepName := '反转延时';
    IF DelayTimer < ReverseDelay THEN
      DelayTimer := DelayTimer + 1;
    ELSE
      CurrentStepInternal := 5;
      StepStartTime := 0;
      ActionTimer := 0;
      
      // 设置目标位置(根据工位二位置)
      TargetPosition := Position2Degree;
    END_IF;
  END_IF;
  
  // 步骤5: 反转动作
  IF CurrentStepInternal = 5 THEN
    StepName := '反转动作';
    TurntableRunOut := TRUE;
    TurntableReverseOut := TRUE;
    IsRotating := TRUE;
    ActionTimer := ActionTimer + 1;
    
    // 计算旋转速度
    RotationSpeed := (RotateSpeed / 10) * PositionIncrement;
    IF RotationSpeed < 1 THEN
      RotationSpeed := 1;
    END_IF;
    
    // 模拟位置更新
    IF NOT UseRealADInput THEN
      Position := Position - RotationSpeed;
      // 处理360度循环
      IF Position < 0 THEN
        Position := Position + 3600;
      END_IF;
    END_IF;
    
    // 检查是否到达目标位置
    IF (UseRealADInput AND RealEncoderInput >= TargetPosition - 5 AND RealEncoderInput <= TargetPosition + 5) OR 
       (NOT UseRealADInput AND Position >= TargetPosition - 5 AND Position <= TargetPosition + 5) THEN
      TurntableRunOut := FALSE;
      TurntableReverseOut := FALSE;
      IsRotating := FALSE;
      CurrentStepInternal := 6;
      StepStartTime := 0;
    END_IF;
    
    // 光电眼检查
    IF UsePhotoelectricSensor AND NOT PhotoelectricSensorInput THEN
      TurntableRunOut := FALSE;
      TurntableReverseOut := FALSE;
      IsRotating := FALSE;
      Error := TRUE;
      CurrentStepInternal := 9;
      StepStartTime := 0;
    END_IF;
  END_IF;
  
  // 步骤6: 定位动作
  IF CurrentStepInternal = 6 THEN
    StepName := '定位动作';
    PositionInOut := TRUE;
    IsPositioning := TRUE;
    
    // 模拟定位完成
    IF StepStartTime >= 10 THEN
      PositionInOut := FALSE;
      IsPositioning := FALSE;
      CurrentStepInternal := 9;
      StepStartTime := 0;
    END_IF;
  END_IF;
  
  // 步骤7: 回零动作
  IF CurrentStepInternal = 7 THEN
    StepName := '回零动作';
    IsHoming := TRUE;
    TurntableRunOut := TRUE;
    TurntableReverseOut := TRUE;
    ActionTimer := ActionTimer + 1;
    
    // 模拟位置更新
    IF NOT UseRealADInput THEN
      Position := Position - 5; // 固定速度回零
      IF Position < 0 THEN
        Position := 0;
      END_IF;
    END_IF;
    
    // 检查是否到达零点
    IF (UseRealADInput AND RealEncoderInput <= 5) OR 
       (NOT UseRealADInput AND Position <= 5) THEN
      TurntableRunOut := FALSE;
      TurntableReverseOut := FALSE;
      IsHoming := FALSE;
      CurrentStepInternal := 8;
      StepStartTime := 0;
    END_IF;
  END_IF;
  
  // 步骤8: 回零确认
  IF CurrentStepInternal = 8 THEN
    StepName := '回零确认';
    IF StepStartTime >= 5 THEN
      CurrentStepInternal := 9;
      StepStartTime := 0;
    END_IF;
  END_IF;
  
  // 步骤9: 完成
  IF CurrentStepInternal = 9 THEN
    StepName := '转盘动作完成';
    Done := TRUE;
    TurntableRunOut := FALSE;
    TurntableForwardOut := FALSE;
    TurntableReverseOut := FALSE;
    PositionInOut := FALSE;
    IsRotating := FALSE;
    IsPositioning := FALSE;
    IsHoming := FALSE;
    
    // 保持完成状态一段时间
    IF StepStartTime >= 5 THEN
      // 可以选择在此处自动重置状态或等待Execute变为FALSE
      // IsRunning := FALSE;
      // CurrentStepInternal := 0;
    END_IF;
  END_IF;
  
  // 更新输出位置和转速
  IF UseRealADInput THEN
    CurrentPosition := RealEncoderInput;
  ELSE
    CurrentPosition := Position;
  END_IF;
  
  // 计算当前转速(简化)
  CurrentRPM := RotationSpeed * 10;
END_IF;

3. 转盘功能实现细节

3.1 工作模式

  1. 正转模式:转盘正向旋转,定位到工位一位置
  2. 反转模式:转盘反向旋转,定位到工位二位置
  3. 回零模式:转盘旋转回到零点位置
  4. 停止模式:停止所有转盘动作

3.2 安全保护机制

  1. 光电眼检测:支持光电眼输入,异常时停止动作
  2. 位置检测:精确的位置控制,确保准确到位
  3. 速度控制:可配置的运行速度,适合不同负载情况
  4. 延时控制:支持正转和反转前的延时,确保动作协调
  5. 定位功能:到位后进行精确定位,提高加工精度

3.3 工位设置

根据不同的工作模式,支持多种工位配置:

  • 正反转型:工位一位置约180度(170-190),工位二位置约0度(350-10)
  • 正转三工位型:工位一位置约120度(110-130),工位二位置约240度(230-250),工位三位置约360度(350-370)

4. MODBUS通信适配指南

4.1 数据类型说明

为确保MODBUS通信的一致性,所有参数均使用INT类型存储,并遵循以下原则:

  1. 角度参数:以度为单位,实际存储值 = 显示值 * 10

    • 例如:显示180.0度,实际存储1800
  2. 速度参数:以%为单位,实际存储值 = 显示值 * 10

    • 例如:显示99.0%,实际存储990
  3. 时间参数:以秒为单位,根据精度要求选择合适的放大倍数

    • 延时参数:显示值*100(0.01s精度)
    • 例如:显示0.20s,实际存储20
  4. 压力参数:以bar为单位,实际存储值 = 显示值 * 10

    • 例如:显示100.0bar,实际存储1000

4.2 HMI与PLC数据交互

  • HMI读取参数:从PLC读取INT值,显示时除以相应的倍数

    • 例如:PLC值1800 → HMI显示180.0度
  • HMI写入参数:用户输入显示值,HMI乘以相应的倍数后写入PLC

    • 例如:用户输入180.0度 → HMI写入1800到PLC

4.3 程序内数据处理

在程序内部需要进行计算时,应将INT类型参数转换为REAL类型:

pascal
// 例如:计算实际角度(度)
ActualPosition := TO_REAL(Position1Degree) / 10.0;

5. 使用注意事项

  1. 转盘模式选择:在脉冲转盘模式下,移模页面的下一页会显示转盘设定页面

  2. 工位设置:根据实际工作需求选择合适的工位配置

  3. 光电眼使用:选择使用光电眼时,转盘运行过程中光电眼异常会停止动作

  4. 速度调整:根据实际负载情况合理调整运行速度和手动速度

  5. 延时设置:根据需要设置正转延时和反转延时,确保动作协调

  6. 定位参数:根据实际情况调整定位进/退的压力和流量参数

  7. MODBUS一致性:确保所有与HMI交互的参数都使用INT类型,遵循数据放大规则

  8. 安全操作:操作转盘时,确保人员安全,避免在旋转过程中接触

  9. 定期维护:定期检查转盘机械部件和传感器,确保正常工作

技术交流:13971612060