From cdc96e4ee3379d4a0e0674bfee2af47bd5eecc33 Mon Sep 17 00:00:00 2001 From: flt6 <1404262047@qq.com> Date: Thu, 5 Jun 2025 14:24:39 +0800 Subject: [PATCH] fix 1 Former-commit-id: 717973133faeb79fa6ed667f3fcf7e614a3f310d --- main.py | 65 +++++++++++++++++++++++++++++++++++++++----------------- utils.py | 12 ++++++++--- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index 7d75ca1..834b1be 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from utils import State, History class MAT: - def __init__(self, videoSourceIndex=0, bounce_time=1, end_bounce_time=5): + def __init__(self, videoSourceIndex=0, bounce_time=2, end_bounce_time=5): # 初始化logging utils.setup_logging() self.system_logger = utils.get_system_logger() @@ -27,6 +27,7 @@ class MAT: atexit.register(self.ch340.stop) self.history = History(max(bounce_time, end_bounce_time)) + self.first_about = 0 self.colored_volume = None self.colored_time = None self.colored_im = None @@ -46,6 +47,8 @@ class MAT: self.ch340.push_async(speed=speed, t=1) def process_left(self, now: float, value_only=False): + if self.state.mode == State.Mode.CRAZY: + value_only = True st = self.ch340.start if not value_only: self.ch340.stop() @@ -80,12 +83,12 @@ class MAT: return ret # 更新滑动窗口历史记录 - 维护最近end_bounce_time内的状态 - self.history.add_record(now, ret,rate, val, im) + self.history.add_record(now, ret, rate, val, im) if self.history.is_empty(): self.control_logger.error("未预期的没有可用的历史记录") return ret - if self.crazy < 3: + if self.state.mode == State.Mode.CRAZY: self._display_status(im, ret, rate, val) return ret @@ -127,12 +130,26 @@ class MAT: if recent_records: trans_ratio = self.history.get_state_ratio("transport", recent_records) - if trans_ratio > 0.3: + if trans_ratio > 0.4: self.process_left(now) self.state.exit_middle_check() # about状态随middle一起退出 - self.state.exit_about_with_middle() - self.control_logger.info(f"middle比例{trans_ratio:.2%}<70%,退出middle检查,返回fast模式") + # self.state.exit_about_with_middle() + self.control_logger.info(f"middle比例{trans_ratio:.2%}<60%,退出middle检查,返回fast模式") + + if self.state.mode == State.Mode.ABOUT and self.state.about_check: + h = self.history.get_recent_records(self.about_time,now) + ratio = self.history.get_state_ratio("about", h) + self.history.about_history.append(ratio<0.3) + while len(self.history.about_history) > 10: + self.history.about_history.pop(0) + if len(self.history.about_history) == 10: + rate = sum(self.history.about_history) / len(self.history.about_history) + if rate > 0.8: + self.state.exit_about() + self.control_logger.info(f"about比例{ratio:.2%}<80%,退出about检查,返回middle模式") + self.state.about_check = False + # end检查: 进入end之后的end_bounce_time,如果end比例<80%,则重置;否则终止实验 if self.state.should_check_end_result(now): @@ -182,6 +199,7 @@ class MAT: State.Mode.FAST: (255, 255, 255), State.Mode.SLOW: (10, 215, 255), State.Mode.ABOUT: (228,116,167), + State.Mode.CRAZY: (0, 255, 0), # State.Mode.END: (0, 0, 255) } @@ -203,7 +221,7 @@ class MAT: rate = val/tot if self.history.base is not None: base = self.history.base - thr = (base*5, base*9, 0.75) + thr = (min(0.05,base*5), min(0.4,base*9), 0.75) if rate < thr[0]: return "transport",rate elif rate mid_time: should_push = True - elif self.state.is_about_mode() and time.time() - self.ch340.start > end_time: + elif self.state.is_about_mode() and time.time() - self.ch340.start > about_time: + if not self.state.about_first_flag: + self.state.about_check = True + else: + self.state.about_first_flag = False should_push = True if should_push: @@ -304,6 +328,7 @@ class MAT: self.ch340_push(speed) self.total_volume += speed + # if (self.state.is_about_mode() and time.time() - self.ch340.start > end_time) or not self.state.is_about_mode(): if self._pred() is None: self.control_logger.error("预测失败,跳过当前帧") continue @@ -329,15 +354,15 @@ if __name__ == "__main__": # 创建MAT类的实例并运行 mat = MAT( videoSourceIndex = 1, - bounce_time=3, + bounce_time=4, end_bounce_time=0.01 ) mat.run( slow_speed = 0.05, quick_speed = 0.15, - end_time= 2, - cap_dir=None + about_time=3, + # cap_dir=None ) diff --git a/utils.py b/utils.py index eda6549..b95d695 100644 --- a/utils.py +++ b/utils.py @@ -9,6 +9,7 @@ import time import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import threading +from collections import deque @dataclass class HistoryRecord: @@ -38,6 +39,7 @@ class History: self.records: List[HistoryRecord] = [] self.max_window_size = max_window_size self.fulled = False + self.about_history = [] # 用于存储最近的about状态记录 self.base = None self._base_time = base_time # 可视化相关属性 self.display = display @@ -280,6 +282,7 @@ class State: FAST = 0 # 快速模式 SLOW = 1 # 慢速模式 (middle) ABOUT = 2 # 接近终点模式 + CRAZY = 2 # CRAZY模式 # END = 3 # 终点模式 def __init__(self, bounce_time=1, end_bounce_time=5): @@ -332,8 +335,10 @@ class State: self.middle_detected_time = None self.mode = self.Mode.FAST - def exit_about_with_middle(self): - """about状态随middle一起退出""" + def exit_about(self): + """about状态退出""" + self.about_check = False + self.about_first_flag = True if self.mode == self.Mode.ABOUT: self.mode = self.Mode.SLOW @@ -341,7 +346,8 @@ class State: """检查是否应该进行middle退出检查""" return (self.in_middle_check and self.middle_detected_time is not None and - current_time - self.middle_detected_time > self.bounce_time) + current_time - self.middle_detected_time > self.bounce_time and + (self.mode == self.Mode.SLOW)) def should_check_end_result(self, current_time): """检查是否应该进行end结果检查"""