-
Notifications
You must be signed in to change notification settings - Fork 0
/
YOFO.lua
executable file
·344 lines (296 loc) · 9.97 KB
/
YOFO.lua
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
343
344
-- YOFO
-- Repeatable astro focusing
------------------------------ constants --------------------------------------
_MAX_POS = 2000 -- max alowed position of the focuser in steps from hard limit
_PRESETS_FILENAME = "ML/SCRIPTS/yofo_settings/presets.lua"
_SCAN_LOG_FILENAME = "ML/SCRIPTS/yofo_scans/scan_logs.lua"
_TEST_LOG_FILENAME = "ML/LOGS/YOFOTEST.LOG"
_YOFO_IMG_PREFIX = "YOF_" -- change to empty string to disable
_STEP_SIZE = 1
----------------------------- helpers -----------------------------------------
function table.save(t, f)
-- by @marcotrosi
-- saves table t into file f
local file, err = io.open(f, "wb")
if err then
print("Error writting file")
return err
end
local function printTableHelper(obj, cnt)
local cnt = cnt or 0
if type(obj) == "table" then
file:write("\n", string.rep("\t", cnt), "{\n")
cnt = cnt + 1
for k, v in pairs(obj) do
if type(k) == "string" then
file:write(string.rep("\t", cnt), '["' .. k .. '"]', ' = ')
end
if type(k) == "number" then
file:write(string.rep("\t", cnt), "[" .. k .. "]", " = ")
end
printTableHelper(v, cnt)
file:write(",\n")
end
cnt = cnt - 1
file:write(string.rep("\t", cnt), "}")
elseif type(obj) == "string" then
file:write(string.format("%q", obj))
else
file:write(tostring(obj))
end
end
file:write(
"-- Focus Settings for YOFO.lua, please don't modify if you don't know what you're doing.\n")
file:write(
"-- Each value represents number steps that the autofocus motor has to go from hard limit to infinity.\n")
file:write("return")
printTableHelper(t)
file:close()
end
function table:load()
local settings = loadfile(_PRESETS_FILENAME)
local default = {["RGB"] = 0, ["Ha"] = 0}
if not settings then
settings = function() return {[lens.name] = default} end
end
settings = settings()
if not settings[lens.name] then settings[lens.name] = default end
return settings
end
function saveFile(settings) table.save(settings, _PRESETS_FILENAME) end
function printf(s, ...)
test_log:writef(s, ...)
if not console.visible then display.notify_box(s:format(...), 5000) end
end
function request_mode(mode, mode_str)
if camera.mode ~= mode or not camera.gui.idle then
printf("Please switch to %s mode.\n", mode_str, mode)
while camera.mode ~= mode or not camera.gui.idle do
console.show();
assert(console.visible)
if camera.gui.idle then alert() end
sleep(1)
end
end
sleep(2)
end
----------------------------- body -----------------------------------------
require("logger")
test_log = nil
test_log = logger(_TEST_LOG_FILENAME)
scan_log = nil
yofo = {}
yofo.value = ""
yofo.presets = {"RGB", "Ha"}
yofo.num_presets = #(yofo.presets)
yofo.settings = table.load()
yofo.current_position = 0
function move_focus(steps)
if not lv.running then
print("Please turn on LiveView first!")
return
end
if not lens.af then
print("Please turn on Autofocus!")
return
end
print("Moving to " .. steps .. ".")
lens.focus(steps, _STEP_SIZE, true, true)
end
----------------------------- goto menu ---------------------------------------
function _goto(preset)
menu.close()
if not lv.running then lv.start() end
move_focus(preset)
yofo.current_position = preset
lv.stop()
menu.open()
end
function gotoRGB()
local preset = yofo.presets_menu.submenu["RGB"].value
_goto(preset)
end
function gotoHa()
local preset = yofo.presets_menu.submenu["Ha"].value
_goto(preset)
end
yofo.goto_menu = menu.new {
parent = "Focus",
name = "YOFO Goto",
help = "Focus this lens (" .. lens.name ..
") to infinity using preset positions.",
submenu = {
{
name = "RGB",
help = "Infinity point for RGB frames",
update = function(this)
return yofo.presets_menu.submenu["RGB"].value
end,
select = function(this) task.create(gotoRGB) end,
depends_on = DEPENDS_ON.AUTOFOCUS
}, {
name = "Ha",
help = "Infinity point for Hydrogen filter",
update = function(this)
return yofo.presets_menu.submenu["Ha"].value
end,
select = function(this) task.create(gotoHa) end,
depends_on = DEPENDS_ON.AUTOFOCUS
}
},
depends_on = DEPENDS_ON.AUTOFOCUS,
update = function(this) return "" end
}
----------------------------- presets menu ------------------------------------
yofo.presets_menu = menu.new {
parent = "Focus",
name = "YOFO Presets",
help = "Manage infinity presets for this lens (" .. lens.name .. ")",
submenu = {
{
name = "RGB",
help = "Infinity point for RGB frames",
min = 0,
max = _MAX_POS,
value = yofo.settings[lens.name]["RGB"],
warning = function(this)
if this.value ~= yofo.settings[lens.name]["RGB"] then
return "Don't forget to save changes!"
end
end,
rinfo = function(this)
if this.value ~= yofo.settings[lens.name]["RGB"] then
return "*"
end
end,
unit = UNIT.DEC
}, {
name = "Ha",
help = "Infinity point for Hydrogen filter",
min = 0,
max = _MAX_POS,
value = yofo.settings[lens.name]["Ha"],
warning = function(this)
if this.value ~= yofo.settings[lens.name]["Ha"] then
return "Don't forget to save changes!"
end
end,
rinfo = function(this)
if this.value ~= yofo.settings[lens.name]["Ha"] then
return "*"
end
end,
unit = UNIT.DEC
}, {
name = "Save",
help = "Save current settings to SD card",
icon_type = ICON_TYPE.ACTION,
update = function(this) return "" end
}
},
update = function(this) return "" end
}
yofo.presets_menu.submenu["Save"].select = function(this)
local lens_settings = {
["RGB"] = yofo.presets_menu.submenu["RGB"].value,
["Ha"] = yofo.presets_menu.submenu["Ha"].value
}
local settings = yofo.settings
settings[lens.name] = lens_settings
saveFile(settings)
this.update = "Done!"
end
----------------------------- scan menu ---------------------------------------
function run_scan()
print("Scan start...")
scan_log = logger(_SCAN_LOG_FILENAME)
menu.close()
local start_pos = yofo.scan_menu.submenu["Start"].value
local end_pos = yofo.scan_menu.submenu["End"].value
local step = yofo.scan_menu.submenu["Step"].value
local image_path = nil
local image_prefix = nil
local position = 0
local num_frames = (end_pos - start_pos) // step + 1
local curr_frame = 0
dryos.image_prefix = _YOFO_IMG_PREFIX
if not lv.running then lv.start() end
move_focus(start_pos)
yofo.current_position = start_pos
position = start_pos
while position <= end_pos do
curr_frame = curr_frame + 1
print("Scanning position " .. position .. "!")
image_path = dryos.shooting_card:image_path(1)
scan_log:writef(image_path .. " " .. position .. "\n")
camera.shoot(false)
if position == end_pos then break end
lv.start()
print("Moving to the next step!")
position = position + step
move_focus(step)
yofo.current_position = position
end
dryos.image_prefix = ""
lv.stop()
display.on()
menu.open()
scan_log:close()
end
yofo.scan_menu = menu.new {
parent = "Focus",
name = "YOFO Scan",
help = "Scan through range to find best position",
submenu = {
{
name = "Start",
help = "Starting position",
min = 0,
max = _MAX_POS,
value = 0,
unit = UNIT.DEC
}, {
name = "End",
help = "End position, must be greater than Start",
min = 0,
max = _MAX_POS,
value = 30,
unit = UNIT.DEC
}, {
name = "Step",
help = "Interval step",
min = 1,
max = 100,
value = 10,
unit = UNIT.DEC
}, {
name = "Run",
help = "Run scan using current settings",
update = function(this) return "" end,
select = function(this) task.create(run_scan) end
}
},
depends_on = DEPENDS_ON.AUTOFOCUS,
update = function(this) return "" end
}
----------------------- focus position menu -----------------------------------
yofo.focus_menu = menu.new {
parent = "Focus",
name = "YOFO Position*",
help = "Current absolute focuser motor position",
warning = function(this) return "*Trust with care!" end,
update = function(this) return yofo.current_position end
}
yofo.focus_menu.select = function(this)
local s = menu.get("Focus", "Focus End Point")
local offset = 0
if s ~= "0 (here)" then
local sign, value = s:match("(.)(%d+).+")
if sign == "-" then
offset = -tonumber(value)
else
offset = tonumber(value)
end
end
this.rinfo = "-> " .. (yofo.current_position + offset)
end