Source code for phobos.classes.filter_wheel
# External imports
import time
import os
# Internal imports
from .. import serial
from .config import Config
from ..utils import Singleton
[docs]
class FilterWheel(metaclass=Singleton):
[docs]
def __init__(self):
"""
Singleton Class to control the Thorlabs filter wheel. The wheel has 6 positions:
- 1: ND?
- 2: ND?
- 3: ND?
- 4: ND?
- 5: ND?
- 6: ND?
"""
config = Config()
try:
port = config.get('filter_wheel.port')
self.session = serial.Serial(port, 115200, timeout=0.1)
print(f"Filter Wheel connected on port {port}")
self._connected = True
except Exception as e:
if not os.environ.get("PHOBOS_SANDBOX"):
print(f"⚠️ FilterWheel connection failed: {e}")
self._connected = False
[docs]
def reset(self):
"""
Reset the filter wheel to the default position defined in config.
"""
selected_filter = Config().get('filter_wheel.selected_filter', 1)
self.move(selected_filter)
def _purge(self):
"""
Purge all the history of the responses of the filter wheel.
"""
# Reading the lines actually flush the info after the request
dummy = self.session.readlines()
[docs]
def close(self):
"""
Close the serial connection.
"""
self.session.close()
[docs]
def get(self):
"""
Get the current info from the filter wheel.
Returns
-------
response : str
Status of the wheel.
"""
self._purge() # flush
self.session.write("pos?\r".encode())
response = self.session.readline().decode()
return response
[docs]
def get_pos(self):
"""
Returns the current position of the filter wheel.
Returns
-------
slot : int
Current position number of the wheel.
"""
time.sleep(0.1)
resp = self.get()
slot = int(resp[5])
return slot
[docs]
def move(self, slot:int):
"""
Move the filter wheel to the specified position.
Parameters
----------
slot : int
Position number of the wheel to reach.
"""
print('FILT - Move to position '+str(slot))
self.session.write(("pos="+str(slot)+"\r").encode())
self.wait()
[docs]
def wait(self) -> None:
"""
Wait for the motor to reach the target position.
"""
position = ''
while len(position) == 0:
position = self.get()
time.sleep(0.1)