Universal Robots+

Optimal use of sync() function in nested loops

Hi,

Our URScript program is doing some heavy calculations in nested loops, which then causes protective stop with the error message “Runtime is too much behind”. Using sync() in the innermost loop would result in unacceptable high delays, but on the other hand, using sync() in the outer loop(s) is not predictable, and cannot avoid the runtime error 100% sure.

I have tried to create a helper thread with a counter, and use sync() in the main thread only when the counter changes. But this does not help much.

Is there any working example with sync() that utilize the available time frame of the thread most efficient without getting the runtime error?

A simple diagram of the thread scheduler algorithm would be also useful.

Thanks,
Csaba

UPDATE: the runtime error seems to be more frequent on E-series (tested with Polyscope versions 5.7 and 5.8) and cannot be reproduced on a CB3.1 robot with Polyscope 3.12 so far.

Hello @csaba,

Interesting this is only seen on the E-series. I would guess this is because of the increased signal frequency which means that sync() would only sleep for a max of .002s.

I’d be curios if you kept the sync() in the outer loop but replaced it with a sleep() command instead what the results would be. Let me know the behavior if you used .008 sec wait instead of sync to see if it runs similar to the CB3.1 version you are testing on.

1 Like

UPDATE: I was able to create a solution that seems to work acceptable in the inner loop, interrupting the calculations after a specific elapsed time. See the following example program below. The values are experimental.

global lastTick = 0
thread tickThread():
  while (True):
    sleep(0.008)
    lastTick = lastTick + 1
  end
end
run tickThread()

thread myWorkerThread():
  lastSync = lastTick
  i = 0
  while (i < 500):
    j = 0
    while (j < 500):
      # placeholder for heavy calculations
      p = pose_inv( p[0.1, 0.1, 0.1, 0.1, 0.1, 0.1] )
      b = is_within_safety_limits(p)
      s = str_cat(i, j)
      j = j + 1
      # interrupt the heavy thread periodically -->
      if (lastTick - lastSync >= 2):     # experimental value 2
        sleep(0.03)     # experimental value 0.03
        lastSync = lastTick
      end
      # <--
    end
    i = i + 1
  end
end
run myWorkerThread()

Note: on CB3.1 no sleep or sync is needed in the inner loop at all.

Thanks to @inu for the good suggestion to use sleep insted of sync.

2 Likes

Did you see the latest version of Polyscope’s Release Notes? URScript now supports matrix and scalar:

*MATRIX AND SCALAR OPERATION SUPPORT IN SCRIPT

It is now possible to assign a matrix to a variable as well as do matrix math like multiplication, transpose and inverse. In addition scalar operation are also added with support for both Matrix and List.

You can read more here:
https://www.universal-robots.com/articles/ur/release-note-software-version-591/

Would you be interested in testing this with you code, if it is a good fit?

Hi @CHSA, thank you for the information. We’ll review our code and try to use matrix operations where possible.

great, thank you @csaba. We are interested in your feedback, so please report back when you have time to try it!