Compare commits

...

3 Commits

3 changed files with 55 additions and 57 deletions

View File

@ -22,6 +22,8 @@ class DaqProducer:
self.ai_device = self.device.get_ai_device() self.ai_device = self.device.get_ai_device()
self.channels = channels self.channels = channels
self.stop = False
def read_analog_continously( def read_analog_continously(
self, self,
*args, *args,
@ -67,7 +69,7 @@ class DaqProducer:
while daq_status != uldaq.ScanStatus.IDLE: while daq_status != uldaq.ScanStatus.IDLE:
prev_count = 0 prev_count = 0
prev_index = 0 prev_index = 0
while time.time() - start_time < 10: while not self.stop:
daq_status, transfer_status = self.ai_device.get_scan_status() daq_status, transfer_status = self.ai_device.get_scan_status()
# The index into the data buffer immediately following the last sample transferred. # The index into the data buffer immediately following the last sample transferred.
current_index = transfer_status.current_index current_index = transfer_status.current_index
@ -154,17 +156,20 @@ class DaqProducer:
break break
return "Done. " return "Done. "
def stop_aquisition(self):
if __name__ == "__main__": self.stop = True
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
try: # if __name__ == "__main__":
daq_device = uldaq.DaqDevice(devices[0]) # devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
except uldaq.ul_exception.ULException as e: # log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
log.error("Did not found daq devices, please connect one") # try:
raise e # daq_device = uldaq.DaqDevice(devices[0])
daq_device.connect() # except uldaq.ul_exception.ULException as e:
# log.error("Did not found daq devices, please connect one")
buf = CircBuffer(size=1_000_000, samplerate=100) # raise e
producer = DaqProducer(buf, daq_device, [1, 1]) # daq_device.connect()
producer.read_analog_continously() #
# buf = CircBuffer(size=1_000_000, samplerate=100)
# producer = DaqProducer(buf, daq_device, [1, 1])
# producer.read_analog_continously()

View File

@ -87,11 +87,20 @@ class PyRelacs(QMainWindow):
self.buffer = CircBuffer( self.buffer = CircBuffer(
size=BUFFERSIZE, samplerate=SAMPLERATE, mutex=self.mutex size=BUFFERSIZE, samplerate=SAMPLERATE, mutex=self.mutex
) )
# self.connect_dac()
# self.daq_producer = DaqProducer(self.buffer, self.daq_device, [1, 1])
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()
start = time.time()
self.connect_dac()
end = time.time()
log.debug(f"Connection to DAQ took {end - start}")
if hasattr(uldaq, "daq_device"):
log.debug("Creating Daq Generator")
self.daq_producer = DaqProducer(self.buffer, self.daq_device, [1, 1])
else:
log.debug("Creating Sinus Generator")
self.sin_producer = SinProducer(self.buffer)
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)
@ -134,8 +143,8 @@ class PyRelacs(QMainWindow):
self._stop_recording = QAction(QIcon(":/icons/stop.png"), "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._refresh = QAction("Refresh", self) self._refresh = QAction("Recenter", self)
self._refresh.triggered.connect(self.refresh_plot) self._refresh.triggered.connect(self.recenter_continously_plot)
self._refresh.setShortcut(QKeySequence("Alt+r")) self._refresh.setShortcut(QKeySequence("Alt+r"))
self.create_menu() self.create_menu()
@ -180,7 +189,7 @@ class PyRelacs(QMainWindow):
daq_toolbar.addAction(self._run_action) daq_toolbar.addAction(self._run_action)
daq_toolbar.addAction(self._run_sinus_action) daq_toolbar.addAction(self._run_sinus_action)
daq_toolbar.addAction(self._stop_recording) daq_toolbar.addAction(self._stop_recording)
daq_toolbar.addAction(self._refresh) # daq_toolbar.addAction(self._refresh)
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, daq_toolbar) self.addToolBar(Qt.ToolBarArea.TopToolBarArea, daq_toolbar)
repro_toolbar = QToolBar("Repros") repro_toolbar = QToolBar("Repros")
@ -194,29 +203,24 @@ class PyRelacs(QMainWindow):
repro_toolbar.addAction(repro_action) repro_toolbar.addAction(repro_action)
self.addToolBar(Qt.ToolBarArea.BottomToolBarArea, repro_toolbar) self.addToolBar(Qt.ToolBarArea.BottomToolBarArea, repro_toolbar)
def refresh_plot(self): def recenter_continously_plot(self):
self.continously_plot.refresh() self.continously_plot.refresh()
def plot_continously(self): def plot_continously(self):
plot_daq = Worker(self.continously_plot.plot_daq) plot_con = Worker(self.continously_plot.plot)
plot_daq.signals.result.connect(self.print_output) plot_con.signals.result.connect(self.print_output)
plot_daq.signals.finished.connect(self.thread_complete) plot_con.signals.finished.connect(self.thread_complete)
plot_daq.signals.progress.connect(self.progress_fn) plot_con.signals.progress.connect(self.progress_fn)
self.threadpool.start(plot_daq) self.threadpool.start(plot_con)
def run_daq(self): def run_daq(self):
read_daq = Worker(self.daq_producer.read_analog_continously) read_daq = Worker(self.daq_producer.read_analog_continously)
read_daq.signals.result.connect(self.print_output) read_daq.signals.result.connect(self.print_output)
read_daq.signals.finished.connect(self.thread_complete) read_daq.signals.finished.connect(self.thread_complete)
read_daq.signals.progress.connect(self.progress_fn) read_daq.signals.progress.connect(self.progress_fn)
# plot_daq = Worker(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(read_daq) self.threadpool.start(read_daq)
time.sleep(0.5)
self.continously_plot.plot_daq() self.continously_plot.plot()
# self.threadpool.start(plot_daq)
def run_sinus(self): def run_sinus(self):
sinus_pro = Worker(self.sin_producer.produce_sin) sinus_pro = Worker(self.sin_producer.produce_sin)
@ -225,19 +229,15 @@ 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)
# plot_daq = Worker(self.continously_plot.plot_daq, self.timer) self.continously_plot.plot()
# 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("Stopping the recording")
# self._stop_recording.setEnabled(False) if hasattr(PyRelacs, "sin_producer"):
self.sin_producer.stop_request() self.sin_producer.stop_request()
self.continously_plot.stop_plotting() self.continously_plot.stop_plotting()
if hasattr(uldaq, "daq_device"):
self.daq_producer.stop_aquisition()
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)
@ -250,13 +250,6 @@ class PyRelacs(QMainWindow):
log.error("DAQ is not connected") log.error("DAQ is not connected")
log.error("Please connect a DAQ device to the system") log.error("Please connect a DAQ device to the system")
if hasattr(PyRelacs, "daq_device"):
try:
self.daq_device.connect()
log.debug("Connected")
except uldaq.ul_exception.ULException as e:
log.error(f"Could not Connect to DAQ: {e}")
def disconnect_dac(self): def disconnect_dac(self):
try: try:
self.daq_device.disconnect() self.daq_device.disconnect()

