# Command to stop only a sub program

**URL:** <https://forum.universal-robots.com/t/command-to-stop-only-a-sub-program/5243>\
**Category:** URScript\
**Created:** [October 1, 2019, 2:14pm UTC](https://forum.universal-robots.com/t/command-to-stop-only-a-sub-program/5243 "2019-10-01T14:14:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marvin.kaphengst](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/marvin.kaphengst/32/1246_2.png) [@marvin.kaphengst](https://forum.universal-robots.com/u/marvin.kaphengst)\
**Post date:** [October 1, 2019, 2:14pm UTC](https://forum.universal-robots.com/t/command-to-stop-only-a-sub-program/5243/1 "2019-10-01T14:14:07Z")

</div>

Hello everybody,

I am working with an SPS and an UR10 and I made the mistake and used a switch-case statement as a menu that can be controlled via the integer register and from there I start sub programs. This was working fine until I wanted to stop a sub program and go back to the main program (which is waiting for a special input boolean register in a loop). The only thing I can do is to use ‘halt’ which stops the whole program. But I only want to go out of the sub program. I know, if I used functions (instead of sub programs) it would have been possible with the return command. But is there also a way to do it with sub programs?

Thanks in advance

Marvin

---

<div class="post-metadata">

**Author:** ![m.birkholz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/m.birkholz/32/7084_2.png) [@m.birkholz](https://forum.universal-robots.com/u/m.birkholz)\
**Post date:** [October 1, 2019, 8:57pm UTC](https://forum.universal-robots.com/t/command-to-stop-only-a-sub-program/5243/2 "2019-10-01T20:57:08Z")

</div>

It should be fine using functions or threads with return statements. Just to be sure i understood this right: Can you post your generated urscript?

---

<div class="post-metadata">

**Author:** ![marvin.kaphengst](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/marvin.kaphengst/32/1246_2.png) [@marvin.kaphengst](https://forum.universal-robots.com/u/marvin.kaphengst)\
**Post date:** [October 2, 2019, 6:12am UTC](https://forum.universal-robots.com/t/command-to-stop-only-a-sub-program/5243/3 "2019-10-02T06:12:13Z")

</div>

Okay, now that I had a look into the gernerated script it appears to me that sub programs are nothing else than functions that are getting called so I will test the “return” statement. Lets see if this works. The code (simplified a bit) can be found below:

```
def MainProgram():
  set_standard_analog_input_domain(0, 1)
  set_standard_analog_input_domain(1, 1)
  set_tool_analog_input_domain(0, 1)
  set_tool_analog_input_domain(1, 1)
  set_analog_outputdomain(0, 0)
  set_analog_outputdomain(1, 0)
  set_tool_voltage(12)
  set_input_actions_to_default()
  set_tcp(p[-0.00126,-0.30352999999999997,0.054009999999999996,1.5707,0.0,0.0])
  set_payload(8.5, [0.0, 0.15, 0.05])
  set_gravity([0.0, 0.0, 9.82])
  rtde_set_watchdog("speed_slider_mask", 10.0, "pause")
  rtde_set_watchdog("input_bit_registers0_to_31", 10.0, "pause")
  rtde_set_watchdog("input_bit_registers32_to_63", 10.0, "pause")
  def subProgram1():
    sleep(0.01)
  end
  def subProgram2():
    write_output_boolean_register(0, False )
    popup("Camera gets triggert", "Meldung", False, False, blocking=True)
    write_output_boolean_register(0, True )
    sleep(0.1)
    while (not(read_input_boolean_register(2) == True )):
      sync()
    end
    sleep(0.1)
    write_output_boolean_register(0, False )
    popup("Move to hole", "Warnung", True, False, blocking=True)
    p_hole=p[-0.15258,-0.71954,0.32050,3.1415,0.0,0.0]
    movel(p_hole, a = 0.01, v = 0.01, t = 0, r = 0)
  end
  def subProgram3():
    sleep(0.01)
  end
  def subProgram4():
    sleep(0.01)
  end
  def subProgram5():
    sleep(0.01)
  end
  $ 72 "Thread_1"
  thread Thread_1():
    while (True):
      global IN_boolReg_0to7=[read_input_boolean_register(0), read_input_boolean_register(1), read_input_boolean_register(2), read_input_boolean_register(3), read_input_boolean_register(4), read_input_boolean_register(5), read_input_boolean_register(6), read_input_boolean_register(7)]
      global OUT_boolReg_0to7=[read_output_boolean_register(0), read_output_boolean_register(1), read_output_boolean_register(2), read_output_boolean_register(3), read_output_boolean_register(4), read_output_boolean_register(5), read_output_boolean_register(6), read_output_boolean_register(7)]
      global IN_floatReg_0to7=[read_input_float_register(0), read_input_float_register(1), read_input_float_register(2), read_input_float_register(3), read_input_float_register(4), read_input_float_register(5), read_input_float_register(6), read_input_float_register(7)]
      global OUT_floatReg_0to7=[read_output_float_register(0), read_output_float_register(1), read_output_float_register(2), read_output_float_register(3), read_output_float_register(4), read_output_float_register(5), read_output_float_register(6), read_output_float_register(7)]
      global IN_intReg_0=read_input_integer_register(0)
      global OUT_actual_tcp=get_actual_tcp_pose()
      sync()
    end
  end
  threadId_Thread_1 = run Thread_1()
  while (True):
    $ 1 "Roboterprogramm"
    $ 2 "TESTâ‰” True "
    global TEST= True  
    $ 3 "Schleife read_input_boolean_register(4)â‰Ÿ False "
    thread Thread_while_3():
      while (True):
        $ 4 "write_output_boolean_register(2, True )"
        write_output_boolean_register(2, True )
        $ 5 "write_output_boolean_register(3, False )"
        write_output_boolean_register(3, False )
        $ 6 "write_output_boolean_register(6, False )"
        write_output_boolean_register(6, False )
        $ 7 "sync()"
        sync()
      end
    end
    if (read_input_boolean_register(4) == False ):
      global thread_handler_3=run Thread_while_3()
      while (read_input_boolean_register(4) == False ):
        sync()
      end
      kill thread_handler_3
    end
    $ 8 "write_output_integer_register(0, read_input_integer_register(0))"
    write_output_integer_register(0, read_input_integer_register(0))
    $ 9 "sps_choice=read_input_integer_register(0)"
    sps_choice=read_input_integer_register(0)
    $ 10 "Script: floatRegMirror.script"
    write_output_float_register(0, read_input_float_register(0))
    write_output_float_register(1, read_input_float_register(1))
    write_output_float_register(2, read_input_float_register(2))
    write_output_float_register(3, read_input_float_register(3))
    write_output_float_register(4, read_input_float_register(4))
    write_output_float_register(5, read_input_float_register(5))
    write_output_float_register(6, read_input_float_register(6))
    write_output_float_register(7, read_input_float_register(7))
    IN_floatReg_0to7=[read_input_float_register(0), read_input_float_register(1), read_input_float_register(2), read_input_float_register(3), read_input_float_register(4), read_input_float_register(5), read_input_float_register(6), read_input_float_register(7)]
    $ 11 "write_output_boolean_register(3, True )"
    write_output_boolean_register(3, True )
    $ 12 "write_output_boolean_register(2, False )"
    write_output_boolean_register(2, False )
    $ 13 "write_output_boolean_register(5, True )"
    write_output_boolean_register(5, True )
    $ 14 "counter1â‰”0"
    global counter1=0
    $ 15 "Schleife read_input_boolean_register(6)â‰Ÿ True "
    while (read_input_boolean_register(6) == True ):
      $ 16 "If read_input_float_register(5)â‰Ÿ True or counter1>9"
      if (read_input_float_register(5) == True or counter1>9):
        $ 17 "break"
        break
      end
      $ 18 "Warten: 0.5"
      sleep(0.5)
      $ 19 "counter1â‰”counter1+1"
      global counter1=counter1+1
    end
    $ 20 "If (read_input_boolean_register(5)â‰Ÿ True and read_input_boolean_register(6)â‰Ÿ False ) or counter1>9"
    if ((read_input_boolean_register(5) == True and read_input_boolean_register(6) == False ) or counter1>9):
      $ 21 "continue"
      continue
    end
    $ 22 "write_output_boolean_register(6, True )"
    write_output_boolean_register(6, True )
    $ 23 "Switch sps_choice"
    switch_1 = sps_choice
    if (0 == switch_1):
      $ 25 "Meldung"
      popup("Case 0", "Meldung", False, False, blocking=True)
    elif (1 == switch_1):
      $ 27 "Meldung"
      popup("Case 1", "Meldung", False, False, blocking=True)
      $ 28 "If TESTâ‰Ÿ True "
      if (TEST == True ):
        $ 29 "continue"
        continue
      end
      $ 30 "Aufruf subProgram1"
      subProgram1()
    elif (2 == switch_1):
      $ 32 "Meldung"
      popup("Case 2", "Meldung", False, False, blocking=True)
      $ 33 "If TESTâ‰Ÿ True "
      if (TEST == True ):
        $ 34 "continue"
        continue
      end
      $ 35 "Aufruf subProgram2"
      subProgram2()
    elif (3 == switch_1):
      $ 37 "Meldung"
      popup("Case 3", "Meldung", False, False, blocking=True)
      $ 38 "If TESTâ‰Ÿ True "
      if (TEST == True ):
        $ 39 "continue"
        continue
      end
      $ 40 "Aufruf subProgram3"
      subProgram3()
    elif (4 == switch_1):
      $ 42 "Meldung"
      popup("Case 4", "Meldung", False, False, blocking=True)
      $ 43 "If TESTâ‰Ÿ True "
      if (TEST == True ):
        $ 44 "continue"
        continue
      end
      $ 45 "Aufruf subProgram4"
      subProgram4()
    elif (5 == switch_1):
      $ 47 "Meldung"
      popup("Case 5", "Meldung", False, False, blocking=True)
      $ 48 "If TESTâ‰Ÿ True "
      if (TEST == True ):
        $ 49 "continue"
        continue
      end
      $ 50 "Aufruf subProgram5"
      subProgram5()
    else:
      $ 52 "Meldung"
      popup("Case standard", "Meldung", False, False, blocking=True)
    end
  end
end
```

---

<div class="post-metadata">

**Author:** ![m.birkholz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/m.birkholz/32/7084_2.png) [@m.birkholz](https://forum.universal-robots.com/u/m.birkholz)\
**Post date:** [October 2, 2019, 7:01am UTC](https://forum.universal-robots.com/t/command-to-stop-only-a-sub-program/5243/4 "2019-10-02T07:01:00Z")

</div>

> [@marvin.kaphengst](#):
>
> it appears to me that sub programs are nothing else than functions

Exactly😉 That‘s why using return should be fine. You can also use Threads and wait for completed execution using join command.

I would also recommend to create your program only using urscript. In this case, mixing polyscope programming with a ton of script commands can be confusing 🙂

---

<div class="post-metadata">

**Author:** ![marvin.kaphengst](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/marvin.kaphengst/32/1246_2.png) [@marvin.kaphengst](https://forum.universal-robots.com/u/marvin.kaphengst)\
**Post date:** [October 14, 2019, 6:59am UTC](https://forum.universal-robots.com/t/command-to-stop-only-a-sub-program/5243/5 "2019-10-14T06:59:40Z")

</div>

> Exactly😉 That‘s why using return should be fine. You can also use Threads and wait for completed execution using join command.
> 
> I would also recommend to create your program only using urscript. In this case, mixing polyscope programming with a ton of script commands can be confusing 🙂

Thank you for the help. And yes you are right, mixing the block building programming with script files is confusing. This should be changed by me 😄
