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

View file

@ -10,7 +10,6 @@ logger = logging.getLogger(__name__)
class BroadbandView(ModuleView): class BroadbandView(ModuleView):
start_broadband_measurement = pyqtSignal() start_broadband_measurement = pyqtSignal()
def __init__(self, module): def __init__(self, module):
@ -21,18 +20,21 @@ class BroadbandView(ModuleView):
self._ui_form.setupUi(self) self._ui_form.setupUi(self)
self.widget = widget 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.connect_signals()
self.init_plots() self.init_plots()
self._ui_form.scrollAreaWidgetContents.setLayout(QVBoxLayout()) 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: 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( self._ui_form.start_frequencyField.editingFinished.connect(
lambda: self.module.controller.change_start_frequency( lambda: self.module.controller.change_start_frequency(
self._ui_form.start_frequencyField.text() 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.stop_frequency_changed.connect(self.on_stop_frequency_change)
self.module.model.frequency_step_changed.connect(self.on_frequency_step_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._ui_form.start_measurementButton.clicked.connect(self.start_measurement_clicked)
self.start_broadband_measurement.connect(self.module._controller.start_broadband_measurement) 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._ui_form.averagesEdit.editingFinished.connect(
self.module.controller.set_frequency_step_failure.connect(self.on_set_frequency_step_failure) 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 # LUT data
self.module.model.LUT_changed.connect(self.on_LUT_changed) self.module.model.LUT_changed.connect(self.on_LUT_changed)
@ -78,80 +95,80 @@ class BroadbandView(ModuleView):
# Create a QMessageBox object # Create a QMessageBox object
msg_box = QMessageBox(parent=self) msg_box = QMessageBox(parent=self)
msg_box.setText("Start the measurement?") 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 # Set the default button to No
msg_box.setDefaultButton(QMessageBox.StandardButton.No) msg_box.setDefaultButton(QMessageBox.StandardButton.No)
# Show the dialog and capture the user's choice # Show the dialog and capture the user's choice
choice = msg_box.exec() choice = msg_box.exec()
# Process the user's choice # Process the user's choice
if choice == QMessageBox.StandardButton.Yes: if choice == QMessageBox.StandardButton.Yes:
self.start_broadband_measurement.emit() self.start_broadband_measurement.emit()
def init_plots(self) -> None: def init_plots(self) -> None:
"""Initialize the plots. """Initialize the plots."""
"""
# Initialization of broadband spectrum # Initialization of broadband spectrum
self._ui_form.broadbandPlot.canvas.ax.set_xlim([0, 250]) self._ui_form.broadbandPlot.canvas.ax.set_xlim([0, 250])
self.set_broadband_labels() self.set_broadband_labels()
# Initialization of last measurement time domain # Initialization of last measurement time domain
self._ui_form.time_domainPlot.canvas.ax.set_xlim([0, 250]) self._ui_form.time_domainPlot.canvas.ax.set_xlim([0, 250])
self.set_timedomain_labels() self.set_timedomain_labels()
# Initialization of last measurement frequency domain # Initialization of last measurement frequency domain
self._ui_form.frequency_domainPlot.canvas.ax.set_xlim([0, 250]) self._ui_form.frequency_domainPlot.canvas.ax.set_xlim([0, 250])
self.set_frequencydomain_labels() self.set_frequencydomain_labels()
def set_timedomain_labels(self) -> None: 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_title("Last Time Domain")
self._ui_form.time_domainPlot.canvas.ax.set_xlabel("time in us") 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.set_ylabel("Amplitude a.u.")
self._ui_form.time_domainPlot.canvas.ax.grid() self._ui_form.time_domainPlot.canvas.ax.grid()
def set_frequencydomain_labels(self) -> None: 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_title("Last Frequency Domain")
self._ui_form.frequency_domainPlot.canvas.ax.set_xlabel("Frequency in MHz") 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.set_ylabel("Amplitude a.u.")
self._ui_form.frequency_domainPlot.canvas.ax.grid() self._ui_form.frequency_domainPlot.canvas.ax.grid()
def set_broadband_labels(self) -> None: 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_title("Broadband Spectrum")
self._ui_form.broadbandPlot.canvas.ax.set_xlabel("Frequency in MHz") 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.set_ylabel("Amplitude a.u.")
self._ui_form.broadbandPlot.canvas.ax.grid() self._ui_form.broadbandPlot.canvas.ax.grid()
@pyqtSlot(float) @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. """This method is called when the start frequency is changed.
It adjusts the view to the new start frequency. It adjusts the view to the new start frequency.
""" """
logger.debug("Adjusting view to new start frequency: " + str(start_frequency * 1e-6)) logger.debug(
self._ui_form.broadbandPlot.canvas.ax.set_xlim(left=start_frequency*1e-6) "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.draw()
self._ui_form.broadbandPlot.canvas.flush_events() 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) @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. """This method is called when the stop frequency is changed.
It adjusts the view to the new stop frequency. It adjusts the view to the new stop frequency.
""" """
logger.debug("Adjusting view to new stop frequency: " + str(stop_frequency * 1e-6)) logger.debug(
self._ui_form.broadbandPlot.canvas.ax.set_xlim(right=stop_frequency*1e-6) "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.draw()
self._ui_form.broadbandPlot.canvas.flush_events() 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) @pyqtSlot(float)
def on_frequency_step_change(self, frequency_step : float) -> None: def on_frequency_step_change(self, frequency_step : float) -> None:
@ -167,7 +184,7 @@ class BroadbandView(ModuleView):
self._ui_form.frequencystepEdit.setText(frequency_step) self._ui_form.frequencystepEdit.setText(frequency_step)
@pyqtSlot() @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. """This method is called when the user finished editing a field.
It sets the value of the field in the model. 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 # Get last measurement from the broadband measurement object that is not None
logger.debug("Updating broadband plot.") 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 td_plotter = self._ui_form.time_domainPlot.canvas.ax
fd_plotter = self._ui_form.frequency_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) td_plotter.plot(measurement.tdx, measurement.tdy)
fd_plotter.plot(measurement.fdx * 1e-6, measurement.fdy * 1e-6) 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_timedomain_labels()
self.set_frequencydomain_labels() self.set_frequencydomain_labels()
@ -221,7 +243,9 @@ class BroadbandView(ModuleView):
self._ui_form.frequency_domainPlot.canvas.draw() self._ui_form.frequency_domainPlot.canvas.draw()
self._ui_form.broadbandPlot.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)) logger.debug("Updating progress bar to: " + str(value))
self._ui_form.measurementProgress.setValue(value) self._ui_form.measurementProgress.setValue(value)
self._ui_form.measurementProgress.update() self._ui_form.measurementProgress.update()
@ -246,7 +270,7 @@ class BroadbandView(ModuleView):
def add_info_text(self, text : str) -> None: def add_info_text(self, text : str) -> None:
"""Add a text to the info box with a timestamp. """Add a text to the info box with a timestamp.
Args: Args:
text (str): The text to add to the info box. text (str): The text to add to the info box.
""" """
@ -255,4 +279,3 @@ class BroadbandView(ModuleView):
text_label = QLabel(text) text_label = QLabel(text)
text_label.setStyleSheet("font-size: 25px;") text_label.setStyleSheet("font-size: 25px;")
self._ui_form.scrollAreaWidgetContents.layout().addWidget(text_label) self._ui_form.scrollAreaWidgetContents.layout().addWidget(text_label)