Set Center of rotating in top center of object(Rope),
we need variable, which indicates the direction of movement («1» - clockwise, «-1» (or "0") - counterclockwise).
At beginning scene our pendulum moves clockwise;
Each Moment of execution, we check direction — CW or CCW;
if Pendulum rotates clockwise — the rotation Speed is positive, otherwise - negative.
then check the Position, and if the Pendulum is at an extreme Point - change Direction.
In File №2 added the object to a rope and fixed rotation.
P.S. This Example without any physics...