Model trains use DC motors. There is a transformer down to low voltage AC, then a rectifier and a polarity
switch for reversing and some sort of power control (in olden times a rheostat I believe). The DC is not smoothed, its half-cycles, typically, and thyristor control is probably common.
Have you searched for "arduino model train controller"
Traditional Märklin used a centre studded power "rail" in the sleepers (so it was not an obvious third rail) with a long "shoe" under the loco. Not entirely practical and they have apparently since gone to simple two rail.
Whatever they use now - and I have not studied it - they originally used AC with an over-voltage pulse to toggle the reversing relay in the locomotive itself (since you can of course, not "reverse" the AC polarity ).