Automating rotation of a block on a track
Here's an idea for converting the movement of a moving train to rotational motion track side. This is shown in set 6991:

The mechanism in this case is a tire hanging from a universal joint so that it engages the train without interfering too much:


This core idea could be mechanically adapted for other purposes, such as the 180 degree rotation that you are looking for.