Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/mcls1 sim #242

Merged
merged 13 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 29 additions & 44 deletions catkit2/services/thorlabs_mcls1_sim/thorlabs_mcls1_sim.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
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')
super().__init__('thorlabs_mcls1_sim')
self.threads = {}
self.communication_queue = queue.Queue()

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

self.status_funcs = {
'temperature': (self.temperature, lambda: self.config['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,44 +63,22 @@ def open(self):

def main(self):
while not self.should_shut_down:
try:
task, args = self.communication_queue.get(timeout=1)
task(self, *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(self):
result = getter(self)
stream.submit_data(np.array([result]).astype(stream.dtype))
return 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)
self.sleep(1)

def set_emission(self, emission):
self.testbed.simulator.set_source_power(
Expand All @@ -119,6 +101,9 @@ def set_target_temperature(self, value):
def get_power(self):
return self.testbed.simulator.light_source_data[self.id + '_power']

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


if __name__ == '__main__':
service = ThorlabsMcls1Sim()
Expand Down
2 changes: 2 additions & 0 deletions catkit2/simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def __init__(self, service_type, max_time_factor=1):
self.make_command('switch_power', self.switch_power)
self.make_command('set_source_power', self.set_source_power)

self.make_property('light_source_data', lambda: self.light_source_data)

self.integrating_cameras = {}
self.camera_integrated_power = {}
self.camera_callbacks = {}
Expand Down
Loading