Skip to content

Instantly share code, notes, and snippets.

@pigeonhill
Last active June 6, 2021 09:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigeonhill/21920aaed39500a75c6bba30f2f5f697 to your computer and use it in GitHub Desktop.
Save pigeonhill/21920aaed39500a75c6bba30f2f5f697 to your computer and use it in GitHub Desktop.
Bracketing (Tv, ISO or Focus) + DOFI
--[[
@DOFIZ
Rev 0.75
Garry George
@chdk_version 1.5
# show_DOFI = 1 "Display DOFI?" {No Yes}
# show_coc = 0 "Show Inf focus blurs?" {No Yes}
# time_2_show = 5 "Display timeout (s)" [3 30]
# pos = 1 "DOFI vertical position" [0 14]
--]]
require('rawoplib')
props=require('propcase')
set_config_value(2270,1) -- always show the console
set_config_value(2271,time_2_show) -- console time out
set_config_value(2271,100)
set_config_value(2151,0) -- subject distance setting to ensure measured from lens
set_config_value(2152,0) -- subject distance setting to ensure measured from lens
state = 1
state_con ={"None", "+1Ev", "+2Ev", "+3Ev", "+4Ev", "ISO400", "ISO800", "ISO1600", "Auto Focus"} -- make sure this list lines up with check_state function
max_states = #state_con
toggled = false
delta = 0
refresh = true
function restore()
set_console_layout(0,0,45,14)
set_config_value(1157,1)
set_config_value(2270,"ALT") -- return console to Alt mode
set_config_value(1150,0)
set_config_value(1152,0)
set_config_value(1153,0)
end
infinity = 99999 -- an acceptably large number that CHDK can handle and that is beyond Canon reporting
dof = get_dofinfo()
coc = dof.coc -- CHDK infinity blur (um) at the hyperfocal, ie the defocus Circle of Confusion, which the script uses as the overlap criterion
N, xx, x, ndof, fdof, f, last_x, last_fdof, last_ndof, last_f, last_N = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
last_step_x, last_step_f, last_step_N = 0, 0, 0
ecnt = get_exp_count()
no_image = true
min = coc*36000/(15*rawop.get_jpeg_width()) -- estimate of 2 sensor pixels infinity focusing limit
function toggle()
wait_click()
if is_key("set") then state = state + 1 end
if state > max_states then state = 1 end
end
function check_state() -- extend/change the following states to meet your needs
toggled = false
if state == 1 then
set_config_value(1150,0)
set_config_value(1152,0)
set_config_value(1153,0)
set_config_value(1156,2)
set_config_value(1154,0)
set_config_value(1154,0)
elseif state == 2 then -- Tv bracket at +1Ev, +2Ev, +3Ev or +4Ev from the base Tv. Set custom timer to any number of shots
set_config_value(1150,3)
set_config_value(1152,0)
set_config_value(1153,0)
set_config_value(1156,2)
elseif state == 3 then
set_config_value(1150,6)
set_config_value(1152,0)
set_config_value(1153,0)
set_config_value(1156,2)
elseif state == 4 then
set_config_value(1150,9)
set_config_value(1152,0)
set_config_value(1153,0)
set_config_value(1156,2)
elseif state == 5 then
set_config_value(1150,12)
set_config_value(1152,0)
set_config_value(1153,0)
set_config_value(1156,2)
elseif state == 6 then -- one additional ISO bracket at either 400, 800 or 1600 plus the base ISO. Set custom timer to 2 shots
set_config_value(1150,0)
iso = get_prop(props.ISO_MODE)
if iso >= 400 then iso = 0 else iso = 400 - iso end
set_config_value(1152,iso)
set_config_value(1153,1)
set_config_value(1156,2)
elseif state == 7 then
set_config_value(1150,0)
iso = get_prop(props.ISO_MODE)
if iso >= 800 then iso = 0 else iso = 800 - iso end
set_config_value(1152,iso)
set_config_value(1153,1)
set_config_value(1156,2)
elseif state == 8 then
set_config_value(1150,0)
iso = get_prop(props.ISO_MODE)
if iso >= 1600 then iso = 0 else iso = 1600 - iso end
set_config_value(1152,iso)
set_config_value(1153,1)
set_config_value(1156,2)
elseif state == 9 then
set_config_value(1150,0)
set_config_value(1152,0)
set_config_value(1153,0)
set_config_value(1156,2)
if 2*x < H then -- That is auto focus bracket, in two image sub-brackets
delta = ((2*x)*(x-f))/(H-2*x)
elseif x <= H then
delta = 2*H
else
delta = 0
end
set_config_value(1154,(delta))
set_config_value(1155,1)
end
end
function something_changed()
dof = get_dofinfo()
N = dof.aperture
f = dof.focal_length/1000 -- within a mm of the actual focal length, ie good enough
x = dof.focus -- CHDK estimate of focus 'from the lens'
ndof = dof.near -- note these are the defocus alone dofs, based on the CHDK CoC. That is no diffraction
fdof = dof.far
H = dof.hyp_dist
if (x < 0) then -- at infinity
ndof = H/2 -- just in case CHDK still hasn't fixed this ;-)
x = infinity
end
if (fdof < 0) then fdof = infinity end
if x ~= last_step_x or f ~= last_step_f or N ~= last_step_N then
return true
else
return false
end
end
function update_info()
if ecnt ~= get_exp_count() then -- register captured image's focus info
last_ndof = ndof
last_fdof = fdof
last_x = x
last_f = f
last_N = N
last_step_N = -N -- to force a console refresh
ecnt = get_exp_count()
no_image = false
toggled = true
end
if (x == infinity) or (x > H) then
num = 0
else
num = (1 + (H/x))/2 -- estimate of number of focus brackets from current focus to H
end
-- Because we are using integer math Lua, we need to muck about a bit :-(
scale = imath.scale
diff_b = imath.mul(1342,N) -- diffraction blur diameter = 2.44*0.550*N = 1'342*N (at vis bands, ie change for IR converted cam)
inf_b = scale*coc -- infinity defocus blur, scaled from H definded blur, ie CHDK coc, around 30um/crop. Consider switching on show_coc, as this reminds you what the 'just acceptable' defocus blur is
temp = imath.div(scale*H,scale*x)
inf_b = imath.mul(inf_b,temp)
if x == infinity then
inf_b = 0
tot_b = diff_b
else
tot_b = imath.mul(inf_b,inf_b) + imath.mul(diff_b,diff_b)
if tot_b < 16384000 then
tot_b = imath.sqrt(tot_b)
else
tot_b = inf_b
end
end
tot_b = imath.round(tot_b)/scale -- all blurs now converted to um
inf_b = imath.round(inf_b)/scale
diff_b = imath.round(diff_b)/scale
info = inf_b.."/"..diff_b.."/"..tot_b
if tot_b == inf_b then info = tot_b end
if x == infinity then
info = "oo: "..info
elseif (x < H) then
info = num.."<H: "..info
else
info = ">H: "..info
end
if no_image then
info = info.." [X]"
elseif x == last_x then
info = info.." [=]"
elseif ((fdof >= last_ndof) and (fdof <= last_fdof)) or ((ndof <= last_fdof) and (fdof >= last_fdof)) then
info = info.." [+]"
elseif (fdof < last_ndof) or (ndof > last_fdof) then
info = info.." [-]"
end
if show_coc == 1 then
info = " ["..coc..":"..min.."] "..info.." "
else
info = " "..info.." "
end
ls = info:len()
set_console_layout(0,pos,ls,pos+1)
end
-- Script will run in the background, in non Alt mode
-- Switch back into Alt mode to toggle between bracketing states
-- Leaving Alt mode locks in that bracketing state
-- If bracketing make sure you set the Canon custom timer, ie n imabges when Tv bracketing or 2 images when ISO bracketing
exit_alt()
while show_DOFI == 1 do
while get_alt_mode() do -- check.change bracketing state
toggled = true
toggle()
if props.TIMER_SHOTS and (get_prop(props.TIMER_MODE) == 3) then
text = " "..get_prop(props.TIMER_SHOTS).." custom brackets set: "..state_con[state].." "
else
text = " Check custom brackets: "..state_con[state].." "
end
set_console_layout(0,pos,text:len(),pos+1)
print(text)
end
if toggled then check_state() end
if is_pressed("shoot_half") then
set_mf(1)
refresh = true
end
if something_changed() or refresh then -- update console
refresh = false
update_info()
if state == 9 then -- focus stacking
check_state()
if no_image and x >= H/2 then
info = ">H/2: no image"
elseif no_image and x < H/2 then
info = "Focus Step:"..delta.."/no image yet"
elseif (last_f == infinity) or (last_x > H) or (delta == 0) then
info = "Focus stacking finshed"
elseif last_f ~= infinity then
info = "Focus Step:"..delta
end
end
set_console_layout(0,pos,info:len(),pos+1)
print(info)
last_step_N = N
last_step_x = x
last_step_f = f
end
sleep(100) -- free to adjust
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment