How-to: Stitch together your own battery power manager

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
0xfb

How-to: Stitch together your own battery power manager

Post by 0xfb »

This is how I stitched together my own battery charge/discharge manager...

The Setup
I live on a restricted budget and can only buy second hand laptops. Due to this I don't really have the benefits of a healthy battery and so, for the last four years I've used a laptop without a battery. Two weeks ago my trusty laptop, a ThinkPad T60, made its last proper shutdown. Dependent on access to a computer as I am, I needed to get a new one as fast as possible. Long story short, today I'm the proud owner of a ThinkPad X230. My very first laptop was a ThinkPad and every one since. I won't change that voluntarily.

My new trusty steed has a relatively new battery and my battery profile is to preserve its life as long as possible. As always when a pain arises; search and you shall find. If you are of the tinkering/hacking kind running Linux you are truly your own master of your computer. Only the leftmost bit is the limit.

What I Needed
To recap. What I wanted was to preserve my battery's lifespan as long as possible. Note that I don't mean battery life between charges which can be achieved by dimming the screen, turning off Bluetooth, spinning down the HD, etc., but long term battery lifespan.

I had heard of "Intelligent" power managers, but had not paid much attention since it was always in a Windows context. Lenovo have a Power Manager as part of their Windows pre-installed software package ThinkVantage Technologies [[url=http://en.wikipedia.org/wiki/ThinkVantage_Technologies]1[/url]]. Not very useful when you have a strong preference for Linux.

What I Had
The BIOS of the X230 has a hardware power manager including operating on battery or AC [2]. Not exactly what I wanted, but probably not bad.
It's not that I distrust Lenovo on its power saving capabilities, in my case however trust is good, but control is better.

Search
What I found was something called TLP, an advanced power manager for Linux [[url=http://linrunner.de/en/tlp/tlp.html]3[/url]]. It has up to 22 settings and functions based on your power source, kernel version etc. It has battery charge thresholds and a recalibrate battery function as well, but only for Lenovo ThinkPads.
The TLP site contained links to some reviews that I looked through which served as a great how-to summary [[url=http://www.webupd8.org/2013/04/improve-power-usage-battery-life-in.html]4[/url],[url=http://refugeeks.com/use-tlp-to-optimize-the-power-consumption-in-ubuntu/]5[/url]].

I had found a great FAQ post on the Lenovo forum, as well, about extending battery life and decided that that was my goal; to keep my battery charged between thresholds so as to prolong its lifespan [[url=http://forums.lenovo.com/t5/Welcome-FAQs-Knowledge-Base/How-can-I-increase-battery-life-ThinkPad/ta-p/244800]6[/url]].

Approach
TLP prerequisites asked me to:
  • Please remove user specific power save settings and scripts (for example in /etc/rc.local), otherwise expect unpredictable results
  • Please remove laptop-mode-tools
I found that my /etc/rc.local was empty, so that was simple. Not knowing what laptop-mode-tools was I found the developer page [[url=http://samwel.tk/laptop_mode/]7[/url]] and a post at askubuntu.com asking if laptop-mode-tools is still relevant [[url=http://askubuntu.com/questions/172391/is-laptop-mode-tools-still-relevant-for-12-04-and-the-3-x-kernels]8[/url]]. The first, and accepted best answer, contains some good links on power management. However. I found that TLP, laptop-mode-tools and Ubuntu/LinuxMint (/usr/lib/pm-utils/power.d/) have intersecting power savings options to some degree. Great, that meant that I could concentrate on the battery. As a precaution I turned off power savings in the BIOS. TLP can do much the same.

I followed the TLP installation instructions for Linux Mint 13 -- 15 (not LMDE, see the Debian section). You'll find it under the Ubuntu 12.04 -- 13.10 section.
That done, I still needed to control when to charge and discharge the battery as mentioned in the Lenovo FAQ post. I had found tpacpi-bat which exposes ACPI interface for battery controls [9].

NB. Be sure to read the TPL Battery Charge Thresholds, especially the hints [[url=http://linrunner.de/en/tlp/docs/tlp-configuration.html#chargethresholds]10[/url]].

/etc/default/tlp contains all the TLP settings. Edit this as root to set start and stop thresholds.
I'm running as a normal user and tpacpi-bat requires write permission on /proc/acpi/call for force discharge to work.
In my /etc/rc.local I'm calling a bash script, in the background (&), which performs chmod 666 /proc/acpi/call.

I could create a cron job calling a script that managed the battery charge/discharge. But as I mentioned before, in my case trust is good, but control is better. I wanted to visually confirm that the battery was charged/discharged accordingly. Especially during initial testing this was important.
I've had a long love/hate relationship with Conky [[url=http://conky.sourceforge.net/]11[/url]]. I love the capabilities that it gives me, but hate the markup syntax and the, at times, meager documentation. Anyways. Conky provides a texeci "variable" that can run a command at an interval inside a thread. With this, I call a python script once every 60 seconds that:
  • gets the start and stop thresholds of the battery,
    tpacpi-bat -g ST 1
    tpacpi-bat -g SP 1
  • calculates how many percent my battery is charged from
    /sys/class/power_supply/BAT0/energy_full
    /sys/class/power_supply/BAT0/energy_now
  • get the battery status
    /sys/class/power_supply/BAT0/status
Based on these informations I then set up three conditionals for when to take action:
NB. Pseudocode

Code: Select all

if (battery_percentage >= stop_threshold and (status == Charging or status == Unknown)):
  force_discharge(True)
elif (battery_percentage < start_threshold and (status == Discharging or status == Unknown)):
  force_discharge(False)
elif (battery_percentage > start_threshold and battery_percentage < stop_threshold and status == Unknown):
  force_discharge(True)
The third condition is useful for when starting up the computer. I find that my battery is typically in an unknown state, plugged in or not. The check for Unknown status in the first two conditions are just precautions since I do not know what causes the battery to be in an unknown state and how I can avoid it.

Using Conky I then read the battery status and display it on the desktop along with the current battery charge percent. That's great for test and debugging. I'm starting Conky via a call to a bash script in the Startup Applications with an initial sleep of 10 seconds to ensure that my system is up and running. I might move the python script scheduling to cron later if I find it more convenient.


That's all folks. Hope you found it useful in some way.

Cheers!


My hardware setup:
ThinkPad X230, one main 6 cell battery
Linux Mint 15, 64-bit, kernel 3.8.0-32


[1] [url]http://en.wikipedia.org/wiki/ThinkVantage_Technologies[/url]
[2] See the ThinkPad X230 User Guide chapter 8 on, Advanced configuration / ThinkPad setup, for what the BIOS offer on power saving capabilities.
[3] [url]http://linrunner.de/en/tlp/tlp.html[/url]
[4] [url]http://www.webupd8.org/2013/04/improve-power-usage-battery-life-in.html[/url]
[5] [url]http://refugeeks.com/use-tlp-to-optimize-the-power-consumption-in-ubuntu/[/url]
[6] [url]http://forums.lenovo.com/t5/Welcome-FAQs-Knowledge-Base/How-can-I-increase-battery-life-ThinkPad/ta-p/244800[/url]
[7] [url]http://samwel.tk/laptop_mode/[/url]
[8] [url]http://askubuntu.com/questions/172391/is-laptop-mode-tools-still-relevant-for-12-04-and-the-3-x-kernels[/url]
[9] https://github.com/teleshoes/tpacpi-bat
[10] [url]http://linrunner.de/en/tlp/docs/tlp-configuration.html#chargethresholds[/url]
[11] [url]http://conky.sourceforge.net/[/url]
Post Reply

Return to “Tutorials”