Energy storage charging and discharging control