Animation by scripting
Consider inserting more keyframes so you have more options to change directions.  Perhaps you would need to calculate intermediate turning locations such as locations on a simplified half ellipse to avoid an obstacle.

frame_num += 30 would become

frame_num += 5 and other python changes
Consider a [Follow Path Constraint] whose offset and influence can be animated via keyframe.  Perhaps left and right turn constraints.