MODULE BWTransfer
    ! BenchWorkSoftware | Inspection and sorting cell
    ! The PLC owns the gripper, inspection decision and part counts.
    ! Station order: inspection, pallet, reject bin, home, infeed.
    PERS wobjdata wStudy := [FALSE,TRUE,"",[[450,0,200],[1,0,0,0]],[[0,0,0],[1,0,0,0]]];
    CONST robtarget pStation{5} := [
        [[-157,20,62],[0,1,0,0],[0,0,0,0],[9E9,9E9,9E9,9E9,9E9,9E9]],
        [[130,-84,51],[0,1,0,0],[0,0,0,0],[9E9,9E9,9E9,9E9,9E9,9E9]],
        [[114,102,38],[0,1,0,0],[0,0,0,0],[9E9,9E9,9E9,9E9,9E9,9E9]],
        [[43,-48,190],[0,1,0,0],[0,0,0,0],[9E9,9E9,9E9,9E9,9E9,9E9]],
        [[-115,95,22],[0,1,0,0],[0,0,0,0],[9E9,9E9,9E9,9E9,9E9,9E9]]];

    PROC main()
        VAR num route;
        WHILE TRUE DO
            ! Accept a station number and advertise the target.
            WaitUntil GInput(giRequest) <> 0;
            route := GInput(giRequest);
            SetDO doRobotBusy,1;
            SetGO goTarget,route;

            ! Stop at the station and wait for arrival feedback.
            MoveJ pStation{route},v100,fine,tool0\WObj:=wStudy;
            WaitDI diAtTarget,1;

            ! Reply with the same number. The PLC clears the request.
            SetGO goRouteReply,route;
            WaitUntil GInput(giRequest) = 0;

            SetGO goTarget,0;
            WaitDI diAtTarget,0;
            SetGO goRouteReply,0;
            SetDO doRobotBusy,0;
        ENDWHILE
    ENDPROC
ENDMODULE
