aboutsummaryrefslogtreecommitdiff
path: root/funcGenerator.py
diff options
context:
space:
mode:
Diffstat (limited to 'funcGenerator.py')
-rw-r--r--funcGenerator.py158
1 files changed, 158 insertions, 0 deletions
diff --git a/funcGenerator.py b/funcGenerator.py
new file mode 100644
index 0000000..2bdb4dd
--- /dev/null
+++ b/funcGenerator.py
@@ -0,0 +1,158 @@
+import time # For sleep, clock, time and perf_counter
+from datetime import datetime, timedelta
+import numpy as np
+
+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 RampGen:
+ # monotonically goes from start to stop, at final point move to start
+ def __init__(self, start=0, stop=0, sweeper=None):
+ self.start = start
+ self.stop = stop
+ self.sweeper = sweeper
+
+ 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.start + sweeper.getRelPos()*(self.stop - self.start)
+
+class TriangleGen:
+ # monotonically goes from start to stop, once reaches stop goes back to start
+ def __init__(self, start=0, stop=0, sweeper=None):
+ self.start = start
+ self.stop = stop
+ self.sweeper = sweeper
+
+ 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
+ if sweeper.getRelPos() < 0.5:
+ return self.start + 2*sweeper.getRelPos()*(self.stop - self.start)
+ return self.start + 2*(1-sweeper.getRelPos())*(self.stop - self.start)
+
+class PulseGen:
+ # monotonically goes from start to stop, once reaches stop goes back to start
+ def __init__(self, ampl=1, sweeper=None):
+ self.ampl = ampl
+ self.sweeper = sweeper
+
+ 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
+ if sweeper.getRelPos() < 0.5:
+ return self.ampl
+ return 0
+
+class Sweeper:
+ def __init__(self, widget, 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.Npoints = Npoints
+ self.start = 1
+ self.stop = self.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 milliseconds
+
+ def reset(self):
+ self.cnt = 0 # onTic will increase it right away
+ self.isRestart = False
+ self.startTime = datetime.now()
+
+ def onTic(self):
+ start = datetime.now()
+ deadline = start + timedelta(milliseconds=self.dTmS)
+
+ if not self.isOn:
+ self.isTicRunning = False
+ return
+ self.isTicRunning = True
+ if self.isRestart:
+ self.reset()
+ self.isRestart = False
+ self.incr()
+ for cb in self.onTicCallbacks:
+ cb(self)
+ stop = datetime.now()
+ 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
+ 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
+