clean and adapt debounce

Former-commit-id: d3368549aeb92c55007f45be4e18171fb8176791
This commit is contained in:
2025-05-18 15:06:35 +08:00
parent eed3f00ae1
commit 7cb0283ca8
2 changed files with 23 additions and 36 deletions

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1ca6d267a1b151f02a2096b1e7fbcea8abc298b6feec224c200a8b9a81fc2fc8
size 107513

View File

@ -127,25 +127,6 @@ class MAT:
data = b"q6h6d"
self.pump_ser.write(data)
def preproc(self, im):
try:
hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
mask = hsv[:,:,1] > 150
mask = mask[:,:,np.newaxis]
cnt = np.count_nonzero(mask)
if cnt == 0:
return 0,0,0
hsv*=mask
h = round(np.sum(hsv[:,:,0])/cnt)
s = round(np.sum(hsv[:,:,1])/cnt)
v = round(np.sum(hsv[:,:,2])/cnt)
return h,s,v
except Exception as e :
import traceback
traceback.print_exc()
return None
# name = f"{cl}_{h}_{s}_{v}.jpg"
def _pred(self):
suc,im = self.cap.read()
if not suc:
@ -169,19 +150,24 @@ class MAT:
self.last[1] += 1
# fps end
now = time.time()
if ret == self.end_kind:
if self.debounce[0]:
if ret == "middle":
if self.debounce[0] and self.typ == 0:
if self.debounce[1] and self.debounce[0]:
# print(self.debounce)
if now-self.debounce[0][-1] > self.bounce_time:
print("Bounce check succeeded, val:",self.debounce[1][0])
self.running = False
self.start_move_3()
return
else:
print("Got stop flag, bounce check start, val:",self.total_volume)
print("Got middle flag, bounce check start, val:",self.total_volume)
self.typ = 1
self.start_move_3()
self.debounce[1].append((time.time(),self.total_volume))
elif ret == self.end_kind:
if self.debounce[1]:
print("Got stop flag, val:",self.total_volume)
self.running = False
self.start_move_3()
return
else:
if self.debounce[0]:
# print(self.debounce)
@ -200,13 +186,15 @@ class MAT:
# im = cv2.imread(file)
hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
s = hsv[:,:,1]
mask = s>100
# print(mask)
mask = s>60
tot = mask.shape[0]*mask.shape[1]
val = np.sum(mask)
# print(val/tot)
if val<tot*0.3:
rate = val/tot
if rate < 0.01:
return "transport"
elif rate <0.2:
return "middle"
else:
return "colored"
@ -259,6 +247,7 @@ class MAT:
def run(self,quick_speed = 0.2, mid_speed=0.1,slow_speed = 0.05,expect:float|int = 5, end_kind = 'orange'):
self.running = True
self.typ = 0
self.end_kind = end_kind
self.last = [time.time(),0]
self.debounce = [[],[]]
@ -270,7 +259,7 @@ class MAT:
self.start_move_1() # 抽取12ml
self.now_volume += 12
if self.total_volume < switching_point: # 每次加0.2ml
if self.typ == 0: # 每次加0.2ml
speed = quick_speed
self.start_move_2(speed)
self.total_volume += speed
@ -280,6 +269,7 @@ class MAT:
self.start_move_2(speed) # 每次加0.05ml
self.total_volume += speed
self.now_volume -= speed
time.sleep(1)
self.total_volume = round(self.total_volume, 3)
@ -308,13 +298,13 @@ if __name__ == "__main__":
weights_path = "resnet34-1Net.pth",
json_path = 'class_indices.json',
classes = 2,
bounce_time=1
bounce_time=0.2
)
# exit()
mat.run(
quick_speed = 0.3,
slow_speed = 0.2,
quick_speed = 0.15,
slow_speed = 0.05,
expect = 10,
end_kind = 'colored',
)