Universal Robots+

Execute distinct scripts within a URCap

Maybe it is a trival question, but I don’t know whether there is a simpler way to launch distinct scripts on buttons click. For example I’d like to record the robot motion after clicking record_button (until a stop_record_button is pressed), and playback the same trajectory everytimes a play_button button is clicked.

A very simple code of the above example:

@Input(id = "record_button")
public void onRecordButtonClick(InputEvent event) {
  if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
    record_phase = true;
  }
}

@Input(id = "stop_record_button")
public void onRecordButtonClick(InputEvent event) {
  if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
    record_phase = false;
  }
}

@Input(id = "play_button")
public void onRecordButtonClick(InputEvent event) {
  if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
    play_phase = true;
  }
}

...

@Override
public void generateScript(ScriptWriter writer) {
  while (true) {
    if (record_phase) {
      writer.appendLine("teach_mode()");

      while (record_phase) {
        // store current joint positions
        // sleep for N ms
      }
    }

    if (stop_record_phase) {
      writer.appendLine("end_teach_mode()");

      // adjust trajectory in M waypoints
    }

    if (play_phase) {
      // follow the stored trajectory

      play_phase = false;
    }
  }
}

Can the switch behavior of the generateScript be avoided in someway?

Alessandro Tondo @qbrobotics


Update: moreover I’m facing out with the fact that get_actual_joint_positions() is a script function and I’m not able to get its return inside my ProgramContribution. Also, I don’t want to have my PC connected to the robot all the time, so I was wondering whether the xmlrpc is the only way to elaborate these data or there is a simpler choice (I think I cannot do the computation directly inside my script since I need a “matrix” variable to store the poses over time).


Update2: I’m sorry for the many nested questions but I’ve noted that there is no way to interact with a URCap while the program is running, i.e. if I click on any button the execution immediately stops. Is it right?

1 Like

This is not quite the way to acheive the behaviour you describe,

The ScriptWriter will only add to or modify the script code, that is put in the program, either as a pre-amble for Installation nodes, and at the immediate place in the program, for Program nodes.
This means that this script would only be executed, when the program is run.

What it seems you are looking for is the ability to execute “programs” (/record function) immediately upon button push - this would be acheived using Java to open a socket connection to our client interfaces, and send the recording script.
And hence stop this script later on the stop recording button,
Client interfaces are explained in the “Communication interfaces” post.

You would not be able to get the return from e.g. get_actual…() directly.
You could either return this using a socket call back to a server running in Java (or something else) or simply read this directly from the stream on the client interface.

Also check out “secondary programs” for your last question.

Thank you Jacob for all the info and sorry for the late response.

I have modified the URCap following your suggestions and it works as expected now!

hi , i also meet this question same as you. could you share some ideas about that , it confuse me for a few days … thank you very much !!!

could you show an code example for this? i mean, inside in URCap contribution