CHDK Wiki
No edit summary
(added note about 1.3.0 MF updates)
(28 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{notice|CHDK Manual Focus capability has been greatly improved starting with version 1.3.0. The rest of this wiki page describes CHDK 1.2.0 or earlier.}}
'''Subtitle :  '''Getting the "set_focus()" command in CHDK scripts,  or the "Override Subject Distance" menu item in the Enhanced Photo Operations menu,  to work.
 
  +
'''''forum thread ->''''' [http://chdk.setepontos.com/index.php?topic=11078.0 Manual Focus Testing Thread]
   
  +
{| align="right" style="border-collapse:collapse; font-size: x-small;"
==set_aflock()==
 
  +
| __TOC__
* set_aflock(1) causes the Canon firmware to stop focussing when shooting
 
  +
|}
  +
  +
==Introduction==
  +
[[File:Out_of_focus.jpg|left|200px]]
  +
One of the very useful features of CHDK is its ability to focus the camera manually,  either as part of a script or through the CHDK menus.  While some Canon cameras have a built-in manual focus options, many do not.  And for all cameras, the ability to set a focus point in a script can be important, either for "focus stacking" shots or to force the camera to focus at infinity (or the hyper-focal point) when shooting with intervalomters.
  +
  +
However,  determining how to use CHDK to reliably manually focus a camera's lens can be difficult as many cameras need to be configured a little differently.   This applies to both the ''"set_focus()''" command in CHDK scripts,  or the "Override Subject Distance" menu item in the CHDK's Enhanced Photo Operations menu. This problem is caused by there being no standard method that works for every camera that allows you to enable manual focus using CHDK.
  +
  +
Some cameras will immediately focus whenever a ''set_focus()'' script command is issued or SD override via menu..
  +
  +
Others will only focus when the camera's built-in autofocus is locked (AFL) prior to a ''set_focus()'' or SD override via menu.
  +
  +
And still others need the camera to be in Canon manual focus (MF) mode before you can focus via CHDK ''set_focus()'' or SD override via menu.
  +
  +
Listed below are some notes collected from various CHDK forum posts and wiki pages that should help you determine how to enable manual focus via CHDK for your camera. A Lua script is also give to simply determining what works for a particular camera.
  +
<br style="clear: both" />
  +
==Enabling CHDK Manual Focus==
  +
  +
The following instructions will show you how to enable CHDK's ability to manually focus (either in a script or via the CHDK SD Override menu item).
  +
  +
1) Can CHDK manually focus with no changes to the Canon setting ? To test this .... If this works, you are done. Congratulations! Otherwise go to step 2 below.
  +
  +
2) Does your camera natively support manual focus using the Canon menus ? If so ..
  +
  +
3) Will your camera allow CHDK to manually focus only when it is Canon AFL mode?
  +
  +
4) Will your camera allow a CHDK script to manually focus when CHDK issues the set_aflock() command ?
  +
  +
==CHDK Manual Focus Scripting Commands==
  +
 
===set_aflock()===
 
* set_aflock(1) causes the Canon firmware to stop focusing when shooting
 
:* does an initial focus measurement and sets focus at that value when called
 
:* does an initial focus measurement and sets focus at that value when called
* set_aflock(0) restores camera automatic focussing.
+
* set_aflock(0) restores camera automatic focusing.
 
* seems to works on almost all cameras
 
* seems to works on almost all cameras
 
* using set_aflock() may be necessary on some camera for set_focus() to work (elph310). On other cameras, it has no effect on whether set_focus() works.
 
* using set_aflock() may be necessary on some camera for set_focus() to work (elph310). On other cameras, it has no effect on whether set_focus() works.
Line 10: Line 43:
 
* if you call set_aflock() and then set_focus() (in a mode on your camera where that works) then the focus for all subsequent shots will stay at the value specified in set_focus().  If set_aflock() has not been called then set_focus() will only apply to the next shot - after that the camera will revery to auto focus.
 
* if you call set_aflock() and then set_focus() (in a mode on your camera where that works) then the focus for all subsequent shots will stay at the value specified in set_focus().  If set_aflock() has not been called then set_focus() will only apply to the next shot - after that the camera will revery to auto focus.
   
==set_focus()==
+
===set_focus()===
   
 
* script command that may or may not actually set the focus setting (in mm) the next time the camera shoots.
 
