python写个称量的类,未完待续

import threading
import tkinter as tk
data=0
def fun():
    global data
    data+=20
    print(data)
    return data




class ScaleSensor:
    '''定义一个称类'''
    def __init__(self,advance_weight,target_weight):
        '''
        :param current_weight: 当前重量
        :param advance_weight: 设定提前量
        :param target_weight: 目标重量
        '''
        self.current_weight = 0
        self.advance_weight = advance_weight
        self.target_weight = target_weight
        self.rough_stability_time=0.5
        self.accuracy_stablility_time=0.1
        self.deleay_time=0.3
        self.discharge_time=1 #卸料延时
        self.is_weightting=True
        self.widget_var = None
    def get_current_weight(self,fun):
        '''
        :param fun: 获取称值函数
        :return:
        '''
        value=fun()
        return value

    def set_advance_weight(self,value:int):
        self.advance_weight=value

    def set_target_weight(self,value:int):
        self.target_weight=value

    def set_stability_time(self,value:int):
        self.stability_time = value

    def set_dealay_time(self,value:int):
        self.deleay_time=value

    def rough_open_door(self):
        print('粗称时开两门')
    def rough_close_door(self):
        print('粗称门关')
    def accuracy_open_door(self):
        print('精称门开')
    def accuracy_close_door(self):
        print('精称门关')

    def open_weighing_door(self):
        print('打开称门')

    def close_weighing_door(self):
        print('关闭称门')

    def widget_show(self,widget_var):
        '''
        :param widget_var: tk.StringVar,tk.IntVar
        '''
        self.widget_var=widget_var

    #称量
    def start_weight(self):
        '''
        :param callback: 需要执行的函数,如给plc指定值
        :param widget: 把值在界面上更新
        :return:
        '''
        import time

        while True:
            self.current_weight=self.get_current_weight(fun) #获取重量值

            if self.widget_var:
                self.widget_var.set(str(self.current_weight))

            if self.is_weightting == False:
                break

            if self.current_weight>self.target_weight:
                return
            elif self.current_weight=self.target_weight-self.advance_weight:
                self.accuracy_open_door()
                time.sleep(self.accuracy_stablility_time)
                self.accuracy_close_door()
            print('----------')

            time.sleep(self.deleay_time)   #稳定延时
            min_wight=self.target_weight-self.target_weight*0.02
            max_wight=self.target_weight+self.target_weight*0.02
            if min_wight<= self.current_weight<=max_wight:
                self.accuracy_close_door()
                print('称量结束',self.current_weight)
                return
    #卸料
    def discharge(self):

        import time
        weight = self.current_weight
        need_weight= weight-self.target_weight
        while True:
            if weight >self.target_weight+self.target_weight*0.02:
                self.open_weighing_door()
                time.sleep(self.deleay_time)
                self.close_weighing_door()
                time.sleep(self.deleay_time)
                if need_weight-need_weight*0.02<=self.current_weight<=need_weight+need_weight*0.02:
                    break
            #如果当前重量在目标重量范围内,直接开门卸料,当检测重量在范围内,直接关门
            elif self.current_weight<=self.target_weight+self.target_weight*0.02:
                self.open_weighing_door()

                if self.current_weight<=20:
                    break






def btn_click():
    t.start()

def fun2():
    scal=ScaleSensor(100,345)

    scal.widget_show(sacl_var)
    scal.start_weight()
    print('******')
    print(scal.current_weight)
    print("******")
    scal.discharge()



if __name__ == '__main__':



    root=tk.Tk()
    root.geometry('500x400')
    sacl_var=tk.StringVar()

    label=tk.Label(root,bg='green',width=20,height=5,textvariable=sacl_var)
    label.place(x=100,y=30)

    btn=tk.Button(root,text='click',command=btn_click)
    btn.place(x=100,y=150)

    t=threading.Thread(target=fun2)

    root.mainloop()



你可能感兴趣的:(python)