Magic Lantern Forum

Developing Magic Lantern => Scripting Corner => Scripting Q&A => Topic started by: garry23 on April 16, 2019, 07:24:52 PM

Title: lens.focus problem
Post by: garry23 on April 16, 2019, 07:24:52 PM
@a1ex

As usual, I hope you can help.

I'm writing a new script and trying to use lens.focus.

I'm in LV but I get a console error saying lens.focus needs to run in LV.

Here is the area of the script that throws the error:

function set_focus(x)
    lv.resume()
    local current_x = lens.focus_distance
    if x == current_x then return x end
    if x > current_x then
        while lens.focus_distance < x do lens.focus(1,2,true) end
        while lens.focus_distance > x do lens.focus(-1,2,true) end
    else
        while lens.focus_distance > x do lens.focus(-1,2,true) end
        while lens.focus_distance < x do lens.focus(1,2,true) end
    end
    return lens.focus.focus_distance -- final focus distance, ie may not be extactly x ;-)
end


I simply can't work out what's happening.

Am I missing something?

BTW I'm on a 5D3 with the latest Lua fix.

Cheers

Garry
Title: Re: lens.focus problem
Post by: a1ex on April 16, 2019, 08:03:34 PM
If it says it needs to run in LiveView, then... it's probably right.

For example, image review after capturing an image, is not LiveView, even if the image capture was initiated from there. LiveView is when the image on the screen is... well... refreshing at some video-like frame rate.

May I see the complete script (i.e. the context used for calling this function) ?
Title: Re: lens.focus problem
Post by: garry23 on April 16, 2019, 08:11:11 PM
@a1ex

I did think along those lines, hence I started experimenting with a few sleep calls.

I also had a CBR error thrown up on the key press that I don't understand.

Title: Re: lens.focus problem
Post by: a1ex on April 16, 2019, 08:26:42 PM
I see a lot of calls to lv.resume() - these are meant to be used in pair with lv.pause(). This "paused" state is not available in vanilla firmware; it's always initiated from ML. If you are not in this "paused" state, lv.resume() will have no effect.

In your case, lv.start() should do the trick. You are outside LiveView after taking a picture, and you want to get back to LiveView.

You could also try: camera.gui.play = false (this will "close" the image review screen), but that's a bit of a stretch, since the image review is actually called QR (QuickReview) in Canon firmware, and it's different from the regular PLAY mode. There is already camera.gui.qr for detecting this mode; I should also add camera.gui.qr = false for consistency.

Or, if you prefer to wait for the image review screen to go away on its own, then you may wait for lv.running to become "true".
Title: Re: lens.focus problem
Post by: garry23 on April 16, 2019, 08:35:58 PM
@a1ex

How stupid of me  :(

Your advice fixed it and I can now get on developing the script.

Many thanks.

Garry
Title: Re: lens.focus problem
Post by: garry23 on April 18, 2019, 02:34:22 PM
@a1ex

I'm trying to make a script as robust as possible using lens.focus().

The issue I'm trying to cover is lens.focus() failing if the smallest step is used. I don't see this on my 5D3, but I know you suggested it could fail on some camera-lens set ups.

The question in my mind is, would you be able to detect this at least and flag it as a return state of the lens.focus() call, so a script could take action, eg change step size and restart moving.

Or is that simply not possible?

Cheers

Garry
Title: Re: lens.focus problem
Post by: a1ex on April 18, 2019, 02:37:44 PM
You should already be able to detect this condition by looking at the return value of lens.focus(). See the examples in api_test.lua.
Title: Re: lens.focus problem
Post by: garry23 on April 18, 2019, 04:26:02 PM
@a1ex

Great: my Easter job  ;)
Title: Re: lens.focus problem
Post by: garry23 on April 18, 2019, 05:44:02 PM
@a1ex

I forgot to ask; do you have any insight as too why or where, ie towards macro or towards infinity, will lens.focus(), with a 1 step, likely fail? Or is ir 'random'.

One thought I had was to put in a short lens test, ie to test if step size 1 works.

Cheers

Garry
Title: Re: lens.focus problem
Post by: a1ex on April 18, 2019, 07:39:43 PM
There is already a retry mechanism built in lens.focus() - it won't return failure right away. I didn't test it much, and still consider that code a bit fragile.

In particular, I'd expect it to fail on 50/1.8 II (and other lenses with similar mechanics). This lens doesn't have a dedicated limit switch; rather, I believe it looks at the current needed by the DC motor. If you stop the lens in the middle of a lens.focus call, it would fail in the same way as if it reached the limit.

If the lens mechanics are not moving perfectly smooth, the motor might require some more force to overcome some "bumps". It may have enough force with large movement steps (from inertia), but a small movement request may not be enough. The motor might even move into the desired position (and confirming it with its encoder), only to come back when the movement stops, since the mechanics are not perfectly rigid.

So... yes, it's a bit of a hit and miss.
Title: Re: lens.focus problem
Post by: scrax on April 18, 2019, 08:07:38 PM
I have problems with focus motors on both the bundled EF-S 18-55mm f3,5-5,6 IS II and EF-S 17-85mm f4-5,6 IS USM. simply they make up more friction, motor can't stand to move then and thinks the lens got stuck or gives erros like fake end of the focus movement.
Both are not so nice lenses and also got a lot of abuse (the 17-85 has the diaphram totally broken can take pic only at full aperture or it gives err 1, I'm unsure to replace it because the focus problem, and it's a lens that even when under warranty had to go to service for a couple of times.)

ThOSeare the "good" ones but still they are not so reliable
EF 50mm f1,4 usm (172 steps)
EF 100mm f2,8 usm macro (632 steps)
EF 70-200mm f4 L usm (303 going up, 292 coming back)
steps are measured with one step increments (changin to 3 makes the focus movement to bee to much in both direction with ML notification Focus: soft limit reached

Title: Re: lens.focus problem
Post by: garry23 on April 18, 2019, 08:29:58 PM
@scrax

Not sure where to go on this. As I say, I'm happy, the script works well on my 5D3 and I've tried it out on the two lenses that I'll use for auto focus bracketing, ie 12-24mm Sigma and 24-105mm F/4L.

I have decided to change the bookends, thus allowing the script to run in M, Av or Tv modes. I'll post over Easter.

@a1ex

Thanks for the insight. The code works for me and I already had a lens.focus() test in the loop  ;)

Bottom line: I'll keep posting my developments with the script and hope others get something out of me sharing my hobby  ;D

Cheers

Garry
Title: Re: lens.focus problem
Post by: garry23 on April 18, 2019, 10:36:37 PM
@a1ex

I truly don't know if I have found a bug in the latest Lua fix or not.

I've decided to use Dual ISO as my bookend frame in my latest focus script, rather than change shutter/aperture values, thus allow Tv, Av or M modes.

All I do is switch Dual ISO on or off, according to whether it is currently on or off.

I've thus changed the bookend function in the script to this:

function bookend()
    local state1 = menu.get("Expo","Dual ISO",0)
    local state2 = 0
    if state1 == 0 then state2 = 1 else state2 = 0 end
    menu.set("Expo","Dual ISO",state2)
    camera.shoot()
    menu.set("Expo","Dual ISO",state1)
end


The problem is: nothing changes in the bookend image.

I truly can't see what I'm doing wrong.

Can you see a silly mistake I've made?

Cheers

Garry