* script command that may or may not actually set the focus setting (in mm) the next time the camera shoots.
Line 22: Line 55:
   
 
:# Canon AFL mode is typically achieved via simulated button presses using ''click()'' - see your camera manual for the correct sequence. May require holding a "half press" while another key is clicked.
 
:# Canon AFL mode is typically achieved via simulated button presses using ''click()'' - see your camera manual for the correct sequence. May require holding a "half press" while another key is clicked.
:# <span style="font-size:13px;">Canon MF mode is may be achieved via simulated button presses, post_levent_for_npt call, and/or call_event_proc calls.</span>
+
:# Canon MF mode is may be achieved via simulated button presses, post_levent_for_npt call, and/or call_event_proc calls.
:# <span style="font-size:13px;">When in Canon MF mode,  focus will be set immediately when a set_focus() command is issued.  Otherwise the focus will not take effect until after the "half-pres" part of the shooting sequence.</span>
+
:# When in Canon MF mode,  focus will be set immediately when a set_focus() command is issued.  Otherwise the focus will not take effect until after the "half-press" part of the shooting sequence.
  +
:# If all of the above fail to work, you may be able to set focus to infinity on any camera by using click() commands to select the "landscape" focus mode.  The needed button is usually indicated by the tulip flower icon on one of the camera buttons but check your camera manual to be sure.
  +
<br />
   
 
==Forum Links==
 
==Forum Links==
Line 31: Line 66:
   
 
http://chdk.setepontos.com/index.php?topic=6672.msg75166#msg75166
 
http://chdk.setepontos.com/index.php?topic=6672.msg75166#msg75166
  +
 
==Test Script==
 
==Test Script==
  +
link >  [[CHDK_Manual_Focus_and_Subject_Distance_Overrides/MF_Test_Status|Test Results]] posted here
--[[
 
  +
@title MF Test Suite
 
  +
The script copied below lets you try out the various options for using set_focus().   Save it to an ASCII plain text file with the name mf-test.lua.
 
  +
@param f Focus Lock
 
  +
<span style="font-size:13px;line-height:21px;">The script provides six choices of camera setup - pick one from the user parameter at run time to see if that mode will work for your camera.  </span>
@default f 2
 
  +
@values f Off AFlock MF1 MF2 MF3
 
  +
The script will attempt to focus at 100, 200, and 300 mm and take a two test shots at each distance.  The first shot is preceeded by a set_focus() command but the second shot is not.   The purpose of the second shot is to see if the focus distance set for the first shot is preserved for the second shot.
--]]
 
  +
set_console_layout(1 ,1, 45, 14 )
 
  +
<syntaxhighlight lang="lua">
print( "f-tester v1")
 
 
--[[
sleep(1000)
 
 
@title MF Test Suite
if ( get_mode() == false ) then
 
set_record(1)
+
@param f MF Mode
 
@default f 0
while ( get_mode() == false ) do sleep(100) end
 
 
@values f None AFlock AFL MF1 MF2 MF3
end
 
 
--]]
 
  +
props=require("propcase")
if( f==1) then -- set_aflock()
 
 
set_console_layout(1 ,1, 45, 14 )
print("aflock mode")
 
  +
print( "Set Focus Tester v1.2")
set_aflock(1)
 
 
sleep(1000)
end
 
 
if ( get_mode() == false ) then
 
  +
set_record(1)
if( f==2) then -- MF1 lock focus via key presses
 
 
while ( get_mode() == false ) do sleep(100) end
print("MF1 mode")
 
 
end
local count = 0
 
  +
local timeout = false
 
 
if ( f==1) then -- method 1 : set_aflock()
press("shoot_half")
 
  +
print("set_aflock() mode")
repeat
 
 
set_aflock(1)
sleep(50)
 
  +
set_prop(props.AF_LOCK,1)
count = count + 1
 
if (count > 40 ) then timeout = true end
+
elseif( f==2) then -- method 2 : enable Canon AFL lock focus via key presses
 
print("AFL mode")
until (get_focus_ok() == 1 ) or (timeout == true)
 
 
local count = 0
if (timeout == true) then print("Warning : focus attempt timed out")
 
else click("left")
+
local timeout = false
 
press("shoot_half")
end
 
sleep(100)
+
repeat
release("shoot_half")
+
sleep(50)
 
count = count + 1
end
 
  +
if (count > 40 ) then timeout = true end
 
 
until (get_focus_ok() == 1 ) or (timeout == true)
if( f==3) then -- MF2 lock focus using levent for key press
 
