Compare commits

...

3 Commits

3 changed files with 87 additions and 44 deletions

View File

@ -18,8 +18,7 @@ class SinProducer:
buffer: CircBuffer, buffer: CircBuffer,
) -> None: ) -> None:
self.buffer = buffer self.buffer = buffer
self.stop = False
# self.stopbutton = stopbutton
def produce_sin( def produce_sin(
self, self,
@ -32,20 +31,27 @@ class SinProducer:
log.debug("producing Sin") log.debug("producing Sin")
start_time = time.time() start_time = time.time()
t = 0 t = 0
while time.time() - start_time < 20: while not self.stop:
s = AMPLITUDE * np.sin(2 * np.pi * FREQUENCY * t) s = AMPLITUDE * np.sin(2 * np.pi * FREQUENCY * t)
self.buffer.append(s) self.buffer.append(s)
t += 1 / self.buffer.samplerate t += 1 / self.buffer.samplerate
time.sleep(1 / self.buffer.samplerate) time.sleep(1 / self.buffer.samplerate)
end_time = time.time()
data = self.buffer.get_all() data = self.buffer.get_all()
log.debug(data.shape[0]) log.debug(f"duration sinus {end_time-start_time}")
log.debug(data.shape[0] / self.buffer.samplerate) log.debug(f"Stimulation time {t}")
# log.debug(data.shape[0])
# log.debug(data.shape[0] / self.buffer.samplerate)
# plt.plot(np.arange(data.size) / self.buffer.samplerate, data) # plt.plot(np.arange(data.size) / self.buffer.samplerate, data)
# plt.show() # plt.show()
def stop_request(self):
self.stop = True
if __name__ == "__main__": if __name__ == "__main__":
buf = CircBuffer(1_000_000, 1, samplerate=10000) buf = CircBuffer(1_000_000, 1, samplerate=10_000)
pro_sin = SinProducer(buf) pro_sin = SinProducer(buf)
pro_sin.produce_sin() pro_sin.produce_sin()

View File

@ -78,14 +78,15 @@ class PyRelacs(QMainWindow):
widget.setLayout(layout) widget.setLayout(layout)
self.setCentralWidget(widget) self.setCentralWidget(widget)
SAMPLERATE = 1000 SAMPLERATE = 1_000
BUFFERSIZE = 1_000 BUFFERSIZE = 1_0000
self.buffer = CircBuffer(size=BUFFERSIZE, samplerate=SAMPLERATE) self.buffer = CircBuffer(size=BUFFERSIZE, samplerate=SAMPLERATE)
# self.connect_dac() # self.connect_dac()
# self.daq_producer = DaqProducer(self.buffer, self.daq_device, [1, 1]) # self.daq_producer = DaqProducer(self.buffer, self.daq_device, [1, 1])
self.sin_producer = SinProducer(self.buffer) self.sin_producer = SinProducer(self.buffer)
self.continously_plot = Continously(self.figure, self.buffer) self.continously_plot = Continously(self.figure, self.buffer)
self.continously_plot.plot_daq()
def create_actions(self): def create_actions(self):
self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self) self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self)
@ -125,7 +126,7 @@ class PyRelacs(QMainWindow):
self._run_sinus_action = QAction(QIcon(":/icons/record.png"), "Sinus", self) self._run_sinus_action = QAction(QIcon(":/icons/record.png"), "Sinus", self)
self._run_sinus_action.triggered.connect(self.run_sinus) self._run_sinus_action.triggered.connect(self.run_sinus)
self._stop_recording = QAction("Stop", self) self._stop_recording = QAction(QIcon(":/icons/stop.png"), "Stop", self)
self._stop_recording.triggered.connect(self.stop_recording) self._stop_recording.triggered.connect(self.stop_recording)
self.create_menu() self.create_menu()
@ -211,12 +212,19 @@ class PyRelacs(QMainWindow):
sinus_pro.signals.progress.connect(self.progress_fn) sinus_pro.signals.progress.connect(self.progress_fn)
self.threadpool.start(sinus_pro) self.threadpool.start(sinus_pro)
# time.sleep(0.05) # plot_daq = Worker(self.continously_plot.plot_daq)
self.continously_plot.plot_daq() # plot_daq.signals.result.connect(self.print_output)
# plot_daq.signals.finished.connect(self.thread_complete)
# plot_daq.signals.progress.connect(self.progress_fn)
# self.threadpool.start(plot_daq)
# self.continously_plot.plot_daq()
def stop_recording(self): def stop_recording(self):
self.add_to_textfield("pressed") self.add_to_textfield("pressed")
self._stop_recording.setEnabled(False) self._stop_recording.setEnabled(False)
self.sin_producer.stop_request()
self.continously_plot.stop_plotting()
def connect_dac(self): def connect_dac(self):
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB) devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
@ -257,6 +265,7 @@ class PyRelacs(QMainWindow):
def on_exit(self): def on_exit(self):
log.info("exit button!") log.info("exit button!")
self.stop_recording()
self.add_to_textfield("exiting") self.add_to_textfield("exiting")
self.disconnect_dac() self.disconnect_dac()
self.close() self.close()

