The logic of tool changer is almost simple.
set spindlerun, 0; !make stop spindle
waitinput isspindlerun; !wait until spindle is stop
waitinput tool5, 1;!cheching the tool on toolchanger station, you can check this signal "if" depends on your algorithm and program structure
waitinput tool3, 0;!checking place position is empty of tool on spindle
movej
movej
movel
set spindleopen, 1;
movel
movel
movel
set spindleopen, 0;
movel
movej
movej
This is the basic logic of tool change but if you have any specific control or safety on your system you can insert.