From a48c5edc1120317e595d5c9b83beb6b06629c163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Weber?= Date: Sun, 8 Sep 2024 20:54:10 +0200 Subject: [PATCH] adding a 1D instrument mocking a spectrometer --- .../daq_move_plugins/daq_move_LEDServo.py | 3 +- .../plugins_1D/daq_1Dviewer_LEDSpectro.py | 100 ++++++++++++++++++ .../hardware/arduino.py | 19 ++-- 3 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 src/pymodaq_plugins_teaching/daq_viewer_plugins/plugins_1D/daq_1Dviewer_LEDSpectro.py diff --git a/src/pymodaq_plugins_teaching/daq_move_plugins/daq_move_LEDServo.py b/src/pymodaq_plugins_teaching/daq_move_plugins/daq_move_LEDServo.py index dbac3ad..b9f768a 100644 --- a/src/pymodaq_plugins_teaching/daq_move_plugins/daq_move_LEDServo.py +++ b/src/pymodaq_plugins_teaching/daq_move_plugins/daq_move_LEDServo.py @@ -48,7 +48,8 @@ def get_actuator_value(self): def close(self): """Terminate the communication protocol""" - self.controller.close() + if self.is_master: + self.controller.close() def commit_settings(self, param: Parameter): """Apply the consequences of a change of value in the detector settings diff --git a/src/pymodaq_plugins_teaching/daq_viewer_plugins/plugins_1D/daq_1Dviewer_LEDSpectro.py b/src/pymodaq_plugins_teaching/daq_viewer_plugins/plugins_1D/daq_1Dviewer_LEDSpectro.py new file mode 100644 index 0000000..267871a --- /dev/null +++ b/src/pymodaq_plugins_teaching/daq_viewer_plugins/plugins_1D/daq_1Dviewer_LEDSpectro.py @@ -0,0 +1,100 @@ +import numpy as np +from pymodaq.utils.daq_utils import ThreadCommand +from pymodaq.utils.data import DataFromPlugins, Axis, DataToExport +from pymodaq.control_modules.viewer_utility_classes import DAQ_Viewer_base, comon_parameters, main +from pymodaq.utils.parameter import Parameter +from pymodaq_plugins_teaching.hardware.arduino import Arduino + + +class DAQ_1DViewer_LEDSpectro(DAQ_Viewer_base): + """ Instrument plugin class for a 1D viewer. + + This object inherits all functionalities to communicate with PyMoDAQ’s DAQ_Viewer module through inheritance via + DAQ_Viewer_base. It makes a bridge between the DAQ_Viewer module and the Python wrapper of a particular instrument. + + + Attributes: + ----------- + controller: object + The particular object that allow the communication with the hardware, in general a python wrapper around the + hardware library. + + """ + params = comon_parameters+[ + ] + + def ini_attributes(self): + # autocompletion + self.controller: Arduino = None + self.x_axis = None + + def commit_settings(self, param: Parameter): + """Apply the consequences of a change of value in the detector settings + + Parameters + ---------- + param: Parameter + A given parameter (within detector_settings) whose value has been changed by the user + """ + pass + + def ini_detector(self, controller=None): + """Detector communication initialization + + Parameters + ---------- + controller: (object) + custom object of a PyMoDAQ plugin (Slave case). None if only one actuator/detector by controller + (Master case) + + Returns + ------- + info: str + initialized: bool + False if initialization failed otherwise True + """ + + self.ini_detector_init(old_controller=controller, + new_controller=None) + + if self.is_master: + self.controller = Arduino() + + info = "Whatever info you want to log" + initialized = True + return info, initialized + + def close(self): + """Terminate the communication protocol""" + if self.is_master: + self.controller.close() + + def grab_data(self, Naverage=1, **kwargs): + """Start a grab from the detector + + Parameters + ---------- + Naverage: int + Number of hardware averaging (if hardware averaging is possible, self.hardware_averaging should be set to + True in class preamble and you should code this implementation) + kwargs: dict + others optionals arguments + """ + + axis_array = self.controller.get_spectrometer_axis() + data_array = self.controller.generate_spectrum() + self.dte_signal.emit(DataToExport( + 'myplugin', + data=[DataFromPlugins(name='Mock1', data=[data_array], + dim='Data1D', labels=['Spectrum'], + units='counts', + axes=[Axis('Wavelength', units='m', data=axis_array * 1e-9)], + )])) + + def stop(self): + """Stop the current grab hardware wise if necessary""" + pass + + +if __name__ == '__main__': + main(__file__) diff --git a/src/pymodaq_plugins_teaching/hardware/arduino.py b/src/pymodaq_plugins_teaching/hardware/arduino.py index c155822..7ba6496 100644 --- a/src/pymodaq_plugins_teaching/hardware/arduino.py +++ b/src/pymodaq_plugins_teaching/hardware/arduino.py @@ -54,24 +54,23 @@ def servo_write(self, pin, value: int): value = self.round_value(value) self.pin_values_output[pin] = value - def generate_spectrum(self) -> DataRaw: + def get_spectrometer_axis(self) -> np.ndarray: + return np.linspace(400, 800, SIZE, endpoint=True) + + def generate_spectrum(self) -> np.ndarray: """ Grab a spectrum revealing the content of the RGB LED""" - axis = Axis('wavelength', units='m', data=np.linspace(400, 800, SIZE, endpoint=True) * 1e-9) + axis = self.get_spectrometer_axis() data_array = np.zeros((SIZE,)) if self.pin_values_output[self.servo_pin] > 70: - data_array += (gauss1D(axis.get_data(), LAMBDA_RED, 15) * + data_array += (gauss1D(axis, LAMBDA_RED, 15) * self.pin_values_output[self.led_pins['red']]) - data_array += (gauss1D(axis.get_data(), LAMBDA_GREEN, 12) * + data_array += (gauss1D(axis, LAMBDA_GREEN, 12) * self.pin_values_output[self.led_pins['green']]) - data_array += (gauss1D(axis.get_data(), LAMBDA_BLUE, 15) * + data_array += (gauss1D(axis, LAMBDA_BLUE, 15) * self.pin_values_output[self.led_pins['blue']]) - return DataRaw('Spectrum', - data=[data_array], - axes=[axis], - labels=['Spectrum'], - units='count') + return data_array def close(self): pass