forked from therandomfactory/speckle-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterWheelServer.tcl
346 lines (296 loc) · 10.2 KB
/
filterWheelServer.tcl
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/wish
#
# This Source Code Form is subject to the terms of the GNU Public
# License, v. 2. If a copy of the GPL was not distributed with this file,
# You can obtain one at https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
#
# Copyright(c) 2017 The Random Factory (www.randomfactory.com)
#
#
set SPECKLE_DIR $env(SPECKLE_DIR)
proc debuglog { msg } {puts stdout "$msg"}
proc loadFiltersConfig { fname } {
global SPECKLE_DIR NESCONFIG FWHEELS SCOPE
if { $SCOPE(telescope) == "GEMINI" } {
set fname "[set fname].gemini"
}
if { [file exists $SPECKLE_DIR/$fname] == 0 } {
errordialog "Filters configuration file $SPECKLE_DIR/$fname\n does not exist"
} else {
source $SPECKLE_DIR/filtersConfiguration
set NESCONFIG(picoChange) 0
}
}
proc saveFiltersConfig { fname } {
global SPECKLE_DIR NESCONFIG FWHEELS
set fcfg [open $SPECKLE_DIR/$fname w]
puts $fcfg "#!/usr/bin/tclsh
echoFiltersConfig $fcfg
close $fcfg
set NESCONFIG(picoChange) 0
debuglog "Saved Filters configuration in $SPECKLE_DIR/$fname"
}
proc logFiltersConfig { } {
global FLOG
echoFiltersConfig $FLOG
}
proc echoFiltersConfig { {fcfg stdout} } {
global FWHEELS
puts $fcfg "# Filters stage configuration parameters
"
foreach i "red blue" {
foreach p "port 1 2 3 4 5 6" {
puts $fcfg "set FWHEELS($i,$p) \"$FWHEELS($i,$p)\""
}
puts $fcfg ""
}
}
proc selectfilter { id n } {
global FILTERWHEEL FWHEELS MAXFILTERS
set i 1
while { $i <= $MAXFILTERS } {
.filters.f$id$i configure -relief raised -bg gray -activebackground gray
incr i 1
}
.filters.f$id$n configure -bg yellow -activebackground yellow
update
set result [setOrielFilter $FWHEELS($id,handle) $n]
set msg [split $result \n]
if { [string range [lindex $msg 3] 0 9] == "USB error:" } {
set it [ tk_dialog .d "SPECKLE Filter Wheel $id" "$result" {} -1 OK]
} else {
.filters.f$id$n configure -bg green -relief sunken -activebackground green
mimicMode $id filter $FWHEELS($id,$n)
set FWHEELS($id,position) $n
if { [info proc adjustTeleFocus] != "" } {
set delta [expr $FWHEELS($id,$FWHEELS($id,position),focus) - $FWHEELS(focusoffset)]
adjustTeleFocus $delta
set FWHEELS(focusoffset) $FWHEELS($id,$FWHEELS($id,position),focus)
}
}
}
proc findWheels { } {
global FWSERIAL
set fw [split [exec lsusb] \n]
set id 1
foreach i $fw {
if { [lindex $i 6] == "Newport" } {
set iusb($id) [string trim [lindex $i 1]:[lindex $i 3] :]
set info [exec lsusb -v -s $iusb($id)]
set FWSERIAL($iusb($id)) [lindex $info [expr [lsearch $info iSerial] +2]]
incr id 1
}
}
}
foreach a "red blue" {
foreach i "1 2 3 4 5 6" {
set FWHEELS($a,$i,focus) 0
}
}
set FWHEELS(focusoffset) 0
set MAXFILTERS 6
destroy .filters
toplevel .filters -bg gray
wm title .filters "SPECKLE Filter Wheels control"
label .filters.lpos -text "Red Position" -bg gray -fg black
label .filters.lname -text "Red Filter Name" -bg gray -fg black
label .filters.lfocus -text "Red Focus offset" -bg gray -fg black
label .filters.lbpos -text "Blue Position" -bg gray -fg black
label .filters.lbname -text "Blue Filter Name" -bg gray -fg black
label .filters.lbfocus -text "Blue Focus offset" -bg gray -fg black
place .filters.lpos -x 30 -y 20
place .filters.lname -x 150 -y 20
place .filters.lfocus -x 290 -y 20
place .filters.lbpos -x 400 -y 20
place .filters.lbname -x 520 -y 20
place .filters.lbfocus -x 680 -y 20
wm geometry .filters 773x[expr $MAXFILTERS*35 + 110]+10+170
set i 0
set iy 50
while { $i < $MAXFILTERS } {
incr i 1
button .filters.fred$i -text "$i" -relief raised -bg gray -fg black -command "selectfilter red $i" -width 8
entry .filters.namered$i -textvariable FWHEELS(red,$i) -bg LightBlue -fg black -width 20
entry .filters.focusred$i -textvariable FWHEELS(red,$i,focus) -bg LightBlue -fg black -width 8
place .filters.fred$i -x 20 -y $iy
button .filters.fblue$i -text "$i" -relief raised -bg gray -fg black -command "selectfilter blue $i" -width 8
entry .filters.nameblue$i -textvariable FWHEELS(blue,$i) -bg LightBlue -fg black -width 20
entry .filters.focusblue$i -textvariable FWHEELS(blue,$i,focus) -bg LightBlue -fg black -width 8
place .filters.fblue$i -x 390 -y $iy
incr iy 3
place .filters.namered$i -x 130 -y $iy
place .filters.focusred$i -x 310 -y $iy
place .filters.nameblue$i -x 500 -y $iy
place .filters.focusblue$i -x 690 -y $iy
incr iy 30
}
button .filters.load -text "Load configuration" -fg black -bg green -width 32 -command "loadFiltersConfig filtersConfiguration"
button .filters.save -text "Save configuration" -fg black -bg green -width 32 -command "saveFiltersConfig"
button .filters.exit -text "Close" -fg black -bg orange -width 32 -command "wm withdraw .filters"
place .filters.load -x 10 -y [expr $iy+30]
place .filters.save -x 262 -y [expr $iy+30]
place .filters.exit -x 502 -y [expr $iy+30]
proc filterWheelHelp { } {
puts stdout "Filter Wheel commands :
tcl Command Function
open Open a connection
close Close connection
getID IDN? Query to determine filter wheel model and firmware revision.
setPos FILT X Sets the active filter position to value X (1-6)
getPos FILT? Query to determine the active filter position (1-6)
setFlt FLTX ID Set the filter type/lable in position X
getFlt FLTX ID? Query to determine the filter type/label in position X
next NEXT Increments the filter position clockwise by 1 position
prev PREV Increments the filter position counter clockwise by 1 position
manual MBTN (Y/N) Sets the manual push button mode to enable (Y) or disable (N)
status STB? Query to determine the status of the unit
reset RST Resets the controller to default settings & operation
clear CLS Clears any status error messages
setHS HSHK X Set the unit handshake mode
getHS HSHK? Query to determine the hanshake mode of the unit
dotest TST Initiates a system self test
getTest TST? Query to determine the self test status/result
"
}
proc fwService { arm op {p1 ""} {p2 ""} {p3 ""} } {
global FWHEEL
switch $op {
open {}
close {}
getID {}
setPos {}
getPos {}
setFlt {}
getFlt {}
next {}
prev {}
manual {}
status {}
reset {}
clear {}
setHS {}
getHS {}
dotest {}
getTest {}
}
}
proc resetFilterWheel { id } {
oriel_write_cmd $id RST
after 7000
set res [oriel_read_result $id]
oriel_write_cmd $id FILT?
after 200
set res [oriel_read_result $id]
return $res
}
proc setOrielFilter { id n } {
oriel_write_cmd $id FILT?
after 200
set res [oriel_read_result $id]
set cpos [string range $res 4 4]
set npos [expr $n - $cpos]
while { $npos > 0 } {
oriel_write_cmd $id NEXT
incr npos -1
after 2000
set res [oriel_read_result $id]
}
while { $npos < 0 } {
oriel_write_cmd $id PREV
incr npos 1
after 2000
set res [oriel_read_result $id]
}
oriel_write_cmd $id FILT?
after 200
set res [oriel_read_result $id]
set cpos [string range $res 4 4]
if { $cpos == $n } {
return $n
}
return -1
}
loadFiltersConfig filtersConfiguration
load $env(SPECKLE_DIR)/lib/liboriel.so.tcl85
foreach p "1 2 3 4 5 6" {
if { $FWHEELS(red,$p) == "clear" } {
set FWHEELS(red,clear) $p
}
if { $FWHEELS(blue,$p) == "clear" } {
set FWHEELS(blue,clear) $p
}
}
set FWHEELS(sim) 0
if { [info exists env(SPECKLE_SIM)] } {
set simdev [split $env(SPECKLE_SIM) ,]
if { [lsearch $simdev filter] > -1 } {
set FWHEELS(sim) 1
debuglog "Filter wheels in SIMULATION mode"
}
}
if { $FWHEELS(sim) == 0 } {
findWheels
set f1 [oriel_connect 1]
set sn1 $FWSERIAL([lindex $f1 1])
if { $sn1 == $FWHEELS(red,serialnum) } {set FWHEELS(red,handle) 1}
if { $sn1 == $FWHEELS(blue,serialnum) } {set FWHEELS(blue,handle) 1}
set f2 [oriel_connect 2]
set sn2 $FWSERIAL([lindex $f2 1])
if { $sn2 == $FWHEELS(red,serialnum) } {set FWHEELS(red,handle) 2}
if { $sn2 == $FWHEELS(blue,serialnum) } {set FWHEELS(blue,handle) 2}
showstatus "Initializing filter wheel 1"
resetFilterWheel 1
showstatus "Initializing filter wheel 2"
resetFilterWheel 2
debuglog "Moving filter wheels to clear positions"
selectfilter red $FWHEELS(red,clear)
selectfilter blue $FWHEELS(blue,clear)
}
wm protocol .filters WM_DELETE_WINDOW {wm iconify .status}
# Handles the input from the client and client shutdown
proc svcHandler {sock} {
set l [gets $sock] ;# get the client packet
if {[eof $sock]} { ;# client gone or finished
close $sock ;# release the servers client channel
} else {
doService $sock $l
}
}
# Accept-Connection handler for Server.
# called When client makes a connection to the server
# Its passed the channel we're to communicate with the client on,
# The address of the client and the port we're using
#
# Setup a handler for (incoming) communication on
# the client channel - send connection Reply and log connection
proc accept {sock addr port} {
# if {[badConnect $addr]} {
# close $sock
# return
# }
# Setup handler for future communication on client socket
fileevent $sock readable [list svcHandler $sock]
# Note we've accepted a connection (show how get peer info fm socket)
puts "Accept from [fconfigure $sock -peername]"
# Read client input in lines, disable blocking I/O
fconfigure $sock -buffering line -blocking 0
# Send Acceptance string to client
# puts $sock "$addr:$port, You are connected to the echo server."
# puts $sock "It is now [exec date]"
# log the connection
puts "Accepted connection from $addr at [exec date]"
}
proc doService {sock msg} {
global FILTERWHEEL FWHEELS MAXFILTERS
set ANDOR_CFG([lindex $msg 0]) [lrange $msg 1 end]
switch [lindex $msg 0] {
selectfilter { selectfilter [lindex $msg 1] [lindex $msg 2] ; puts $sock "OK" }
getfilter { puts $sock "$FWHEELS([lindex $msg 0],position)" }
reset { resetFilterWheel $FWHEELS([lindex $msg 0],handle) }
}
}
# Create a server socket on port $svcPort.
# Call proc accept when a client attempts a connection.
set svcPort 2003
socket -server accept $svcPort
vwait events ;# handle events till variable events is set