What machine is this?
The G28 line is suspect as you should be in G91 for it...
I'm guessing you're on a vertical here? Odds are, the home position for ATC is actually G30.
Is the machine using a seperate EIA toolchange program? That generally explains why toolchange works in MDI (using the screen buttons) because that runs the toolchange by the ladder. In a EIA program, if someone wrote a tool change program, the typical problems are caused by the fact that safety lines or codes to home out the axes didn't get written into the program.
.... also, X isn't at home and depending on where the program is coming from (or rather where the machine is at when you read your code), it also may not tool change.