@@ -61,6 +61,7 @@ def __init__(
61
61
device_connection : Any ,
62
62
configuration : Dict [str , Any ],
63
63
laser_id : int ,
64
+ modulation_type = "digital" ,
64
65
) -> None :
65
66
"""Initialize the LaserNI class.
66
67
@@ -74,13 +75,74 @@ def __init__(
74
75
The device configuration.
75
76
laser_id : int
76
77
The laser id.
78
+ modulation_type : str
79
+ The modulation type of the laser - Analog, Digital, or Mixed.
77
80
"""
78
81
super ().__init__ (microscope_name , device_connection , configuration , laser_id )
79
82
83
+ #: str: The modulation type of the laser - Analog, Digital, or Mixed.
84
+ self .modulation_type = modulation_type
85
+
80
86
#: str: Modulation type of the laser - Analog or Digital.
81
- self .on_off_type = None
87
+ self .digital_port_type = None
88
+
89
+ #: float: The minimum digital modulation voltage.
90
+ self .laser_min_do = None
91
+
92
+ #: float: The maximum digital modulation voltage.
93
+ self .laser_max_do = None
94
+
95
+ #: nidaqmx.Task: The laser digital modulation task.
96
+ self .laser_do_task = None
97
+
98
+ #: float: The minimum analog modulation voltage.
99
+ self .laser_min_ao = None
100
+
101
+ #: float: The maximum analog modulation voltage.
102
+ self .laser_max_ao = None
103
+
104
+ #: nidaqmx.Task: The laser analog modulation task.
105
+ self .laser_ao_task = None
106
+
107
+ #: float: Current laser intensity.
108
+ self ._current_intensity = 0
109
+
110
+ # Initialize the laser modulation type.
111
+ if self .modulation_type == "mixed" :
112
+ self .initialize_digital_modulation ()
113
+ self .initialize_analog_modulation ()
114
+ logger .info (f"{ str (self )} initialized with mixed modulation." )
115
+
116
+ elif self .modulation_type == "analog" :
117
+ self .initialize_analog_modulation ()
118
+ logger .info (f"{ str (self )} initialized with analog modulation." )
119
+
120
+ elif self .modulation_type == "digital" :
121
+ self .initialize_digital_modulation ()
122
+ logger .info (f"{ str (self )} initialized with digital modulation." )
123
+
124
+ def initialize_analog_modulation (self ) -> None :
125
+ """Initialize the analog modulation of the laser."""
126
+ try :
127
+ laser_ao_port = self .device_config ["power" ]["hardware" ]["channel" ]
128
+
129
+ #: float: The minimum analog modulation voltage.
130
+ self .laser_min_ao = self .device_config ["power" ]["hardware" ]["min" ]
131
+
132
+ #: float: The maximum analog modulation voltage.
133
+ self .laser_max_ao = self .device_config ["power" ]["hardware" ]["max" ]
134
+
135
+ #: object: The laser analog modulation task.
136
+ self .laser_ao_task = nidaqmx .Task ()
137
+ self .laser_ao_task .ao_channels .add_ao_voltage_chan (
138
+ laser_ao_port , min_val = self .laser_min_ao , max_val = self .laser_max_ao
139
+ )
140
+ except DaqError as e :
141
+ logger .debug (f"{ str (self )} error:, { e } , { e .error_type } , { e .error_code } " )
142
+ print (f"{ str (self )} error:, { e } , { e .error_type } , { e .error_code } " )
82
143
83
- # Digital out (if using mixed modulation mode)
144
+ def initialize_digital_modulation (self ) -> None :
145
+ """Initialize the digital modulation of the laser."""
84
146
try :
85
147
laser_do_port = self .device_config ["onoff" ]["hardware" ]["channel" ]
86
148
@@ -94,18 +156,18 @@ def __init__(
94
156
self .laser_do_task = nidaqmx .Task ()
95
157
96
158
if "/ao" in laser_do_port :
97
- # Artificial Digital Modulation via an Analog Port
159
+ # Perform the digital modulation with an analog output port.
98
160
self .laser_do_task .ao_channels .add_ao_voltage_chan (
99
161
laser_do_port , min_val = self .laser_min_do , max_val = self .laser_max_do
100
162
)
101
- self .on_off_type = "analog"
163
+ self .digital_port_type = "analog"
102
164
103
165
else :
104
166
# Digital Modulation via a Digital Port
105
167
self .laser_do_task .do_channels .add_do_chan (
106
168
laser_do_port , line_grouping = LineGrouping .CHAN_FOR_ALL_LINES
107
169
)
108
- self .on_off_type = "digital"
170
+ self .digital_port_type = "digital"
109
171
except (KeyError , DaqError ) as e :
110
172
self .laser_do_task = None
111
173
if isinstance (e , DaqError ):
@@ -115,36 +177,16 @@ def __init__(
115
177
print (e .error_code )
116
178
print (e .error_type )
117
179
118
- #: float: Current laser intensity.
119
- self ._current_intensity = 0
120
-
121
- # Analog out
122
- try :
123
- laser_ao_port = self .device_config ["power" ]["hardware" ]["channel" ]
124
-
125
- #: float: The minimum analog modulation voltage.
126
- self .laser_min_ao = self .device_config ["power" ]["hardware" ]["min" ]
127
-
128
- #: float: The maximum analog modulation voltage.
129
- self .laser_max_ao = self .device_config ["power" ]["hardware" ]["max" ]
130
-
131
- #: object: The laser analog modulation task.
132
- self .laser_ao_task = nidaqmx .Task ()
133
- self .laser_ao_task .ao_channels .add_ao_voltage_chan (
134
- laser_ao_port , min_val = self .laser_min_ao , max_val = self .laser_max_ao
135
- )
136
- except DaqError as e :
137
- logger .debug (f"{ str (self )} error:, { e } , { e .error_type } , { e .error_code } " )
138
- print (f"{ str (self )} error:, { e } , { e .error_type } , { e .error_code } " )
139
-
140
180
def set_power (self , laser_intensity : float ) -> None :
141
- """Sets the laser power.
181
+ """Sets the analog laser power.
142
182
143
183
Parameters
144
184
----------
145
185
laser_intensity : float
146
186
The laser intensity.
147
187
"""
188
+ if self .laser_ao_task is None :
189
+ return
148
190
try :
149
191
scaled_laser_voltage = (int (laser_intensity ) / 100 ) * self .laser_max_ao
150
192
self .laser_ao_task .write (scaled_laser_voltage , auto_start = True )
@@ -154,34 +196,40 @@ def set_power(self, laser_intensity: float) -> None:
154
196
155
197
def turn_on (self ) -> None :
156
198
"""Turns on the laser."""
199
+ self .set_power (self ._current_intensity )
200
+
201
+ if self .laser_do_task is None :
202
+ return
157
203
try :
158
- self .set_power (self ._current_intensity )
159
- if self .laser_do_task is not None :
160
- if self .on_off_type == "digital" :
161
- self .laser_do_task .write (True , auto_start = True )
162
- elif self .on_off_type == "analog" :
163
- self .laser_do_task .write (self .laser_max_do , auto_start = True )
204
+ if self .digital_port_type == "digital" :
205
+ self .laser_do_task .write (True , auto_start = True )
206
+ elif self .digital_port_type == "analog" :
207
+ self .laser_do_task .write (self .laser_max_do , auto_start = True )
164
208
except DaqError as e :
165
209
logger .exception (e )
166
210
167
211
def turn_off (self ) -> None :
168
212
"""Turns off the laser."""
213
+ # set ao power to zero
214
+ tmp = self ._current_intensity
215
+ self .set_power (0 )
216
+ self ._current_intensity = tmp
217
+
218
+ if self .laser_do_task is None :
219
+ return
169
220
try :
170
- tmp = self ._current_intensity
171
- self .set_power (0 )
172
- self ._current_intensity = tmp
173
- if self .laser_do_task is not None :
174
- if self .on_off_type == "digital" :
175
- self .laser_do_task .write (False , auto_start = True )
176
- elif self .on_off_type == "analog" :
177
- self .laser_do_task .write (self .laser_min_do , auto_start = True )
221
+ if self .digital_port_type == "digital" :
222
+ self .laser_do_task .write (False , auto_start = True )
223
+ elif self .digital_port_type == "analog" :
224
+ self .laser_do_task .write (self .laser_min_do , auto_start = True )
178
225
except DaqError as e :
179
226
logger .exception (e )
180
227
181
228
def close (self ) -> None :
182
229
"""Close the NI Task before exit."""
183
230
try :
184
- self .laser_ao_task .close ()
231
+ if self .laser_ao_task is not None :
232
+ self .laser_ao_task .close ()
185
233
if self .laser_do_task is not None :
186
234
self .laser_do_task .close ()
187
235
except DaqError as e :
0 commit comments