clean code and use ch340
Former-commit-id: 3e401751a24183eb1fa03df0a835624ed6ca6f9d
This commit is contained in:
@ -6,7 +6,7 @@ import serial
|
||||
from datetime import datetime
|
||||
import numpy as np
|
||||
import joblib
|
||||
import Find_COM
|
||||
import ch340
|
||||
from threading import Thread
|
||||
import atexit
|
||||
|
||||
@ -23,9 +23,7 @@ class MAT:
|
||||
self.videoSourceIndex = videoSourceIndex # 摄像机编号
|
||||
self.cap = cv2.VideoCapture(videoSourceIndex, cv2.CAP_DSHOW) # 打开摄像头
|
||||
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
||||
if not LOCAL_DEBUG:
|
||||
self.port = Find_COM.list_ch340_ports()[0] # 串口名
|
||||
self.pump_ser = serial.Serial(self.port, 9600) # 初始化串口
|
||||
self.ch340 = ch340.CH340()
|
||||
self.classes = classes
|
||||
self.bounce_time = bounce_time # 防抖时间
|
||||
self.total_volume = 0 # 记录总体积
|
||||
@ -38,90 +36,24 @@ class MAT:
|
||||
# 将开始时间转化为年月日时分秒的格式,后续文件命名都已此命名
|
||||
self.formatted_time = datetime.fromtimestamp(self.start_time).strftime('%Y%m%d_%H%M%S')
|
||||
|
||||
self.model = joblib.load("model.pkl")
|
||||
atexit.register(self.start_move_3)
|
||||
print("实验开始于", self.formatted_time)
|
||||
|
||||
def start_move_1(self): # 抽料程序
|
||||
if LOCAL_DEBUG:return
|
||||
self.start_move_init()
|
||||
data = b"q1h40d" # *2
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q2h0d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q4h0d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q5h9d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q6h3d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(9)
|
||||
self.ch340.max_speed()
|
||||
self.ch340.pull(vol=12)
|
||||
print('完成抽取')
|
||||
|
||||
def start_move_init(self): # init
|
||||
if LOCAL_DEBUG:return
|
||||
data = b"q1h15d" # *2
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q2h0d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q4h0d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q5h2d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q6h2d"
|
||||
self.pump_ser.write(data)
|
||||
print("send1")
|
||||
time.sleep(2)
|
||||
data = b"q1h20d" # *2
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q5h1d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.1)
|
||||
data = b"q6h3d"
|
||||
self.pump_ser.write(data)
|
||||
print("send2")
|
||||
time.sleep(1)
|
||||
self.ch340.push(speed=1,t=1)
|
||||
self.ch340.pull(speed=1.2,vol=3)
|
||||
print('INITED')
|
||||
|
||||
def start_move_2(self, speed=0.1): # 进料程序
|
||||
if LOCAL_DEBUG:
|
||||
time.sleep(1)
|
||||
return
|
||||
# 计算单次滴定体积并传输至控制器
|
||||
speed_min = speed * 30
|
||||
speed_min_int = int(speed_min)
|
||||
speed_min_float = int((speed_min - speed_min_int) * 100)
|
||||
# print(speed_min_int, speed_min_float)
|
||||
data = f"q1h{speed_min_int}d"
|
||||
self.pump_ser.write(data.encode('ascii'))
|
||||
time.sleep(0.01)
|
||||
data = f"q2h{speed_min_float}d"
|
||||
self.pump_ser.write(data.encode('ascii'))
|
||||
time.sleep(0.01)
|
||||
data = b"q4h0d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
data = b"q5h1d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(0.01)
|
||||
# 进料
|
||||
data = b"q6h2d"
|
||||
self.pump_ser.write(data)
|
||||
time.sleep(1)
|
||||
self.ch340.push(speed=speed, t=1)
|
||||
|
||||
def start_move_3(self): # 进料急停
|
||||
if LOCAL_DEBUG:return
|
||||
data = b"q6h6d"
|
||||
self.pump_ser.write(data)
|
||||
self.ch340.stop()
|
||||
|
||||
def _pred(self):
|
||||
suc,im = self.cap.read()
|
||||
@ -195,7 +127,6 @@ class MAT:
|
||||
return "colored"
|
||||
|
||||
def __del__(self):
|
||||
self.pump_ser.close()
|
||||
self.cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
print("Experiment finished.")
|
||||
@ -218,7 +149,6 @@ class MAT:
|
||||
self.debounce = [[],[]]
|
||||
self.thr = Thread(target=self._pred)
|
||||
self.thr.start()
|
||||
switching_point = expect * 0.9
|
||||
while self.running:
|
||||
if self.now_volume <= 0:
|
||||
self.start_move_1() # 抽取12ml
|
||||
@ -247,16 +177,11 @@ class MAT:
|
||||
self.save_img()
|
||||
print('----->>Visual Endpoint<<-----')
|
||||
print(f"Total Volume: {self.total_volume} ml")
|
||||
# print(f"Image File: {im_file}")
|
||||
print("Volume List:", self.volume_list)
|
||||
print("Color List:", self.color_list)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import warnings
|
||||
# 忽略所有警告
|
||||
warnings.filterwarnings('ignore')
|
||||
|
||||
# 创建MAT类的实例并运行
|
||||
mat = MAT(
|
||||
videoSourceIndex = 1,
|
||||
|
Reference in New Issue
Block a user