@meng LYou can determine which Task Executor is in front by checking the positions and rotations of each TE involved in the collision. Here is some custom code I added to the "Handle Collision" function of each TE in your model:
/**Custom Code*/
Object current = ownerobject(c);
Object otherobject = param(1);
treenode thissphere = param(2);
treenode othersphere = param(3);
Object thisobject = current;
int thisrotation = thisobject.attrs.spatialrz.value;
int thisxloc = thisobject.attrs.spatialx.value;
int otherxloc = otherobject.attrs.spatialx.value;
int thisyloc = thisobject.attrs.spatialy.value;
int otheryloc = otherobject.attrs.spatialy.value;
if(thisrotation > -90 && thisrotation < 90){ //Travelling right
//Stop if left of other AGV
if(thisxloc < otherxloc){
thisobject.stop(48);
senddelayedmessage(thisobject,0.5,thisobject);
}
} else if (thisrotation > 90 || thisrotation < -90){ //Travelling left
//Stop if right of other AGV
if(thisxloc > otherxloc){
thisobject.stop(48);
senddelayedmessage(thisobject,0.5,thisobject);
}
} else if (thisrotation > 0 && thisrotation < 180){ //Travelling up
//Stop if below other AGV
if(thisyloc < otheryloc){
thisobject.stop(48);
senddelayedmessage(thisobject,0.5,thisobject);
}
} else { //Travelling down
//Stop if above other AGV
if(thisyloc > otheryloc){
thisobject.stop(48);
senddelayedmessage(thisobject,0.5,thisobject);
}
}
This uses the rotation of the current TE to determine in which direction it is moving. Then, by checking its position and the position of the other TE in the collision, it can determine whether it is behind the other TE. If it is behind the other TE, it will stop.
Here is your model with this code added in: 001answer.fsm
Try changing the speeds of the two TEs so that TaskExecuter1 is faster than TaskExecuter2, and you will see that it works no matter which TE is behind.
When applying this to your model, there may still be problems when the TEs go around corners or sharp turns, but I would just adjust the shape of the collision sphere so that they detect other TEs in front of them and not to the sides.