This commit is contained in:
jupfi 2023-12-07 17:18:48 +01:00
commit 4c7feff05c
2 changed files with 97 additions and 62 deletions

View file

@ -7,8 +7,8 @@ from nqrduck.module.module_controller import ModuleController
logger = logging.getLogger(__name__)
class BroadbandController(ModuleController):
class BroadbandController(ModuleController):
start_broadband_measurement = pyqtSignal()
set_averages_failure = pyqtSignal()
set_frequency_step_failure = pyqtSignal()
@ -16,7 +16,6 @@ class BroadbandController(ModuleController):
def __init__(self, module):
super().__init__(module)
@pyqtSlot(str, object)
def process_signals(self, key: str, value: object) -> None:
"""Process incoming signal from the nqrduck module.
@ -31,7 +30,10 @@ class BroadbandController(ModuleController):
logger.debug("Received single measurement.")
self.module.model.current_broadband_measurement.add_measurement(value)
elif key == "failure_set_averages" and value == self.module.view._ui_form.averagesEdit.text():
elif (
key == "failure_set_averages"
and value == self.module.view._ui_form.averagesEdit.text()
):
logger.debug("Received set averages failure.")
self.set_averages_failure.emit()
# receive LUT data
@ -56,8 +58,8 @@ class BroadbandController(ModuleController):
self.change_frequency_step(self.module.model.LUT.frequency_step)
@pyqtSlot(str)
def set_frequency(self, value : str) -> None:
""" Emits the set frequency signal to the nqrduck module.
def set_frequency(self, value: str) -> None:
"""Emits the set frequency signal to the nqrduck module.
Args:
value (str): Frequency in MHz.
@ -70,7 +72,7 @@ class BroadbandController(ModuleController):
self.set_frequency_step_failure.emit()
@pyqtSlot(str)
def set_averages(self, value : str) -> None:
def set_averages(self, value: str) -> None:
"""Emits the set averages signal to the nqrduck module.
Args:
@ -80,7 +82,7 @@ class BroadbandController(ModuleController):
self.module.nqrduck_signal.emit("set_averages", value)
@pyqtSlot(str)
def change_start_frequency(self, value : str) -> None:
def change_start_frequency(self, value: str) -> None:
"""Changes the start frequency of the measurement."""
value = float(value)
if value > self.module.model.MIN_FREQUENCY:
@ -89,7 +91,7 @@ class BroadbandController(ModuleController):
self.module.model.start_frequency = self.module.model.MIN_FREQUENCY
@pyqtSlot(str)
def change_stop_frequency(self, value:str) -> None:
def change_stop_frequency(self, value: str) -> None:
"""Changes the stop frequency of the measurement."""
value = float(value)
if value < self.module.model.MAX_FREQUENCY:
@ -98,16 +100,15 @@ class BroadbandController(ModuleController):
self.module._model.stop_frequency = self.module.model.MAX_FREQUENCY
@pyqtSlot(str)
def change_frequency_step(self, value :str) -> None:
def change_frequency_step(self, value: str) -> None:
"""Changes the frequency step of the measurement."""
try:
value = float(value) * 1e6
if value > 0:
self.module.model.frequency_step = value
except ValueError:
except ValueError:
logger.debug("Invalid frequency step value")
@pyqtSlot()
def start_broadband_measurement(self) -> None:
"""Starts a broadband measurement."""
@ -117,19 +118,28 @@ class BroadbandController(ModuleController):
stop_frequency = self.module.model.stop_frequency
frequency_step = self.module.model.frequency_step
frequency_list = np.arange(start_frequency, stop_frequency + frequency_step, frequency_step)
frequency_list = np.arange(
start_frequency, stop_frequency + frequency_step, frequency_step
)
logger.debug("Frequency list: " + str(frequency_list))
# Create a new broadband measurement object
self.module.model.current_broadband_measurement = self.module.model.BroadbandMeasurement(frequency_list, self.module.model.frequency_step)
self.module.model.current_broadband_measurement.received_measurement.connect(self.module.view.on_broadband_measurement_added)
self.module.model.current_broadband_measurement.received_measurement.connect(self.on_broadband_measurement_added)
self.module.model.current_broadband_measurement = (
self.module.model.BroadbandMeasurement(
frequency_list, self.module.model.frequency_step
)
)
self.module.model.current_broadband_measurement.received_measurement.connect(
self.module.view.on_broadband_measurement_added
)
self.module.model.current_broadband_measurement.received_measurement.connect(
self.on_broadband_measurement_added
)
self.module.view.add_info_text("Starting broadband measurement.")
# Start the first measurement
self.start_single_measurement(start_frequency)
@pyqtSlot()
def on_broadband_measurement_added(self) -> None:
"""This slot is called when a single measurement is added to the broadband measurement.
@ -140,7 +150,9 @@ class BroadbandController(ModuleController):
# Check if there are more frequencies to measure
if not self.module.model.current_broadband_measurement.is_complete():
# Get the next frequency to measure
next_frequency = self.module.model.current_broadband_measurement.get_next_measurement_frequency()
next_frequency = (
self.module.model.current_broadband_measurement.get_next_measurement_frequency()
)
logger.debug("Next frequency: " + str(next_frequency))
self.start_single_measurement(next_frequency)
else:

View file

@ -10,7 +10,6 @@ logger = logging.getLogger(__name__)
class BroadbandView(ModuleView):
start_broadband_measurement = pyqtSignal()
def __init__(self, module):
@ -21,18 +20,21 @@ class BroadbandView(ModuleView):
self._ui_form.setupUi(self)
self.widget = widget
logger.debug("Facecolor %s" % str(self._ui_form.broadbandPlot.canvas.ax.get_facecolor()))
logger.debug(
"Facecolor %s" % str(self._ui_form.broadbandPlot.canvas.ax.get_facecolor())
)
self.connect_signals()
self.init_plots()
self._ui_form.scrollAreaWidgetContents.setLayout(QVBoxLayout())
self._ui_form.scrollAreaWidgetContents.layout().setAlignment(Qt.AlignmentFlag.AlignTop)
self._ui_form.scrollAreaWidgetContents.layout().setAlignment(
Qt.AlignmentFlag.AlignTop
)
def connect_signals(self) -> None:
"""Connect the signals of the view to the slots of the controller.
"""
"""Connect the signals of the view to the slots of the controller."""
self._ui_form.start_frequencyField.editingFinished.connect(
lambda: self.module.controller.change_start_frequency(
self._ui_form.start_frequencyField.text()
@ -50,17 +52,32 @@ class BroadbandView(ModuleView):
)
)
self.module.model.start_frequency_changed.connect(self.on_start_frequency_change)
self.module.model.start_frequency_changed.connect(
self.on_start_frequency_change
)
self.module.model.stop_frequency_changed.connect(self.on_stop_frequency_change)
self.module.model.frequency_step_changed.connect(self.on_frequency_step_change)
self._ui_form.start_measurementButton.clicked.connect(self.start_measurement_clicked)
self.start_broadband_measurement.connect(self.module._controller.start_broadband_measurement)
self._ui_form.averagesEdit.editingFinished.connect(lambda: self.on_editing_finished(self._ui_form.averagesEdit.text()))
self._ui_form.start_measurementButton.clicked.connect(
self.start_measurement_clicked
)
self.start_broadband_measurement.connect(
self.module._controller.start_broadband_measurement
)
self.module.controller.set_averages_failure.connect(self.on_set_averages_failure)
self.module.controller.set_frequency_step_failure.connect(self.on_set_frequency_step_failure)
self._ui_form.averagesEdit.editingFinished.connect(
lambda: self.on_editing_finished(self._ui_form.averagesEdit.text())
)
self.module.controller.set_averages_failure.connect(
self.on_set_averages_failure
)
self.module.controller.set_frequency_step_failure.connect(
self.on_set_frequency_step_failure
)
# LUT data
self.module.model.LUT_changed.connect(self.on_LUT_changed)
@ -78,7 +95,9 @@ class BroadbandView(ModuleView):
# Create a QMessageBox object
msg_box = QMessageBox(parent=self)
msg_box.setText("Start the measurement?")
msg_box.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
msg_box.setStandardButtons(
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
)
# Set the default button to No
msg_box.setDefaultButton(QMessageBox.StandardButton.No)
@ -91,8 +110,7 @@ class BroadbandView(ModuleView):
self.start_broadband_measurement.emit()
def init_plots(self) -> None:
"""Initialize the plots.
"""
"""Initialize the plots."""
# Initialization of broadband spectrum
self._ui_form.broadbandPlot.canvas.ax.set_xlim([0, 250])
self.set_broadband_labels()
@ -101,57 +119,56 @@ class BroadbandView(ModuleView):
self._ui_form.time_domainPlot.canvas.ax.set_xlim([0, 250])
self.set_timedomain_labels()
# Initialization of last measurement frequency domain
self._ui_form.frequency_domainPlot.canvas.ax.set_xlim([0, 250])
self.set_frequencydomain_labels()
def set_timedomain_labels(self) -> None:
"""Set the labels of the time domain plot.
"""
"""Set the labels of the time domain plot."""
self._ui_form.time_domainPlot.canvas.ax.set_title("Last Time Domain")
self._ui_form.time_domainPlot.canvas.ax.set_xlabel("time in us")
self._ui_form.time_domainPlot.canvas.ax.set_ylabel("Amplitude a.u.")
self._ui_form.time_domainPlot.canvas.ax.grid()
def set_frequencydomain_labels(self) -> None:
"""Set the labels of the frequency domain plot.
"""
"""Set the labels of the frequency domain plot."""
self._ui_form.frequency_domainPlot.canvas.ax.set_title("Last Frequency Domain")
self._ui_form.frequency_domainPlot.canvas.ax.set_xlabel("Frequency in MHz")
self._ui_form.frequency_domainPlot.canvas.ax.set_ylabel("Amplitude a.u.")
self._ui_form.frequency_domainPlot.canvas.ax.grid()
def set_broadband_labels(self) -> None:
"""Set the labels of the broadband plot.
"""
"""Set the labels of the broadband plot."""
self._ui_form.broadbandPlot.canvas.ax.set_title("Broadband Spectrum")
self._ui_form.broadbandPlot.canvas.ax.set_xlabel("Frequency in MHz")
self._ui_form.broadbandPlot.canvas.ax.set_ylabel("Amplitude a.u.")
self._ui_form.broadbandPlot.canvas.ax.grid()
@pyqtSlot(float)
def on_start_frequency_change(self, start_frequency : float) -> None:
def on_start_frequency_change(self, start_frequency: float) -> None:
"""This method is called when the start frequency is changed.
It adjusts the view to the new start frequency.
"""
logger.debug("Adjusting view to new start frequency: " + str(start_frequency * 1e-6))
self._ui_form.broadbandPlot.canvas.ax.set_xlim(left=start_frequency*1e-6)
logger.debug(
"Adjusting view to new start frequency: " + str(start_frequency * 1e-6)
)
self._ui_form.broadbandPlot.canvas.ax.set_xlim(left=start_frequency * 1e-6)
self._ui_form.broadbandPlot.canvas.draw()
self._ui_form.broadbandPlot.canvas.flush_events()
self._ui_form.start_frequencyField.setText(str(start_frequency* 1e-6))
self._ui_form.start_frequencyField.setText(str(start_frequency * 1e-6))
@pyqtSlot(float)
def on_stop_frequency_change(self, stop_frequency : float) -> None:
def on_stop_frequency_change(self, stop_frequency: float) -> None:
"""This method is called when the stop frequency is changed.
It adjusts the view to the new stop frequency.
"""
logger.debug("Adjusting view to new stop frequency: " + str(stop_frequency * 1e-6))
self._ui_form.broadbandPlot.canvas.ax.set_xlim(right=stop_frequency*1e-6)
logger.debug(
"Adjusting view to new stop frequency: " + str(stop_frequency * 1e-6)
)
self._ui_form.broadbandPlot.canvas.ax.set_xlim(right=stop_frequency * 1e-6)
self._ui_form.broadbandPlot.canvas.draw()
self._ui_form.broadbandPlot.canvas.flush_events()
self._ui_form.stop_frequencyField.setText(str(stop_frequency* 1e-6))
self._ui_form.stop_frequencyField.setText(str(stop_frequency * 1e-6))
@pyqtSlot(float)
def on_frequency_step_change(self, frequency_step : float) -> None:
@ -167,7 +184,7 @@ class BroadbandView(ModuleView):
self._ui_form.frequencystepEdit.setText(frequency_step)
@pyqtSlot()
def on_editing_finished(self, value : str) -> None:
def on_editing_finished(self, value: str) -> None:
"""This method is called when the user finished editing a field.
It sets the value of the field in the model.
"""
@ -199,7 +216,9 @@ class BroadbandView(ModuleView):
"""
# Get last measurement from the broadband measurement object that is not None
logger.debug("Updating broadband plot.")
measurement = self.module.model.current_broadband_measurement.get_last_completed_measurement()
measurement = (
self.module.model.current_broadband_measurement.get_last_completed_measurement()
)
td_plotter = self._ui_form.time_domainPlot.canvas.ax
fd_plotter = self._ui_form.frequency_domainPlot.canvas.ax
@ -211,7 +230,10 @@ class BroadbandView(ModuleView):
td_plotter.plot(measurement.tdx, measurement.tdy)
fd_plotter.plot(measurement.fdx * 1e-6, measurement.fdy * 1e-6)
broadband_plotter.plot(self.module.model.current_broadband_measurement.broadband_data_fdx, self.module.model.current_broadband_measurement.broadband_data_fdy)
broadband_plotter.plot(
self.module.model.current_broadband_measurement.broadband_data_fdx,
self.module.model.current_broadband_measurement.broadband_data_fdy,
)
self.set_timedomain_labels()
self.set_frequencydomain_labels()
@ -221,7 +243,9 @@ class BroadbandView(ModuleView):
self._ui_form.frequency_domainPlot.canvas.draw()
self._ui_form.broadbandPlot.canvas.draw()
value = int(self.module.model.current_broadband_measurement.get_finished_percentage())
value = int(
self.module.model.current_broadband_measurement.get_finished_percentage()
)
logger.debug("Updating progress bar to: " + str(value))
self._ui_form.measurementProgress.setValue(value)
self._ui_form.measurementProgress.update()
@ -255,4 +279,3 @@ class BroadbandView(ModuleView):
text_label = QLabel(text)
text_label.setStyleSheet("font-size: 25px;")
self._ui_form.scrollAreaWidgetContents.layout().addWidget(text_label)