Good day @Nik,
I'd guess what you are looking for is some kind of object recognition (as in I assume you'd go for a camera + image recognition). I found some examples (separated links) you might find useful, but I'd say you'll have to work this out on your own to some extent, as it's quite specific.
Don't know if it's of any help to you, but I'd suggest focusing on detecting both rails and trying to force the copter to be between them while moving forward. A stabilized "camera" pointing downwards seems like the best option for that.
I'd say using the sonar for this will be quite difficult, as the readings on the rails' model might not be enough to detect it's center. You could just differentiate the ground from the rails, but this would only work inside the simulation. If you want something that can be implemented in the real world, I'd go with the camera. If you only want this for the simulation simply checking for anything over the ground could be sufficient.