class App: def __init__(self, master): # Plot Settings self.fig = plt.figure(1) # Graph params for link self.link, = plt.plot([], [], 'r-', linewidth=4) # Graph params for joint self.joints, = plt.plot([], [], marker='o', ls="", markersize=10) # 1)Create a plot self.plot1 = FigureCanvasTkAgg(self.fig, master=window).get_tk_widget() # 2)Input fields self.entry1 = IntVar() self.entry2 = IntVar() self.entry3 = IntVar() self.entry4 = IntVar() self.input1 = Entry(window, textvariable=self.entry1) self.input2 = Entry(window, textvariable=self.entry2) self.input3 = Entry(window, textvariable=self.entry3) self.input4 = Entry(window, textvariable=self.entry4) # 3)Button: self.button1 = Button(window, text="START", command=self.start) self.button2 = Button(window, text="STOP", command=self.stop) # 4)Label: self.output1 = Label(window, text="Longest:") self.output2 = Label(window, text="Smallest:") self.output3 = Label(window, text="L3:") self.output4 = Label(window, text="L4:") self.warning = Label(window, text="") self.label = Label(window, text="Done by group 10") # Locations self.plot1.place(x=0, y=0, height=500, width=800) self.output1.place(x=0, y=500, height=20, width=100) self.input1.place(x=100, y=500, height=20, width=100) self.output2.place(x=200, y=500, height=20, width=100) self.input2.place(x=300, y=500, height=20, width=100) self.output3.place(x=400, y=500, height=20, width=100) self.input3.place(x=500, y=500, height=20, width=100) self.output4.place(x=600, y=500, height=20, width=100) self.input4.place(x=700, y=500, height=20, width=100) self.warning.place(x=100, y=550, height=20, width=600) self.label.place(x=550, y=660, height=20, width=300) self.button1.place(x=100, y=600, height=30, width=200) self.button2.place(x=500, y=600, height=30, width=200) # 5)Mechanism Animation self.anim = animation.FuncAnimation(self.fig, self.animate, np.arange(0, 2 * np.pi, 0.01), interval=10, blit=False) # Start and stop Animation self.k = 0 # (0, 1) :: (stop, start). # Calc variables based on theta2: def calculate(self, theta2): L1 = self.L1 L2 = self.L2 L3 = self.L3 L4 = self.L4 BD = np.sqrt(L1**2 + L2**2 - 2 * L1 * L2 * np.cos(theta2)) alfa = np.arccos((L3**2 + L4**2 - BD**2) / (2 * L3 * L4)) theta3 = 2 * np.arctan( (-L2 * np.sin(theta2) + L4 * np.sin(alfa)) / (L1 + L3 - L2 * np.cos(theta2) - L4 * np.cos(alfa))) theta4 = 2 * np.arctan( (L2 * np.sin(theta2) - L3 * np.sin(alfa)) / (L4 - L1 + L2 * np.cos(theta2) - L3 * np.cos(alfa))) # (x, y) of A, B, C, D A = [0, 0] B = [L2 * np.cos(theta2), L2 * np.sin(theta2)] C = [ L2 * np.cos(theta2) + L3 * np.cos(theta3), L2 * np.sin(theta2) + L3 * np.sin(theta3) ] D = [L1, 0] return BD, alfa, theta3, theta4, A, B, C, D # Automatic Axis adjuster def axis_setter(self): L1 = self.L1 # from constructor. t2 = np.arange(0, 2 * np.pi, 0.01) BD, alfa, theta3, theta4, A, B, C, D = self.calculate(t2) B = np.array(B) C = np.array(C) K = C.max(axis=1) M = C.min(axis=1) Cx_max = K[0] Cy_max = K[1] Cx_min = M[0] Cy_min = M[1] F = [np.amin(B), M[0]] G = [np.amin(B), M[1]] upper_x = L1 + abs(Cx_max - L1) + 0.5 lower_x = np.amin(F) - 0.5 upper_y = Cy_max + 0.5 lower_y = np.amin(G) - 0.5 del BD, alfa, theta3, theta4, A, B, C, D, t2 return upper_x, lower_x, upper_y, lower_y # -------------------------------------------------------------- # When Started: def start( self ): # Since variable k is registered in self, it is enough to input self, not self.k! self.L1 = self.entry1.get() self.L2 = self.entry2.get() self.L3 = self.entry3.get() self.L4 = self.entry4.get() try: upper_x, lower_x, upper_y, lower_y = self.axis_setter() plt.axis([lower_x, upper_x, lower_y, upper_y]) # Set Axis plt.gca().set_aspect('equal', adjustable='box') self.k = 1 self.anim.event_source.start() # Animation loop self.warning.config(text="", background="white") except: self.warning.config(text="Error in link lengths.", background="red", font=("Courier", 14)) self.stop() return self.k # When the stop button is pressed: def stop(self): self.k = 0 # Stop animation return self.k # Animation part theta2 = 0: 0.01: 2pi def animate(self, theta2): if (self.k == 0): self.anim.event_source.stop() # exit animate loop\ else: # Instantaneous parameter values of the mechanism BD, alfa, theta3, theta4, A, B, C, D = self.calculate(theta2) x = [A[0], B[0], C[0], D[0]] # X of joint y = [A[1], B[1], C[1], D[1]] # Y of joint self.link.set_data(x, y) # Instant location of links plotted # The instantaneous location of the joints has been plotted. self.joints.set_data(x, y) return self.link, self.joints
class App: def __init__(self, master): #Plot Ayarları self.fig = plt.figure(1) #Figure 1'e fig ismi verildi. self.link, = plt.plot( [], [], 'r-', linewidth=4) #link için oluşturulan grafik parametreleri self.joints, = plt.plot( [], [], marker='o', ls="", markersize=10) #joint için oluşturulan grafik parametreleri #1)Plot oluşturma(plot1 ismi değişkendir) -fig'e kayıtlı 1.plot alındı: self.plot1 = FigureCanvasTkAgg(self.fig, master=window).get_tk_widget() #2)Giriş kutusu oluşturma: self.giris1 = IntVar() self.giris2 = IntVar() self.giris3 = IntVar() self.giris4 = IntVar() self.input1 = Entry(window, textvariable=self.giris1) self.input2 = Entry(window, textvariable=self.giris2) self.input3 = Entry(window, textvariable=self.giris3) self.input4 = Entry(window, textvariable=self.giris4) #3)Buton oluşturma: self.buton1 = Button(window, text="Start", command=self.start) self.buton2 = Button(window, text="Stop", command=self.stop) #4)Label oluşturma: self.output1 = Label(window, text="L1:") self.output2 = Label(window, text="L2:") self.output3 = Label(window, text="L3:") self.output4 = Label(window, text="L4:") self.warning = Label(window, text="") self.label = Label(window, text="Designed by Berke Ogulcan Parlak") #Konumlar self.plot1.place(x=0, y=0, height=500, width=800) self.output1.place(x=0, y=500, height=20, width=100) self.input1.place(x=100, y=500, height=20, width=100) self.output2.place(x=200, y=500, height=20, width=100) self.input2.place(x=300, y=500, height=20, width=100) self.output3.place(x=400, y=500, height=20, width=100) self.input3.place(x=500, y=500, height=20, width=100) self.output4.place(x=600, y=500, height=20, width=100) self.input4.place(x=700, y=500, height=20, width=100) self.warning.place(x=100, y=550, height=20, width=600) self.label.place(x=550, y=660, height=20, width=300) self.buton1.place(x=100, y=600, height=30, width=200) self.buton2.place(x=500, y=600, height=30, width=200) #5)Animasyon oluşturma self.anim = animation.FuncAnimation(self.fig, self.animate, np.arange(0, 2 * np.pi, 0.01), interval=10, blit=False) #Animasyonu başlat/durdur değişkeni self.k = 0 # k = 0 ise animasyonu durdur, 1 ise başlat. #-------------------------------------------------------------- #Theta2'ye bağlı olarak değişken değişkenleri hesaplamak için fonksiyon tanımlandı: def calculate(self, theta2): L1 = self.L1 L2 = self.L2 L3 = self.L3 L4 = self.L4 BD = np.sqrt(L1**2 + L2**2 - 2 * L1 * L2 * np.cos(theta2)) alfa = np.arccos((L3**2 + L4**2 - BD**2) / (2 * L3 * L4)) theta3 = 2 * np.arctan( (-L2 * np.sin(theta2) + L4 * np.sin(alfa)) / (L1 + L3 - L2 * np.cos(theta2) - L4 * np.cos(alfa))) theta4 = 2 * np.arctan( (L2 * np.sin(theta2) - L3 * np.sin(alfa)) / (L4 - L1 + L2 * np.cos(theta2) - L3 * np.cos(alfa))) #A,B,C,D jointlerin x ve y konumları A = [0, 0] B = [L2 * np.cos(theta2), L2 * np.sin(theta2)] C = [ L2 * np.cos(theta2) + L3 * np.cos(theta3), L2 * np.sin(theta2) + L3 * np.sin(theta3) ] D = [L1, 0] return BD, alfa, theta3, theta4, A, B, C, D #-------------------------------------------------------------- #Otomatik eksen ayarlayıcı def axis_setter(self): L1 = self.L1 #self içinden L1 alındı. t2 = np.arange(0, 2 * np.pi, 0.01) BD, alfa, theta3, theta4, A, B, C, D = self.calculate(t2) B = np.array(B) C = np.array(C) K = C.max(axis=1) M = C.min(axis=1) Cx_max = K[0] Cy_max = K[1] Cx_min = M[0] Cy_min = M[1] F = [np.amin(B), M[0]] G = [np.amin(B), M[1]] upper_x = L1 + abs(Cx_max - L1) + 0.5 lower_x = np.amin(F) - 0.5 upper_y = Cy_max + 0.5 lower_y = np.amin(G) - 0.5 del BD, alfa, theta3, theta4, A, B, C, D, t2 return upper_x, lower_x, upper_y, lower_y #-------------------------------------------------------------- #Start butonuna basıldığında: def start( self ): #k değişkeni self içine kayıtlı olduğundan, self.k değilde self'i giriş almak yeterli! self.L1 = self.giris1.get() self.L2 = self.giris2.get() self.L3 = self.giris3.get() self.L4 = self.giris4.get() try: upper_x, lower_x, upper_y, lower_y = self.axis_setter() plt.axis([lower_x, upper_x, lower_y, upper_y]) #Ekseni ayarla plt.gca().set_aspect('equal', adjustable='box') self.k = 1 self.anim.event_source.start() #animate döngüsüne girer self.warning.config(text="", background="white") except: self.warning.config( text="Link lengths are not suitable, please change.", background="red", font=("Courier", 14)) self.stop() return self.k #-------------------------------------------------------------- #Stop butonuna basıldığında: def stop(self): self.k = 0 #Animasyonu durdur return self.k #-------------------------------------------------------------- #Animasyon kısmı theta2 = 0:0.01:2pi def animate(self, theta2): if (self.k == 0): self.anim.event_source.stop() #animate döngüsünden çıkar else: BD, alfa, theta3, theta4, A, B, C, D = self.calculate( theta2) #Mekanizmanın anlık parametre değerleri x = [A[0], B[0], C[0], D[0]] #Jointlerin X konumları y = [A[1], B[1], C[1], D[1]] #Jointlerin Y konumları # Neden burada delete kullanmıyoruz? # -Çünkü x ve y vektör değil skalerdir(Mekanizmanın anlık konumu) # -Bizde line.set_data(x,y) diyerek mekanizmanın sadece anlık konumu almasını sağlıyoruz. self.link.set_data(x, y) #Linklerin anlık konumu çizdirildi self.joints.set_data(x, y) #Jointlerin anlık konumu çizdirildi return self.link, self.joints
class Treadmill: def __init__(self, top=None): '''This class configures and populates the toplevel window. top is the toplevel containing window.''' _bgcolor = '#d9d9d9' # X11 color: 'gray85' _fgcolor = '#000000' # X11 color: 'black' _compcolor = '#d9d9d9' # X11 color: 'gray85' _ana1color = '#d9d9d9' # X11 color: 'gray85' _ana2color = '#d9d9d9' # X11 color: 'gray85' self.style = ttk.Style() if sys.platform == "win32": self.style.theme_use('winnative') self.style.configure('.', background=_bgcolor) self.style.configure('.', foreground=_fgcolor) self.style.configure('.', font="TkDefaultFont") self.style.map('.', background=[('selected', _compcolor), ('active', _ana2color)]) top.geometry("1024x600+0+0") top.attributes("-fullscreen", True) top.title("Treadmill") top.configure(highlightcolor="black") self.style.configure('TNotebook.Tab', background=_bgcolor) self.style.configure('TNotebook.Tab', foreground=_fgcolor) self.style.map('TNotebook.Tab', background=[('selected', _compcolor), ('active', _ana2color)]) self.TNotebook1 = ttk.Notebook(top) self.TNotebook1.place(relx=0.019, rely=0.1, relheight=0.887, relwidth=0.967) self.TNotebook1.configure(width=1002) self.TNotebook1.configure(takefocus="") self.TNotebook1_t0 = Frame(self.TNotebook1) self.TNotebook1.add(self.TNotebook1_t0, padding=3) self.TNotebook1.tab( 0, text="Graph", compound="left", underline="-1", ) self.TNotebook1_t1 = Frame(self.TNotebook1) self.TNotebook1.add(self.TNotebook1_t1, padding=3) self.TNotebook1.tab( 1, text="Recap", compound="left", underline="-1", ) ## grafik kanvas ## f = Figure(figsize=(8,4), dpi=100) ## a = f.add_subplot(111) ####t = arange(0.0, 3.0, 0.01) ####s = sin(2*pi*t) #### ####a.plot(t, s) ## a.plot([1,2,3,4,5,6,7,8],[60,65,85,90,92,95,94,90]) ## a.set_title('BPM') ## a.set_xlabel('X axis label') ## a.set_ylabel('Y label') # data ## global waktu ## dir = open('path.txt', 'r', os.O_NONBLOCK).read() ## data_dir = open(dir, 'r', os.O_NONBLOCK).read() data_dir = open( '/home/pi/Downloads/page/logs/log_22-12-2018 23:32:22.txt', 'r', os.O_NONBLOCK).read() lines = data_dir.split('\n') batas = len(lines) - 2 waktu = [] bpm = [] step = [] spd = [] dist = [] kal = [] mn = [] mx = [] spdm = [] for line in lines: ## for line in lines: ## (timeString, hr, steps, speed, jarak, cal, min, max, speed_mode) if len(line) >= 1: a, b, c, d, e, f, g, h, i = line.split(',') waktu.append(str(a)) bpm.append(str(b)) step.append(str(c)) spd.append(str(d)) dist.append(str(e)) kal.append(str(f)) mn.append(str(g)) mx.append(str(h)) spdm.append(str(i)) ## print (bpm, len(lines)) numbering = [] selector = [] max_cal = max(kal) max_bpm = max(bpm) ## print (bpm, kal) for min in range(0, len(bpm), 5): selector.append(int(bpm[min])) numbering.append(len(selector)) average = int(sum(selector) / len(selector)) ## maxhr = max(selector) # a tk.DrawingArea currentDT = datetime.datetime.now() time_stamp = currentDT.strftime("%d-%m-%Y %H:%M:%S") file_path = '/home/pi/Downloads/page/recap/log_' + time_stamp + '.txt' fp = open(file_path, 'a', os.O_NONBLOCK) data = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" % ( waktu[batas], step[batas], spd[batas], dist[batas], max_cal, mn[0], mx[0], spdm[0], max_bpm, average) result = firebase.post( '/data', { 'mode': spdm[0], 'time': waktu[batas], 'timestamp': time_stamp, 'step': step[batas], 'speed': spd[batas], 'distance': dist[batas], 'calories': max_cal, 'THRmin': mn[0], 'THRmax': mx[0], 'maximum_bpm': max_bpm, 'average': average }) print(result) fp.write(data) ## print (data) fp.close self.Canvas1 = Canvas(self.TNotebook1_t0) self.Canvas1.place(relx=0.08, rely=0.118, relheight=0.865, relwidth=0.821) self.Canvas1.configure(borderwidth="2") self.Canvas1.configure(relief=RIDGE) self.Canvas1.configure(selectbackground="#c4c4c4") self.Canvas1.configure(width=821) self.f = Figure(figsize=(8, 4), dpi=100) self.a = self.f.add_subplot(111) self.a.plot(numbering, selector) self.a.set_title('BPM') self.a.set_xlabel('time') self.a.set_ylabel('Heart Rate (Bpm)') self.Canvas1 = FigureCanvasTkAgg(self.f, self.Canvas1) self.Canvas1.draw() self.Canvas1.get_tk_widget().pack() self.Canvas1._tkcanvas.pack(fill=BOTH, expand=1) self.Label1 = Label(self.TNotebook1_t0) self.Label1.place(relx=0.08, rely=0.059, height=17, width=84) self.Label1.configure(activebackground="#f9f9f9") self.Label1.configure(text='''Heart Rate''') self.Label2 = Label(self.TNotebook1_t0) self.Label2.place(relx=0.21, rely=0.059, height=17, width=34) self.Label2.configure(activebackground="#f9f9f9") self.Label2.configure(text='''Max :''') self.max_hr = Label(self.TNotebook1_t0) self.max_hr.place(relx=0.27, rely=0.059, height=17, width=78) self.max_hr.configure(activebackground="#f9f9f9") self.max_hr.configure(text=max_bpm) self.Label4 = Label(self.TNotebook1_t0) self.Label4.place(relx=0.4, rely=0.059, height=17, width=58) self.Label4.configure(activebackground="#f9f9f9") self.Label4.configure(text='''Average :''') self.avg_hr = Label(self.TNotebook1_t0) self.avg_hr.place(relx=0.48, rely=0.059, height=17, width=108) self.avg_hr.configure(activebackground="#f9f9f9") self.avg_hr.configure(text=average) self.Labelframe1 = LabelFrame(self.TNotebook1_t1) self.Labelframe1.place(relx=0.02, rely=0.02, relheight=0.951, relwidth=0.96) self.Labelframe1.configure(relief=GROOVE) self.Labelframe1.configure(text='''Labelframe''') self.Labelframe1.configure(width=960) self.Labelframe2 = LabelFrame(self.Labelframe1) self.Labelframe2.place(relx=0.229, rely=0.247, relheight=0.299, relwidth=0.156, bordermode='ignore') self.Labelframe2.configure(relief=GROOVE) self.Labelframe2.configure(text='''Average Heart Rate''') self.Labelframe2.configure(width=150) self.avg_heart = Label(self.Labelframe2) self.avg_heart.place(relx=0.133, rely=0.207, height=37, width=114, bordermode='ignore') self.avg_heart.configure(activebackground="#f9f9f9") self.avg_heart.configure(text=average) self.Label5 = Label(self.Labelframe2) self.Label5.place(relx=0.133, rely=0.552, height=37, width=114, bordermode='ignore') self.Label5.configure(activebackground="#f9f9f9") self.Label5.configure(text='''BPM''') self.Labelframe2 = LabelFrame(self.Labelframe1) self.Labelframe2.place(relx=0.417, rely=0.247, relheight=0.299, relwidth=0.156, bordermode='ignore') self.Labelframe2.configure(relief=GROOVE) self.Labelframe2.configure(text='''Total Steps''') self.Labelframe2.configure(width=150) self.step_val = Label(self.Labelframe2) self.step_val.place(relx=0.133, rely=0.207, height=37, width=114, bordermode='ignore') self.step_val.configure(activebackground="#f9f9f9") self.step_val.configure(text=step[batas]) self.Label5 = Label(self.Labelframe2) self.Label5.place(relx=0.133, rely=0.552, height=37, width=114, bordermode='ignore') self.Label5.configure(activebackground="#f9f9f9") self.Label5.configure(text='''Steps''') self.Labelframe2 = LabelFrame(self.Labelframe1) self.Labelframe2.place(relx=0.604, rely=0.247, relheight=0.299, relwidth=0.156, bordermode='ignore') self.Labelframe2.configure(relief=GROOVE) self.Labelframe2.configure(text='''Burned Calories''') self.Labelframe2.configure(width=150) self.cal_val = Label(self.Labelframe2) self.cal_val.place(relx=0.133, rely=0.207, height=37, width=114, bordermode='ignore') self.cal_val.configure(activebackground="#f9f9f9") self.cal_val.configure(text=max_cal) self.Label5 = Label(self.Labelframe2) self.Label5.place(relx=0.133, rely=0.552, height=37, width=114, bordermode='ignore') self.Label5.configure(activebackground="#f9f9f9") self.Label5.configure(text='''Cal''') self.Labelframe2 = LabelFrame(self.Labelframe1) self.Labelframe2.place(relx=0.229, rely=0.557, relheight=0.299, relwidth=0.156, bordermode='ignore') self.Labelframe2.configure(relief=GROOVE) self.Labelframe2.configure(text='''Maximum Heart Rate''') self.Labelframe2.configure(width=150) self.max_heart_val = Label(self.Labelframe2) self.max_heart_val.place(relx=0.133, rely=0.207, height=37, width=114, bordermode='ignore') self.max_heart_val.configure(activebackground="#f9f9f9") self.max_heart_val.configure(text=max_bpm) self.Label5 = Label(self.Labelframe2) self.Label5.place(relx=0.133, rely=0.552, height=37, width=114, bordermode='ignore') self.Label5.configure(activebackground="#f9f9f9") self.Label5.configure(text='''BPM''') self.Labelframe2 = LabelFrame(self.Labelframe1) self.Labelframe2.place(relx=0.417, rely=0.557, relheight=0.299, relwidth=0.156, bordermode='ignore') self.Labelframe2.configure(relief=GROOVE) self.Labelframe2.configure(text='''Treadmill Mode''') self.Labelframe2.configure(width=150) self.step_per_min_val = Label(self.Labelframe2) self.step_per_min_val.place(relx=0.133, rely=0.207, height=37, width=114, bordermode='ignore') self.step_per_min_val.configure(activebackground="#f9f9f9") self.step_per_min_val.configure(text=spdm[0]) self.Label5 = Label(self.Labelframe2) self.Label5.place(relx=0.133, rely=0.552, height=37, width=114, bordermode='ignore') self.Label5.configure(activebackground="#f9f9f9") self.Label5.configure(text='''Steps''') self.Labelframe2 = LabelFrame(self.Labelframe1) self.Labelframe2.place(relx=0.604, rely=0.557, relheight=0.299, relwidth=0.156, bordermode='ignore') self.Labelframe2.configure(relief=GROOVE) self.Labelframe2.configure(text='''Distance''') self.Labelframe2.configure(width=150) self.dist_val = Label(self.Labelframe2) self.dist_val.place(relx=0.133, rely=0.207, height=37, width=114, bordermode='ignore') self.dist_val.configure(activebackground="#f9f9f9") self.dist_val.configure(text=dist[batas]) self.Label5 = Label(self.Labelframe2) self.Label5.place(relx=0.133, rely=0.552, height=37, width=114, bordermode='ignore') self.Label5.configure(activebackground="#f9f9f9") self.Label5.configure(text='''Km''') self.Labelframe2 = LabelFrame(self.Labelframe1) self.Labelframe2.place(relx=0.052, rely=0.392, relheight=0.299, relwidth=0.156, bordermode='ignore') self.Labelframe2.configure(relief=GROOVE) self.Labelframe2.configure(text='''Speed''') self.Labelframe2.configure(width=150) self.speed_val = Label(self.Labelframe2) self.speed_val.place(relx=0.133, rely=0.207, height=37, width=114, bordermode='ignore') self.speed_val.configure(activebackground="#f9f9f9") self.speed_val.configure(text=spd[batas]) self.Label5 = Label(self.Labelframe2) self.Label5.place(relx=0.133, rely=0.552, height=37, width=114, bordermode='ignore') self.Label5.configure(activebackground="#f9f9f9") self.Label5.configure(text='''Km/hr''') self.Labelframe2 = LabelFrame(self.Labelframe1) self.Labelframe2.place(relx=0.781, rely=0.392, relheight=0.299, relwidth=0.156, bordermode='ignore') self.Labelframe2.configure(relief=GROOVE) self.Labelframe2.configure(text='''Time''') self.Labelframe2.configure(width=150) self.time_val = Label(self.Labelframe2) self.time_val.place(relx=0.067, rely=0.345, height=37, width=114, bordermode='ignore') self.time_val.configure(activebackground="#f9f9f9") self.time_val.configure(text=waktu[batas]) self.back = Button(top) self.back.place(relx=0.019, rely=0.017, height=35, width=62) self.back.configure(activebackground="#d9d9d9") self.back.configure(command=recap_support.destroy_window) self.back.configure(text='''Back''') self.back.configure(width=62)