Universal Robots Forum

Command to stop only a sub program

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

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?

1 Like

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

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 :slightly_smiling_face:

2 Likes

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 :slightly_smiling_face:

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 :smile: