Deal with left time.

Former-commit-id: 0810fdfff0e156ece79ff5848a3dcc94727b9a54
This commit is contained in:
2025-05-26 22:20:13 +08:00
parent b271dd3468
commit 39e841f503
2 changed files with 22 additions and 9 deletions

View File

@ -46,7 +46,7 @@ if OFFLINE_DEBUG:
return time.time() - self.start < self._get_time() return time.time() - self.start < self._get_time()
def _get_time(self): 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): def _prepare(self,speed,t,vol):
if speed is not None: if speed is not None:

View File

@ -3,7 +3,6 @@ import time
from datetime import datetime from datetime import datetime
import numpy as np import numpy as np
import ch340 import ch340
from threading import Thread
import atexit import atexit
class MAT: class MAT:
@ -38,6 +37,15 @@ class MAT:
def ch340_push(self, speed=0.1): def ch340_push(self, speed=0.1):
self.ch340.push_async(speed=speed, t=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): def _pred(self):
"""预测当前图像状态,返回'transport''middle''colored'""" """预测当前图像状态,返回'transport''middle''colored'"""
suc, im = self.cap.read() suc, im = self.cap.read()
@ -52,8 +60,10 @@ class MAT:
now = time.time() now = time.time()
val = self.process_left(now,velue_only=True)
# 记录历史状态保留bounce_time时间内的记录 # 记录历史状态保留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: while self.history and self.history[0][0] < now - self.bounce_time:
self.history.pop(0) self.history.pop(0)
@ -65,13 +75,14 @@ class MAT:
if ret == "middle": if ret == "middle":
# 检测到middle立即切换到慢速模式 # 检测到middle立即切换到慢速模式
if self.typ == 0: if self.typ == 0:
print(f"检测到middle切换到慢速模式当前体积: {self.total_volume}") self.process_left(now)
self.typ = 1 self.typ = 1
print(f"检测到middle切换到慢速模式当前体积: {val}")
elif ret == "colored": elif ret == "colored":
# 检测到colored时记录当前体积 # 检测到colored时记录当前体积
if self.colored_volume is None: if self.colored_volume is None:
self.colored_volume = self.total_volume self.colored_volume = val
self.colored_time = now self.colored_time = now
print(f"检测到colored记录体积: {self.colored_volume}") print(f"检测到colored记录体积: {self.colored_volume}")
@ -84,8 +95,8 @@ class MAT:
self.ch340.stop() self.ch340.stop()
return "colored" return "colored"
else: else:
print(f"colored比例小于90%,当前体积: {self.total_volume}, {colored_count / len(self.history)}") print(f"colored比例小于90%,当前体积: {val}, {colored_count / len(self.history)}")
self.colored_volume = self.total_volume self.colored_volume = val
self.colored_time = now self.colored_time = now
else: # ret == "transport" else: # ret == "transport"
pass pass
@ -94,8 +105,9 @@ class MAT:
if self.typ == 1: if self.typ == 1:
non_middle_count = sum(1 for _, state, _ in self.history if state == "transport") 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: 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.typ = 0
self.process_left(now)
# 如果已记录colored但在bounce_time内colored比例小于90%,重置 # 如果已记录colored但在bounce_time内colored比例小于90%,重置
if self.colored_volume is not None and now - self.colored_time > self.bounce_time: 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("Warning: Unusual condition, colored for over 90%% but last is transport")
print(f"疑似滴定终点: {self.colored_volume}") print(f"疑似滴定终点: {self.colored_volume}")
cv2.putText(im, 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) (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (10,215, 255) if self.typ else (255,255,255), 2)
cv2.imshow("Frame", im) cv2.imshow("Frame", im)
cv2.waitKey(1) cv2.waitKey(1)
@ -142,6 +154,7 @@ class MAT:
self.running = True self.running = True
self.typ = 0 self.typ = 0
self.last = 0 self.last = 0
self.speeds = (quick_speed, slow_speed)
while self.running: while self.running:
if self.total_volume % 12 == 0: if self.total_volume % 12 == 0:
self.ch340_pull() # 抽取12ml self.ch340_pull() # 抽取12ml