diff --git a/ch340.py b/ch340.py index bdeb4ea..efda3e1 100644 --- a/ch340.py +++ b/ch340.py @@ -46,7 +46,7 @@ if OFFLINE_DEBUG: return time.time() - self.start < self._get_time() def _get_time(self): - return (self.time[0]*3600 + self.time[1]*60 + self.time[2])/2 + return (self.time[0]*3600 + self.time[1]*60 + self.time[2]) def _prepare(self,speed,t,vol): if speed is not None: diff --git a/predictor_Syringe_Pump.py b/predictor_Syringe_Pump.py index d72eb35..fd1727e 100644 --- a/predictor_Syringe_Pump.py +++ b/predictor_Syringe_Pump.py @@ -3,7 +3,6 @@ import time from datetime import datetime import numpy as np import ch340 -from threading import Thread import atexit class MAT: @@ -38,6 +37,15 @@ class MAT: def ch340_push(self, speed=0.1): self.ch340.push_async(speed=speed, t=1) + def process_left(self,now:float, velue_only=False): + st = self.ch340.start + if not velue_only: self.ch340.stop() + assert abs(self.ch340._get_time()-1) < 1e3 # run time should be 1s + r = now-st + ret = self.total_volume - (1-r)*self.speeds[self.typ] # 减去未完成的体积 + if velue_only:return ret + else:self.total_volume = ret + def _pred(self): """预测当前图像状态,返回'transport'、'middle'或'colored'""" suc, im = self.cap.read() @@ -52,8 +60,10 @@ class MAT: now = time.time() + val = self.process_left(now,velue_only=True) + # 记录历史状态,保留bounce_time时间内的记录 - self.history.append((now, ret, self.total_volume)) + self.history.append((now, ret, val)) while self.history and self.history[0][0] < now - self.bounce_time: self.history.pop(0) @@ -65,13 +75,14 @@ class MAT: if ret == "middle": # 检测到middle立即切换到慢速模式 if self.typ == 0: - print(f"检测到middle,切换到慢速模式,当前体积: {self.total_volume}") + self.process_left(now) self.typ = 1 + print(f"检测到middle,切换到慢速模式,当前体积: {val}") elif ret == "colored": # 检测到colored时记录当前体积 if self.colored_volume is None: - self.colored_volume = self.total_volume + self.colored_volume = val self.colored_time = now print(f"检测到colored,记录体积: {self.colored_volume}") @@ -84,8 +95,8 @@ class MAT: self.ch340.stop() return "colored" else: - print(f"colored比例小于90%,当前体积: {self.total_volume}, {colored_count / len(self.history)}") - self.colored_volume = self.total_volume + print(f"colored比例小于90%,当前体积: {val}, {colored_count / len(self.history)}") + self.colored_volume = val self.colored_time = now else: # ret == "transport" pass @@ -94,8 +105,9 @@ class MAT: if self.typ == 1: 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%,切回快速模式,当前体积: {self.total_volume}") + print(f"非middle比例超过90%,切回快速模式,当前体积: {val}") self.typ = 0 + self.process_left(now) # 如果已记录colored但在bounce_time内colored比例小于90%,重置 if self.colored_volume is not None and now - self.colored_time > self.bounce_time: @@ -113,7 +125,7 @@ class MAT: print("Warning: Unusual condition, colored for over 90%% but last is transport") print(f"疑似滴定终点: {self.colored_volume}") cv2.putText(im, - f"State: {ret}, rate: {round(rate,2)}, Vol: {self.total_volume:.2f} ml, typ: {'slow' if self.typ else 'fast'}", + f"State: {ret}, rate: {round(rate,2)}, Vol: {val:.2f} ml, typ: {'slow' if self.typ else 'fast'}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (10,215, 255) if self.typ else (255,255,255), 2) cv2.imshow("Frame", im) cv2.waitKey(1) @@ -142,6 +154,7 @@ class MAT: self.running = True self.typ = 0 self.last = 0 + self.speeds = (quick_speed, slow_speed) while self.running: if self.total_volume % 12 == 0: self.ch340_pull() # 抽取12ml