Having trouble with sleep command in LUA scripting

Started by csg2, January 03, 2024, 02:04:35 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

csg2

Hi Folks,

I am working on a LUA script. Have been stumbling along OK, but recently hit a wall. I want to be able to pause in the scrip0t for certain lengths of time. So I have been trying to use the sleep global function.

When i do this I get a console message "variable 'sleep' is not declared. If ti matters, this is on a Canon 550d.

My script is supposed to start with an auto-focus, and take 99 more pictures, then auto focus and so on until complete. I have a delay to start so any motion caused by pressing the button to start the script can die down. There is also supposed to be a delay between shots so as not to fill up the buffer.

Code follows:

require("logger")

-- global logger
test_log = nil
LAF = nil
Number_of_Shots=nil
AF_every_Shot=nil

function main()
    test_log = logger("ML/LOGS/LUATEST.LOG")
    Number_of_Shots=100
    AF_every_Shot=10
    menu.close()
    console.show()
    print "Lunatic!"
    sleep(2)
    for i=Number_of_Shots,1,-1
    do
if ((Number_of_Shots % AF_every_Shot) == 0) then
            lunatic_AF()
        else
            camera.shoot(false)
end
        sleep(1.6)
    end
    print "Press any key to exit."
    key.wait()
    console.hide()
end

function lunatic_AF()
    if lens.name == "" then
       printf("No Lens Detected.\n")
       return false
    end

    if not lens.af then
        printf("Please enable autofocus.\n")
        return false
    end

    if not lv.running then
       lv.start()
       assert(lv.running)
    end

    if lens.af then
LAF=camera.shoot(true)
        lv.stop()
        return LAF
    end

    lv.stop()
    return false

end -- lunatic_AF

function printf(s,...)
    test_log:writef(s,...)

    if not console.visible then
        display.notify_box(s:format(...), 5000)
    end
end

keymenu = menu.new
{
    name   = "Lunatic - Moon Stack Automation",
    help   = "Automates Lunar Stack Capture",
    select = function(this) task.create(main) end,
}

names_are_hard

What build of ML are you using?

If you reduce your script so it only does a sleep(2) in main, do you still see an error?
If you replace sleep(2) with msleep(2000), do you see an error?

Walter Schulz

For scripting use lua_fix or derived build.
It uses a newer script engine/API.

csg2

Quote from: names_are_hard on January 03, 2024, 02:57:48 AM
What build of ML are you using?

If you reduce your script so it only does a sleep(2) in main, do you still see an error?
If you replace sleep(2) with msleep(2000), do you see an error?

Nightly.2018.Jul03.550D.109

With a sleep being the only call in function_main() I get a different error message.

[C] in function 'globals.error'
[dtring "-- strict.lua..."]:49 in metamethod '__index'
ML/Scripts/Lunatic.lua:10: in function 'globals.main'

Line 10 is the line sleep is called from in the modified script.

With msleep in place of sleep, there is no error.

require("logger")

-- global logger
test_log = nil
LAF = nil
Number_of_Shots=nil
AF_every_Shot=nil

function main()
    sleep(2)
end

function main2()
    test_log = logger("ML/LOGS/LUATEST.LOG")
    Number_of_Shots=100
    AF_every_Shot=10
    menu.close()
    console.show()
    print "Lunatic!"
    sleep(2)
    for i=Number_of_Shots,1,-1
    do
if ((Number_of_Shots % AF_every_Shot) == 0) then
            lunatic_AF()
        else
            camera.shoot(false)
end
        sleep(1.6)
    end
    print "Press any key to exit."
    key.wait()
    console.hide()
end

function lunatic_AF()
    if lens.name == "" then
       printf("No Lens Detected.\n")
       return false
    end

    if not lens.af then
        printf("Please enable autofocus.\n")
        return false
    end

    if not lv.running then
       lv.start()
       assert(lv.running)
    end

    if lens.af then
LAF=camera.shoot(true)
        lv.stop()
        return LAF
    end

    lv.stop()
    return false

end -- lunatic_AF

function printf(s,...)
    test_log:writef(s,...)

    if not console.visible then
        display.notify_box(s:format(...), 5000)
    end
end

keymenu = menu.new
{
    name   = "Lunatic - Moon Stack Automation",
    help   = "Automates Lunar Stack Capture",
    select = function(this) task.create(main) end,
}

Walter Schulz

I can only strongly recommend not to use nightly build for LUA scripting!

csg2

Quote from: Walter Schulz on January 03, 2024, 10:44:31 AM
I can only strongly recommend not to use nightly build for LUA scripting!

I was looking at the lua-fix builds under experimental builds. Is this for all cameras?

Walter Schulz


names_are_hard

Quote from: csg2 on January 03, 2024, 10:13:51 AM
Nightly.2018.Jul03.550D.109

Nightly build doesn't have sleep(), so this is expected behaviour.  Use msleep(), with a correspondingly longer time, or use lua_fix build, which does have sleep() (and other improvements).