From b95037a30191a7880b215e0849d6bc44d7bc8edf Mon Sep 17 00:00:00 2001 From: flt6 <1404262047@qq.com> Date: Mon, 26 May 2025 22:35:43 +0800 Subject: [PATCH] Fix debounce bug, fix bounce time error Former-commit-id: 4771ecad93519acb1316e8839f1aeb56c7aa2622 --- predictor_Syringe_Pump.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/predictor_Syringe_Pump.py b/predictor_Syringe_Pump.py index fd1727e..8f18020 100644 --- a/predictor_Syringe_Pump.py +++ b/predictor_Syringe_Pump.py @@ -14,6 +14,7 @@ class MAT: self.bounce_time = bounce_time self.total_volume = 0 self.start_time = time.time() + self.middle_time = time.time() # 将开始时间转化为年月日时分秒的格式,后续文件命名都已此命名 self.formatted_time = datetime.fromtimestamp(self.start_time).strftime('%Y%m%d_%H%M%S') @@ -77,6 +78,7 @@ class MAT: if self.typ == 0: self.process_left(now) self.typ = 1 + self.middle_time = now print(f"检测到middle,切换到慢速模式,当前体积: {val}") elif ret == "colored": @@ -102,12 +104,14 @@ class MAT: pass # 在慢速模式下检查是否要切回快速模式 - if self.typ == 1: + if self.typ == 1 and now - self.middle_time > self.bounce_time: non_middle_count = sum(1 for _, state, _ in self.history if state == "transport") - if len(self.history) > 3 and non_middle_count / len(self.history) > 0.9: - print(f"非middle比例超过90%,切回快速模式,当前体积: {val}") + if non_middle_count / len(self.history) > 0.9: self.typ = 0 self.process_left(now) + # TODO: 滑动到第一个middle状态 + self.middle_time = 0 + print(f"非middle比例超过90%,切回快速模式,当前体积: {val}") # 如果已记录colored但在bounce_time内colored比例小于90%,重置 if self.colored_volume is not None and now - self.colored_time > self.bounce_time: @@ -115,8 +119,12 @@ class MAT: if colored_count / len(self.history) < 0.9: print(f"colored比例小于90%,重置colored记录") self.colored_volume = None + flag = False for t, state,vol in self.history: if state == "colored": + if not flag: + flag = True + continue print(f"滑动窗口到: {vol} at {t}") self.colored_volume = vol self.colored_time = t @@ -178,13 +186,15 @@ class MAT: print('----->>Visual Endpoint<<-----') print(f"Total Volume: {self.total_volume} ml") + print(f"End point volume: {self.colored_volume} ml") + print(f"Theorical fixed volume: {self.colored_volume} ml") if __name__ == "__main__": # 创建MAT类的实例并运行 mat = MAT( videoSourceIndex = 1, - bounce_time=0.2 + bounce_time=1 ) mat.run(