Self-tuning PSM controller based on state machine