An optimization based approach to autonomous drifting