How Do You Implement A Python Gekko Application For Real Time Systems?
I want to connect a Python Gekko application to a real-time system. For each 'cycle' of the controller there are three steps: current values are read from the measurement devices
Solution 1:
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 connectionimport 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 implementationsfrom 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, INSTfor i inrange(0,4):
# Channelsfor j inrange(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.
Post a Comment for "How Do You Implement A Python Gekko Application For Real Time Systems?"