Home Programming


Left ArrowBack to discussions page
JstewartJstewart Posts: 62 Apprentice

Attached is a URscript that was generated from the sim software. Im new to script programming and im trying to understand more on how to use this to get more out of my UR. 

the questions i have are:

  1. The program created a variable called "step_count_7679db51_583c_48aa_afeb_a37e29d0d27f" and it is being used in the thread "Step_Counter_Thread_1f0542e5_48a9_4cd4_8e00_e9bb9f64c151". is this created to tell the thread to run only once?
  2. If i wanted to run this code on my UR, would i just call the "Step_Counter_Thread_1f0542e5_48a9_4cd4_8e00_e9bb9f64c151()"?

Off topic but has anyone used an automatic tool changer with a UR and can give me some in-site on how they did it as far as the programming goes?

Thanks in advanced

Best Answer

  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,266 Handy
    Answer ✓
    The question I would ask is why a thread in the function?   Do you need this to be processed in the background?  If not, this is a quick function call and so could easily be inline.  If you need to know always what tool is loaded, like if someone paused the robot and swapped tools then you need the thread to loop so that it is always running. The way it's written it's a single execution background function.

    The function would simply be as below, notice I am using a return statement and not assigning the global variable directly.

    def toolStatus():
      if (get_standard_digital_in(0)):  
         return 1
      elif (get_standard_digital_in(1)):
         return 2
      return 0

    Use it like this:
    Couple of things to point out, since this function is not dependent on global state its easily tested and secondly, get_digital_in was deprecated quite a while ago and replaced with more specific calls which you see represented in the code above.  Since the function has been officially deprecated they will most likely remove it from the language in the future which would cause your code to break when the update comes out.

    Since the status of the `get_standard_digital_in(n)` call is a boolean return value I do not need to compare it to True or False as it is True or False, I can simply state in my logic that it either is that value by stating it directly or I can negate it by saying something like this

    if (!get_standard_digital_in(0)):  #Read as "if not true" or in more common vernacular "if false"


  • bcastetsbcastets Vacuum Beta tester Posts: 652 Expert
    I am not sure about what you want to do with this script.

    From what I understand the thread inside the script is counting the number of time the thread have been executed.
    The robot execute program command with a pace of 0.008s for eSerie and 0.1s for CB. As there is nothing else than a step counter in the thread is behave like a time counter.

    If you use eSerie, Step_Counter_Thread_1f0542e5_48a9_4cd4_8e00_e9bb9f64c151*0.008 will give you program execution time.

    The variable is not used outside of the thread. It looks like it is not used.

    Side remark: it is good practice to add a sync() command at the end of a each thread to avoid having interference with program execution pace.

    If you want to run the complete code you can save your script file on the robot via FTP for example or with USB stick. Create a program, import the script and call it with the script command:

    I don't see so much things in this program. It is probably better to redo it on the teach pendent of the robot without using script.

  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,266 Handy
    That variable is an execution step counter that is being added by either UR behind the scenes or possibly a URCap if you have one installed.  What version of polyscope are you running?  Since this is defined as a thread you have to use the run command to cause it to start, you can see that in the code below

    Thread definition
    thread Step_Counter_Thread_1f0542e5_48a9_4cd4_8e00_e9bb9f64c151():
      while (True):
        step_count_7679db51_583c_48aa_afeb_a37e29d0d27f = step_count_7679db51_583c_48aa_afeb_a37e29d0d27f + 1
    Thread Execution
    run Step_Counter_Thread_1f0542e5_48a9_4cd4_8e00_e9bb9f64c151()
    When you run a thread you can also assign it to a variable such as this allowing you to kill the thread by using the id that is returned from the run command
    local stepId = run Step_Counter_Thread_1f0542e5_48a9_4cd4_8e00_e9bb9f64c151()
    at some time later or on some condition being met
    kill threadId
    Looking at the generated script file is a really good way to learn URScript as it will show you how UR is converting the GUI Polyscope commands into the actual script that is being executed by the robot

  • JstewartJstewart Posts: 62 Apprentice
    Thanks for the reply’s!

    @bcastets I don’t want to do anything with that particular script. I am trying to learn script programming and when I created a program on the TP that’s the script file that was generated. So would the sync() be used at the end of thread like shown in script?

    @matthewd92 so if I created a script file it would be placed say in the before program sequence? And then whenever I need that code I would do a script line command with the “run scriptcodefilename()” or would it just be “scriptcodefilename()”
  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,266 Handy
    edited January 2021
    If it a thread you use run command. Otherwise just call the function direct. 

    Threads are background functions. They do not block your code that’s controlling your robot. A good built-in example is the timer function. When you start a timer it starts a thread and when you stop it the thread is killed which stops the timer running. 
  • bcastetsbcastets Vacuum Beta tester Posts: 652 Expert
    As Matthew said this thread is automatically generated by URCAP or UR system. I did not know about it. It is not so important. The most important is the portion which concern your program.

    The first part of the script are some variables and functions used by the system to run the program. It is automatically added to the script by URCAP or polyscope. You don't need to dive into it.

    The second portion is the main program.
  • JstewartJstewart Posts: 62 Apprentice
    Thanks for all the help guys, definitely helped me out! 
  • JstewartJstewart Posts: 62 Apprentice
    @matthewd92 @bcastets

    So in my project i have an auto tool changing system that gets connected to the DIO of the robot. DI(0) is tool 1 and DI(1) is tool 2 when the are connected to the robot. In my URScript i want to be able to output to variable "Tool_act" a 1 or 2 for whichever tool is currently on the robot. my script starts as follows:

    def toolStatus():

      global Tool1 = get_digital_in(0)
      global Tool2 = get_digital_in(1)
      global Tool_act = 0

      thread tool_Active():
        if (Tool1 == True):
          Tool_act = 1
          elif (Tool2 == True):
            Tool_act = 2
              Tool_act = 0

      run tool_Active()

    would this be the correct way of doing this? also the "global" variables could be ones i have created on the TP correct?

  • bcastetsbcastets Vacuum Beta tester Posts: 652 Expert
    edited January 2021
    If you are beginning with UR, I would recommend to make your program directly on the teach pendant.

    I think you need some modifications:

    def toolStatus():

      global Tool1 = get_digital_in(0)
      global Tool2 = get_digital_in(1)
      global Tool_act = 0

      thread tool_Active():
        #Refresh variable values
        Tool1 = get_digital_in(0)
        Tool2 = get_digital_in(1)
        if (Tool1 == True):
          Tool_act = 1
        elif (Tool2 == True):
          Tool_act = 2
          Tool_act = 0
        #sync() command have to be at the end of the thread definition
      run tool_Active()

    You can then execute your code with the following script command:

  • JstewartJstewart Posts: 62 Apprentice
    Thanks for the reply

    when i run the code however it is saying the no tool is currently active even though tool 1 is connected to my robot. 
  • JstewartJstewart Posts: 62 Apprentice
    ignore my last post, i ended up figuring it out!
  • JstewartJstewart Posts: 62 Apprentice

    Wow that helps me out a ton. While reading the script manual i did notice that some functions were being updated with newer ones, but must have over looked that one.

    "if (!get_standard_digital_in(0)):"
    The "!" in this line of code, what is the proper name of this? I've seen these all through out the forums and other websites, but i dont
    understand the meaning of all of them. Are they located within the URScript API Manual?

  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,266 Handy
    It’s the negate identifier. It’s read as “if NOT get_standard_digital_in(0)” and causes the if statement to look for a Falsy value instead of a Truthy value. So the if statement is looking for when the input is off instead of on. 
  • JstewartJstewart Posts: 62 Apprentice
    im assuming there is more than just this one? is there a document that explains these?
  • matthewd92matthewd92 Founding Pro, Tactile Sensor Beta Testers Posts: 1,266 Handy
    These are not unique to URScript, there is a section in the manual that describes their logic operators. The ‘!’ is a rather unique operator. You could also use the keyword ‘not’ in its place. The main operators we use daily are ‘not’ ‘or’ ‘and’   

    In other languages they may have symbols that represent those keywords but URScript uses keywords. 
Sign In or Register to comment.
Left ArrowBack to discussions page