Magic Lantern Forum

Developing Magic Lantern => Feature Requests => Topic started by: l_d_allan on March 27, 2018, 10:40:22 PM

Title: Battery management: drain from high % to protect Li-ion. Prevent very low %.
Post by: l_d_allan on March 27, 2018, 10:40:22 PM
Title: Re: Battery management: drain from high % to protect Li-ion. Prevent very low %.
Post by: a1ex on March 27, 2018, 11:14:41 PM
To shutdown the camera, the only method I know (at the time of writing) is to set auto power off to some low value, then keep the camrea active while you want it not to shutdown. I know how to reboot the camera on demand (camera.reboot() in Lua), but not how to turn it off.

With the experimental lua_fix or crop_rec_4k builds, powersave is disabled by default while running a simple Lua script:


-- Discharge battery until 50%
-- battery.level is reported in percentage
while battery.level > 50 do
    -- optionally print battery level somewhere (exercise for the reader)
    msleep(10000)
end


When the script ends, the powersave is re-enabled and should turn off the camera after 1 minute (or whatever you have set for auto power off).

Not tested; it's just how I'd expect it to work.
Title: Re: Battery management: drain from high % to protect Li-ion. Prevent very low %.
Post by: l_d_allan on March 28, 2018, 12:01:33 AM
Quote from: a1ex on March 27, 2018, 11:14:41 PM
To shutdown the camera, the only method I know (at the time of writing) is to set auto power off to some low value, then keep the camrea active while you want it not to shutdown. I know how to reboot the camera on demand (camera.reboot() in Lua), but not how to turn it off.

With the experimental lua_fix or crop_rec_4k builds, powersave is disabled by default while running a simple Lua script:

Thanks! for the prompt reply. Yet again, I'm impressed.

If I read the above correctly, it seems like the second half of my feature request is already possible, at least with an experimental build. Or not?

Quote

-- Discharge battery until 50%
-- battery.level is reported in percentage
while battery.level > 50 do
    -- optionally print battery level somewhere (exercise for the reader)
    msleep(10000)
end


When the script ends, the powersave is re-enabled and should turn off the camera after 1 minute (or whatever you have set for auto power off).

Not tested; it's just how I'd expect it to work.

This retired C++ developer guesses that the above Lua loop with 10 sec delay may closely or even fully accomplish the first part of my request for the threshold of 50%. Wow! You know your stuff!

I'll look around for a ML read-me or tutorial on HowTo get started getting lua code written and then uploaded to my 6d/5dm2/600d. Any hints where to look? (which would be greatly appreciated).
Title: Requesting help in Reply to active dmilligan thread which invited Lua script
Post by: l_d_allan on March 28, 2018, 12:47:56 AM
Quote from: a1ex on March 27, 2018, 11:14:41 PMNot tested; it's just how I'd expect it to work.

Sorry if I'm being useless / lazy, but this ML fan-boy is ignorant of Lua scripting. I admit to my shame that I am clueless how to get even something as simple as "Hello, World" to show up on my 6d.

