"Realtime" over the RS232 port is often an oxymoron. I'm not going to say it's impossible, but it's definitely questionable. I've seen near-realtime RS232 operations done using the SPS, but only in applications that could tolerate the occasional "hiccup" in communication.
There is a way to interrupt robot program execution and re-direct motion on the fly, using Interrupts. However, this is a "chunky" method -- if you're trying to chase a moving target, you're going to have problems doing it this way. If the target is moving in a straight line at a constant speed, such that you can interrupt the robot's motion and send it on a pursuit course, this may be workable. But if you're thinking in terms of constantly updating the robot motion in near-realtime, I have to say I don't think it's going to work very well. Maybe if the target was moving slowly.
The
best way to handle an application of this kind is to buy the KUKA RSI tech package, which is intended exactly for the kind of thing you're doing -- realtime control of robot position and velocity by an external sensor. It won't work with RS232, because RS232 isn't timing-determinant, but the latest versions will work with streaming TCP/IP data.
However, if you can't afford that, my next-best advice is as follows:
1: don't go opening/closing the RS232 channel in the SPS, this causes timing lags. Open it on a request from the robot main program and keep it open until the main program signals that the operation is over.
2: Global variables are, yes, the best way to communicate between the SPS and the main program.
3: if you can send "delta" corrections to the robot (that is, "from your current position, move +x, +y, +z"), that should make things easier.
4: Try to buffer at least 2 corrections ahead of the robot. Maybe feed a looping array of positions. I would avoid using interrupts in the robot program unless you don't need smooth on-the-fly updating, as noted above.
5: If you want smooth robot motion, you have to keep $ADVANCE higher than 0, but anything higher than 1 will cause other problems, with the robot trying to plan motion more than 1 move ahead. Factory default is 3.
My off-the-cuff program would look something like this:
WHILE $IN[50] DO ; input to keep running
FOR i = 1 TO 3
PTP_REL MoveBuffer[i] C_PTP ; "ring" buffer of relative moves filled by SPS, SPS must keep at least one full move ahead
ENDFOR
ENDWHILE
Speed will be an issue -- the faster you go, the bigger and more "chunky" the corrections will be.