print("MF2 mode")
+
if (timeout == true) then print("Warning : focus attempt timed out")
 
else click("left")
post_levent_for_npt("PressSw1AndMF")
 
end
+
end
+
sleep(100)
 
release("shoot_half")
if( f==4) then -- MF3 lock focus using event_proc for key press
 
 
elseif( f==3) then -- method 3 : MF1 lock focus via key presses
print("MF3 mode")
 
call_event_proc("SS.Create")
+
print("MF1 mode")
call_event_proc("SS.MFOn")
+
click("up")
 
elseif( f==4) then -- method 4 : MF2 lock focus using levent for key press
end
 
 
print("MF2 mode")
 
 
post_levent_for_npt("PressSw1AndMF")
sleep(1000)
 
 
elseif( f==5) then -- method 5 : MF3 lock focus using event_proc for key press
print("Focus mode = ".. get_focus_mode())
 
 
print("MF3 mode")
for focus=100,300,100 do
 
  +
call_event_proc("SS.Create")
set_focus(focus)
 
  +
call_event_proc("SS.MFOn")
sleep(2000)
 
  +
else print("no focus mode selected")
press("shoot_half")
 
 
end
sleep(1000)
 
  +
before=get_focus()
 
 
sleep(1000)
click("shoot_full")
 
 
for focus=100,300,100 do
release("shoot_half")
 
after=get_focus()
+
set_focus(focus)
 
sleep(2000)
if ((focus-after)/5 == 0) then fstr=" : okay" else fstr=" : fault" end
 
 
press("shoot_half")
print("Set:"..focus," Before:"..before," After:"..after..fstr)
 
sleep(2000)
+
sleep(1000)
 
before=get_focus()
press("shoot_half")
 
sleep(1000)
+
click("shoot_full")
 
release("shoot_half")
before=get_focus()
 
 
after=get_focus()
click("shoot_full")
 
 
if ((focus-after)/5 == 0) then fstr=" : okay" else fstr=" : fault" end
release("shoot_half")
 
 
print("Set:"..focus," Before:"..before," After:"..after..fstr)
after=get_focus()
 
 
sleep(2000)
if ((focus-after)/5 == 0) then fstr=" : okay" else fstr=" : fault" end
 
 
press("shoot_half")
print(" "..focus," Before:"..before," After:"..after..fstr)
 
sleep(1000)
+
sleep(1000)
 
before=get_focus()
end
 
 
click("shoot_full")
<span style="line-height:13.993056297302246px;white-space:pre;"> 
 
  +
release("shoot_half")
</span>
 
  +
after=get_focus()
 
 
 
if ((focus-after)/5 == 0) then fstr=" : locked" else fstr=" : unlocked" end
 
print(" "..focus," Before:"..before," After:"..after..fstr)
 
sleep(1000)
 
end
  +
  +
if (f == 1 ) then -- method 1 : aflock disable
  +
set_aflock(0)
  +
elseif (f == 2 ) then -- method 2 : deselect AFL
  +
click("left")
  +
elseif (f == 3 ) then -- method 3 : MF1 off
  +
click("up")
  +
elseif (f == 4 ) then -- method 4 : MF2 off
  +
post_levent_for_npt("PressSw1AndMF")
  +
elseif (f == 5 ) then -- method 5 : MF3 off
  +
call_event_proc("SS.Create")
  +
call_event_proc("SS.MFOff")
 
end
  +
 
print(".. done")
  +
sleep(100)
  +
  +
</syntaxhighlight>
  +
  +
==Links to MF Scripts==
  +
