Skip to content

Commit

Permalink
adding a 1D instrument mocking a spectrometer
Browse files Browse the repository at this point in the history
  • Loading branch information
seb5g committed Sep 8, 2024
1 parent fef08c1 commit a48c5ed
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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__)
19 changes: 9 additions & 10 deletions src/pymodaq_plugins_teaching/hardware/arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a48c5ed

Please sign in to comment.