subDesTagesMitExtraKaese 4 سال پیش
والد
کامیت
a9a6083a91
1فایلهای تغییر یافته به همراه22 افزوده شده و 73 حذف شده
  1. 22 73
      software/ui/tkinter_windkanal.py

+ 22 - 73
software/ui/tkinter_windkanal.py

@@ -9,60 +9,25 @@ import matplotlib.animation as animation
 from matplotlib import style
 
 import tkinter as tk
-import serial
 import tk_tools
-import threading
-import queue
 from time import *
 
 LARGE_FONT= ("Verdana", 12)
 style.use("ggplot")
 
-#initialize serial port
-ser = serial.Serial()
-ser.port = '/dev/ttyUSB0' #Arduino serial port
-ser.baudrate = 9600
-ser.timeout = 10 #specify timeout when using readline()
-ser.open()
-if ser.is_open==True:
-    print("\nAll right, serial port now open. Configuration:\n")
-    print(ser, "\n") #print serial parameters
-
-fig = Figure()
-ax = fig.add_subplot(111)
-xs = [] #store trials here (n)
-ys = [] #store relative frequency here
-
-class getdata(threading.Thread):
-    def __init__(self, label, t, q):
-        threading.Thread.__init__(self)       
-        self.jobqueue = q # get job queue for sending commands
-        self.t = t 
+class Plot():
+    def __init__(self, n):
+        self.fig = Figure()
+        self.ax = self.fig.add_subplot(111)
+        self.xs = range(n)
+        self.ys = [0] * n
+        self.i = 0
+        self.n = n
 
-       
-    def run(self):
-        while True:# and self.mb.entrycget(1,"label")=="Stop":
-            sleep(0.1) # wait a second
-            SerialObj = Serial()
-            self.t = SerialObj.animate(self)
-
-            # send to job queue !!!
-            self.jobqueue.put(("cnt_label",self.t))
-
-class Serial(tk.Tk):
-    def __init__(self):
-        tk.Frame.__init__(self)
-
-    def animate(i,self):
-        line=ser.readline()      #ascii
-        line_as_list = line.split(b',')
-        i = int(line_as_list[0])
-        relProb = line_as_list[1]
-        relProb_as_list = relProb.split(b'\n')
-        relProb_float = float(relProb_as_list[0])
+    def update(self, y):
         # Add x and y to lists
-        xs.append(i)
-        ys.append(relProb_float)
+        ys[i] = y
+        i = i+1 if i+1 < self.n else 0
 
 
         ax.clear()
@@ -70,7 +35,7 @@ class Serial(tk.Tk):
         ax.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=2, borderaxespad=0)
         ax.set_title('Windkanal')
 
-        return relProb_float
+        return self.fig
        
 
 class Main(tk.Tk):
@@ -174,7 +139,8 @@ class Page_1(tk.Frame):
         button5.pack(side=tk.LEFT)
         top.pack(side="top", expand=True, fill="both")
         # graph
-        canvas = FigureCanvasTkAgg(fig, self)
+        self.serialPlot = Plot()
+        canvas = FigureCanvasTkAgg(self.serialPlot.fig, self)
         canvas.draw()
         canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
         canvas._tkcanvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
@@ -183,26 +149,10 @@ class Page_1(tk.Frame):
         container = tk.Frame(left, borderwidth=2, relief="solid")
         label1 = tk.Label(container, text="I could be a canvas, but I'm a label right now")
 
-        SerialObj = Serial()
-        #p = tk_tools.RotaryScale(self, max_value=22.0, unit='m/s')
-        #p.pack()
-        #p.set_value(SerialObj.animate(self))
-        #print(SerialObj.animate(self))
-
-        self.seconds = SerialObj.animate(self)
-
         self.label4 = tk.Label(self,font=("Arial","30"),fg="red")
         self.label4.pack()
         self.label4.config(text=str(self.t))
 
-        self.q = queue.Queue() # Make job queue (Queue)
-        offswitch = threading.Event() # Make offswitch (Event)
-        
-        #self.label4.after(100,self.label_update)
-        
-        cd = getdata(self.label4, self.t, self.q)
-        cd.start()
-
         SendButton = tk.Button(left, text='Quit', command=quit)
         label2 = tk.Label(left, text="I could be a button")
         label3 = tk.Label(left, text="So could I")
@@ -215,16 +165,17 @@ class Page_1(tk.Frame):
         label2.pack()
         label3.pack()
 
-    def label_update(self):
-        job = self.q.get() #get job form queue!!!
+        self.after(100,self.interval)
 
+    def interval(self):
         
-        if job[0] == "cnt_label":
-            self.label4.config(text=str(job[1]) + " Nm")
-        else:
-            print("Unknown job:", job)
+
+
+        self.serialPlot.update(3)
+
+        self.label4.config(text="{:3d} Nm".format(3))
             
-        self.label4.after(100,self.label_update)
+        self.after(100,self.interval)
 
 
     def sendFactorToMCU(self):
@@ -233,6 +184,4 @@ class Page_1(tk.Frame):
 
 app = Main()
 app.geometry("1280x720")
-SerialObj = Serial()
-ani = animation.FuncAnimation(fig, SerialObj.animate, interval=500)
 app.mainloop()