从 MT2-AM8 模拟量模块迁移到 Easy521 PLC(Modbus TCP)

- 读取 D700(阀后流量)、D710(阀后压力)、D720(阀前压力),
  读用功能码 03,0~32000 线性映射到各自量程(300 SLM / 400 kPa / 300 kPa)
- 输出 D730(电机位置,0~10V),写用功能码 06,行程仍为 0~1000、死区 800
- 复用 MT2 骨架,保留 get_flow/get_pressure/set_motor_position 接口,
  channel 参数改为直接 Modbus 地址
- config.py 新增 PLC 设置,保留 MT2AM8 设置为死代码(MT2AM8Client 类保留)
- 同步更新 README.md 中的硬件接线、配置默认值与模块职责描述
This commit is contained in:
2026-09-01 17:03:26 +08:00
parent abab25326a
commit 5888b4ccce
10 changed files with 507 additions and 374 deletions
+27 -22
View File
@@ -4,7 +4,7 @@
python main.py --target 50
程序启动后直接从当前阀门开度(默认全开)开始闭环。Ctrl+C、控制故障或
其他异常会断开 MT2-AM8;阀门不被驱动时默认回到全开。
其他异常会断开 PLC;阀门不被驱动时默认回到全开。
"""
import argparse
@@ -96,10 +96,10 @@ def build_control_loop(hardware, logger):
hardware=hardware,
pid=pid,
period_s=config.CONTROL_PERIOD_S,
flow_channel=config.FLOW_INPUT_CHANNEL,
pressure_channel=config.PRESSURE_INPUT_CHANNEL,
pressure_before_channel=config.PRESSURE_BEFORE_CHANNEL,
motor_channel=config.MOTOR_OUTPUT_CHANNEL,
flow_channel=config.FLOW_AFTER_ADDR,
pressure_channel=config.PRESSURE_AFTER_ADDR,
pressure_before_channel=config.PRESSURE_BEFORE_ADDR,
motor_channel=config.MOTOR_OUTPUT_ADDR,
motor_open_position=config.MOTOR_OPEN_POSITION,
motor_closed_position=config.MOTOR_CLOSED_POSITION,
target_min_slm=config.TARGET_FLOW_MIN_SLM,
@@ -148,7 +148,7 @@ def build_control_loop(hardware, logger):
def parse_args(argv=None):
parser = argparse.ArgumentParser(description="MT2-AM8 气体流量 PID 控制")
parser = argparse.ArgumentParser(description="PLC 气体流量 PID 控制")
parser.add_argument(
"--target",
type=float,
@@ -197,7 +197,7 @@ def run(target_flow_slm):
config.validate_config()
logger, console_gate = build_logger()
try:
from PcControl import MT2AM8Client
from PcControl import Easy521ModbusClient
except ModuleNotFoundError as exc:
if exc.name and exc.name.startswith("pymodbus"):
logger.critical(
@@ -207,12 +207,17 @@ def run(target_flow_slm):
return 6
raise
hardware = MT2AM8Client(
host=config.MT2AM8_HOST,
port=config.MT2AM8_PORT,
slave_id=config.MT2AM8_SLAVE_ID,
pressure_range=config.PRESSURE_RANGE_KPA,
flow_range=config.FLOW_METER_RANGE_SLM,
hardware = Easy521ModbusClient(
host=config.PLC_HOST,
port=config.PLC_PORT,
slave_id=config.PLC_SLAVE_ID,
flow_addr=config.FLOW_AFTER_ADDR,
flow_range=config.FLOW_AFTER_RANGE_SLM,
pressure_before_addr=config.PRESSURE_BEFORE_ADDR,
pressure_before_range=config.PRESSURE_BEFORE_RANGE_KPA,
pressure_after_addr=config.PRESSURE_AFTER_ADDR,
pressure_after_range=config.PRESSURE_AFTER_RANGE_KPA,
motor_output_addr=config.MOTOR_OUTPUT_ADDR,
)
controller = build_control_loop(hardware, logger)
controller.set_target_flow(target_flow_slm)
@@ -223,9 +228,9 @@ def run(target_flow_slm):
exit_code = 0
try:
logger.info(
"正在连接 MT2-AM8 %s:%s,目标流量=%.3f SLM",
config.MT2AM8_HOST,
config.MT2AM8_PORT,
"正在连接 PLC %s:%s,目标流量=%.3f SLM",
config.PLC_HOST,
config.PLC_PORT,
target_flow_slm,
)
connected = bool(hardware.connect())
@@ -233,17 +238,17 @@ def run(target_flow_slm):
raise FlowControlFault(
"DEVICE_CONNECT_FAILED",
"STARTUP",
"无法连接 MT2-AM8",
"无法连接 PLC",
{
"host": config.MT2AM8_HOST,
"port": config.MT2AM8_PORT,
"slave_id": config.MT2AM8_SLAVE_ID,
"host": config.PLC_HOST,
"port": config.PLC_PORT,
"slave_id": config.PLC_SLAVE_ID,
},
)
# 控制前读取一次当前流量,作为超调量方向的基准。
try:
initial_flow_slm = hardware.get_flow(config.FLOW_INPUT_CHANNEL)
initial_flow_slm = hardware.get_flow(config.FLOW_AFTER_ADDR)
except Exception:
initial_flow_slm = None
@@ -376,7 +381,7 @@ def run(target_flow_slm):
hardware.disconnect()
except Exception:
exit_code = max(exit_code, 5)
logger.exception("断开 MT2-AM8 时发生异常")
logger.exception("断开 PLC 时发生异常")
if recorder is not None:
try: