How to control the charging of batteries