Visit Zacobria Webshop

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 2015-2018 Universal Robots support Centre and Forum.


Tweet

Follow @zacobria