aboutsummaryrefslogtreecommitdiff
path: root/panel.py
blob: c1b707a0a91806b3fed3a27937d5b9f22bb92117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)

import ue9qol

class Sweeper:
    def __init__(self, start, stop, Npoints, SweepTime):
        # 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.start = start
        self.stop = stop
        self.Npoints = Npoints
        self.SweepTime = SweepTime

        self.span = self.stop - self.start
        self.center = (self.stop + self.start)/2
        self.dPos = self.span/(self.Npoints-1)
        self.restart()

    def restart(self):
        self.cnt = 1
        self.startTime = datetime.now()
        self.updPos()

    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(1, 10, 10, 10)
        self.root.after(1000, self.hello )
    
    def hardwareSetup(self):
        self.hardware['LabJack'] = ue9qol.UE9qol()

    def guiSetup(self, root): 

        self.bStart=Button(root,text="START",command=self.hello,font=('Arial','24'))
        self.bStart.pack()

        self.bStop=Button(root,text="STOP",command=exit,font=('Arial','24'))
        self.bStop.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 hello(self):
        self.root.after(1000, self.hello )
        self.sweeper.incr()
        tic = self.sweeper.getCnt()
        x =   self.sweeper.getPos()
        # 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, tic) )

 
if __name__ == '__main__': 
    root=Tk()

    experiment=Experiment(root) 

    root.mainloop()