View File

@ -14,18 +14,18 @@ class Continously:
self.figure = figure self.figure = figure
self.buffer = buffer self.buffer = buffer
def plot_daq(self, *args, **kwargs): def plot(self, *args, **kwargs):
self.figure.setBackground("w") self.figure.setBackground("w")
prev_plot = self.figure.getItem(row=0, col=0) prev_plot = self.figure.getItem(row=0, col=0)
if prev_plot: if prev_plot:
self.figure.removeItem(prev_plot) self.figure.removeItem(prev_plot)
self.continous_plot = self.figure.addPlot(row=0, col=0) self.continous_ax = 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)
self.line = self.continous_plot.plot( self.line = self.continous_ax.plot(
self.time, self.time,
self.data, self.data,
pen=pen, pen=pen,
@ -40,7 +40,7 @@ class Continously:
self.timer.start() self.timer.start()
def update_plot(self): def update_plot(self):
log.debug(self.buffer.totalcount()) # log.debug(self.buffer.totalcount())
if self.buffer.totalcount() > self.CHUNK_PLOT: if self.buffer.totalcount() > self.CHUNK_PLOT:
log.debug(self.buffer.totalcount()) log.debug(self.buffer.totalcount())
try: try:
@ -72,4 +72,4 @@ class Continously:
self.timer.stop() self.timer.stop()
def refresh(self): def refresh(self):
self.continous_plot.enableAutoRange() self.continous_ax.enableAutoRange()