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.channels = channels
self.stop = False
def read_analog_continously(
self,
*args,
@ -67,7 +69,7 @@ class DaqProducer:
while daq_status != uldaq.ScanStatus.IDLE:
prev_count = 0
prev_index = 0
while time.time() - start_time < 10:
while not self.stop:
daq_status, transfer_status = self.ai_device.get_scan_status()
# The index into the data buffer immediately following the last sample transferred.
current_index = transfer_status.current_index
@ -154,17 +156,20 @@ class DaqProducer:
break
return "Done. "
if __name__ == "__main__":
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
try:
daq_device = uldaq.DaqDevice(devices[0])
except uldaq.ul_exception.ULException as e:
log.error("Did not found daq devices, please connect one")
raise e
daq_device.connect()
buf = CircBuffer(size=1_000_000, samplerate=100)
producer = DaqProducer(buf, daq_device, [1, 1])
producer.read_analog_continously()
def stop_aquisition(self):
self.stop = True
# if __name__ == "__main__":
# devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
# log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
# try:
# daq_device = uldaq.DaqDevice(devices[0])
# except uldaq.ul_exception.ULException as e:
# log.error("Did not found daq devices, please connect one")
# raise e
# daq_device.connect()
#
# 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(
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.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):
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.triggered.connect(self.stop_recording)
self._refresh = QAction("Refresh", self)
self._refresh.triggered.connect(self.refresh_plot)
self._refresh = QAction("Recenter", self)
self._refresh.triggered.connect(self.recenter_continously_plot)
self._refresh.setShortcut(QKeySequence("Alt+r"))
self.create_menu()
@ -180,7 +189,7 @@ class PyRelacs(QMainWindow):
daq_toolbar.addAction(self._run_action)
daq_toolbar.addAction(self._run_sinus_action)
daq_toolbar.addAction(self._stop_recording)
daq_toolbar.addAction(self._refresh)
# daq_toolbar.addAction(self._refresh)
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, daq_toolbar)
repro_toolbar = QToolBar("Repros")
@ -194,29 +203,24 @@ class PyRelacs(QMainWindow):
repro_toolbar.addAction(repro_action)
self.addToolBar(Qt.ToolBarArea.BottomToolBarArea, repro_toolbar)
def refresh_plot(self):
def recenter_continously_plot(self):
self.continously_plot.refresh()
def plot_continously(self):
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(plot_daq)
plot_con = Worker(self.continously_plot.plot)
plot_con.signals.result.connect(self.print_output)
plot_con.signals.finished.connect(self.thread_complete)
plot_con.signals.progress.connect(self.progress_fn)
self.threadpool.start(plot_con)
def run_daq(self):
read_daq = Worker(self.daq_producer.read_analog_continously)
read_daq.signals.result.connect(self.print_output)
read_daq.signals.finished.connect(self.thread_complete)
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)
time.sleep(0.5)
self.continously_plot.plot_daq()
# self.threadpool.start(plot_daq)
self.continously_plot.plot()
def run_sinus(self):
sinus_pro = Worker(self.sin_producer.produce_sin)
@ -225,19 +229,15 @@ class PyRelacs(QMainWindow):
sinus_pro.signals.progress.connect(self.progress_fn)
self.threadpool.start(sinus_pro)
# plot_daq = Worker(self.continously_plot.plot_daq, self.timer)
# 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()
self.continously_plot.plot()
def stop_recording(self):
self.add_to_textfield("pressed")
# self._stop_recording.setEnabled(False)
self.sin_producer.stop_request()
self.add_to_textfield("Stopping the recording")
if hasattr(PyRelacs, "sin_producer"):
self.sin_producer.stop_request()
self.continously_plot.stop_plotting()
if hasattr(uldaq, "daq_device"):
self.daq_producer.stop_aquisition()
def connect_dac(self):
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
@ -250,13 +250,6 @@ class PyRelacs(QMainWindow):
log.error("DAQ is not connected")
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):
try:
self.daq_device.disconnect()

View File

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