IF Then Else conditions.

Also checkout the new CB3 forum

Use of – IF Then Else conditions.

In programming one of the most used features is the IF or conditions based statement because that’s the hearth of automatic choice of conditions and this is very often then main purpose of computer program.

In the UR we have the IF statement to use.

Note: Comparison of Strings are not available in UR.

universal_robots_zacobria_programming_if_then_else_1

Insert an IF statement into the program.

Define the IF statement by clicking on the formula button.

universal_robots_zacobria_programming_if_then_else_2

This will bring up a screen where we can choose conditions for the IF statement to check on. In this case we choose an Input to check on.

In this case it means that if Input 0 is true the IF statement is true and the lines in the IF statement will be executed.

universal_robots_zacobria_programming_if_then_else_3

Below the IF expression definition field is a check box called ”Check expression continuously”.

If this is checked the robot will check IF ”digital_in[0]”= True is true also during the execution of the program lines in the IF statement. This means that if the ” digital_in[0]” becomes 0 during the IF execution then the rest of the program lines inside the IF will not be executed. This can lead to unintended function if not handled correct.

universal_robots_zacobria_programming_if_then_else_4

Below the IF statement program lines is to be inserted that will be executed if the IF statement is True. This can also be considered a program inside the IF statement and it can be as big as our main program, but often this is short and to set outputs that is dependant of the IF condition.

In this case we choose to set a output high – wait 2 seconds and set it low again e.g. starting a conveyor for 2 seconds.

Check expression continuously.

This is an explanation for what happens if the “Check expression continuously” is not handled carefully.

Instead of an Input we will check on a variable instead because then it is easy to see the meaning and difference.

Consider this small program.

universal_robots_zacobria_programming_if_then_else_5

Initially the variable “var_1” is set to 0 – which means the IF statement is false and will not be executed – until we reach after Waypoint_3 – then the “var_1” is set to 1 and therefore the IF statement is true and we be started to be executed.

But in this case we have set the “Check expression continuously” checked.

universal_robots_zacobria_programming_if_then_else_6

What happens now – is that the first few lines under the IF statement will be executed e.g. the Digital output 2 will go on, but when we reach “var_1 = 0” in the IF statement, then we actually change the condition of the IF expression check – which now becomes false – and therefore the program jumps out of this IF routine already – and the digital output 7 will never go on.

Sometimes we will use Subroutines, but the effect will be the same.

universal_robots_zacobria_programming_if_then_else_7

This program will do exactly like the previous program, but we have used SubProgram method and same thing – the Digital Output 7 will never go on although it says clearly so in the SubProgram, but the IF expression is already False.

Here we have used a variable to show the effect, but it could as well have been a Digital Input we have used for the IF expression check – and the same will happen if the Digital Input state change while the program are executing the IF program lines – if the IF expression becomes False during this time – the rest of the program lines will not be executed. If such state changes right at the moment the IF was true – (but now false) none of the line in the IF statement are executed.

Combinations of expressions:

In a conditional expression you can have combinations e.g.

IF input_1 = High AND input_2 = Low

Then do something

But make sure you are using normal mathematically rules – so use of parentheses are a good thing like this

IF (input_1 = High) AND (input_2 = Low)

Then do somethin

However instead of long mathematically statements – then better have more IF statements.

Disclaimer: While the Zacobria Pte. Ltd. believes that information and guidance provided is correct, parties must rely upon their skill and judgement when making use of them. Zacobria Pte. Ltd. assumes no liability for loss or damage caused by error or omission, whether such an error or omission is the result of negligence or any other cause. Where reference is made to legislation it is not to be considered as legal advice. Any and all such liability is disclaimed.

If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.

Author:
By Zacobria Lars Skovsgaard
Accredited Universal Robots support Centre and Forum.

Also check out the CB3 forum



