Delay urscript execution

I have searched around and found out that the script generated by the function generateScript() is run immediately after pressing the play button on polyscope.
What I want to accomplish is run my script between two waypoint nodes inside a single movej.
Is it possible to delay the execution of urscript up to this point?

I have tried an infinite loop while a variable is 0, then turn the variable to 1 at the moment I want the script executed.

while callServoJFlag == 0:
#do nothing
 end

def pathRecordMove(i):
 global j=0
 q1=p[0.4,0.4,0.0,0.0,3.14159,0.0]
 q2=p[0.5,0.4,0.0,0.0,3.14159,0.0]
 q3=p[0.6,0.4,0.0,0.0,3.14159,0.0]
 q4=p[0.7,0.4,0.0,0.0,3.14159,0.0]
 q5=p[0.8,0.4,0.0,0.0,3.14159,0.0]
 q6=p[0.8,0.5,0.0,0.0,3.14159,0.0]
 array=[q1,q2,q3,q4,q5,q6]
 while j < i:
  global joint_pos=get_inverse_kin(array[j])
  servoj(joint_pos, 0, 0, 0.05, 0.1, 300)
  global j =j+1
  sync()
  end
 end

pathRecordMove(6)

result: as if callServoJFlag is never set to 0, pathRecordMove(6) gets called at the beginning of the program execution

I would greatly appreciate any help or advice. Thank you.

Your script is executed at the start because you define the pathRecordMove function and then just call it.
What I would do is to put the def pathRecordMove(i) in a single script at the BeforeStart, so it is defined from the beginning and it will compile without errors.

Then, you can use two approaches for calling the function:

  1. Use an IF statement with “Check expression continuously”. So when the condition happens, it will execute. I am personally not that familiar with this feature from Polyscope, but I have seen in this forum that it was the answer to some topics.

  1. Use a parallel thread that runs continuously and has a wait or IF statement to call the function.

Hope this helps!

1 Like

@cags Thank you for the answer.
I declared the function in the script and just called it between the two nodes and it worked!

I’ll be marking this as the solution.

After implementing that, though, I still have two more problems if you could kindly share some of your insight.

  1. I will be needing to generate the pathRecordMove call via URCaps and I think ScriptNode creation is currently not supported. Any suggestions how to implement this?
  2. I have several servoj inside pathRecordMove and, without a wait node, it immediately executes the next movej. How can I make such that the next movej wait until all servoj are finished executing.

Hej, I’m glad for helping :smiley:

I am not 100% sure about those issues, but to give it a try:

  1. What do you mean “generate the pathRecordMove”? You mean the content of the function? You can only define a function, but at least you can use different global variables or multiple input arguments to make the trajectory generation.
  2. Use the stopj() command. I have seen it around in order solutions that implement motions on the robot.

cgs, thanks again for your time answering.

  1. I mean the script node containing the function call of pathRecordMove. In Java, I can generate the Move and Waypoints nodes but the generation of Script node is currently not supported.
  2. This wasn’t a problem in the first place, there was just user error.