import tkinter as tk from tkinter import * from tkinter import ttk import time # For sleep, clock, time and perf_counter from datetime import datetime import numpy as np import matplotlib.pyplot as plt from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk) import ue9qol class SinGen: def __init__(self, ampl=1, offset=0, phase=0, sweeper=None): self.ampl = ampl self.phase = phase self.sweeper = sweeper self.offset = offset def getValue(self, sweeper=None): if sweeper is None and self.sweeper is None: print("Error: generator needs sweeper") return 0 if sweeper is None: sweeper = self.sweeper return self.ampl * np.sin( 2*np.pi*sweeper.getRelPos()) + self.offset class Sweeper: def __init__(self, widget, start, stop, Npoints, SweepTime, onTicCallbacks=[]): # walk from start to stop with Npoints # cnt = 1 corresponds to start # cnt = Npoints corresponds to stop # variables like relVar are relative to the start of the period self.cnt = 0 # onTic will increase it right away self.widget = widget self.start = start self.stop = stop self.Npoints = Npoints self.SweepTime = SweepTime self.onTicCallbacks = onTicCallbacks self.isOn = False self.isRestart = True self.isTicRunning = False self.span = self.stop - self.start self.center = (self.stop + self.start)/2 self.dPos = self.span/(self.Npoints-1) self.dT = self.SweepTime/(self.Npoints-1) self.dTmS = round(self.dT*1000) # dT in microSeconds def reset(self): self.cnt = 0 # onTic will increase it right away self.isRestart = False self.startTime = datetime.now() def onTic(self): if not self.isOn: self.isTicRunning = False return self.widget.after(self.dTmS, self.onTic) if self.isTicRunning: print("Overflow in Sweeper: need more time to finish a tic.") return self.isTicRunning = True if self.isRestart: self.reset() self.isRestart = False self.incr() for cb in self.onTicCallbacks: cb(self) self.isTicRunning = False def cmdRestart(self): self.cnt = 0 if self.isOn: self.isRestart = True return self.reset() self.isOn = True self.onTic() def cmdStart(self): self.isOn = True self.onTic() def cmdStop(self): self.isOn = False def incr(self): self.cnt += 1 self.updPos() def updPos(self): self.relCnt = 1 + ((self.cnt-1) % self.Npoints) self.pos = self.start + self.dPos * (self.relCnt - 1) self.relPos = (self.pos-self.start)/self.span def getCnt(self): return self.cnt def getRelCnt(self): return self.relCnt def getPos(self): return self.pos def getRelPos(self): return self.relPos class Experiment: def __init__(self, root): self.root = root self.tic = 0 self.data = {} self.data['tic'] = [] self.data['ch1'] = [] self.guiSetup(root) self.hardware = {} self.hardwareSetup() self.sweeper = Sweeper(self.root, 1, 10, 100, 10, onTicCallbacks=[self.onTic]) self.funcGen = SinGen(1, 0, sweeper = self.sweeper) # self.root.after(1000, self.hello ) def hardwareSetup(self): self.hardware['LabJack'] = ue9qol.UE9qol() def guiSetup(self, root): self.bRestart=Button(root,text="RESTART",command=self.restart,font=('Arial','24')) self.bRestart.pack() self.bStart=Button(root,text="START",command=self.start,font=('Arial','24')) self.bStart.pack() self.bStop=Button(root,text="STOP",command=self.stop,font=('Arial','24')) self.bStop.pack() self.bExit=Button(root,text="EXIT",command=exit,font=('Arial','24')) self.bExit.pack() self.fig=plt.figure() self.ax = self.fig.add_subplot(1,1,1) # self.ax.set_xlim([0,20]) # self.ax.set_ylim([0,20]) self.ax.plot([i for i in range(10)],[i for i in range(10)]) self.canvas = FigureCanvasTkAgg(self.fig, master = root) self.canvas.draw() # placing the canvas on the Tkinter window self.canvas.get_tk_widget().pack() # creating the Matplotlib toolbar self.toolbar = NavigationToolbar2Tk(self.canvas, root) self.toolbar.update() # placing the toolbar on the Tkinter window self.canvas.get_tk_widget().pack() def stop(self): self.sweeper.cmdStop() def start(self): self.sweeper.cmdStart() def restart(self): self.sweeper.cmdRestart() def onTic(self,swp=None): if swp is None: swp = self.sweeper # self.root.after(1000, self.hello ) # self.sweeper.incr() # tic = self.sweeper.getCnt() x = swp.getRelPos() y = self.funcGen.getValue(swp) # y = self.sweeper.getPos() # self.hardware['LabJack'].setOutputCh(0, x/2) self.hardware['LabJack'].setOutputCh(0, 2) self.data['tic'].append(x) # y= self.hardware['LabJack'].getInputCh(1) # self.data['ch1'].append( self.hardware['LabJack'].getInputCh(1) ) self.data['ch1'].append( y ) start = datetime.now() self.ax.cla() self.ax.plot(self.data['tic'], self.data['ch1']) self.canvas.draw() stop = datetime.now() runTime = (stop-start).seconds + float((stop-start).microseconds)/1000000 print("Replot took %s seconds to plot %s points." % (runTime, len(self.data['ch1'])) ) if __name__ == '__main__': root=Tk() experiment=Experiment(root) root.mainloop()