重构目录结构:调整项目布局
This commit is contained in:
8
core/__init__.py
Normal file
8
core/__init__.py
Normal file
@ -0,0 +1,8 @@
|
||||
"""
|
||||
核心模块
|
||||
包含系统核心控制逻辑和状态管理
|
||||
"""
|
||||
from .system import FeedingControlSystem
|
||||
from .state import SystemState
|
||||
|
||||
__all__ = ['FeedingControlSystem', 'SystemState']
|
||||
BIN
core/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
core/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
core/__pycache__/state.cpython-39.pyc
Normal file
BIN
core/__pycache__/state.cpython-39.pyc
Normal file
Binary file not shown.
BIN
core/__pycache__/system.cpython-39.pyc
Normal file
BIN
core/__pycache__/system.cpython-39.pyc
Normal file
Binary file not shown.
1114
core/feeding_system.py
Normal file
1114
core/feeding_system.py
Normal file
File diff suppressed because it is too large
Load Diff
27
core/state.py
Normal file
27
core/state.py
Normal file
@ -0,0 +1,27 @@
|
||||
# core/state.py
|
||||
class SystemState:
|
||||
def __init__(self):
|
||||
# 系统运行状态
|
||||
self.running = False
|
||||
|
||||
# 下料控制相关
|
||||
self.upper_door_position = 'default' # default(在搅拌楼下接料), over_lower(在下料斗上方), returning(返回中)
|
||||
self.lower_feeding_stage = 0 # 0:未下料, 1:第一阶段, 2:第二阶段, 3:第三阶段, 4:等待模具车对齐
|
||||
self.lower_feeding_cycle = 0 # 下料斗下料循环次数
|
||||
self.upper_feeding_count = 0 # 上料斗已下料次数
|
||||
|
||||
# 重量相关
|
||||
self.last_upper_weight = 0
|
||||
self.last_lower_weight = 0
|
||||
self.last_weight_time = 0
|
||||
|
||||
# 错误计数
|
||||
self.upper_weight_error_count = 0
|
||||
self.lower_weight_error_count = 0
|
||||
|
||||
# 视觉系统状态
|
||||
self.angle_control_mode = "normal" # 角度控制模式: normal, reducing, maintaining, recovery
|
||||
self.overflow_detected = False # 堆料检测
|
||||
self.door_opening_large = False # 夹角
|
||||
self.vehicle_aligned = False # 模具车是否对齐
|
||||
self.last_angle = None # 上次检测角度
|
||||
166
core/system.py
Normal file
166
core/system.py
Normal file
@ -0,0 +1,166 @@
|
||||
# core/system.py
|
||||
import threading
|
||||
import time
|
||||
import cv2
|
||||
from config.settings import Settings
|
||||
from core.state import SystemState
|
||||
from hardware.relay import RelayController
|
||||
from hardware.inverter import InverterController
|
||||
from hardware.transmitter import TransmitterController
|
||||
from vision.camera import CameraController
|
||||
from vision.detector import VisionDetector
|
||||
from feeding.controller import FeedingController
|
||||
|
||||
|
||||
class FeedingControlSystem:
|
||||
def __init__(self, settings: Settings):
|
||||
self.settings = settings
|
||||
self.state = SystemState()
|
||||
|
||||
# 初始化硬件控制器
|
||||
self.relay_controller = RelayController(
|
||||
host=settings.relay_host,
|
||||
port=settings.relay_port
|
||||
)
|
||||
|
||||
self.inverter_controller = InverterController(self.relay_controller)
|
||||
self.transmitter_controller = TransmitterController(self.relay_controller)
|
||||
|
||||
# 初始化视觉系统
|
||||
self.camera_controller = CameraController()
|
||||
self.vision_detector = VisionDetector(settings)
|
||||
|
||||
# 初始化下料控制器
|
||||
self.feeding_controller = FeedingController(
|
||||
self.relay_controller,
|
||||
self.inverter_controller,
|
||||
self.transmitter_controller,
|
||||
self.vision_detector,
|
||||
self.camera_controller,
|
||||
self.state,
|
||||
settings
|
||||
)
|
||||
|
||||
# 线程管理
|
||||
self.monitor_thread = None
|
||||
self.visual_control_thread = None
|
||||
self.alignment_check_thread = None
|
||||
|
||||
def initialize(self):
|
||||
"""初始化系统"""
|
||||
print("初始化控制系统...")
|
||||
|
||||
# 设置摄像头配置
|
||||
self.camera_controller.set_config(
|
||||
camera_type=self.settings.camera_type,
|
||||
ip=self.settings.camera_ip,
|
||||
port=self.settings.camera_port,
|
||||
username=self.settings.camera_username,
|
||||
password=self.settings.camera_password,
|
||||
channel=self.settings.camera_channel
|
||||
)
|
||||
|
||||
# 初始化摄像头
|
||||
if not self.camera_controller.setup_capture():
|
||||
raise Exception("摄像头初始化失败")
|
||||
|
||||
# 加载视觉模型
|
||||
if not self.vision_detector.load_models():
|
||||
raise Exception("视觉模型加载失败")
|
||||
|
||||
# 启动系统监控
|
||||
self.start_monitoring()
|
||||
|
||||
# 启动视觉控制
|
||||
self.start_visual_control()
|
||||
|
||||
# 启动对齐检查
|
||||
self.start_alignment_check()
|
||||
|
||||
print("控制系统初始化完成")
|
||||
|
||||
def start_monitoring(self):
|
||||
"""启动系统监控"""
|
||||
self.state.running = True
|
||||
self.monitor_thread = threading.Thread(
|
||||
target=self._monitor_loop,
|
||||
daemon=True
|
||||
)
|
||||
self.monitor_thread.start()
|
||||
|
||||
def _monitor_loop(self):
|
||||
"""监控循环"""
|
||||
while self.state.running:
|
||||
try:
|
||||
self.feeding_controller.check_upper_material_request()
|
||||
self.feeding_controller.check_arch_blocking()
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
print(f"监控线程错误: {e}")
|
||||
|
||||
def start_visual_control(self):
|
||||
"""启动视觉控制"""
|
||||
self.visual_control_thread = threading.Thread(
|
||||
target=self._visual_control_loop,
|
||||
daemon=True
|
||||
)
|
||||
self.visual_control_thread.start()
|
||||
|
||||
def _visual_control_loop(self):
|
||||
"""视觉控制循环"""
|
||||
while self.state.running:
|
||||
try:
|
||||
current_frame = self.camera_controller.capture_frame()
|
||||
if current_frame is not None:
|
||||
# 执行视觉控制逻辑
|
||||
self.feeding_controller.visual_control(current_frame)
|
||||
time.sleep(self.settings.visual_check_interval)
|
||||
except Exception as e:
|
||||
print(f"视觉控制循环错误: {e}")
|
||||
time.sleep(self.settings.visual_check_interval)
|
||||
|
||||
def start_alignment_check(self):
|
||||
"""启动对齐检查"""
|
||||
self.alignment_check_thread = threading.Thread(
|
||||
target=self._alignment_check_loop,
|
||||
daemon=True
|
||||
)
|
||||
self.alignment_check_thread.start()
|
||||
|
||||
def _alignment_check_loop(self):
|
||||
"""对齐检查循环"""
|
||||
while self.state.running:
|
||||
try:
|
||||
if self.state.lower_feeding_stage == 4: # 等待对齐阶段
|
||||
current_frame = self.camera_controller.capture_frame()
|
||||
if current_frame is not None:
|
||||
self.state.vehicle_aligned = self.vision_detector.detect_vehicle_alignment(current_frame)
|
||||
if self.state.vehicle_aligned:
|
||||
print("检测到模具车对齐")
|
||||
else:
|
||||
print("模具车未对齐")
|
||||
time.sleep(self.settings.alignment_check_interval)
|
||||
except Exception as e:
|
||||
print(f"对齐检查循环错误: {e}")
|
||||
time.sleep(self.settings.alignment_check_interval)
|
||||
|
||||
def start_lower_feeding(self):
|
||||
"""启动下料流程"""
|
||||
self.feeding_controller.start_feeding()
|
||||
|
||||
def stop(self):
|
||||
"""停止系统"""
|
||||
print("停止控制系统...")
|
||||
self.state.running = False
|
||||
|
||||
# 等待线程结束
|
||||
if self.monitor_thread:
|
||||
self.monitor_thread.join()
|
||||
if self.visual_control_thread:
|
||||
self.visual_control_thread.join()
|
||||
if self.alignment_check_thread:
|
||||
self.alignment_check_thread.join()
|
||||
|
||||
# 释放摄像头资源
|
||||
self.camera_controller.release()
|
||||
print("控制系统已停止")
|
||||
Reference in New Issue
Block a user