如何为实时系统实现Python Gekko应用程序? [英] How do you implement a Python Gekko application for real time systems?

查看:205
本文介绍了如何为实时系统实现Python Gekko应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Python Gekko应用程序连接到实时系统.对于控制器的每个周期",分三个步骤:

  1. 从测量设备读取当前值
  2. Python应用程序基于测量值和预测模型创建新的移动计划
  3. 将移动计划重新插入到物理过程中

该过程的可视化显示在此视频或此视频中保持静止.

要连接到实时系统,客户端必须读取和写入值.用于连接到过程控制系统(例如Python中的分布式控制系统(DCS)或可编程逻辑控制器(PLC))的标准通信协议是什么?

解决方案

Python可以从MODBUS,OPC以及SQL或其他协议读取和写入值.这是针对Modbus(pymodbus)和OPC(OpenOPC)的示例代码./p>

Python中的OPC示例

 # #######################################
# OPC write
# #######################################
try:
    # OPC connection
    import OpenOPC
    opc=OpenOPC.client()
    b=opc.connect('Kepware.KEPServerEX.V5')
    #opc.connect('Kepware.KEPServerEX.V5','localhost')

    Load1_avg  = opcm[0][0]
    Load2_avg  = opcm[0][1]
    Load3_avg  = opcm[0][2]
    Load4_avg  = opcm[0][3]
    Load1_max  = opcm[1][0]
    Load2_max  = opcm[1][1]
    Load3_max  = opcm[1][2]
    Load4_max  = opcm[1][3]
    Load1_min  = opcm[2][0]
    Load2_min  = opcm[2][1]
    Load3_min  = opcm[2][2]
    Load4_min  = opcm[2][3]
    Load_T12   = opcm[3][0]
    Load_T21   = opcm[3][1]
    Load_T32   = opcm[3][2]
    Load_T41   = opcm[3][3]

    opc.write( ('Channel2.Device1.T_12_Load_AVG',Load1_avg) )
    opc.write( ('Channel2.Device1.T_21_Load_AVG',Load2_avg) )
    opc.write( ('Channel2.Device1.T_32_Load_AVG',Load3_avg) )
    opc.write( ('Channel2.Device1.T_41_Load_AVG',Load4_avg) )
    opc.write( ('Channel2.Device1.T_12_Load_MAX',Load1_max) )
    opc.write( ('Channel2.Device1.T_21_Load_MAX',Load2_max) )
    opc.write( ('Channel2.Device1.T_32_Load_MAX',Load3_max) )
    opc.write( ('Channel2.Device1.T_41_Load_MAX',Load4_max) )
    opc.write( ('Channel2.Device1.T_12_Load_MIN',Load1_min) )
    opc.write( ('Channel2.Device1.T_21_Load_MIN',Load2_min) )
    opc.write( ('Channel2.Device1.T_32_Load_MIN',Load3_min) )
    opc.write( ('Channel2.Device1.T_41_Load_MIN',Load4_min) )
    opc.write( ('Channel2.Device1.T_12_Load_INST',Load_T12) )
    opc.write( ('Channel2.Device1.T_21_Load_INST',Load_T21) )
    opc.write( ('Channel2.Device1.T_32_Load_INST',Load_T32) )
    opc.write( ('Channel2.Device1.T_41_Load_INST',Load_T41) )

    opc.close()
except:
    print 'OPC communication failed'
    pass
 

Python中的MODBUS示例

 # #######################################
# Modbus write
# #######################################
try:
    # import the various server implementations
    from pymodbus.client.sync import ModbusTcpClient as ModbusClient
    from pymodbus.constants import Endian
    from pymodbus.payload import BinaryPayloadBuilder
    from pymodbus.client.sync import ModbusTcpClient as ModbusClient
    from pymodbus.payload import BinaryPayloadDecoder

    # initiate client
    ##client = ModbusClient('192.168.0.1')
    client = ModbusClient(host='localhost', port=502) 
    slave_address = 0

    # AVG Registers Modbus 40001,3,5,7
    # MAX Registers Modbus 40009,11,13,15
    # MIN Registers Modbus 40017,19,21,23
    # INST Registers Modbus 40025,27,29,31
    # registers
    reg = 0
    # AVG, MAX, MIN, INST
    for i in range(0,4):
        # Channels
        for j in range(0,4):
            builder = BinaryPayloadBuilder(endian=Endian.Little)
            builder.add_32bit_float(opcm[j][i])
            payload = builder.build()
            result  = client.write_registers(int(reg), payload, skip_encode=True, unit=int(slave_address))
            # two registers for floating point numbers
            reg = reg + 2
    client.close()
except:
    print 'Modbus communication failed'
    pass
 

除了MODBUS和OPC,公司还支持其他基于文本的文件传输和其他通信方式,尽管这些在石油和天然气行业中最常见,它们具有分布式控制系统(DCS)或可编程逻辑控制器( PLC),用于计划和数据获取(DAQ).据我所知,工业应用中大约有140个Gekko/APMonitor实例.其中一些在此处列出.

