Battery state machine.
Merged in several branch files.