Battery management: drain from high % to protect Li-ion. Prevent very low %.

Started by l_d_allan, March 27, 2018, 10:40:22 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

l_d_allan

    Please consider features related to "battery management"

    • Drain battery to [fill-in-the-blank] percent if over that state-of-charge (SOC).
      My understanding is that to "baby" Li-Ion batteries for longer life, they prefer to be stored at a SOC significantly less than 100%.
    • My K.I.S.S. practice has been to return from a photo event, upload the photos, and plug the batteries used into one or more chargers.
    • More than a few times, this non-professional doesn't take photos for several days or weeks, which is somewhat hard on the battery to sit at high SOC.
    • ...
    • Related feature request:
    • On/Off toggle to not allow battery to be in sleep state with the Power switch ON to go below [fill-in-the-blank] percent.
    • Can happen with Time-Lapses, which can also corrupt the SD card. Why do I know that?  :o
    • The intent is to diminish the occurrences of the battery getting to fully drained status.
      That can also be somewhat hard on the battery, especially if the battery stays at 0% for an extended period of time.
    • ...
    • Corner cases probably make this more complex than I realize.
      Wish this rusty embedded developer (not ARM) had the skills with the ML tool-chain to help implement, but alas, not. Mea culpa.
    • ...
    • A model to perhaps serve as a baseline to reverse engineer:
      My recently acquired Dell laptop has built-in Windows-10 battery management. It seems quite a bit improved since the last time it played with it with Win-7.
      It also has a Dell-specific app for somewhat more advanced battery management capabilities.

    Sorry if something equivalent has already been requested ... or if this rusty ML fan-boy isn't aware that it has already been implemented.

a1ex

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.

l_d_allan

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).

l_d_allan

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 started by dmilligan. I then did a Reply with your proposed Lua loop.

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

l_d_allan

Crude PoC 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.

a1ex

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.drain_rate and time_remaining (updated whenever the percentage value reported by Canon firmware changes).

l_d_allan

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.

Walter Schulz

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?

a1ex

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).

l_d_allan

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.

a1ex

Of course there is, and runs Lua scripts, too. Video tutorial (well, animated GIF) is on the todo list (like this - 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 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.