I want to connect a Python Gekko application to a real-time system. For each "cycle" of the controller there are three steps:

  1. current values are read from the measurement devices
  2. the Python application creates a new move plan based on the measurements and a predictive model
  3. the move plan is re-inserted into the physical process

A visualization of the process is shown in this video or from this frame still.

For connection to real-time systems, the client must read and write values. What are standard communication protocols for connecting to process control systems such as a Distributed Control System (DCS) or Programmable Logic Controller (PLC) in Python?

解决方案

Python can read and write values from MODBUS, OPC, and with SQL or other protocols. Here is example code for Modbus (pymodbus) and OPC (OpenOPC).

OPC Example in Python

# #######################################
# OPC write
# #######################################
try:
    # OPC connection
    import OpenOPC
    opc=OpenOPC.client()
    b=opc.connect('Kepware.KEPServerEX.V5')
    #opc.connect('Kepware.KEPServerEX.V5','localhost')

    Load1_avg  = opcm[0][0]
    Load2_avg  = opcm[0][1]
    Load3_avg  = opcm[0][2]
    Load4_avg  = opcm[0][3]
    Load1_max  = opcm[1][0]
    Load2_max  = opcm[1][1]
    Load3_max  = opcm[1][2]
    Load4_max  = opcm[1][3]
    Load1_min  = opcm[2][0]
    Load2_min  = opcm[2][1]
    Load3_min  = opcm[2][2]
    Load4_min  = opcm[2][3]
    Load_T12   = opcm[3][0]
    Load_T21   = opcm[3][1]
    Load_T32   = opcm[3][2]
    Load_T41   = opcm[3][3]

    opc.write( ('Channel2.Device1.T_12_Load_AVG',Load1_avg) )
    opc.write( ('Channel2.Device1.T_21_Load_AVG',Load2_avg) )
    opc.write( ('Channel2.Device1.T_32_Load_AVG',Load3_avg) )
    opc.write( ('Channel2.Device1.T_41_Load_AVG',Load4_avg) )
    opc.write( ('Channel2.Device1.T_12_Load_MAX',Load1_max) )
    opc.write( ('Channel2.Device1.T_21_Load_MAX',Load2_max) )
    opc.write( ('Channel2.Device1.T_32_Load_MAX',Load3_max) )
    opc.write( ('Channel2.Device1.T_41_Load_MAX',Load4_max) )
    opc.write( ('Channel2.Device1.T_12_Load_MIN',Load1_min) )
    opc.write( ('Channel2.Device1.T_21_Load_MIN',Load2_min) )
    opc.write( ('Channel2.Device1.T_32_Load_MIN',Load3_min) )
    opc.write( ('Channel2.Device1.T_41_Load_MIN',Load4_min) )
    opc.write( ('Channel2.Device1.T_12_Load_INST',Load_T12) )
    opc.write( ('Channel2.Device1.T_21_Load_INST',Load_T21) )
    opc.write( ('Channel2.Device1.T_32_Load_INST',Load_T32) )
    opc.write( ('Channel2.Device1.T_41_Load_INST',Load_T41) )

    opc.close()
except:
    print 'OPC communication failed'
    pass

MODBUS Example in Python

# #######################################
# Modbus write
# #######################################
try:
    # import the various server implementations
    from pymodbus.client.sync import ModbusTcpClient as ModbusClient
    from pymodbus.constants import Endian
    from pymodbus.payload import BinaryPayloadBuilder
    from pymodbus.client.sync import ModbusTcpClient as ModbusClient
    from pymodbus.payload import BinaryPayloadDecoder

    # initiate client
    ##client = ModbusClient('192.168.0.1')
    client = ModbusClient(host='localhost', port=502) 
    slave_address = 0

    # AVG Registers Modbus 40001,3,5,7
    # MAX Registers Modbus 40009,11,13,15
    # MIN Registers Modbus 40017,19,21,23
    # INST Registers Modbus 40025,27,29,31
    # registers
    reg = 0
    # AVG, MAX, MIN, INST
    for i in range(0,4):
        # Channels
        for j in range(0,4):
            builder = BinaryPayloadBuilder(endian=Endian.Little)
            builder.add_32bit_float(opcm[j][i])
            payload = builder.build()
            result  = client.write_registers(int(reg), payload, skip_encode=True, unit=int(slave_address))
            # two registers for floating point numbers
            reg = reg + 2
    client.close()
except:
    print 'Modbus communication failed'
    pass

Besides MODBUS and OPC, there are also other text based file transfers and other methods of communication supported by companies although these are the most common in oil and gas industries where they have a Distributed Control System (DCS) or Programmable Logic Controller (PLC) that is for Scheduling and Data Aquisition (DAQ). There are about 140 instances of Gekko/APMonitor in industrial applications that I know about. Some of them are listed here.

这篇关于如何为实时系统实现Python Gekko应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