forked from Cephla-Lab/Squid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcelesta.py
executable file
·190 lines (162 loc) · 6.62 KB
/
celesta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
Generic Lumencor laser control via HTTP (ethernet connection).
Bogdan 3/19
revised HL 2/2024
"""
import urllib.request
import traceback
from squid.abc import LightSource
from control.microscope import LightSourceType, IntensityControlMode, ShutterControlMode
import squid.logging
log = squid.logging.get_logger(__name__)
def lumencor_httpcommand(command="GET IP", ip="192.168.201.200"):
"""
Sends commands to the lumencor system via http.
Plese find commands here:
http://lumencor.com/wp-content/uploads/sites/11/2019/01/57-10018.pdf
"""
command_full = r"http://" + ip + "/service/?command=" + command.replace(" ", "%20")
with urllib.request.urlopen(command_full) as response:
message = eval(response.read()) # the default is conveniently JSON so eval creates dictionary
return message
class CELESTA(LightSource):
"""
This controls a lumencor object (default: Celesta) using HTTP.
Please connect the provided cat5e, RJ45 ethernet cable between the PC and Lumencor system.
"""
def __init__(self, **kwds):
"""
Connect to the Lumencor system via HTTP and check if you get the right response.
"""
self.on = False
self.ip = kwds.get("ip", "192.168.201.200")
[self.pmin, self.pmax] = 0, 1000
try:
# See if the system returns back the right IP.
self.message = self.get_IP()
assert self.message["message"] == "A IP " + self.ip
self.n_lasers = self.get_number_lasers()
self.live = True
except:
log.error(traceback.format_exc())
self.live = False
log.error("Failed to connect to Lumencor Laser at ip:", ip)
if self.live:
[self.pmin, self.pmax] = self.get_intensity_range()
self.set_shutter_control_mode(True)
for i in range(self.n_lasers):
if not self.get_shutter_state(i):
self.set_shutter_state(i, False)
self.channel_mappings = {
405: 0,
470: 2,
488: 2,
545: 4,
550: 4,
555: 4,
561: 4,
638: 5,
640: 5,
730: 6,
735: 6,
750: 6,
}
def initialize(self):
pass
def set_intensity_control_mode(self, mode):
pass
def get_intensity_control_mode(self):
pass
def get_number_lasers(self):
"""Return the number of lasers the current lumencor system can control"""
self.message = lumencor_httpcommand(command="GET CHMAP", ip=self.ip)
if self.message["message"][0] == "A":
return len(self.message["message"].split(" ")) - 2
return 0
def get_color(self, laser_id):
"""Returns the color of the current laser"""
self.message = lumencor_httpcommand(command="GET CHMAP", ip=self.ip)
colors = self.message["message"].split(" ")[2:]
log.info(colors)
return colors[int(laser_id)]
def get_IP(self):
self.message = lumencor_httpcommand(command="GET IP", ip=self.ip)
return self.message
def get_shutter_control_mode(self):
self.message = lumencor_httpcommand(command="GET TTLENABLE", ip=self.ip)
response = self.message["message"]
if response[-1] == "1":
return ShutterControlMode.TTL
else:
return ShutterControlMode.Software
def set_shutter_control_mode(self, mode):
if mode == ShutterControlMode.TTL:
ttl_enable = "1"
else:
ttl_enable = "0"
self.message = lumencor_httpcommand(command="SET TTLENABLE " + ttl_enable, ip=self.ip)
def get_shutter_state(self, laser_id):
self.message = lumencor_httpcommand(command="GET CH " + str(laser_id), ip=self.ip)
response = self.message["message"]
self.on = response[-1] == "1"
return self.on
def get_intensity_range(self):
max_int = 1000 # default
self.message = lumencor_httpcommand(command="GET MAXINT", ip=self.ip)
if self.message["message"][0] == "A":
max_int = float(self.message["message"].split(" ")[-1])
return [0, max_int]
def get_intensity(self, laser_id):
self.message = lumencor_httpcommand(command="GET CHINT " + str(laser_id), ip=self.ip)
log.debug(command = 'GET CHINT '+str(laser_id), ip=self.ip)
response = self.message["message"]
power = float(response.split(" ")[-1])
intensity = power / self.pmax * 100
return intensity
def set_shutter_state(self, laser_id, on):
if on:
self.message = lumencor_httpcommand(command="SET CH " + str(laser_id) + " 1", ip=self.ip)
self.on = True
else:
self.message = lumencor_httpcommand(command="SET CH " + str(laser_id) + " 0", ip=self.ip)
self.on = False
log.info("Turning On/Off", self.on, self.message)
def set_intensity(self, laser_id, intensity):
log.info("Setting intensity to ", intensity)
power_in_mw = self.pmax * intensity / 100
self.message = lumencor_httpcommand(
command="SET CHINT " + str(laser_id) + " " + str(int(power_in_mw)), ip=self.ip
)
if self.message["message"][0] == "A":
return True
return False
def shut_down(self):
if self.live:
for i in range(self.n_lasers):
self.set_intensity(i, 0)
self.set_shutter_state(i, False)
def get_status(self):
return self.live
#
# The MIT License
#
# Copyright (c) 2013 Zhuang Lab, Harvard University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#