Lead-acid battery charging and discharging algorithm