I am trying to figure out how to use threads in URCaps, and I have the following code in my programNodeContribution file:
public void generateScript(ScriptWriter writer)
{
writer.defineThread(“name_of_thread”);
writer.appendLine(“textmsg("I am writing into the log from a thread")”);
writer.returnMethod();
writer.end();
When I run this I get the following error in the simulator: “Syntax error on line 31: end”
I have no idea why I get this, as far as I know I have simply translated line-by-line the example found in the URscript manual into URCaps
My ultimate goal is to read get the position of the tool head while moving the robot, so I can interrupt it and change target position if needed.
Can you upload the generated script file? Hence we could investigate as to where line 31 is.
Alternatively:
Try to remove the return statement from the thread.
The join command could be omitted.
If you want the thread to run continuously, place the code inside a while(True) statement.
Check out this outputted script code from PolyScope:
I was able to manually create that example you posted using the GUI, but I am not sure how to get the script code produced by it. How do I do that?
I tried modifying my code to this:
public void generateScript(ScriptWriter writer)
{
writer.defineThread("name_of_thread");
writer.whileTrue();
writer.appendLine("textmsg(\"I am writing into the log from a thread\")");
writer.sleep(1.0);
writer.end();
writer.end();
writer.runThread("name_of_thread_handle","name_of_thread");
}
I still get the same error. The following is the output from the terminal:
ERROR [AWT-EventQueue-1] 15:43:47 12/12/16 URDialogCreator.popupRuntimeExceptionDialog(line:356): An error occurred in the running program
WARNING [AWT-EventQueue-1] 15:43:47 12/12/16 HTML.wrapTag(line:176): The text “Syntax error on line 34: end” was already wrapped with tags that should probably be removed from the translation.
The program is based on the “Hello World” sample, except for editing the html and installationNode files I haven’t done any major changes to it.
Do you have a working Java that uses threads? All the samples posted on this forum that I have found does not utilize threads in the scriptwriter part.
Is the ScriptWriter the one from your ProgramNode or InstallationNode?
You would often really prefer to spawn threads for this use case in the Installation, unless you kill it later on in the program, before you start it again. The definition should only be necessary once for your thread.
If you define the thread inside the program node ScriptWriter, you will infinitely fast spawn a new thread, and then a new one, etc.
Since you never write “write children” in the ScriptWriter, the Wait 5.0 is never used in the program.
Hence your program just keeps infinite looping until it reaches the max amount of allowed threads, where it will error out.