Universal Robots Forum

Create variable before program is run

Hi everyone,
I am facing an issue, I would like to be able to create a variable when adding my ProgramNode before executing the whole program.
In this way I would be able to use this variable for example with an “if”. But I can manage to do it.

I tried sending URscripts commands with “global myVar = False” and tried the GlobalVariable from the SDK API too but they both have no effect.

Any tips on how to do this?

Thank you

Federico

If you want to send script from a URCap when a program isn’t running, you can use a secondary client interface, as outlined in jbm’s post in this thread:


This allows you to send any URScript to the robot directly from within a ProgramNode, as opposed to script written in generateScript, which is only sent once you press play on the prgram.

Im not using the ScriptWriter but I am sending a TCP command which I think it’s the same thing the secondary client interface does, only through the URCap SDK

EDIT: Moreover in that way the script is only received if the robot is On and Started.

So I managed to make it work.

The problem with sending a command on the secondary interface is that is going to work only if the robot is up and running. But I think it needs to be working even when the only the teach pendant is on and the robot is idle.

So I used the GlobalVariable and VariableFactory from the SDK, which I found a bit confusing.

try {
     VariableFactory varFactory = api.getVariableModel().getVariableFactory();
     GlobalVariable myVar= varFactory.createGlobalVariable("var");
     model.set("var", myVar);
} catch (Exception e) {
     e.printStackTrace();
}

What I found confusing from the doc “Working_with_variables.pdf” was the model.set(SELECTED_VAR, myVar) and the naming/registering. This way it works.

The only problem I have is that the variable is displayed in dropdown menus, for example if I put an assignement node I can see “myVar” but I cannot see it from the Variables tab. Any help on this?

1 Like

I suspect the reason you can’t see it from the variables tab is because it hasn’t been assigned yet? The variable exists but doesn’t have a value so isn’t displayed. Try giving it a value and seeing if it pops up in the variables tab

1 Like

well actually I am initializing all of them to an initial value with

Expression initialValue = expressionBuilder.append("False").build();

GlobalVariable myVar = varFactory.createGlobalVariable("var", initialValue);

But I cannot see them. Maybe something related with the model.set() ?

Federico

From what jacob said here, it seems that the value of a variabel is only known when a program is running, If you need the value of a polyscope variable in java, could you store it in a separate java variable and pass it to polyscope when needed?

I don’t need it in Java. I just need the variable to be present in the Variable Tab in Polyscope when writing the robot program.

Right now I can see the variable from the drop down list (e.g. wehn selecting variable with the assignment node or with If node).

If it’s not clear I can post a couple of pictures, in the meantime thank you.

Federico

Variables only show up in the Variables-tab, when they have been created by assignment in URScript.
Before an assignment is made to them, they are not shown in the Variables tab.
You may want to expressly “global” your variable like: global varName = 42

I tried sending an urscript command “global myVar = False” on secondary interface after creating the global variable but it’s still not showing on the URcaps Variables tab.

I am also facing the same issue.

I need to create new Variables and assign them a value and store them in the Variables tab found in the Installation screen, so that the value is retained when the robot is restarted.
I tried sending the command global varName = 42 through GenerateScript when a button is pressed; also followed the example of creating variables as per the working_with_variables.pdf (even though it creates the variable when the program is run) but no effect. Both approaches create variables that are available when the program is run and they disappear as soon as the robot is switched off.

@jbm Any suggestion on how to create a function in the Installation Node of a URCap that creates variables that retain the value and are stored for future use ? I do not wish to use the manual way of creating such variables from the Variables tab in the Installation screen

any update on this?

I still wasn’t able to display the variables until pressing Play on the robot

Ok it seems that this is giving me more problems than I though.

Apart from the variables not showing up in the “Variables” tab in the programming section what is happening is that I can create the global variables (and see them from drop down menus) but I am not able to change the values.

This is due to the fact that the variables names will get the suffix _n but I will store them in the data model using the same name, i.e.:

ExpressionBuilder expressionBuilder = api.getValueFactoryProvider().createExpressionBuilder();
Expression initialValue = expressionBuilder.append("False").build();
VariableFactory varFactory = api.getVariableModel().getVariableFactory();
GlobalVariable myVar = varFactory.createGlobalVariable("MyVar_1", initialValue);
model.set("MyVar", myVar);

Then how can I modify the value from the generate script? I used something like:

line = "myVar= True\n";
writer.appendRaw(line);

But it’s just going to create a new variable “myVar” with the value and by the way it’s not going to be visible neither in the variables tab nor in the drop down menus.

Ok, so I was able to fix that by using ScriptWriter "getResolvedName(myVar) and use the returned string as var name in the script.

The problem of variables not showing up in the variable tab is still there though