Skip to content

Commit

Permalink
refactor simulated mcls service in line with non-simulated mcls service
Browse files Browse the repository at this point in the history
  • Loading branch information
steigersg committed Aug 29, 2024
1 parent ae6a8ce commit 2bdb471
Showing 1 changed file with 25 additions and 39 deletions.
64 changes: 25 additions & 39 deletions catkit2/services/thorlabs_mcls1_sim/thorlabs_mcls1_sim.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import time
import queue
import numpy as np
import threading

from catkit2.testbed.service import Service


def make_monitor_func(stream, setter):
def func(self):
while not self.should_shut_down:
try:
frame = stream.get_next_frame(1)
except Exception:
continue

setter(frame.data[0])

return func


class ThorlabsMcls1Sim(Service):

def __init__(self):
super().__init__('thorlabs_mcls1_sim')
self.threads = {}
self.communication_queue = queue.Queue()

def open(self):
# Make datastreams
Expand All @@ -29,20 +40,14 @@ def open(self):
'target_temperature': self.set_target_temperature,
}

self.status_funcs = {
'temperature': (self.temperature, self.get_temperature),
'power': (self.power, self.get_power)
}

funcs = {
'emission': self.monitor_func(self.emission, self.setters['emission']),
'current_setpoint': self.monitor_func(self.current_setpoint, self.setters['current_setpoint']),
'target_temperature': self.monitor_func(self.target_temperature, self.setters['target_temperature']),
}
self.getters = [
self.get_temperature,
self.get_power
]

# Start all threads.
for key, func in funcs.items():
thread = threading.Thread(target=func)
for key, setter in self.setters.items():
func = make_monitor_func(getattr(self, key), setter)
thread = threading.Thread(target=func, args=(self,))
thread.start()

self.threads[key] = thread
Expand All @@ -59,32 +64,16 @@ def open(self):

def main(self):
while not self.should_shut_down:
try:
task, args = self.communication_queue.get(timeout=1)
task(*args)
self.communication_queue.task_done()
except queue.Empty:
pass
self.sleep(1)

def close(self):
# Turn off the source
self.setters['emission'](self, 0)
self.set_emission(0)

# Join all threads.
for thread in self.threads.values():
thread.join()

def monitor_func(self, stream, setter):
def func():
while not self.should_shut_down:
try:
frame = stream.get_next_frame(1)
except Exception:
continue
self.communication_queue.put((setter, [frame.data[0]]))

return func

def update_status_func(self, getter, stream):
def func():
result = getter()
Expand All @@ -93,8 +82,8 @@ def func():

def update_status(self):
while not self.should_shut_down:
for stream, getter in self.status_funcs.values():
self.communication_queue.put((self.update_status_func(getter, stream), []))
for getter in self.getters:
getter()

time.sleep(1)

Expand All @@ -117,10 +106,7 @@ def set_target_temperature(self, value):
pass

def get_power(self):
try:
return self.testbed.simulator.light_source_data[self.id + '_power']
except KeyError:
return 0
return self.testbed.simulator.light_source_data[self.id + '_power']

def get_temperature(self):
return self.config['target_temperature']
Expand Down

0 comments on commit 2bdb471

Please sign in to comment.