Universal Robots Forum

Get Variable values in Toolbar

This is my code to get all the variables on my instalaltion but I cannot find methods to get the value of the variable. The objective is to visualize some variables (such as counters) in a toolbar.

import java.util.Collection;
import java.util.Iterator;

import com.ur.urcap.api.domain.variable.Variable;
import com.ur.urcap.api.domain.variable.VariableModel;

public class VariableHandler {

private final VariableModel variableModel;

public VariableHandler(VariableModel variableModel) {
	this.variableModel = variableModel;
}


/*Returns variable object found by its default name
 * 
 */
public Variable getVariable(String defaultName){
	Collection<Variable> Variablecollection = variableModel.getAll();
	int VAR_count = Variablecollection.size();
	if(VAR_count > 0){
		Iterator<Variable> VAR_itr = Variablecollection.iterator();
		while(VAR_itr.hasNext()){
			Variable thisVAR = VAR_itr.next();
			String thisDefaultName = thisVAR.getDisplayName();
			System.out.println("Found A VAR named "+thisDefaultName);
			if(thisDefaultName.equals(defaultName)){
				return thisVAR;
			}
		}
	}
	return null;
}

}

Thanks.

This is not as simple. I’m also trying to find the best way to view and edit a variable from a toolbar. These are the possibilities I know to get the current value of a URScript variabile in a URCap:

  • Read it from Primary Interface - You have to write a “reader” for the packages sent by the controller, maybe based on this sample which is for Realtime: getRobotData
  • Send it from URScript through a socket to a java server (see ScriptCommunicator or this older one ScriptCommunicator)
  • Send it from URScript through XML-RPC (see mydaemonswing sample or Dialog Script URCap)

I think maybe the second method is the simplest one.

I tried with the ScriptCommunicator (the ScriptExtractor part), but I can get only data by using built-in function like get_…().Do you have an example where you get the value of a pose (like i_home_pose) or a general int variable (i_var_1) stored in the installation variables?

You are correct, I forgot I was using scriptExporter to export a value from the integer registers, as e.g.:

ScriptExporter export = new ScriptExporter();
ScriptCommand commandInt = new ScriptCommand("Command1");
commandInt.appendLine("int_value = read_output_integer_register(21)");
int resultInt = export.exportIntegerFromURScript(commandInt, "int_value");

So a possible workaround is to constantly copy the variable you are interested in to a register and read from that. Sorry but I don’t know if it is possible to read a variable with this code.

Thank you!!! I did not know this feature, I’m testing it right now and it’s working. It is a good workaround.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.