Instead, I looked around and found an existing Lua related thread (https://www.magiclantern.fm/forum/index.php?topic=14828.0) started by dmilligan. I then did a Reply with your proposed Lua loop (https://www.magiclantern.fm/forum/index.php?topic=14828.msg198955#msg198955).

Quote from: dmilliganYou don't even need to compile this branch, just write your test script and post it in this thread and I'll run it
Title: Starting to work. Progress showing on camera's LCD console. Times out
Post by: l_d_allan on March 28, 2018, 04:34:26 AM
Crude PoC (https://en.wikipedia.org/wiki/Proof_of_concept) version starting to work. Will attempt to drain to 90%. The script shows progress every 10 seconds.


function main()
    menu.close()
    console.show()
    io.write ("Drain battery to 90% starting at: ", battery.level, "% \nProgress: ")

    while battery.level > 90 do
        io.write(battery.level, "% ")
        io.flush()
        msleep(10000)
    end

    print "Press any key to exit."
    key.wait()
    console.hide()
end

keymenu = menu.new
{
    name   = "Drain battery to 90 percent",
    help   = "Prints progress of battery-draining on the console. The intent is to extend the life of the Li-Ion battery by avoiding storage at 100%.",
    select = function(this) task.create(main) end,
}


Intended output to camera's LCD console:
Drain battery to 90% starting at: 93%
Progress: 93% 93% ... 93% 92% 92% 92% 92%
92% 92% ... 92% 91% 91% 91% 91% 91% 91%
91% 91%
Press any key to exit

Problem: seems to work by draining several percent, but then the camera apparently times-out.
Title: Re: Battery management: drain from high % to protect Li-ion. Prevent very low %.
Post by: a1ex on March 28, 2018, 08:16:41 AM
The powersave feature only kicks in for simple scripts, not for complex ones that start their own tasks.

Yes, the above script makes use of an undocumented trick. I've assumed some simple scripts may need to run for more than 1 minute, so with this powersave trick, it would work out of the box.

In contrast, scripts that create a menu will run as long as you have the camera powered on, so powersave is not disabled in this case.

In ML core, tasks spawned from ML menu (run_in_separate_task) also have the powersave trick enabled, as they are meant to be temporary (see e.g. benchmarks - they usually take more than 30 seconds - the default auto power off setting on certain models). Tasks spawned from Lua could use the same trick - looking into it; meanwhile, try running your script as "simple" (from the main body, without creating a menu). My example is the complete script (but you need the experimental Lua build to run it).

There's no option for parameters in simple scripts yet (todo: borrow it from CHDK), but you can edit the script directly on the camera.

To drain the battery faster, just go to LiveView, and exit when finished (lv.start() and lv.stop()). The 60p mode will drain the battery even faster (but you can't select it from Lua yet).

You also have battery (https://builds.magiclantern.fm/lua_api/modules/battery.html).drain_rate and time_remaining (updated whenever the percentage value reported by Canon firmware changes).
Title: Work-around with camera.shoot() defeats time-out, but adds to shutter-count :-(
Post by: l_d_allan on March 28, 2018, 01:26:52 PM
Thanks for the coaching. I admit to my shame that much of it is over my head at this point.

I did use a hold-my-nose work-around to defeat the time-out ... include a camera.shoot() within the loop with 60 second sleep.

while battery.level > 90 do
    io.write(battery.level, "% ")
    io.flush()
    camera.shoot()
    msleep(60000)
end


I speculate that the existing Intervalometer functionality + advanced BULB could accomplish my objective by perhaps doing ten five-minute captures. I'll guess that would depend on how healthy or unhealthy the battery was.

I wonder if I would be yet again shooting myself in the foot to baby the battery while sacrificing the shutter.  :-\

I did briefly look around for the equivalent of beep() or blink() functionality within existing ML code, pondering if it might also work-around the time-out.

I tried to use msleep(120000) for a two minute delay within the loop, but that seemed to also time-out. I'll search around for the powersave functionality.

*** Edit ***
I did find a reference to powersave within ML .zip code, but within the binary file silent.mo. I'll scavenge around the forum.
Title: Re: Battery management: drain from high % to protect Li-ion. Prevent very low %.
Post by: Walter Schulz on March 31, 2018, 09:15:20 PM
Quote from: a1ex on March 27, 2018, 11:14:41 PM
I know how to reboot the camera on demand (camera.reboot() in Lua), but not how to turn it off.

Stupid idea (and quite convinced you thought about that before): Is it possible to fool the cam to detect compartment switches being opened?
Title: Re: Battery management: drain from high % to protect Li-ion. Prevent very low %.
Post by: a1ex on March 31, 2018, 10:07:15 PM
Tried (card switch and power switch). The problem is that shutdown decision is taken by the MPU (a black box for me, only leegong can read its code at the time of writing), and sending button codes to it doesn't exactly work (normally, the MPU sends button codes to main CPU). The result is that our main CPU does the preparations for shutdown, but then it expects some cooperation from the MPU, which doesn't happen (and, of course, nobody cuts the power).

Got some success setting the auto power off timer value to 10 seconds; however, I'd like to avoid this method, as it's a persistent setting with a nonstandard value, saved on the MPU memory, that affects the decision to turn off the camera, taken by a processor outside our control (so, a programming mistake might result in setting this timer to a very low value that would result in camera turning off before it's even fully started).
Title: Wrestling with Lua book. Thx for getting involved in this low priority request
Post by: l_d_allan on April 03, 2018, 04:43:37 PM
Quote from: l_d_allan on March 28, 2018, 01:26:52 PM
Thanks for the coaching. I admit to my shame that much of it is over my head at this point.
Our local library has a book on Lua, which I've checked out. First impression: powerful and impressive language with modern features.

Is there some kind of Canon DSLR simulator / emulator that runs on a Windows desktop computer? If so, this newbie Lua developer wouldn't have to continually take the flash card in and out of the camera and the card reader / writer. At some point, it seems like there could be some wear & tear on the camera, reader, cables, etc.

I recall "back in the day" when doing development for now-ancient Palm Pilot devices, it was much more productive to develop and test with a Palm emulator.
Title: Re: Battery management: drain from high % to protect Li-ion. Prevent very low %.
Post by: a1ex on April 03, 2018, 04:52:33 PM
Of course there is (https://www.magiclantern.fm/forum/index.php?topic=20214.0), and runs Lua scripts, too. Video tutorial (well, animated GIF) is on the todo list (like this (https://twitter.com/autoexec_bin/status/913530810686418944) - commands are pretty much the same).

It won't emulate the auto power off feature though, other than printing messages about it.

BTW, if you looked at the change log for the lua_fix experimental build, you may already noticed your source (with custom menu) will run with just a minor change (task.create (https://builds.magiclantern.fm/lua_api/modules/task.html#create) got an option to disable powersaving). However, I don't see a valid reason for turning this particular script into a "complex" one (with custom menu). Reason: it uses additional RAM while sitting in background, and possibly a few CPU cycles as well. Rather, I recommend keeping it simple (without custom menu); just launch it on demand.