文章目录
前言
本文分享一个自己编写的电机控制逻辑块。
一、功能概述
- 手动状态、自动状态、机旁状态、强制状态、检修状态自由切换;
- 具有启动超时报警功能;
- 手动状态下,按启动按钮启动,按停止按钮停止;
- 自动状态下,根据程序联锁要求自动启停;
- 强制状态下,跳过允许启动条件,直接启动;
- 检修状态下,自动停止,且按下启动按钮不允许启动。
二、单输出电机程序编写
1.创建自定义数据类型
2.创建FB功能块“单输出电机”
建立如下引脚变量:
3.编写程序
代码如下:
1.赋值
#HMI.Ready_DI := #Ready_DI;
#HMI.Locally_DI := #Locally_DI;
#HMI.Running_DI := #Running_DI;
#Start_DO := #HMI.Start_DO;
2.时间转换、启动超时判断、故障复位
#HMI.Fault_All := #Fault OR #HMI.Time_Out; //故障赋值
#HMI.Lcl_DO := #Locally_DI AND #HMI.HMI_Lcl; //有就地信号,且按下机旁允许按钮,才会显示机旁允许状态
#Start_Time := DINT_TO_TIME(REAL_TO_DINT(#HMI.Start_T_Set * 1000.0)); //输入1表示1S
#RUN_IEC(IN := #HMI.#Start_DO AND NOT #HMI.HMI_Stop AND NOT #HMI.#Running_DI AND NOT #HMI.Fault_All,
PT := #Start_Time); //启动延时
IF #RUN_IEC.Q THEN
#HMI.Time_Out := 1;
#HMI.#Start_DO := 0;
END_IF; //启动超时输出
IF #HMI.HMI_Rst THEN
#HMI.Fault_All := 0;
#HMI.Time_Out := 0; //复位启动超时输出
END_IF;
3.检测信号上升沿
#P_HMI_Lcl(CLK := #HMI.HMI_Lcl);
#P_HMI_EmgManual(CLK := #HMI.HMI_EmgManual);
#P_HMI_Pause(CLK := #HMI.HMI_Pause);
4.手动状态显示
IF #HMI.#HMI_Manual = 1 AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN
#HMI.#Manual_DO := 1;
#HMI.#Auto_DO := 0;
#HMI.#EmgManual_DO := 0;
END_IF;
//默认手动状态
IF NOT #HMI.#Auto_DO AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #HMI.#EmgManual_DO THEN
#HMI.#Manual_DO := 1;
#HMI.#Auto_DO := 0;
#HMI.#EmgManual_DO := 0;
END_IF;
5.自动状态显示
IF #HMI.#HMI_Auto = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN
#HMI.#Manual_DO := 0;
#HMI.#Auto_DO := 1;
#HMI.#EmgManual_DO := 0;
END_IF;
6.强制状态显示
IF #HMI.#HMI_EmgManual = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN
#HMI.#Manual_DO := 0;
#HMI.#Auto_DO := 0;
#HMI.#EmgManual_DO := 1;
#HMI.#HMI_Pause := 0;
END_IF;
IF #HMI.#HMI_EmgManual = 0 THEN
#HMI.#EmgManual_DO := 0;
END_IF;
7.休止状态显示
IF #HMI.#HMI_Pause = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#Lcl_DO THEN
#HMI.#Manual_DO := 0;
#HMI.#Auto_DO := 0;
#HMI.#EmgManual_DO := 0;
#HMI.#Pause_DO := 1;
#HMI.#HMI_EmgManual := 0;
END_IF;
IF #HMI.#HMI_Pause = 0 THEN
#HMI.#Pause_DO := 0;
END_IF;
8.手动状态下电机启动、停止
IF #Ready_DI AND #HMI.#HMI_Start AND #HMI.#Manual_DO AND NOT #HMI.#HMI_Stop AND NOT #HMI.#Fault_All AND NOT #HMI.#Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 1;
END_IF;
IF #Ready_DI AND #HMI.#HMI_Stop AND #HMI.#Manual_DO AND NOT #HMI.#HMI_Start AND NOT #HMI.#Fault_All AND NOT #HMI.#Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 0;
END_IF;
9.自动状态下电机启动、停止
IF #Ready_DI AND #Auto_Start AND #HMI.#Auto_DO AND NOT #HMI.#HMI_Stop AND NOT #HMI.#Fault_All AND NOT #HMI.#Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 1;
END_IF;
IF #Ready_DI AND #Auto_Stop AND #HMI.#Auto_DO AND NOT #HMI.#HMI_Start AND NOT #HMI.#Fault_All AND NOT #HMI.#Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 0;
END_IF;
10.强制状态下电机启动、停止
IF #HMI.#EmgManual_DO AND #HMI.#HMI_Start AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 1;
END_IF;
IF #HMI.#EmgManual_DO AND #HMI.#HMI_Stop AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 0;
END_IF;
11.休止状态下电机停止
IF #HMI.#Pause_DO THEN
#HMI.#Start_DO := 0;
END_IF;
12.按钮互锁
IF #HMI.#HMI_Lcl AND #P_HMI_EmgManual.Q THEN
#HMI.#HMI_EmgManual := 0;
END_IF;
IF #HMI.#HMI_Lcl AND #P_HMI_Pause.Q THEN
#HMI.#HMI_Pause := 0;
END_IF;
IF #HMI.#HMI_EmgManual AND #P_HMI_Lcl.Q THEN
#HMI.#HMI_Lcl := 0;
END_IF;
IF #HMI.#HMI_EmgManual AND #P_HMI_Pause.Q THEN
#HMI.#HMI_Pause := 0;
END_IF;
IF #HMI.#HMI_Pause AND #P_HMI_Lcl.Q THEN
#HMI.#HMI_Lcl := 0;
END_IF;
IF #HMI.#HMI_Pause AND #P_HMI_EmgManual.Q THEN
#HMI.#HMI_EmgManual := 0;
END_IF;
13.按钮自复位
IF NOT "AlwaysFALSE" THEN
#HMI.#HMI_Start := 0;
#HMI.#HMI_Stop := 0;
#HMI.#HMI_Manual := 0;
#HMI.#HMI_Auto := 0;
#HMI.#HMI_Rst := 0;
END_IF;
三、双输出电机程序编写
1.创建自定义数据类型
同单输出电机。
2.创建FB功能块“双输出电机”
建立如下引脚变量:
3.编写程序
代码如下:
1.赋值
#HMI.Ready_DI := #Ready_DI;
#HMI.Locally_DI := #Locally_DI;
#HMI.Running_DI := #Running_DI;
#Start_DO := #HMI.Start_DO;
#Stop_DO := #HMI.Stop_DO;
2.时间转换、启动超时判断、故障复位
#HMI.#Fault_All := #Fault OR #HMI.#Time_Out; //故障赋值
#HMI.#Lcl_DO := #Locally_DI AND #HMI.#HMI_Lcl; //有就地信号,且按下机旁允许按钮,才会显示机旁允许状态
#Start_Time := DINT_TO_TIME(REAL_TO_DINT(#HMI.#Start_T_Set * 1000.0)); //输入1表示1S
#RUN_IEC(IN := #HMI.#Start_DO AND NOT #HMI.#HMI_Stop AND NOT #HMI.#Running_DI AND NOT #HMI.#Fault_All,
PT := #Start_Time); //启动延时
IF #RUN_IEC.Q THEN
#HMI.#Time_Out := 1;
#HMI.#Start_DO := 0;
END_IF; //启动超时输出
IF #HMI.#HMI_Rst THEN
#HMI.#Fault_All := 0;
#HMI.#Time_Out := 0; //复位启动超时输出
END_IF;
3.检测信号上升沿
#P_HMI_Lcl(CLK := #HMI.HMI_Lcl);
#P_HMI_EmgManual(CLK := #HMI.HMI_EmgManual);
#P_HMI_Pause(CLK := #HMI.HMI_Pause);
4.手动状态显示
IF #HMI.#HMI_Manual = 1 AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN
#HMI.#Manual_DO := 1;
#HMI.#Auto_DO := 0;
#HMI.#EmgManual_DO := 0;
END_IF;
//默认手动状态
IF NOT #HMI.#Auto_DO AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #HMI.#EmgManual_DO THEN
#HMI.#Manual_DO := 1;
#HMI.#Auto_DO := 0;
#HMI.#EmgManual_DO := 0;
END_IF;
5.自动状态显示
IF #HMI.#HMI_Auto = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN
#HMI.#Manual_DO := 0;
#HMI.#Auto_DO := 1;
#HMI.#EmgManual_DO := 0;
END_IF;
6.强制状态显示
IF #HMI.#HMI_EmgManual = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN
#HMI.#Manual_DO := 0;
#HMI.#Auto_DO := 0;
#HMI.#EmgManual_DO := 1;
#HMI.#HMI_Pause := 0;
END_IF;
IF #HMI.#HMI_EmgManual = 0 THEN
#HMI.#EmgManual_DO := 0;
END_IF;
7.休止状态显示
IF #HMI.#HMI_Pause = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#Lcl_DO THEN
#HMI.#Manual_DO := 0;
#HMI.#Auto_DO := 0;
#HMI.#EmgManual_DO := 0;
#HMI.#Pause_DO := 1;
#HMI.#HMI_EmgManual := 0;
END_IF;
IF #HMI.#HMI_Pause = 0 THEN
#HMI.#Pause_DO := 0;
END_IF;
8.手动状态下电机启动、停止
IF #Ready_DI AND #HMI.#HMI_Start AND #HMI.#Manual_DO AND NOT #HMI.#HMI_Stop AND NOT #HMI.#Fault_All AND NOT #HMI.#Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 1;
#HMI.#Stop_DO := 0;
END_IF;
IF #Ready_DI AND #HMI.#HMI_Stop AND #HMI.#Manual_DO AND NOT #HMI.#HMI_Start AND NOT #HMI.#Fault_All AND NOT #HMI.#Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 0;
#HMI.#Stop_DO := 1;
END_IF;
9.自动状态下电机启动、停止
IF #Ready_DI AND #Auto_Start AND #HMI.#Auto_DO AND NOT #HMI.#HMI_Stop AND NOT #HMI.#Fault_All AND NOT #HMI.#Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 1;
#HMI.#Stop_DO := 0;
END_IF;
IF #Ready_DI AND #Auto_Stop AND #HMI.#Auto_DO AND NOT #HMI.#HMI_Start AND NOT #HMI.#Fault_All AND NOT #HMI.#Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 0;
#HMI.#Stop_DO := 1;
END_IF;
10.强制状态下电机启动、停止
IF #HMI.#EmgManual_DO AND #HMI.#HMI_Start AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 1;
#HMI.#Stop_DO := 0;
END_IF;
IF #HMI.#EmgManual_DO AND #HMI.#HMI_Stop AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 0;
#HMI.#Stop_DO := 1;
END_IF;
11.休止状态下电机停止
IF #HMI.#Pause_DO AND NOT #HMI.#HMI_Lcl AND NOT #HMI.#Lcl_DO THEN
#HMI.#Start_DO := 0;
#HMI.#Stop_DO := 1;
END_IF;
12.按钮互锁
IF #HMI.#HMI_Lcl AND #P_HMI_EmgManual.Q THEN
#HMI.#HMI_EmgManual := 0;
END_IF;
IF #HMI.#HMI_Lcl AND #P_HMI_Pause.Q THEN
#HMI.#HMI_Pause := 0;
END_IF;
IF #HMI.#HMI_EmgManual AND #P_HMI_Lcl.Q THEN
#HMI.#HMI_Lcl := 0;
END_IF;
IF #HMI.#HMI_EmgManual AND #P_HMI_Pause.Q THEN
#HMI.#HMI_Pause := 0;
END_IF;
IF #HMI.#HMI_Pause AND #P_HMI_Lcl.Q THEN
#HMI.#HMI_Lcl := 0;
END_IF;
IF #HMI.#HMI_Pause AND #P_HMI_EmgManual.Q THEN
#HMI.#HMI_EmgManual := 0;
END_IF;
13.按钮自复位
IF NOT "AlwaysFALSE" THEN
#HMI.#HMI_Start := 0;
#HMI.#HMI_Stop := 0;
#HMI.#HMI_Manual := 0;
#HMI.#HMI_Auto := 0;
#HMI.#HMI_Rst := 0;
END_IF;
标签:DO,Pause,END,.#,西门子,PLC,HMI,EmgManual,底层
From: https://blog.csdn.net/laohei026/article/details/136697934