diff options
Diffstat (limited to 'panel.py')
-rw-r--r-- | panel.py | 44 |
1 files changed, 34 insertions, 10 deletions
@@ -2,8 +2,10 @@ import tkinter as tk from tkinter import * from tkinter import ttk +from threading import Thread + import time # For sleep, clock, time and perf_counter -from datetime import datetime +from datetime import datetime, timedelta import numpy as np import matplotlib.pyplot as plt @@ -88,26 +90,33 @@ class Sweeper: self.startTime = datetime.now() def onTic(self): + start = datetime.now() + deadline = start + timedelta(milliseconds=self.dTmS) + print(start) + print(self.dTmS) + print(deadline) + 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() - start = datetime.now() - print(start) for cb in self.onTicCallbacks: cb(self) stop = datetime.now() - runTime = (stop-start).seconds + float((stop-start).microseconds)/1000000 - print("Callbacks took %s seconds." % (runTime) ) self.isTicRunning = False + if stop > deadline: + runTime = (stop-start).seconds + float((stop-start).microseconds)/1000000 + print("Overrun: Callbacks took %s seconds instead of %s" % (runTime, self.dTmS/1000) ) + self.widget.after(0, self.onTic) + + idleTime_mS = round((deadline-stop).seconds * 1000 + (deadline-stop).microseconds/1000) + print("Will idle for %s" % (idleTime_mS) ) + self.widget.after(idleTime_mS, self.onTic) + def cmdRestart(self): self.cnt = 0 @@ -154,12 +163,15 @@ class Experiment: self.tic = 0 self.clearData() self.guiSetup(root) + self.guiSweeper = Sweeper(self.root, Npoints=2, SweepTime=1, onTicCallbacks=[self.updatePlot]) + self.guiSweeper.cmdStart() self.hardware = {} self.hardwareSetup() self.sweeper = Sweeper(self.root, Npoints=100, SweepTime=1, onTicCallbacks=[self.onTic]) # self.funcGen = SinGen(2, 2, sweeper = self.sweeper) # self.funcGen = RampGen(0, 5, sweeper = self.sweeper) self.funcGen = TriangleGen(0, 5, sweeper = self.sweeper) + def hardwareSetup(self): self.hardware['LabJack'] = ue9qol.UE9qol() @@ -189,6 +201,7 @@ class Experiment: # 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.line, = self.ax.plot(self.data['tic'], self.data['ch1'], '.') self.canvas = FigureCanvasTkAgg(self.fig, master = self.dataDisplay) self.canvas.draw() @@ -219,6 +232,7 @@ class Experiment: self.sweeper.cmdRestart() def onTic(self,swp=None): + start = datetime.now() if swp is None: swp = self.sweeper # self.root.after(1000, self.hello ) @@ -233,10 +247,20 @@ class Experiment: y= self.hardware['LabJack'].getInputCh(1) # self.data['ch1'].append( self.hardware['LabJack'].getInputCh(1) ) self.data['ch1'].append( y ) + stop = datetime.now() + runTime = (stop-start).seconds + float((stop-start).microseconds)/1000000 + # print("onTic DAQ took %s seconds." % (runTime) ) + + def updatePlot(self,swp=None): start = datetime.now() self.ax.cla() - self.ax.plot(self.data['tic'], self.data['ch1'], '.') + self.line, = self.ax.plot(self.data['tic'], self.data['ch1'], '.') + # t = Thread(target=self.canvas.draw) + # self.line.set_data([1, 2, 3], [1, 2, 3]) + # self.ax.draw_artist(self.line) + # self.canvas.update() self.canvas.draw() + # self.fig.canvas.flush_events() 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'])) ) |