31 thoughts on “IF Then Else conditions.

  1. Mark

    Hello Lars,

    What exactly is the syntax for a “check continuously if statement”?

    I’m trying to hook up a digital input that will make the UR10 move to the right while the button is held down and when the user lets go of the said button the arm should stop.

    I have two threads one to send speedl commands and another to call a stopl command. However, I’m getting a runtime error when I try and implement this.

    Mark

    Reply
    1. zacopressadmin Post author

      Hello Mark

      Thanks for the question.

      It will not work to have commands that can move the robot in different threads together with the main program – because it is ambigius – the robot can only be at one position at any given time. And since Threads runs in parallel the robot might be asked to be a different positions at the exact time if such thing was allowed- which the robot cannot.

      Movement commands include – Waypoints – SpeedJ – SpeedL – StopJ – StopL etc. And such commands is best to keep in the main program and only in the main program.

      This article you have logged into show the Polyscope syntax for the “check continuously if statement”

      http://www.zacobria.com/universal-robots-zacobria-forum-hints-tips-how-to/if-then-else-conditions/

      If you wish to see how that looks like in Script – then the Polyscope program xx.urp can be saved – which also creates a xx.txt and xx.script file. In the xx.script file the script format can be studied.

      Author:
      By Zacobria Lars Skovsgaard
      Accredited Universal Robots support Centre and Forum.

      Also check out the CB3 forum



      Reply
      1. Mark

        Thanks Lars!

        Just a follow-up question how could I go about telling the UR10 to close a thread.

        For example, if I have some motion controls running in the background waiting for the user to give some input and in parallel, I’m waiting to see if the free drive button is pressed how can I tell the robot to stop sending speedl commands if the user hit the free drive button.

        currently, I have two threads running continuously checking for free drive input and another checking for some analog input that controls the arm.

        regards,

        Mark

        Reply
        1. zacopressadmin Post author

          Hello Mark

          Maybe consider to have a variable in the thread to be used as a flag. Then when the variable (Flag) is set in the thread – then use the variable (Flag) in the main program to take action accordingly.

          Author:
          By Zacobria Lars Skovsgaard
          Accredited Universal Robots support Centre and Forum.

          Also check out the CB3 forum



          Reply
  2. Izabella

    Hi,
    I have quite complicated program with palletizing at start, is there a command which will make robot to go to next palletizing spot in case of some event like loosing the grip for example?

    Reply
    1. zacopressadmin Post author

      Hi Izabella

      Thanks for thr question.

      There is no direct command for that – so it have to be programmed by the programmer with some logic (e.g. IF statment) to branch to other part of the program.

      Author:
      By Zacobria Lars Skovsgaard
      Accredited Universal Robots support Centre and Forum.

      Also check out the CB3 forum



      Reply
  3. JeromeK

    Hello,

    is there a way to trigger an Output Signal if the program stops.
    We conntected a welding machine to a UR10 Robot and we have to stop the welding process if an error occurs.

    Unfortunatly in case of an emergency stop of the robot the program does not send any information to our machine.

    How can we solve this problem?

    Reply
    1. zacopressadmin Post author

      Hello Jerome

      Thanks for the question

      In the Installation tab – I/O menu there is an option for set the Output High or Low when program stops.

      Author:
      By Zacobria Lars Skovsgaard
      Accredited Universal Robots support Centre and Forum.

      Also check out the CB3 forum



      Reply
      1. JeromeK

        First of all, thanks for your quick reply.
        I am aware of that function. But my problem is that once the program stopps I/Os are not checked anymore. So basically what i need is a background program that checks certain conditions and runs even if the main robot program is stopped. Like after an emergency stopp for example.

        Reply
        1. zacopressadmin Post author

          Hi Jerome

          Maybe it can be considered to use the MODBUS to control I/O – at this link is some informations.

          http://www.zacobria.com/universal-robots-knowledge-base-tech-support-forum-hints-tips/knowledge-base/modbus-registers-input-and-output-handling-via-port-502/

          Author:
          By Zacobria Lars Skovsgaard
          Accredited Universal Robots support Centre and Forum.

          Also check out the CB3 forum



          Reply
  4. Jeff

    Is it possible to perform an if statement based on a location? and example being that if the robot finds its self beyond a certain plane to go to a set of locations/run a sub routine? I am not trying to put in a hard stop because if I do the robot won’t go to the desired location I just need it to do some specific things if it find’s itself there at specific times.

    Reply
    1. zacopressadmin Post author

      Hi Jeff

      Thanks for the question.

      Maybe it can be considered to read the position of the robot with “get_actual_tcp_pose()” or getting the position from the MODBUS registers.

      http://www.zacobria.com/universal-robots-knowledge-base-tech-support-forum-hints-tips/knowledge-base/modbus-registers-read-position/

      Then the position of the robot will be known and some desired actions can be performed.

      Author:
      By Zacobria Lars Skovsgaard
      Accredited Universal Robots support Centre and Forum.

      Also check out the CB3 forum



      Reply
  5. Philip

    Hello Lars
    Thanks for the quick reply.
    I do not use any threads in my program right now, that’s why my own conclusion is that the robot can’t handle nested IF statements. I use subprograms to make the program tree more straightforward but they should only affect the main program when called upon.
    As for your suggestion it would unfortunately not work as i need the robot to Wait in case the robot is faster than the screw and only jump out of the whole IF statement when to torque is sufficient. The program is quite long and not easy to copy into this message board. I have tried this:
    *IF digital_in1= FALSE(
    *IF force() < 100 or digital_in1=FALSE(
    Waypoint (follow the screw vertically)
    Wait 2
    Pop up: Error screw not found))
    Waypont out (to the next screw)

    * – Check expression continuously
    But to no avail. again this is working 4/5 times but suddenly the robot stops with the error message as described. Is it possible this is a bug that can be fixed by upgradig the Polyscope software? last time i updated was 10/10 – 16
    thank you again for your time

    Philip

    Reply
    1. zacopressadmin Post author

      Hi Philip

      Can you then make a small program with the IF statements and waypoints that can reproduce the situation ?

      I would be good to see how the real code is that produces the situation and where the code branch to after the condition that terminates the IF occur.

      /Lars

      Reply
    2. Jawad Hassan

      Hello Philips,

      I am working with UR3 for screw insertion by autofeeder screw driver.
      I need help in making program and how to use way points for screw insertion. basically i am stuck in way-point where the robot stuck while inserting screw due to protection stop .
      Can you please help me in this regard.

      Thanks in advance
      BR,
      Jawad

      Reply
      1. zacopressadmin Post author

        Hello Jawad

        Maybe consider to use a spring device on the bit – so there is some tolerance where the robot pressure can work in. In this video an example is shown where the bit has a spring inside – so the robot can go down and turn the bit in steps – so the spring acts as a buffer for the pressure.

        https://www.youtube.com/watch?v=yBBo6NvYfhM

        Author:
        By Zacobria Lars Skovsgaard
        Accredited Universal Robots support Centre and Forum.


        Reply
  6. Mary

    Hi,
    I have a pallet program that involves picking pieces up and placing them somewhere else. I currently have it set so that if the gripper detects a part then it continues with the rest of the program, otherwise it continues in the pallet until it finds a part. I want to set the pallet so that it continues to a subprogram once it finds 6 parts and places them in the correct position. Do you know if there is a way to do this? Currently I have it on a Loop of 6 times, but I only want it to count the loop if a part was detected.
    Thanks!

    Reply
    1. zacopressadmin Post author

      Hi Mary

      I am not sure I understand your question, but it seems like there needs to be a routine that checks the condition for detection of the 6 parts been placed in the correct position which maybe sets a variable as flag – and then use this flag information (True or False) for example with a If statement to determine to enter the subroutine or not.

      Author:
      By Zacobria Lars Skovsgaard
      Accredited Universal Robots support Centre and Forum.

      Also check out the CB3 forum

      Reply
  7. Dani

    Hey,
    is there a solution to continue the Programm at the Point were i leaved the if Statement ?
    so i check an if statement (under the if statement there is a movement between two waypoints), it turns false and i go out of the statement, to do another Task(for example: wait a few seconds). Is there a way to continue that movement to the second Point(just go on with it) ?
    Would be very happy, if u can help me with that !

    Dani

    Reply
    1. zacopressadmin Post author

      Hey Dani

      Thanks for your question.

      That would require some tracking for which lines under the If statement has been executed.

      It can maybe be considered to introduce a variable under the If statement – so this variable counted one up after each statement under the If statement – and then have a If statement that use the status of this variable to select where to start again under the If statement.

      Although there might be some variations for where the position of the robot restarts if the interruption happen when the robot was moving – if this is important then the position of the robot also has to be recorded in a check variable and used for the restart.

      Author:
      By Zacobria Lars Skovsgaard
      Accredited Universal Robots support Centre and Forum.

      Also check out the CB3 forum

      Reply
      1. Dani

        Okay thank you very much!
        Thats a good hint to go on :)
        If you have some influence to the development of the programming language,
        it would be so nice, if there will be something like a “jump”.
        But for now, that’s very helpful to me!

        Dani

        Reply
        1. zacopressadmin Post author

          Hi Dani

          I think it is a matter of program technique. When I get the questions for “Jump” or similar “GoTo” makes me think 35 years back to 1981 when I got my first ZX81 with the program language BASIC that had such GoTo function, it was fun, but it was also not easy to make a structured program and difficult to trouble shoot.

          I typically get this question when a program statement for example like “Wait Until D1 = True” is used – because this statement stops the program flow and the program is just hanging there until D1 becomes True – and then if the programmer wish to do something else if another condition happens – it leads to the wish for a “jump” or “GoTo”. A “Wait until” also prevents discovering other changes of conditions in the main program (although a Thread could be used for that) – so I do not favour such hanging “Wait unitl” function, but sure there are many different ways to make programs the method that suits each individual is the best.

          However Instead it is possible to achieve something similar with a different programming approach where the program is constantly flowing – even if there is nothing to do for the program. This is similar as a PLC programs works where the program run down a loop and repeat from start when the botom is reached. Like running down a hallway where there are doors left and right to enter for the right condition that opens a door – and if no condition is True then keep running down the hallway and when the end of hallway is reach then start running from the start again

          In programming this can be achieved with “If” or “Case” statements – so when the program starts it meets such “If” statements – and if a statement is true – then this under laying program lines under the “If” is executed (like entering the door) and when these program lines has been executed or being interrupted if the conditions to enter has changed – then the program pointer goes back out to the hallway and continue running further down and checks next condition in line.

          So now something that looks like a “jump” can be achieved by having a condition for the “jump” as a “If” statement to enter if it happens.

          The statement “Wait until” can be used in a very simple program that is likely not to change, but as soon there is a desire to change or add something to the program- then such “Wait until” is typically in the way.

          Author:
          By Zacobria Lars Skovsgaard
          Accredited Universal Robots support Centre and Forum.

          Also check out the CB3 forum

          Reply
  8. Mary

    I am trying to use the if else program with a pallet program. I want to add a sub program to the if statement. I have a program that will detect if there is an object in the gripper, so I want my if statement to be “if object is detected, continue doing the pallet, else skip this point and move to the next point in the pallet.” Do you know if there is a way to do this? Sorry if that is confusing.

    Reply
    1. zacopressadmin Post author

      When making the “If” statement there is a check box option “Check expression continuously” that might be consider for such function.

      Reply
      1. Mary

        I tried that, but it doesn’t let me add a sub program to the “If” statement. Do you know if that is possible?

        Reply
  9. Henry Driedger

    Is there a limit to If statement nesting.

    I am getting “another thread is already controlling the robot” faults and wondered if it was due to a nesting limit issue. As i understand it an if statement that has “continuously check expression” checked, it is considered a thread.

    example
    -if
    -if
    -if
    -if
    etc.

    Reply
    1. zacopressadmin Post author

      Hi Henry

      Thanks for your question.

      Yes there is a limit to the level of nesting as it is heavy on the load for the system resources. Although no specific number of levels are provided because it properly depends on the complexity of the rest of the program – I normally try to keep no more than 2 levels when programming.

      Author:
      By Zacobria Lars Skovsgaard
      Accredited Universal Robots support Centre and Forum.

      Also check out the CB3 forum

      Reply
      1. Philip

        Hello Lars

        I have a problem with the robot program. I use the robot to control an electric screwdriver. The screwdriver has push-start so the only thing the robot needs to do is follow the screw until it gets an input from the screwdriver that the correct torque has been achieved. To make sure the screwdriver has time to tighten the screw i am using a nested IF statement.
        My program looks roughly like this:

        *IF digital_in1= FALSE
        *IF force() < 100
        Waypoint (follow the screw vertically)
        Wait 2
        Pop up: Error screw not found
        Waypont out (to the next screw

        * – Check expression continuously
        The robot is meant to jump out of the whole IF- code as soon as the digIn1 = TRUE meaning the torque is sufficient. OR if the robot is going down slightly faster than the screw the force will be grater than 100N and it will stop and wait 2 seconds – giving the screwdriver time to finish. If none of this is achieved it will give a user defined error message – "screw not found".
        The problem is this: Sometimes – not always – the robot will completely stop as soon as the digitalIn1 = true giving the error message "another thread is currently controlling the robot". it is extremely frustrating because i can't see how the program can be done any differently, and the only conclusion i have is the robot can't handle a simple nested IF statement.
        I hope you understand my problem and will be of help.
        Thank you for your time.
        Philip

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>