View File

@ -19,49 +19,77 @@ class Continously:
self.figure.setBackground("w") self.figure.setBackground("w")
self.daq_plot = self.figure.addPlot(row=0, col=0) self.daq_plot = self.figure.addPlot(row=0, col=0)
pen = pg.mkPen("red") pen = pg.mkPen("red")
self.time = np.zeros(self.buffer.size) # self.time = np.zeros(self.buffer.size)
self.data = np.zeros(self.buffer.size) # self.data = np.zeros(self.buffer.size)
log.debug(self.data.size)
log.debug(self.time.size)
self.line = self.daq_plot.plot( self.line = self.daq_plot.plot(
self.time, # self.time,
self.data, [0],
pen=pen, pen=pen,
setCliptoView=True, setCliptoView=True,
symbol="o", symbol="o",
) )
while self.buffer.totalcount() < 4000: # self.item = 0
# while self.buffer.totalcount() < 3_000_000:
# log.debug(self.buffer.totalcount())
# self.time = np.roll(self.time, -1)
# self.data = np.roll(self.data, -1)
# try:
# item, time_index = self.buffer.get(self.item)
# except IndexError:
# item = 0
# time_index = 0
# self.time[-1] = time_index
# self.data[-1] = item
# self.line.setData(self.time, self.data)
# self.item += 1
self.plot_index = 0
self.CHUNK_PLOT = 1000
self.timer = QTimer()
self.timer.setInterval(200)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
# self.update_plot()
def update_plot(self):
log.debug(self.buffer.totalcount())
if self.buffer.totalcount() > self.CHUNK_PLOT:
log.debug(self.buffer.totalcount()) log.debug(self.buffer.totalcount())
self.time = np.roll(self.time, -1)
self.data = np.roll(self.data, -1)
try: try:
item, time_index = self.buffer.get(self.buffer.write_index() - 1) # item, time_index = self.buffer.get(self.buffer.write_index() - 1)
items = self.buffer.read(
self.buffer.write_index() - self.CHUNK_PLOT, count=self.CHUNK_PLOT
)
# times = (np.arange(len(items)) / self.buffer.samplerate) + self.time[-1]
except IndexError: except IndexError:
item = 0 item = 0
items = np.zeros(self.CHUNK_PLOT)
times = np.zeros(self.CHUNK_PLOT)
# log.debug("Stopping the timer")
# self.timer.stop()
time_index = 0 time_index = 0
self.time[-1] = time_index log.debug("No Data Available")
self.data[-1] = item # self.time = np.roll(self.time, -len(items))
self.line.setData(self.time, self.data) # self.data = np.roll(self.data, -len(items))
#
# # self.time[-len(times) :] = times
# self.data[-len(items) :] = items
# self.timer = QTimer() self.line.setData(
# self.timer.setInterval(1) # self.time,
# self.timer.timeout.connect(self.update_plot) items,
# self.timer.start() )
self.plot_index += len(items)
else:
# self.timer.stop()
# embed()
# exit()
pass
# self.update_plot() def stop_plotting(self):
# self.timer.stop()
# def update_plot(self):
# log.debug(self.buffer.totalcount())
# while self.buffer.totalcount() < 4000:
# log.debug(self.buffer.totalcount())
# self.time = np.roll(self.time, -1)
# self.data = np.roll(self.data, -1)
# try:
# item, time_index = self.buffer.get(self.buffer.write_index() - 1)
# except IndexError:
# item = 0
# time_index = 0
# self.time[-1] = time_index
# self.data[-1] = item
# self.line.setData(self.time, self.data)