Sync() uses up the remaining cycle time on the processor, so the reason why it is 8ms in the CB3 is due to the communication frequency of 125 Hz, and in the E-series the 500 Hz communication frequency, making it 2 ms if used with nothing else.
In a lot of How-Tos, they recommend you to add Wait in a thread if it is only for reading values, like force etc. But the best would be to use the sync() function instead, to take up the rest of the cycle time of the “frame”, 8 ms on CB and 2 on the E-series
But this is basically written in the script manual, with different wording. Also with much more info under 1.8.2 Thread scheduling in the script manual
Edit addition:
An example, if you would use
Thread_1
Wait 0.003
sync()
then sync() should take 5 ms and then loop the thread again, having the thread loop every frame (communication cycle), every 8 ms.
I have spawned a thread like this. The code assumes that it is 8 ms for sync() function. It is true only for CB. How do we calculate the exact time taken for sync() call irrespective of the controller that is used?
Thread_1
while(true)
sync()
count = count + 1
if (count == 125)
seconds = seconds + 1
end
end
end
With that function that @jbm wrote, I tried to do a counter from the GUI, and it seems to be fine, here is the auto-generated script
def StepTimeCounter():
# All the set and URCap functions are removed
global StepTime=get_steptime()
global Count=0
global TimeS=0
while (True):
sync()
global Count=Count+1
if (Count == (1/StepTime)):
global TimeS=TimeS+1
global Count=0
end
end
end
Another way is just
def StepTimeTimer():
global StepTime=get_steptime()
global Count=0
global TimeS=0
while (True):
sync()
global TimeS=TimeS+StepTime
end
end
Which is basically the same as the UR builtin Timer (From version 3.7). Create one of those timers in the GUI and read the script how they have built it, you can see they use the function get_steptime()