Magic Lantern Forum

Developing Magic Lantern => Scripting Corner => Scripting API suggestions => Topic started by: garry23 on April 14, 2019, 08:11:53 AM

Title: set_focus()
Post by: garry23 on April 14, 2019, 08:11:53 AM

Over at CHDK I can use set_focus() in Lua, see my post here

As the G1X is an APS-C class camera, this is my preferred camera when doing deep focus landscape shots. For example this test image attached that I actually took with my G7X.

( (

I see in the latest Lua fix experimental that you reference unlocking rack focus for EF lens, at least.

But to get the full power out of controlling the focus one needs to control focus position in Lua, which is currently only available by moving a fixed amount and looping until you have converged to your required focus position. I’m sure if I can do this in Lua, and I can and have, it would be more stable and faster doing in in C and having something like this:

set_focus (the current focus distance in mm)

get_focus may also be used directly in an expression where its numerical value might fit: set_focus ((3 * get_focus) / 2) would set a new focus distance 1.5X farther than the current distance.


Assuming you think it can be done (technically and your time), may I request you consider adding set_focus() to the next Lua fix update.


Title: Re: set_focus()
Post by: garry23 on April 14, 2019, 07:31:30 PM

If set_focus(x) does become a reality, here are a few further thoughts:

* Recognizing that positional accuracy will not be the same over the focus range, it would be sensible for the 'stopping algorithm' to be sensitive to what direction it's moving in:
- If moving near to far, then x_stop <= requested_x
-  if moving far to near, then x_stop >= requested_x
* The above is a sensible strategy that respects focus overlap needs
* Maybe set_focus(x) could return the following:
- Null = lens doesn't allow focus control
- false = lens solution not obtained
- dimension = new focus position, ie not the requested one, but the actual one reported after the move

As I say, just some thoughts.


Title: Re: set_focus()
Post by: a1ex on April 14, 2019, 07:49:50 PM
Just acknowledging the suggestion, as reviewing and implementing it requires a large "context switch" for me right now. Below are some ideas I already had in mind for quite some time.

Precise focus distance (i.e. in fine steps with good repeatability) can be estimated by interpolating focus_pos (i.e. number of motor steps) between reported focus distances (a few discrete values, usually with a small hysteresis); I've looked into it in this thread ( A calibration procedure that would sweep the lens back and forth a few times, writing down motor step position alongside with reported focus distance, should be doable and likely useful.

If the user can provide some absolute measurements (e.g. with a ruler + a focus chart) or even a distance sensor like this ( (alongside with some way to interface it with the camera), accurate focus distances (i.e. precise + also matching real-world distances) might be doable as well.
Title: Re: set_focus()
Post by: garry23 on April 14, 2019, 08:22:00 PM

Thanks for finding the time to respond.

As you know, I consider Lua focus control one of the big things that is outstanding.

But I also know you know, that we are in your hands regarding implementation ;-)


Title: Re: set_focus()
Post by: garry23 on April 14, 2019, 09:09:15 PM

One more thought, I would like to suggest that there are two ways at looking at focus position accuracy.

First, as you imply, absolutely, ie if focus is reported as X then the focus is at X.

The other is relative, that is the focus shift is relative to the current focus position.

In relative mode, the absolute focus reporting is irrelevant. Most use cases will start from a position of focus and the user will wish to shift focus by a delta amount. Having shifted, the new position, and its reported position, establishes a new reference position to shift from.

I wonder if it might be possible to offer the user a choice, ie use set_focus() in absolute or relative mode. Also, in relative mode, calibration should be less of an issue.

As usual, just me thinking out loud.



Title: Re: set_focus()
Post by: c_joerg on April 15, 2019, 09:14:18 AM
I'm playing with my EOS M3 and trying to set the focus;topicseen#new

In the M3 there is information about Subject Distance Near / Far. I suspect that this is the same information that you find in the EXIF data under 'Focus Distance'. Maybe this information will help you further.

However, this information is probably not very accurate.
If I use small steps, the EXIF Information are not changing
Code: [Select]
It looks like that the DoF Information in the EXIF simpy calulatete by
Code: [Select]
Subject distance = (Focus Distance Upper  - Focus Distance Lower) / 2

EXIF Info from 6D

Camera Model Name               : Canon EOS 6D
Lens Model                      : EF24mm f/2.8 IS USM
Aperture Value                  : 2.8
Circle Of Confusion             : 0.030 mm
Hyperfocal Distance             : 6.76 m
Focus Distance Lower            : 2.1 m
Focus Distance Upper            : 3.67 m
Depth Of Field                  : 2.97 m (2.03 - 5.00 m)

Title: Re: set_focus()
Post by: IDA_ML on April 15, 2019, 10:29:40 AM

I don't understand much of what you are writing but your example looks fantastic!  If such focusing could be automated, that would be great!
Title: Re: set_focus()
Post by: garry23 on April 15, 2019, 01:18:31 PM

It can be automated now, ie I did this with a Lua script several years ago.

Because of my recent CHDK work, I'll likely revisit my auto focus scripts in ML.

But my gut feeling is, that a set_focus() function, controlled in C, would be 'better' that me looping to convergence with Lua.

Bottom line: watch out for a demo script from me  ;)
Title: Re: set_focus()
Post by: IDA_ML on April 15, 2019, 01:48:26 PM
Thanks Garry, I will!  All your work is amazing - innovative, creative and very useful!