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.
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?
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
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?
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?
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.
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.
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.
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.