Python连接FactoryIO仿真控制练习(二)
我想了一下在PLC中通过SCL进行定时器、计数器、脉冲检测等功能块的实现逻辑,重新进行了功能实现。传送带控制加入了延时停机定时器,当超出时间未检测到离开传送带末端传感器信号时,传送带停止运行。
一、定时器:
参考在PLC中使用SCL实现自定义定时器的一般逻辑,实现如下:
class MyTimer:
"""定时器
def __init__(self, cycle, my_time):以循环周期为时基
"""
def __init__(self):
self.timer_on = 0
self.end_time = 0
self.start_time = 0
self.cycle_time = 0
self.pt = 0
self.et = 0
def timing(self, enable, pt):
"""--"""
self.end_time = int(time.perf_counter()*1000)
self.cycle_time = self.end_time - self.start_time
if enable:
if self.et < pt:
self.et += self.cycle_time
self.timer_on = 0
else:
self.timer_on = 1
else:
self.et = 0
self.timer_on = 0
self.start_time = int(time.perf_counter()*1000)
二、计数器
加计数,给定输入信号,自动检测上升沿。
class MyCounter:
"""计数器"""
def __init__(self):
self.clk = 0
self.clk_TR = RiseTrigger()
self.counter_current_value = 0
self.reset_counter_signal = 0
def __str__(self):
"""--"""
return str(self.counter_current_value)
def get_tr(self):
"""获取计数脉冲"""
return self.clk_TR.r_trig(self.clk)
def countering(self, clk):
"""--"""
if self.reset_counter_signal:
self.counter_current_value = 0
self.reset_counter_signal = 0
return 0
else:
self.clk = clk
self.counter_current_value += self.clk_TR.r_trig(self.clk)
return self.counter_current_value
三、上升沿检测
实现逻辑比较简单:
class RiseTrigger:
"""上升沿检测"""
def __init__(self):
self.clk = 0
self.ckl_1 = 0
self.out = 0
def r_trig(self, clk):
"""--"""
self.clk = clk
if self.clk == 1 and self.ckl_1 == 0:
self.out = 1
else:
self.out = 0
self.ckl_1 = self.clk
return self.out
四、传送带控制
传送带一般包含两个接近传感器,用于检测物品进入和离开,然后输出控制点给电机进行启停控制:
class ConveyorControl:
"""--"""
def __init__(self, name,):
self.run_control = 0
self.counter_on_conveyor = 0
self.counter_enter_value = 0
self.counter_exit_value = 0
self.enter_counter = MyCounter()
self.exit_counter = MyCounter()
self.timer_1 = MyTimer()
def control(self, enter_signal, exit_signal):
"""--"""
self.counter_enter_value = self.enter_counter.countering(enter_signal)
self.counter_exit_value = self.exit_counter.countering(exit_signal)
self.counter_on_conveyor = self.counter_enter_value - self.counter_exit_value
self.run_control = 1 if self.counter_on_conveyor > 0 else 0
self.timer_1.timing(self.counter_on_conveyor and not exit_signal, 30000)
if self.timer_1.timer_on:
self.counter_on_conveyor = 0
self.enter_counter.reset_counter_signal = 1
self.exit_counter.reset_counter_signal = 1
self.run_control = 0
return self.run_control
五、主循环
传送带超时时间设置为30s,当传送带末端传感器接通时,会复位定时器:
当定时器到达定时时间,将强制停止传送带运行,并复位定时器,并等待下一次的触发信号:
if __name__ == "__main__":
master = mt.TcpMaster(host="192.168.29.50") # host="192.168.29.50", port=502, timeout_in_sec=5.0
master.set_timeout(5.0)
conveyor_1 = ConveyorControl("conveyor_1")
conveyor_2 = ConveyorControl("conveyor_2")
while True:
InputBool = list(master.execute(255, md.READ_DISCRETE_INPUTS, 0, 4))
conveyor_1_enter_signal = InputBool[0]
conveyor_1_exit_signal = InputBool[1]
conveyor_2_enter_signal = InputBool[2]
conveyor_2_exit_signal = InputBool[3]
conveyor_1_control = conveyor_1.control(conveyor_1_enter_signal, conveyor_2_enter_signal)
conveyor_2_control = conveyor_2.control(conveyor_1_exit_signal, conveyor_2_exit_signal)
OutputBool = [conveyor_1_control, conveyor_2_control]
output_coils_bool = master.execute(255, md.WRITE_MULTIPLE_COILS, 0, output_value=OutputBool)
time.sleep(0.5)
完整代码如下,使用例程为"FromAToB":
# _*_ coding:utf-8 _*_
""" --------------------------------------------------
文件名称: PythonWithFactoryIO-FactoryIOFromAToB-qsp34
日期时间: 2020-08-16-9:02
文件备注:
---------------------------------------------------"""
import modbus_tk.modbus_tcp as mt
import modbus_tk.defines as md
import tkinter as tk
import time
class ConveyorControl:
"""--"""
def __init__(self, name, ):
self.run_control = 0
self.counter_on_conveyor = 0
self.counter_enter_value = 0
self.counter_exit_value = 0
self.enter_counter = MyCounter()
self.exit_counter = MyCounter()
self.timer_1 = MyTimer()
def control(self, enter_signal, exit_signal):
"""--"""
self.counter_enter_value = self.enter_counter.countering(enter_signal)
self.counter_exit_value = self.exit_counter.countering(exit_signal)
self.counter_on_conveyor = self.counter_enter_value - self.counter_exit_value
self.run_control = 1 if self.counter_on_conveyor > 0 else 0
self.timer_1.timing(self.counter_on_conveyor and not exit_signal, 30000)
if self.timer_1.timer_on:
self.counter_on_conveyor = 0
self.enter_counter.reset_counter_signal = 1
self.exit_counter.reset_counter_signal = 1
self.run_control = 0
return self.run_control
class MyTimer:
"""定时器
def __init__(self, cycle, my_time):以循环周期为时基
"""
def __init__(self):
self.timer_on = 0
self.end_time = 0
self.start_time = 0
self.cycle_time = 0
self.pt = 0
self.et = 0
def timing(self, enable, pt):
"""--"""
self.end_time = int(time.perf_counter() * 1000)
self.cycle_time = self.end_time - self.start_time
if enable:
if self.et < pt:
self.et += self.cycle_time
self.timer_on = 0
else:
self.timer_on = 1
else:
self.et = 0
self.timer_on = 0
self.start_time = int(time.perf_counter() * 1000)
class MyCounter:
"""计数器"""
def __init__(self):
self.clk = 0
self.clk_TR = RiseTrigger()
self.counter_current_value = 0
self.reset_counter_signal = 0
def __str__(self):
"""--"""
return str(self.counter_current_value)
def get_tr(self):
"""获取计数脉冲"""
return self.clk_TR.r_trig(self.clk)
def countering(self, clk):
"""--"""
if self.reset_counter_signal:
self.counter_current_value = 0
self.reset_counter_signal = 0
return 0
else:
self.clk = clk
self.counter_current_value += self.clk_TR.r_trig(self.clk)
return self.counter_current_value
class RiseTrigger:
"""上升沿检测"""
def __init__(self):
self.clk = 0
self.ckl_1 = 0
self.out = 0
def r_trig(self, clk):
"""--"""
self.clk = clk
if self.clk == 1 and self.ckl_1 == 0:
self.out = 1
else:
self.out = 0
self.ckl_1 = self.clk
return self.out
if __name__ == "__main__":
master = mt.TcpMaster(host="192.168.29.50") # host="192.168.29.50", port=502, timeout_in_sec=5.0
master.set_timeout(5.0)
conveyor_1 = ConveyorControl("conveyor_1")
conveyor_2 = ConveyorControl("conveyor_2")
while True:
InputBool = list(master.execute(255, md.READ_DISCRETE_INPUTS, 0, 4))
conveyor_1_enter_signal = InputBool[0]
conveyor_1_exit_signal = InputBool[1]
conveyor_2_enter_signal = InputBool[2]
conveyor_2_exit_signal = InputBool[3]
conveyor_1_control = conveyor_1.control(conveyor_1_enter_signal, conveyor_2_enter_signal)
conveyor_2_control = conveyor_2.control(conveyor_1_exit_signal, conveyor_2_exit_signal)
OutputBool = [conveyor_1_control, conveyor_2_control]
output_coils_bool = master.execute(255, md.WRITE_MULTIPLE_COILS, 0, output_value=OutputBool)
time.sleep(0.5)