First version of the pid code for the balance function (Alis Rodriguez Manso)