# Delay urscript execution

**URL:** <https://forum.universal-robots.com/t/delay-urscript-execution/22244>\
**Category:** URScript\
**Created:** [June 27, 2022, 4:20am UTC](https://forum.universal-robots.com/t/delay-urscript-execution/22244 "2022-06-27T04:20:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![francis](https://avatars.discourse-cdn.com/v4/letter/f/ac8455/32.png) [@francis](https://forum.universal-robots.com/u/francis)\
**Post date:** [June 27, 2022, 4:20am UTC](https://forum.universal-robots.com/t/delay-urscript-execution/22244/1 "2022-06-27T04:20:00Z")

</div>

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.

```auto
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)

```

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/9/9d072073cd73d8c6e3da5047c7b83bcbe5b22bb0.png)

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.

---

<div class="post-metadata">

**Author:** ![cags](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/cags/32/6304_2.png) [@cags](https://forum.universal-robots.com/u/cags)\
**Post date:** [June 27, 2022, 8:08am UTC](https://forum.universal-robots.com/t/delay-urscript-execution/22244/2 "2022-06-27T08:08:15Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/e/eb8898dc3695ecc8da3c66f20d38eb38f956d196.png)

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/8/86190d8187234dd09c4cfe51f6b835902a5da568.png)

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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/f/fbef5133a335a9cc9b736c590d243f2560af0cad.png)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![francis](https://avatars.discourse-cdn.com/v4/letter/f/ac8455/32.png) [@francis](https://forum.universal-robots.com/u/francis)\
**Post date:** [June 28, 2022, 12:06am UTC](https://forum.universal-robots.com/t/delay-urscript-execution/22244/3 "2022-06-28T00:06:12Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![cags](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/cags/32/6304_2.png) [@cags](https://forum.universal-robots.com/u/cags)\
**Post date:** [June 28, 2022, 1:32pm UTC](https://forum.universal-robots.com/t/delay-urscript-execution/22244/4 "2022-06-28T13:32:10Z")

</div>

Hej, I’m glad for helping 😃

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.

---

<div class="post-metadata">

**Author:** ![francis](https://avatars.discourse-cdn.com/v4/letter/f/ac8455/32.png) [@francis](https://forum.universal-robots.com/u/francis)\
**Post date:** [June 29, 2022, 5:27am UTC](https://forum.universal-robots.com/t/delay-urscript-execution/22244/5 "2022-06-29T05:27:35Z")

</div>

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.
