Control of electric machines