Universal Robots Forum

Global Variable setting Value removes the Variable in PolyScope

Hi,

i want to create a global variable and Change the value by the user. The creating works fine, but when i want to update the value it removes the global Var in Datamodel.

programAPI.getUndoRedoManager().recordChanges(new UndoableChanges() {
				@Override
				public void executeChanges() {
							if (isDefined()) {
								
								ArrayList<Variable> gVars = new ArrayList<Variable>();
								gVars.addAll(programAPI.getVariableModel().get(new Filter<Variable>() {
									@Override
									public boolean accept(Variable element) {
										return element.getType().equals(Variable.Type.GLOBAL) && element.getDisplayName().equals(getSelectedPottingPoint().getDisplayName() + "_PT1");
									}
								}));
								
								
								if (gVars.isEmpty()) {
									
									GlobalVariable variablePT1 = null;
									try {
										System.out.println("Variable: " + getSelectedPottingPoint().getDisplayName()
												+ "_PT1" + " was created");
										variablePT1 = variableFactory.createGlobalVariable(
												getSelectedPottingPoint().getDisplayName() + "_PT1",
												programAPI.getValueFactoryProvider().createExpressionBuilder()
														.append(String.valueOf(model.get(POTTING_TIME, 0d))).build());
										model.set(getSelectedPottingPoint().getDisplayName() + "_PT1", variablePT1);
											if(model.isSet(getSelectedPottingPoint().getDisplayName() + "_PT1")) {
												System.out.println(getSelectedPottingPoint().getDisplayName() + "_PT1 is set");
											}else {
												System.out.println(getSelectedPottingPoint().getDisplayName() + "_PT1 is not set");
											}
											
									} catch (VariableException e) {

										e.printStackTrace();
									} catch (InvalidExpressionException e) {

										e.printStackTrace();
									} 
									
								} else {
									
									try {
										System.out.println("Variable: " + getSelectedPottingPoint().getDisplayName()
												+ "_PT1" + " was updated");
										model.set(getSelectedPottingPoint().getDisplayName() + "_PT1", model.get(POTTING_TIME, 0d));
										
											if(model.isSet(getSelectedPottingPoint().getDisplayName() + "_PT1")) {
												System.out.println(getSelectedPottingPoint().getDisplayName() + "_PT1 is set");
											}else {
												System.out.println(getSelectedPottingPoint().getDisplayName() + "_PT1 is not set");
											}
											
									} catch (IllegalArgumentException e) {
										
										e.printStackTrace();
									}
									
								}

Am i doing something wrong or forgetting to define something?

Ronny

Just for getting me wright…

I want to create a GlobalVar with a Value set by the user(RoboCallBack). This works fine. I Need the globalVar to use the PoseData from the usersetting in another UrCap. They can choose it by a dropDownBox. When the User changes the pose of the created WaypointNode in the first URCap, i want to refresh/update the data in the created globalVar. So the user has the latest poseData available in the second URCap. When i create the globalVar again, PolyScope creates another globalVar with other Ending (globalVar_1 -> globalVar_2). And when i´m just Setting the new Value(pose) by dataModel.set(globalVar_1, new pose), PolyScope deletes globalVar_1 or if globalVar is in an Assignment it dosn´t changes the Value to the new pose.

is there any example vor creating and Refreshing a globalVar in Java?

Or is there a better way to get the UserCreatedWaypointData for the second URCap?

Okay I found some explaining for the behavior:

For URCap nodes, the registration is done by putting the variable in the data model of the UR-
Cap node. This signals to PolyScope that this variable is in use by the URCap. If the URCap
is no longer using the variable, the URCap must remove the variable from its data model. This
happens by either calling the remove() method on the data model or by overwriting the value
using the same key

so i have to rethink my cap

1 Like

Hi Ronny, did you found a solution to this problem?
I have currently the same issue. When I try to update a self created global Pose variable I remove it first and create it again. This only works when the created variable is not yet used in other node of the program, e.g. a Variable Waypoint. When it is already in use, I am not able to update the value, instead a new one is created.
So I wonder, how can I update an already existing global variable been in use by another node?