This commit is contained in:
2025-11-01 17:33:26 +08:00
parent bd0815d0e7
commit 32c14c2e7b
15 changed files with 543 additions and 102 deletions

View File

@ -1,19 +1,22 @@
# feeding/controller.py
import time
from feeding.process import FeedingProcess
from busisness.blls import ArtifactBll
class FeedingController:
def __init__(self, relay_controller, inverter_controller,
transmitter_controller, vision_detector,
camera_controller, state, settings):
camera_controller, rfid_controller,state, settings):
self.relay_controller = relay_controller
self.inverter_controller = inverter_controller
self.transmitter_controller = transmitter_controller
self.vision_detector = vision_detector
self.camera_controller = camera_controller
self.rfid_controller = rfid_controller
self.state = state
self.settings = settings
self.artifact_bll = ArtifactBll()
# 初始化下料流程
self.process = FeedingProcess(
@ -22,21 +25,28 @@ class FeedingController:
camera_controller, state, settings
)
def start_feeding(self):
#获取当前的管片任务
#API读取生产任务 --》RFID读取模具配对检测--数据库入库》生产-》同步到数据库
#API读取生产任务--》未读到--》RFID读取模具-数据库入库》生产 --》同步到数据库
#从数据库获取当前的管片任务
"""启动下料流程"""
self.process.start_feeding()
def check_upper_material_request(self):
"""检查是否需要要料"""
current_weight = self.transmitter_controller.read_data(1)
if current_weight is None:
self.state.upper_weight_error_count += 1
print(f"上料斗重量读取失败,错误计数: {self.state.upper_weight_error_count}")
if self.state.upper_weight_error_count >= self.settings.max_error_count:
print("警告:上料斗传感器连续读取失败,请检查连接")
return False
#需要搅拌楼通知下完料后移到上料斗上方
if self.state._upper_door_position != 'over_lower':
self.state._upper_door_position = 'over_lower'
self.state.upper_weight_error_count = 0
# 判断是否需要要料:当前重量 < 目标重量 + 缓冲重量
if current_weight < (self.settings.single_batch_weight + self.settings.min_required_weight):
@ -50,6 +60,8 @@ class FeedingController:
请求搅拌楼下料
"""
print("发送要料请求至搅拌楼...")
# self.settings.
self.process.return_upper_door_to_default()
# 这里需要与同事对接具体的通信方式
# 可能是Modbus写寄存器、TCP通信、HTTP请求等
@ -60,31 +72,35 @@ class FeedingController:
current_time = time.time()
# 检查下料斗破拱(只有在下料过程中才检查)
if self.state.lower_feeding_stage in [1, 2, 3]: # 在所有下料阶段检查
if self.state._lower_feeding_stage in [1, 2, 3]: # 在所有下料阶段检查
lower_weight = self.transmitter_controller.read_data(2)
if lower_weight is not None:
# 检查重量变化是否过慢小于0.1kg变化且时间超过10秒
if (abs(lower_weight - self.state.last_lower_weight) < 0.1) and \
(current_time - self.state.last_weight_time) > 10:
print("下料斗可能堵塞,启动破拱")
self.state._lower_is_arch_=True
self.relay_controller.control(self.relay_controller.BREAK_ARCH_LOWER, 'open')
time.sleep(2)
self.relay_controller.control(self.relay_controller.BREAK_ARCH_LOWER, 'close')
self.state._lower_is_arch_=False
self.state.last_lower_weight = lower_weight
# 检查上料斗破拱(在上料斗向下料斗下料时检查)
if (self.state.upper_door_position == 'over_lower' and
self.state.lower_feeding_stage in [0, 1, 2, 3, 4]): # 在任何阶段都可能需要上料斗破拱
if (self.state._upper_door_position == 'over_lower' and
self.state._lower_feeding_stage in [0, 1, 2, 3, 4]): # 在任何阶段都可能需要上料斗破拱
upper_weight = self.transmitter_controller.read_data(1)
if upper_weight is not None:
# 检查重量变化是否过慢小于0.1kg变化且时间超过10秒
if (abs(upper_weight - self.state.last_upper_weight) < 0.1) and \
(current_time - self.state.last_weight_time) > 10:
print("上料斗可能堵塞,启动破拱")
self.state._upper_is_arch_=True
self.relay_controller.control(self.relay_controller.BREAK_ARCH_UPPER, 'open')
time.sleep(2)
self.relay_controller.control(self.relay_controller.BREAK_ARCH_UPPER, 'close')
self.state._upper_is_arch_=False
self.state.last_upper_weight = upper_weight

View File

@ -1,75 +1,105 @@
from enum import IntEnum
from core.state import FeedStatus
from service.mould_service import MouldService
from busisness.blls import ArtifactBll
from busisness.models import ArtifactInfoModel
class FeedingProcess:
def __init__(self, relay_controller, inverter_controller,
transmitter_controller, vision_detector,
camera_controller, state, settings):
self.relay_controller = relay_controller
self.artifact_bll = ArtifactBll()
self.inverter_controller = inverter_controller
self.transmitter_controller = transmitter_controller
self.vision_detector = vision_detector
self.camera_controller = camera_controller
self.state = state
self.state.feed_status = FeedStatus.FNone
self.settings = settings
def start_feeding(self):
"""开始分步下料"""
if self.state.lower_feeding_stage != 0:
print("下料已在进行中")
"""开始生产管片"""
if self.state.feed_status == FeedStatus.FNone:
self.state.feed_status = FeedStatus.FCheckM
return
elif self.state.feed_status == FeedStatus.FCheckM:
self.state._lower_feeding_stage = 4
self.wait_for_vehicle_alignment()
self.state.feed_status = FeedStatus.FStart
print("生产已检查模车")
return
elif self.state.feed_status == FeedStatus.FStart:
print("生产已开始")
time.sleep(2)
loc_modules = MouldService.get_not_pour_artifacts()
if loc_modules:
# 取第一个未浇筑的管片
loc_module = loc_modules[0]
self.state.current_artifact = loc_module
else:
#未读取到AIP接口数据.
self.state.current_artifact = None
return
elif self.state.feed_status == FeedStatus.FRFID:
print("生产已检查RFID")
#RFID格式模具编号,分块号,尺寸规格,方量
rfid_info =''
loc_MouldCode='SHZB1-4'
loc_SizeSpecification='6600*1200'
loc_BlockNumber='B1'
loc.BetonVolume=1.56
if self.state.current_artifact:
#检测是否和RFID识别的管理一致
self.state.feed_status = FeedStatus.FCheckGB
else:
#以RFID为准
loc_module= ArtifactInfoModel()
loc_module.MouldCode=loc_MouldCode
loc_module.SizeSpecification=loc_SizeSpecification
loc_module.BlockNumber=loc_BlockNumber
loc_module.BetonVolume=loc.BetonVolume
self.state.current_artifact = loc_module
# 检查关键设备是否可连接
if not self._check_device_connectivity():
print("关键设备连接失败,无法开始下料")
#确认是否保存到数据库
self.state.feed_status = FeedStatus.FCheckGB
return
elif self.state.feed_status == FeedStatus.FCheckGB:
print("生产已检查盖板")
time.sleep(10)
self.state.feed_status = FeedStatus.FFeed
return
elif self.state.feed_status == FeedStatus.FFeed:
self._start_feeding_stage()
print("生产已下料")
return
elif self.state.feed_status == FeedStatus.FFinished:
print("生产已完成")
self.state.feed_status = FeedStatus.FCheckM
return
def _start_feeding_stage(self):
"""启动指定下料阶段"""
"""开始分步下料"""
if self.state._lower_feeding_stage != 0:
print("下料已在进行中")
return
print("开始分步下料过程")
# 重置计数器
#下料斗下料循环次数
self.state.lower_feeding_cycle = 0
#上料斗已下料次数
self.state.upper_feeding_count = 0
#上料斗最大下料次数
self.state.upper_feeding_max = 2
# 第一次上料
self.transfer_material_from_upper_to_lower()
# 等待模具车对齐并开始第一轮下料
self.state.lower_feeding_stage = 4
self.wait_for_vehicle_alignment()
def _check_device_connectivity(self):
"""检查关键设备连接状态"""
try:
# 检查网络继电器连接
test_response = self.relay_controller.send_command(self.relay_controller.read_status_command)
if not test_response:
print("网络继电器连接失败")
return False
# 检查变频器连接
if not self.relay_controller.modbus_client.connect():
print("无法连接到网络继电器Modbus服务")
return False
# 尝试读取变频器一个寄存器(测试连接)
test_result = self.relay_controller.modbus_client.read_holding_registers(
address=0x00,
count=1,
slave=self.inverter_controller.config['slave_id']
)
if isinstance(test_result, Exception):
print("变频器连接测试失败")
return False
# 检查下料斗变送器连接
test_weight = self.transmitter_controller.read_data(2)
if test_weight is None:
print("下料斗变送器连接失败")
return False
self.relay_controller.modbus_client.close()
return True
except Exception as e:
print(f"设备连接检查失败: {e}")
return False
# self.state._lower_feeding_stage = 4
# self.wait_for_vehicle_alignment()
def transfer_material_from_upper_to_lower(self):
"""上料斗向下料斗下料"""
@ -81,12 +111,12 @@ class FeedingProcess:
# 如果无法读取重量,直接报错
if initial_upper_weight is None:
raise Exception("无法读取上料斗重量传感器数据,下料操作终止")
# 单次下料重量(kg),self.settings.single_batch_weight
target_upper_weight = initial_upper_weight - self.settings.single_batch_weight
target_upper_weight = max(target_upper_weight, 0) # 确保不低于0
print(f"上料斗初始重量: {initial_upper_weight:.2f}kg, 目标重量: {target_upper_weight:.2f}kg")
# 确保下料斗出砼门关闭
self.relay_controller.control(self.relay_controller.DOOR_LOWER_2, 'close')
# 打开上料斗出砼门
@ -136,14 +166,14 @@ class FeedingProcess:
def wait_for_vehicle_alignment(self):
"""等待模具车对齐"""
print("等待模具车对齐...")
self.state.lower_feeding_stage = 4
self.state._lower_feeding_stage = 4
import time
while self.state.lower_feeding_stage == 4 and self.state.running:
while self.state._lower_feeding_stage == 4 and self.state.running:
if self.state.vehicle_aligned:
print("模具车已对齐,开始下料")
self.state.lower_feeding_stage = 1
self.feeding_stage_one()
self.state._lower_feeding_stage = 1
# self.feeding_stage_one()
break
time.sleep(self.settings.alignment_check_interval)
@ -168,7 +198,7 @@ class FeedingProcess:
target_weight = initial_weight + self.settings.single_batch_weight
while self.state.lower_feeding_stage == 1:
while self.state._lower_feeding_stage == 1:
current_weight = self.transmitter_controller.read_data(2)
if current_weight is None:
self.state.lower_weight_error_count += 1
@ -180,7 +210,7 @@ class FeedingProcess:
self.state.lower_weight_error_count = 0
if (current_weight is not None and current_weight >= target_weight) or (time.time() - start_time) > 30:
self.state.lower_feeding_stage = 2
self.state._lower_feeding_stage = 2
self.feeding_stage_two()
break
time.sleep(2)
@ -205,7 +235,7 @@ class FeedingProcess:
target_weight = initial_weight + self.settings.single_batch_weight
while self.state.lower_feeding_stage == 2:
while self.state._lower_feeding_stage == 2:
current_weight = self.transmitter_controller.read_data(2)
if current_weight is None:
self.state.lower_weight_error_count += 1
@ -217,7 +247,7 @@ class FeedingProcess:
self.state.lower_weight_error_count = 0
if (current_weight is not None and current_weight >= target_weight) or (time.time() - start_time) > 30:
self.state.lower_feeding_stage = 3
self.state._lower_feeding_stage = 3
self.feeding_stage_three()
break
time.sleep(2)
@ -242,7 +272,7 @@ class FeedingProcess:
target_weight = initial_weight + self.settings.single_batch_weight
while self.state.lower_feeding_stage == 3:
while self.state._lower_feeding_stage == 3:
current_weight = self.transmitter_controller.read_data(2)
if current_weight is None:
self.state.lower_weight_error_count += 1
@ -254,7 +284,7 @@ class FeedingProcess:
self.state.lower_weight_error_count = 0
if (current_weight is not None and current_weight >= target_weight) or (time.time() - start_time) > 30:
self.state.lower_feeding_stage = 4
self.state._lower_feeding_stage = 4
self.finish_current_batch()
break
time.sleep(2)
@ -288,19 +318,19 @@ class FeedingProcess:
# 继续等待当前模具车对齐(不需要重新等待对齐,因为是同一辆模具车)
print("第二次上料完成,继续三阶段下料")
self.state.lower_feeding_stage = 1 # 直接进入第一阶段下料
self.state._lower_feeding_stage = 1 # 直接进入第一阶段下料
self.feeding_stage_one() # 开始第二轮第一阶段下料
def finish_feeding_process(self):
"""完成整个下料流程"""
print("整个下料流程完成")
self.state.lower_feeding_stage = 0
self.state._lower_feeding_stage = 0
self.state.lower_feeding_cycle = 0
self.state.upper_feeding_count = 0
self.return_upper_door_to_default()
# self.return_upper_door_to_default()
def return_upper_door_to_default(self):
"""上料斗回到默认位置(搅拌楼下接料位置)"""
print("上料斗回到默认位置")
self.relay_controller.control(self.relay_controller.DOOR_UPPER, 'close')
self.state.upper_door_position = 'default'
self.state._upper_door_position = 'default'