Former-commit-id: 7df457400907998e272578e9cc6ce08e767916bf
This commit is contained in:
2025-06-05 14:01:14 +08:00
parent aa4dac05d2
commit 668bae72fa
2 changed files with 52 additions and 25 deletions

66
main.py
View File

@ -85,9 +85,13 @@ class MAT:
if self.history.is_empty(): if self.history.is_empty():
self.control_logger.error("未预期的没有可用的历史记录") self.control_logger.error("未预期的没有可用的历史记录")
return ret return ret
if self.crazy < 3:
self._display_status(im, ret, rate, val)
return ret
# === 状态进入逻辑 === # === 状态进入逻辑 ===
if now - self._start_time<17: if now - self._start_time<10:
self._display_status(im, ret, rate, val) self._display_status(im, ret, rate, val)
return ret return ret
# 1. middle: predictor返回middle立即进入slow状态 # 1. middle: predictor返回middle立即进入slow状态
@ -199,7 +203,7 @@ class MAT:
rate = val/tot rate = val/tot
if self.history.base is not None: if self.history.base is not None:
base = self.history.base base = self.history.base
thr = (base*5, base*10, base*30) thr = (base*5, base*9, 0.75)
if rate < thr[0]: if rate < thr[0]:
return "transport",rate return "transport",rate
elif rate <thr[1]: elif rate <thr[1]:
@ -257,28 +261,48 @@ class MAT:
self.ch340_init() self.ch340_init()
cnt = 0 cnt = 0
self._start_time = time.time() self._start_time = time.time()
self.crazy = 4
while self.running: while self.running:
if not self.ch340.running: if not self.ch340.running:
if 12 * cnt - self.total_volume < 0.5: if self.crazy < 3:
self.ch340_pull() match self.crazy:
cnt += 1 case 0:
time.sleep(0.01) self.control_logger.info("crazy 0")
self.ch340_pull()
# 简化的推送逻辑 self.ch340.speed = 1.00
should_push = False self.ch340.push_async(vol=12)
if self.state.is_fast_mode(): self.crazy = 1
should_push = True case 1:
elif self.state.is_slow_mode() and time.time() - self.ch340.start > mid_time: self.control_logger.info("crazy 1")
should_push = True self.ch340.pull_async(vol=12)
elif self.state.is_about_mode() and time.time() - self.ch340.start > end_time: self.crazy = 2
should_push = True case 2:
self.control_logger.info("crazy 2")
if should_push: self.ch340.push_async(vol=8)
speed = self.speeds[self.state.mode.value] self.total_volume = 20
self.volume_logger.info(f"当前体积: {self.total_volume:.2f} ml, 加入速度: {speed:.2f} ml/次") cnt = 2
self.ch340_push(speed) self.crazy = 3
self.total_volume += speed else:
if 12 * cnt - self.total_volume < 0.5:
self.ch340_pull()
cnt += 1
time.sleep(0.01)
# 简化的推送逻辑
should_push = False
if self.state.is_fast_mode():
should_push = True
elif self.state.is_slow_mode() and time.time() - self.ch340.start > mid_time:
should_push = True
elif self.state.is_about_mode() and time.time() - self.ch340.start > end_time:
should_push = True
if should_push:
speed = self.speeds[self.state.mode.value]
self.volume_logger.info(f"当前体积: {self.total_volume:.2f} ml, 加入速度: {speed:.2f} ml/次")
self.ch340_push(speed)
self.total_volume += speed
if self._pred() is None: if self._pred() is None:
self.control_logger.error("预测失败,跳过当前帧") self.control_logger.error("预测失败,跳过当前帧")

View File

@ -23,7 +23,7 @@ class HistoryRecord:
class History: class History:
"""滑动窗口历史记录管理类""" """滑动窗口历史记录管理类"""
def __init__(self, max_window_size: float = 5.0, base_time = 5.0, display: bool = True): def __init__(self, max_window_size: float = 5.0, base_time = 5.0, display: bool = False):
""" """
初始化历史记录管理器 初始化历史记录管理器
@ -290,6 +290,8 @@ class State:
# 状态检查标志 # 状态检查标志
self.in_middle_check = False self.in_middle_check = False
self.in_end_check = False self.in_end_check = False
self.about_check = False
self.about_first_flag = False
# 时间记录 # 时间记录
self.middle_detected_time = None self.middle_detected_time = None
@ -333,7 +335,7 @@ class State:
def exit_about_with_middle(self): def exit_about_with_middle(self):
"""about状态随middle一起退出""" """about状态随middle一起退出"""
if self.mode == self.Mode.ABOUT: if self.mode == self.Mode.ABOUT:
self.mode = self.Mode.FAST self.mode = self.Mode.SLOW
def should_check_middle_exit(self, current_time): def should_check_middle_exit(self, current_time):
"""检查是否应该进行middle退出检查""" """检查是否应该进行middle退出检查"""
@ -355,9 +357,10 @@ class State:
def get_status_text(self): def get_status_text(self):
"""获取状态显示文本""" """获取状态显示文本"""
status = [] status = []
if self.in_middle_check: current_time = time.time()
if self.in_middle_check and current_time - self.middle_detected_time > self.bounce_time:
status.append("MIDCHK") status.append("MIDCHK")
if self.in_end_check: if self.in_end_check and current_time - self.end_detected_time > self.end_bounce_time:
status.append("ENDCHK") status.append("ENDCHK")
return ", " + ", ".join(status) if status else "" return ", " + ", ".join(status) if status else ""