When to use sync()

Hi,
I’m not quite sure what exactly sync() does. I know in the manual it states that it takes up the remaining physical time a thread has in the current frame, but I don’t know when to use it.

I haven’t really been using threads yet so that’s probably why, but would I need a sync() in my “generateScript(ScriptWriter writer)” if the commands I run are:
-socket_open
-socket_send_string
-and variable assignments involing mathematical calculations (eg. “X=(Y+Z-3)/R”)

1 Like

The way we think about using it is when I have commands in a loop for instance that take effectively no time from the cpu to process. So if I had a loop that was simply assigning some value to a variable, this would effectively not take any actual clock time and so I would get an error stating that I had not instructed the robot what to do and basically have a situation where the loop gets out of sync with the rest of the code.

If you have something like a socket command where there is physical time taken up, network latency, you should not have any issue. The main area where we use them is if I have a loop that is continuous and inside that loop I have an if statement that only occasionally gets invoked. At the end of the loop (inside) I will place a sync() or a sleep(0.01) or something that takes physical time to perform.

The system is really quick to throw an error when you have a case that requires this.

Easiest way to see the effect of this is to write the following program in Polyscope

Make sure the program is set to Loop Always and then add an if statement with a popup under the if statement, doesn’t matter what the message is. For the condition of the if statement make it False

so basically

Program
if (False)
popup(“say something”)

Run that program, then after the if (outside) add a script command of sync()

Program
if (False)
popup(“say something”)
sync()

Hope that helps you understand it better

4 Likes

ahh I think I understand now, so basically If you have moves, socket commands or anything that takes up actual physical time, you don’t really need sync();

The reason I asked was because I didn’t have any sync() in my URCap that performed a bunch of variable assignments and socket communication. My URCap took a snapshot of a platform with a object and sent that objects x,y pixel position to the Robot (Also applying some ratio calibration & offset in the “generateScript()” of the ProgramNode) I normally transfer the URCap file onto the tablet and restart the robot which usually always works fine. However, the previous time I did this (while also deleting the previous(old) version of the urcap) and I ran the robot, it would complete the program node of the URCap and after a move the robot just went to a complete stop and turned off. I thought it was because I was missing a sync().

But It might be because I didn’t completely remove the old URCap when I ran it (Like kept the old URCaps program node in the program even after I uninstalled the old URCap because I assume the new URCap would just overwrite it). That may just have been the problem because after removing the node and re adding it, the problem seem to have stopped.