[[User_Written_Scripts#Focus_.26_Depth_of_Field.C2.A0Stacking|Focus and DOF Stacking]]
  +
  +
==For Developers :  SD Override / Manual Focus==
  +
{{notice|This section under active construction as of Dec 29 2013.}}
  +
To enable SD overrides to work via the CHDK menus, the following default settings in include/camera.h related to manual focus must be configured correctly :
  +
  +
#define CAM_CAN_SD_OVER_NOT_IN_MF 1 // Camera allows subject distance (focus) override when not in manual focus mode
  +
#undef CAM_CAN_SD_OVER_IN_AF_LOCK // Camera allows subject distance (focus) override when in AF Lock mode
  +
#undef CAM_CAN_SD_OVER_IN_AF_LOCK_ONLY // Camera allows subject distance (focus) override only when in AF Lock mode OR in movie mode
  +
#define CAM_CAN_SD_OVERRIDE 1 // Camera allows to do subject distance override
  +
#define CAM_HAS_MANUAL_FOCUS 1 // Camera has manual focus mode
  +
  +
Use these as follows :
  +
[[Category:help]]
  +
[[Category:Scripts]]

Revision as of 02:04, 19 June 2014

Notice

CHDK Manual Focus capability has been greatly improved starting with version 1.3.0. The rest of this wiki page describes CHDK 1.2.0 or earlier.

forum thread -> Manual Focus Testing Thread

Introduction

Out of focus

One of the very useful features of CHDK is its ability to focus the camera manually,  either as part of a script or through the CHDK menus.  While some Canon cameras have a built-in manual focus options, many do not.  And for all cameras, the ability to set a focus point in a script can be important, either for "focus stacking" shots or to force the camera to focus at infinity (or the hyper-focal point) when shooting with intervalomters.

However,  determining how to use CHDK to reliably manually focus a camera's lens can be difficult as many cameras need to be configured a little differently.   This applies to both the "set_focus()" command in CHDK scripts,  or the "Override Subject Distance" menu item in the CHDK's Enhanced Photo Operations menu. This problem is caused by there being no standard method that works for every camera that allows you to enable manual focus using CHDK.

Some cameras will immediately focus whenever a set_focus() script command is issued or SD override via menu..

Others will only focus when the camera's built-in autofocus is locked (AFL) prior to a set_focus() or SD override via menu.

And still others need the camera to be in Canon manual focus (MF) mode before you can focus via CHDK set_focus() or SD override via menu.

Listed below are some notes collected from various CHDK forum posts and wiki pages that should help you determine how to enable manual focus via CHDK for your camera. A Lua script is also give to simply determining what works for a particular camera.

Enabling CHDK Manual Focus

The following instructions will show you how to enable CHDK's ability to manually focus (either in a script or via the CHDK SD Override menu item).

1) Can CHDK manually focus with no changes to the Canon setting ? To test this .... If this works, you are done. Congratulations! Otherwise go to step 2 below.

2) Does your camera natively support manual focus using the Canon menus ? If so ..

3) Will your camera allow CHDK to manually focus only when it is Canon AFL mode?

4) Will your camera allow a CHDK script to manually focus when CHDK issues the set_aflock() command ?

CHDK Manual Focus Scripting Commands

set_aflock()

  • set_aflock(1) causes the Canon firmware to stop focusing when shooting
  • does an initial focus measurement and sets focus at that value when called
  • set_aflock(0) restores camera automatic focusing.
  • seems to works on almost all cameras
  • using set_aflock() may be necessary on some camera for set_focus() to work (elph310). On other cameras, it has no effect on whether set_focus() works.
  • on some cameras,  the set_aflock() command must be issued after a "half-press" focus completes and before the half press is released
  • if you call set_aflock() and then set_focus() (in a mode on your camera where that works) then the focus for all subsequent shots will stay at the value specified in set_focus().  If set_aflock() has not been called then set_focus() will only apply to the next shot - after that the camera will revery to auto focus.

set_focus()

  • script command that may or may not actually set the focus setting (in mm) the next time the camera shoots.
  • on some cameras, set_focus() will only work if the camera is in Canon AFL mode (ex: A1200)
  • on some cameras, set_focus() will only work if the camera is in Canon MF mode (ex: SX50HS)
  • on some cameras, set_focus() will work when set_aflock(1) has been called (ex: elph310)
  • on some cameras, set_focus() will only work when the camera is in P mode (or at least not in AUTO)
  • on some cameras, set_focus works every time regardless of mode (ex: G10, SD940)
Notes :
  1. Canon AFL mode is typically achieved via simulated button presses using click() - see your camera manual for the correct sequence. May require holding a "half press" while another key is clicked.
  2. Canon MF mode is may be achieved via simulated button presses, post_levent_for_npt call, and/or call_event_proc calls.
  3. When in Canon MF mode,  focus will be set immediately when a set_focus() command is issued.  Otherwise the focus will not take effect until after the "half-press" part of the shooting sequence.
  4. If all of the above fail to work, you may be able to set focus to infinity on any camera by using click() commands to select the "landscape" focus mode.  The needed button is usually indicated by the tulip flower icon on one of the camera buttons but check your camera manual to be sure.


Forum Links

http://chdk.setepontos.com/index.php?topic=9533.0

http://chdk.setepontos.com/index.php?topic=8016.msg94251#msg94251

