Universal Robots Forum

How to run a personal method when program tree runs?

I’ve write a method start another program in a program node but I don’t know how it will be automatically called. I’ve add this program node in a program tree, and I want this method be called when the program node is blue.
Does anybody have idea?

You can use Subprogram Node or Thread. Depends if you want to run in sequence or parallel.

Yes, I’ve set up a sub node, but I don’t know where I should put the method. Should I write it in generateScript? That works for only one sub node, but not for 2 sub nodes…

If you need to run a secondary program sequence, next to the “main” robot program, you must use a Thread.
This can be done direcly in script, so from your generateScript() method.
The overall structure is like:

thread myThreadName():
   foo = "bar"
   foo = "foo"
myThreadHandler = run myThreadName()

That will create a thread, the thread starts when the thread handler variable invokes the run command on the thread. When the thread is finished, it terminates automatically, so if this should keep running, remember to include a while(True). However remember that the exactly same thread would be created, in the second, third, etc. iteration of the program. So to clean up, you probably want to kill myThreadName() at a suitable location as well.

And keep in mind, that function definitions (like def myFunction(): are unique, so if you just re-use this name, it will overwrite the old name.


my program tree looks like this.
I’ve written 2 methods in Java Class, and want them to be automatically called in a program tree. In my program user interface I’ve written two buttons, when each button is pressed, it will add a corresponding subtree like funtion1 and function2. In program node contribution of my program I’ve add a function0 in generateScript to start another program so that function1 and 2 can be called. I want that when I press start button, this program tree will automatically repeated, function0 function1 function2 function0 function1 function2…
However I’m new to URCap and don’t know, where I should put my method. I just added my java function1 in generateScript() in program node contribution of java function1 and function2 in generateScript() in program node contribution of funtion2. But in the .script data I did’t see my method in program node, it seems that my java function will not be writen in URScript and so it will be called only at generation of the URScript, so nothing will be implemented when I press the play button again, the play button changed shortly to pause button and than to again play button.

This is how my generateScript() looks like
public void generateScript(ScriptWriter writer) {
// Directly generate this Program Node’s popup message + access the popup title through a global variable
//writer.appendLine(“popup(”" + generatePopupMessage() + “”, hello_world_popup_title, False, False, blocking=True)");
loadSkill(); // my own java function

I may explain it in a easier way. In helloworld samples, I can add as many helloworld node as I want in program tree and when program tree plays, it will show popups after each other. In helloworld program node there is

public void generateScript(ScriptWriter writer) {
writer.appendLine(“popup(”" + generatePopupMessage() + “”, hello_world_popup_title, False, False, blocking=True)");
How can I edit this generateScript() or something else so that my own method will be called in play mode just like popup?

Many thanks in advance!

It does not work like this. Java does not mix with URScript.
You can rewrite your method in URScript and use writer.appendLine() to put it in the program.
Otherwise you need to create a separate program, that will be listening for XMLRPC connection. This XMLRPC server daemon will have your method registered and then robot’s program will have to call it over the network through object created with rpc_factory.

UR program <-----XMLRPC-----> Java XMLRPC Server

1 Like

Thank you very much!
I’ve seen c++ daemon and python daemon, is there an example of java?

I haven’t seen one and prefer to use python.

Thank you very very much!

I would recommend starting a Thread in the URCap Activator, that can run as a background thread.
And that you could access over XML-RPC.

Thank you very much!