I am try to write a URCap for saving pose and and assign it to variable. I also want to add an input button so that when i press it, it will execute movej or movel ur script for robot to move to target pose. Is it possible to execute ur script at this situation? similar case should be the “perform action now” button in Set or “Preview Popup” button in Popup.
Here are the code that I am writing:
thanks
You cannot insert the URScript directly in Java as above.
However check out the Communication interfaces as sending the motion command over e.g. the secondary client interface might create the desired action.
does this means that we must have external device to send the UR Script command over TCP/IP?
is it possible that in java, we establish the socket communication using native ip 127.0.0.1 and port 30001, and using it to send the UR Script command?
Thanks
You could use localhost IP 127.0.0.1 and the respective ports (30001, 30002 or 30003).
I had tried to establish the socket communication within java using localhost IP address 127.0.0.1 and port 30001,30002,30003. I test by press the button and sending data stream ‘popup(“Hello World”)’ and except it to show the popup window for Hello World. But after i pressed the button, the popup does not appear. I output show text to screen to show me the progress and it shows that the code execute until close socket.Somehow the URScript sent was not responding.
here are the code that I modify from helloworld sample
package com.ur.urcap.examples.helloworld.impl;
import com.ur.urcap.api.contribution.ProgramNodeContribution;
import com.ur.urcap.api.domain.URCapAPI;
import com.ur.urcap.api.domain.data.DataModel;
import com.ur.urcap.api.domain.script.ScriptWriter;
import com.ur.urcap.api.ui.annotation.Input;
import com.ur.urcap.api.ui.annotation.Label;
import com.ur.urcap.api.ui.component.InputEvent;
import com.ur.urcap.api.ui.component.InputButton;
import com.ur.urcap.api.ui.component.InputTextField;
import com.ur.urcap.api.ui.component.LabelComponent;
import java.io.;
import java.net.;
public class HelloWorldProgramNodeContribution implements ProgramNodeContribution{
private static final String NAME = “name”;
private final DataModel model;
private final URCapAPI api;
private String cachedPopupMessage = "";
private String cachedPopupMessage1 = "";
private String cachedPopupMessage2 = "";
private String cachedPopupMessage3 = "";
private String cachedPopupMessage4 = "";
private String cachedPopupMessage5 = "";
private String cachedPopupMessage6 = "";
private String cachedPopupMessage7 = "";
private Socket connection;
private PrintWriter output;
public HelloWorldProgramNodeContribution(URCapAPI api, DataModel model) {
this.api = api;
this.model = model;
}
@Input(id = "yourname")
private InputTextField nameTextField;
@Label(id = "titlePreviewLabel")
private LabelComponent titlePreviewLabel;
@Label(id = "messagePreviewLabel")
private LabelComponent messagePreviewLabel;
@Label(id = "messagePreviewLabel1")
private LabelComponent messagePreviewLabel1;
@Label(id = "messagePreviewLabel2")
private LabelComponent messagePreviewLabel2;
@Label(id = "messagePreviewLabel3")
private LabelComponent messagePreviewLabel3;
@Label(id = "messagePreviewLabel4")
private LabelComponent messagePreviewLabel4;
@Label(id = "messagePreviewLabel5")
private LabelComponent messagePreviewLabel5;
@Label(id = "messagePreviewLabel6")
private LabelComponent messagePreviewLabel6;
@Label(id = "messagePreviewLabel7")
private LabelComponent messagePreviewLabel7;
@Input(id = "button")
private InputButton button;
@Input(id = "button")
public void onButton(InputEvent event) throws InterruptedException{
if(event.getEventType()==InputEvent.EventType.ON_PRESSED){
startRun();
}
}
public void startRun() throws InterruptedException{
try{
connectToServer();
setupStreams();
//sendScript("popup(\"Hello World\")");
sendScript("popup(\"Hello World\")");
closeSocket();
}catch(EOFException eofException){
}catch(IOException ioException){
}
}
private void connectToServer() throws IOException{
setName("Attempting.....");
updatePopupMessageAndPreview();
connection = new Socket(InetAddress.getByName("127.0.0.1"),30001);
if(connection.isConnected()){
setName("Connected to 127.0.0.1");
updatePopupMessageAndPreview1();
}else{
setName("Connection Fail!!!!!");
updatePopupMessageAndPreview1();
}
}
private void setupStreams() throws IOException, InterruptedException{
setName("Set up stream......");
updatePopupMessageAndPreview2();
output = new PrintWriter(connection.getOutputStream(),true);
if(output.checkError()){
setName("Stream setup FAIL!!!!");
updatePopupMessageAndPreview3();
}
else{
setName("Stream setup done!!!!");
updatePopupMessageAndPreview3();
}
}
private void sendScript(String s) throws InterruptedException{
setName("Sending Script.....");
updatePopupMessageAndPreview4();
output.write(s);
setName("Done");
updatePopupMessageAndPreview5();
}
private void closeSocket() throws InterruptedException{
setName("Closing socket......");
updatePopupMessageAndPreview6();
try{
connection.close();
output.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
setName("Socket closed!!!!");
updatePopupMessageAndPreview7();
}
@Input(id = "yourname")
public void onInput(InputEvent event) {
if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
setName(nameTextField.getText());
updatePopupMessageAndPreview();
}
}
@Override
public void openView() {
setName(nameTextField.getText());
updatePopupMessageAndPreview();
}
@Override
public void closeView() {
}
@Override
public String getTitle() {
return "HelloWorld: " + (model.isSet(NAME) ? getName() : "");
}
@Override
public boolean isDefined() {
return getInstallation().isDefined() && !getName().isEmpty();
}
@Override
public void generateScript(ScriptWriter writer) {
// Directly generate this Program Node's popup message + access the popup title through a global variable
writer.appendLine("popup(\"" + cachedPopupMessage + "\", hello_world_popup_title, False, False, blocking=True)");
writer.writeChildren();
}
private void updatePopupMessageAndPreview() {
cachedPopupMessage = getName();
messagePreviewLabel.setText(cachedPopupMessage);
titlePreviewLabel.setText(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
}
private void updatePopupMessageAndPreview1() {
cachedPopupMessage1 = getName();
messagePreviewLabel1.setText(cachedPopupMessage1);
titlePreviewLabel.setText(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
}
private void updatePopupMessageAndPreview2() {
cachedPopupMessage2 = getName();
messagePreviewLabel2.setText(cachedPopupMessage2);
titlePreviewLabel.setText(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
}
private void updatePopupMessageAndPreview3() {
cachedPopupMessage3 = getName();
messagePreviewLabel3.setText(cachedPopupMessage3);
titlePreviewLabel.setText(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
}
private void updatePopupMessageAndPreview4() {
cachedPopupMessage4 = getName();
messagePreviewLabel4.setText(cachedPopupMessage4);
titlePreviewLabel.setText(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
}
private void updatePopupMessageAndPreview5() {
cachedPopupMessage5 = getName();
messagePreviewLabel5.setText(cachedPopupMessage5);
titlePreviewLabel.setText(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
}
private void updatePopupMessageAndPreview6() {
cachedPopupMessage6 = getName();
messagePreviewLabel6.setText(cachedPopupMessage6);
titlePreviewLabel.setText(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
}
private void updatePopupMessageAndPreview7() {
cachedPopupMessage7 = getName();
messagePreviewLabel7.setText(cachedPopupMessage7);
titlePreviewLabel.setText(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
}
private String getName() {
return model.get(NAME, "");
}
private void setName(String name) {
if ("".equals(name)){
model.remove(NAME);
}else{
model.set(NAME, name);
}
}
private HelloWorldInstallationNodeContribution getInstallation() {
return api.getInstallationNode(HelloWorldInstallationNodeContribution.class);
}
}
helloworld-1.0-SNAPSHOT-sources.tar.gz (6.3 KB)
also if we want to receive the return value from URScript and assign it to variable .eg(get_digital_in(0)), how should we do it? is echo a correct approach?
Thanks
It seems that in startRun() method you miss the “\n” character when calling sendScript.
From the “URScript API Reference” manual:
When connected URScript programs or commands are sent i clear text on the socket.
Each line is terminated by “\n”.
do you mean like this ?
tried but still same result, popup does not appear
Hi All,
I’m about to start creating a urcap mainly using microscan gige camera connected to the UR3 robot.
Since I’m newbie I’m confused how to start doing this. Does anyone have any idea how to start this?
many thanks
Any update on this? I’d like to execute URSCript code from the URCap somehow too (like pressing “play” but for a single block, not the whole program).
Did any of those workarounds using the TCP ports work?
URScript code is executed during program execution within the ‘public void generateScript(ScriptWriter writer).’ The Hello World example URCap should be a good example of URScript being executed from a URCap during run time for this example it creates a popup message.
@Override
public void generateScript(ScriptWriter writer) {
// Directly generate this Program Node's popup message + access the popup title through a global variable
writer.appendLine("popup(\"" + cachedPopupMessage + "\", hello_world_popup_title, False, False, blocking=True)");
writer.writeChildren();
}
In this example, a custom popup is sent to the secondary client, when the method createPopup(message)
is called.
package com.ur.urcap.yourUrCap.impl;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class clientSendScript {
public clientSendScript() { }
// localhost IP
private String TCP_IP = "127.0.0.1";
// Port for secondary client
private int TCP_port = 30002;
// Public method to send popup script to client interface
public void createPopup(String message){
sendToSecondary("popup(\""+message+"\")");
}
// Internal method that sends script to client
private void sendToSecondary(String command){
try{
// Create a new Socket Client
Socket sc = new Socket(TCP_IP, TCP_port);
if (sc.isConnected()){
System.out.println("Connected to UR Secondary Client");
}
// Create stream for data
DataOutputStream out;
out = new DataOutputStream(sc.getOutputStream());
// Wrap command in "def" and "end"
String thisCommand = "def myCustomCode():\n "+command+"\n end\n";
// Send command
out.writeUTF(thisCommand);
System.out.println("Send this: "+thisCommand);
out.flush();
// Perform housekeeping
out.close();
sc.close();
System.out.println("Disconnected from UR Secondary Client");
}
catch (IOException e){
System.out.println(e);
}
}
}
@roman.reiner thanks, but that function is called when you press the “play” button, and all the program is executed. What I meant was to run the code of a single block corresponding to the program node without needing to run the whole program.
Thanks @jbm, that is what I was looking for! It seems a good workaround; I will try it out.
hello, i also meet the same problem today . Have you solved it yet ? can you share about it . thank you so much!
Have a look at @jbm response (Can urcap send and execute ur script within the program node? - #12 by jbm). It’s all included.
What that code do is basically a socket connexion to the Secondary client (port 30002), and send urscript through that socket. then disconnect.
Also, I doubt it display the popup, as it wrap the command in a function, (!! It’s maybe to be able to send multiples lines at a time), but it not call it. try changing the line out.writeUTF(thisCommand);
to out.writeUTF(command);
. Then you just copy all his function “sendToSecondary”, and then you can use this method to send anything you want.
You could see the documentation of the communication interfaces https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/overview-of-client-interfaces-21744/.
Also, little trick to test easily these communications interfaces, you could use the “netcat” program, also called “nc”, that allow you to open and read/write in a socket. When there is a lot of value to read (like the secondary client), you can send this to /dev/null, to don’t display it, and have the time to write in the socket.
Exemple:
# connecting to the dashboard
nc 127.0.0.1 29999
# connecting to the secondary
nc 127.0.0.1 30002
# as it display a lot of data, send them in /dev/null to hide them, and have the time to write something like "popup('hello-world')"
nc 127.0.0.1 30002 > /dev/null
Even when sending a function to the client interface, it is executed immediately.
I cannot be called at a later point.
So whether you are sending:
popup("My text as one line")
or
def program():
popup("My text as function")
end
will show a popup.
Actually, this is also the case, when you are pressing “Play” in the GUI:
PolyScope then sends the entire program as a function (refer to the contents of the .script file) as a string to the Primary Client interface.
It make sense, thank you for the explanation.
I tryed to generate a Popup message as it is montioned above,but i become always the same error message : - Software Error
- No such Method Error
have any one faced the same problem ?
thanks
Can you show the code that display this error ?
I think it’s probably a confusion between “send to secondary” and “write in java code”, the ‘popup’ function is urscript, not java.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.