http://chdk.setepontos.com/index.php?topic=6672.msg75166#msg75166

Test Script

link >  Test Results posted here

The script copied below lets you try out the various options for using set_focus().   Save it to an ASCII plain text file with the name mf-test.lua.

The script provides six choices of camera setup - pick one from the user parameter at run time to see if that mode will work for your camera.  

The script will attempt to focus at 100, 200, and 300 mm and take a two test shots at each distance.  The first shot is preceeded by a set_focus() command but the second shot is not.   The purpose of the second shot is to see if the focus distance set for the first shot is preserved for the second shot.

--[[
@title MF Test Suite
   @param   f MF Mode
   @default f 0
   @values  f None AFlock AFL MF1 MF2 MF3
--]]
props=require("propcase")
set_console_layout(1 ,1, 45, 14 )
print( "Set Focus Tester v1.2")
sleep(1000)
if ( get_mode() == false ) then
    set_record(1)
    while ( get_mode() == false ) do sleep(100) end
end

if    ( f==1) then              -- method 1 : set_aflock()
    print("set_aflock() mode")
    set_aflock(1)  
    set_prop(props.AF_LOCK,1)
elseif( f==2) then              -- method 2 : enable Canon AFL lock focus via key presses 
    print("AFL mode")
    local count = 0
    local timeout = false
    press("shoot_half")
    repeat  
        sleep(50)
        count = count + 1
        if (count > 40 ) then timeout = true end        
    until (get_focus_ok() == 1 ) or (timeout == true) 
    if (timeout == true) then print("Warning : focus attempt timed out")
    else  click("left")
    end
    sleep(100)
    release("shoot_half")
elseif( f==3) then             -- method 3 : MF1 lock focus via key presses 
    print("MF1 mode")
    click("up")
elseif( f==4) then             -- method 4 : MF2 lock focus using levent for key press
    print("MF2 mode")
    post_levent_for_npt("PressSw1AndMF") 
elseif( f==5) then             -- method 5 : MF3 lock focus using event_proc for key press  
    print("MF3 mode")
    call_event_proc("SS.Create")
    call_event_proc("SS.MFOn")
else print("no focus mode selected")
end

sleep(1000)
for focus=100,300,100  do
    set_focus(focus)
    sleep(2000)
    press("shoot_half")
    sleep(1000)
    before=get_focus()
    click("shoot_full")
    release("shoot_half")
    after=get_focus()
    if ((focus-after)/5 == 0) then fstr=" : okay" else fstr=" : fault" end
    print("Set:"..focus," Before:"..before," After:"..after..fstr)
    sleep(2000)
    press("shoot_half")
    sleep(1000)
    before=get_focus()
    click("shoot_full")
    release("shoot_half")
    after=get_focus()
    if ((focus-after)/5 == 0) then fstr=" : locked" else fstr=" : unlocked" end
    print("    "..focus," Before:"..before," After:"..after..fstr)
    sleep(1000)
end

if     (f == 1 ) then           -- method 1 : aflock disable
    set_aflock(0)  
elseif (f == 2 ) then           -- method 2 : deselect AFL
    click("left")
elseif (f == 3 ) then           -- method 3 : MF1 off
    click("up")  
elseif (f == 4 ) then           -- method 4 : MF2 off
    post_levent_for_npt("PressSw1AndMF")
elseif (f == 5 ) then           -- method 5 : MF3 off
    call_event_proc("SS.Create")
    call_event_proc("SS.MFOff")
end

print(".. done")
sleep(100)

Links to MF Scripts

Focus and DOF Stacking

For Developers :  SD Override / Manual Focus

Notice

This section under active construction as of Dec 29 2013.

To enable SD overrides to work via the CHDK menus, the following default settings in include/camera.h related to manual focus must be configured correctly :

#define CAM_CAN_SD_OVER_NOT_IN_MF       1   // Camera allows subject distance (focus) override when not in manual focus mode
#undef  CAM_CAN_SD_OVER_IN_AF_LOCK          // Camera allows subject distance (focus) override when in AF Lock mode
#undef  CAM_CAN_SD_OVER_IN_AF_LOCK_ONLY     // Camera allows subject distance (focus) override only when in AF Lock mode OR in movie mode
#define CAM_CAN_SD_OVERRIDE             1   // Camera allows to do subject distance override
#define CAM_HAS_MANUAL_FOCUS            1   // Camera has manual focus mode

Use these as follows :