Skip to content

Commit a4bf414

Browse files
Merge pull request #11 from Daniel-Buckelew/victoria/galvo
Galvo waveform added to asi.daq file
2 parents 7aef3a6 + 558c8ae commit a4bf414

File tree

3 files changed

+383
-1
lines changed

3 files changed

+383
-1
lines changed

src/navigate/model/devices/APIs/asi/asi_tiger_controller.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,4 +1055,5 @@ def SAM(self, axis: str, mode: int):
10551055
"""
10561056

10571057
self.send_command(f"SAM {axis}={mode}")
1058-
self.read_response()
1058+
self.read_response()
1059+

src/navigate/model/devices/daq/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,4 @@ def enable_microscope(self, microscope_name: str) -> None:
168168
self.sample_rate = self.configuration["configuration"]["microscopes"][
169169
microscope_name
170170
]["daq"]["sample_rate"]
171+
Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
# Copyright (c) 2021-2024 The University of Texas Southwestern Medical Center.
2+
# All rights reserved.
3+
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted for academic and research use only (subject to the
6+
# limitations in the disclaimer below) provided that the following conditions are met:
7+
8+
# * Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
11+
# * Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
15+
# * Neither the name of the copyright holders nor the names of its
16+
# contributors may be used to endorse or promote products derived from this
17+
# software without specific prior written permission.
18+
19+
# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
20+
# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21+
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23+
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27+
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
# POSSIBILITY OF SUCH DAMAGE.
31+
#
32+
33+
# Standard Library Imports
34+
import logging
35+
import time
36+
from typing import Any, Dict
37+
38+
39+
# Local Imports
40+
from navigate.model.devices.galvo.base import GalvoBase
41+
from navigate.model.devices.device_types import SerialDevice
42+
from navigate.model.devices.APIs.asi.asi_tiger_controller import TigerController
43+
from navigate.tools.decorators import log_initialization
44+
45+
# # Logger Setup
46+
p = __name__.split(".")[1]
47+
logger = logging.getLogger(p)
48+
49+
50+
@log_initialization
51+
class ASIGalvo(GalvoBase , SerialDevice):
52+
"""GalvoASI Class - ASI DAQ Control of Galvanometers"""
53+
54+
def __init__(
55+
self,
56+
microscope_name: str,
57+
device_connection: Any,
58+
configuration: Dict[str, Any],
59+
device_id: int = 0,
60+
) -> None:
61+
"""Initialize the GalvoNI class.
62+
63+
Parameters
64+
----------
65+
microscope_name : str
66+
Name of the microscope.
67+
device_connection : Any
68+
Connection to the NI DAQ device.
69+
configuration : Dict[str, Any]
70+
Dictionary of configuration parameters.
71+
device_id : int
72+
Galvo ID. Default is 0.
73+
"""
74+
super().__init__(microscope_name, device_connection, configuration, device_id)
75+
76+
#: Any: Device connection.
77+
self.galvo = device_connection
78+
79+
#: dict: Dictionary of microscope configuration parameters.
80+
self.configuration = configuration
81+
82+
#: str: Name of the microscope.
83+
self.microscope_name = microscope_name
84+
85+
#: int: Galvo ID.
86+
self.galvo_id = device_id
87+
88+
#: str: Name of the NI port for galvo control.
89+
self.trigger_source = configuration["configuration"]["microscopes"][
90+
microscope_name
91+
]["daq"]["trigger_source"]
92+
93+
#: str: Name of the galvo.
94+
self.galvo_name = "Galvo " + str(device_id)
95+
96+
#: dict: Dictionary of device connections.
97+
self.device_config = configuration["configuration"]["microscopes"][
98+
microscope_name
99+
]["galvo"][device_id]
100+
101+
#: int: Sample rate.
102+
self.sample_rate = configuration["configuration"]["microscopes"][
103+
microscope_name
104+
]["daq"]["sample_rate"]
105+
106+
#: float: Sweep time.
107+
self.sweep_time = 0
108+
109+
#: float: Camera delay
110+
self.camera_delay = (
111+
configuration["configuration"]["microscopes"][microscope_name]["camera"][
112+
"delay"
113+
]
114+
/ 1000
115+
)
116+
117+
#: float: Galvo max voltage.
118+
self.galvo_max_voltage = self.device_config["hardware"]["max"]
119+
120+
#: float: Galvo min voltage.
121+
self.galvo_min_voltage = self.device_config["hardware"]["min"]
122+
123+
# Galvo Waveform Information
124+
#: str: Galvo waveform. Waveform or Sawtooth.
125+
self.galvo_waveform = self.device_config.get("waveform", "sawtooth")
126+
127+
self.axis = self.device_config["hardware"].get("axis","B")
128+
129+
def __str__(self) -> str:
130+
"""Return string representation of the GalvoASI."""
131+
return "GalvoASI"
132+
133+
@classmethod
134+
def connect(cls, port, baudrate=115200, timeout=0.25):
135+
"""Build ASILaser Serial Port connection
136+
137+
Parameters
138+
----------
139+
port : str
140+
Port for communicating with the filter wheel, e.g., COM1.
141+
baudrate : int
142+
Baud rate for communicating with the filter wheel, default is 115200.
143+
timeout : float
144+
Timeout for communicating with the filter wheel, default is 0.25.
145+
146+
Returns
147+
-------
148+
tiger_controller : TigerController
149+
ASI Tiger Controller object.
150+
"""
151+
# wait until ASI device is ready
152+
tiger_controller = TigerController(port, baudrate)
153+
tiger_controller.connect_to_serial()
154+
if not tiger_controller.is_open():
155+
logger.error("ASI stage connection failed.")
156+
raise Exception("ASI stage connection failed.")
157+
return tiger_controller
158+
159+
def adjust(self, exposure_times, sweep_times):
160+
"""Adjust the galvo waveform to account for the camera readout time.
161+
162+
Parameters
163+
----------
164+
exposure_times : dict
165+
Dictionary of camera exposure time in seconds on a per-channel basis.
166+
e.g., exposure_times = {"channel_1": 0.1, "channel_2": 0.2}
167+
sweep_times : dict
168+
Dictionary of acquisition sweep time in seconds on a per-channel basis.
169+
e.g., sweep_times = {"channel_1": 0.1, "channel_2": 0.2}
170+
171+
Returns
172+
-------
173+
waveform_dict : dict
174+
Dictionary that includes the galvo waveforms on a per-channel basis.
175+
"""
176+
microscope_state = self.configuration["experiment"]["MicroscopeState"]
177+
microscope_name = microscope_state["microscope_name"]
178+
zoom_value = microscope_state["zoom"]
179+
galvo_factor = self.configuration["waveform_constants"]["other_constants"].get(
180+
"galvo_factor", "none"
181+
)
182+
galvo_parameters = self.configuration["waveform_constants"]["galvo_constants"][
183+
self.galvo_name
184+
][microscope_name][zoom_value]
185+
self.sample_rate = self.configuration["configuration"]["microscopes"][
186+
self.microscope_name
187+
]["daq"]["sample_rate"]
188+
189+
for channel_key in microscope_state["channels"].keys():
190+
# channel includes 'is_selected', 'laser', 'filter', 'camera_exposure'...
191+
channel = microscope_state["channels"][channel_key]
192+
193+
# Only proceed if it is enabled in the GUI
194+
if channel["is_selected"] is True:
195+
196+
# Get the Waveform Parameters - Assumes ETL Delay < Camera Delay.
197+
# Should Assert.
198+
exposure_time = exposure_times[channel_key]
199+
self.sweep_time = sweep_times[channel_key]
200+
201+
# galvo Parameters
202+
try:
203+
galvo_amplitude = float(galvo_parameters.get("amplitude", 0))
204+
galvo_offset = float(galvo_parameters.get("offset", 0))
205+
galvo_frequency = (
206+
float(galvo_parameters.get("frequency", 0)) / exposure_time
207+
)
208+
factor_name = None
209+
if galvo_factor == "channel":
210+
factor_name = (
211+
f"Channel {channel_key[channel_key.index('_')+1:]}"
212+
)
213+
elif galvo_factor == "laser":
214+
factor_name = channel["laser"]
215+
if factor_name and factor_name in galvo_parameters.keys():
216+
galvo_amplitude = float(
217+
galvo_parameters[factor_name].get("amplitude", 0)
218+
)
219+
galvo_offset = float(
220+
galvo_parameters[factor_name].get("offset", 0)
221+
)
222+
223+
except ValueError as e:
224+
logger.debug(
225+
f"{e} waveform constants.yml doesn't have parameter "
226+
f"amplitude/offset/frequency for {self.galvo_name}"
227+
)
228+
return
229+
230+
# Calculate the Waveforms
231+
if self.galvo_waveform == "sawtooth":
232+
frequency=galvo_frequency
233+
amplitude=galvo_amplitude
234+
offset=galvo_offset
235+
236+
self.sawtooth(frequency, amplitude, offset)
237+
238+
elif self.galvo_waveform == "sine":
239+
frequency=galvo_frequency
240+
amplitude=galvo_amplitude
241+
offset=galvo_offset
242+
243+
self.sine_wave(frequency, amplitude, offset)
244+
245+
elif self.galvo_waveform == "halfsaw":
246+
frequency=galvo_frequency
247+
amplitude=galvo_amplitude
248+
offset=galvo_offset
249+
250+
self.half_saw(frequency, amplitude, offset)
251+
else:
252+
print("Unknown Galvo waveform specified in configuration file.")
253+
continue
254+
255+
def sawtooth(
256+
self,
257+
frequency=10,
258+
amplitude=1,
259+
offset=0,
260+
):
261+
"""
262+
Sends the tiger controller commands to make the sawtooth wave
263+
264+
Parameters
265+
----------
266+
frequency : Float
267+
Unit - Hz
268+
amplitude : Float
269+
Unit - Volts
270+
offset : Float
271+
Unit - Volts
272+
"""
273+
274+
period = (1 / frequency)*1000
275+
amplitude *= 1000
276+
offset *= 1000
277+
278+
self.galvo.SA_waveform(self.axis, 128, amplitude, offset, period)
279+
self.galvo.SAM(self.axis, 4)
280+
281+
# need to adjust it so it only runs for the duration of the sweep time
282+
# do we want to do anything with duty cycle or phase, or accept that as a limitation
283+
284+
def sine_wave(
285+
self,
286+
frequency=10,
287+
amplitude=1,
288+
offset=0
289+
):
290+
"""Returns a numpy array with a sine waveform
291+
292+
Used for creating analog laser drive voltage.
293+
294+
Parameters
295+
----------
296+
sample_rate : int, optional
297+
Unit - Hz, by default 100000
298+
sweep_time : float, optional
299+
Unit - Seconds, by default 0.4
300+
frequency : int, optional
301+
Unit - Hz, by default 10
302+
amplitude : float, optional
303+
Unit - Volts, by default 1
304+
offset : float, optional
305+
Unit - Volts, by default 0
306+
phase : float, optional
307+
Unit - Radians, by default 0
308+
309+
Returns
310+
-------
311+
waveform : np.array
312+
313+
Examples
314+
--------
315+
>>> typical_laser = sine_wave(sample_rate, sweep_time, 10, 1, 0, 0)
316+
317+
"""
318+
period = (1 / frequency)*1000
319+
amplitude *= 1000
320+
offset *= 1000
321+
322+
self.galvo.SA_waveform(self.axis, 131, amplitude, offset, period)
323+
self.galvo.SAM(self.axis, 4)
324+
325+
# need to adjust it so it only runs for the duration of the sweep time
326+
# do we want to do anything with phase, or accept that as a limitation
327+
328+
def half_saw(
329+
self,
330+
frequency=10,
331+
amplitude=1,
332+
offset=0,
333+
):
334+
"""Sends the tiger controller commands to make the ramp wave
335+
336+
Parameters
337+
----------
338+
exposure_time : Float
339+
Unit - Seconds
340+
sweep_time : Float
341+
Unit - Seconds
342+
remote_focus_delay : Float
343+
Unit - seconds
344+
camera_delay : Float
345+
Unit - seconds
346+
fall : Float
347+
Unit - seconds
348+
amplitude : Float
349+
Unit - Volts
350+
offset : Float
351+
Unit - Volts
352+
"""
353+
354+
# rise period
355+
period = (1 / frequency)*1000
356+
357+
amplitude *= 1000/2
358+
offset *= 1000
359+
360+
self.galvo.SA_waveform(self.axis, 128, amplitude, offset, period)
361+
time.sleep(1/frequency)
362+
self.galvo.SAM(self.axis, 2)
363+
364+
def turn_off(self):
365+
"""Stops the galvo waveform"""
366+
self.galvo.SAM(self.axis, 0)
367+
368+
def close(self):
369+
"""Close the ASI galvo serial port.
370+
371+
Stops the remote focus waveform and then closes the port.
372+
"""
373+
if self.galvo.is_open():
374+
self.turn_off()
375+
logger.debug("ASI Remote Focus - Closing Device.")
376+
self.galvo.disconnect_from_serial()
377+
378+
def __del__(self):
379+
"""Destructor for the ASIGalvo class."""
380+
self.close()

0 commit comments

Comments
 (0)