Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split the urscript into a header and a control loop. #14

Merged
merged 3 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,22 @@ To enable external control of a UR robot from a remote PC, this URcap must be in
* Add this URcap to a program by selecting it from the side menu under the tab _URcap_.
* Execute the program by pressing the _play_ button in the _Program_ tab of Polyscope.

### Multiple URCap nodes
urmahp marked this conversation as resolved.
Show resolved Hide resolved
To use this URCap node multiple times in a ur program, the control script is divided into two
scripts. After receiving the script, it is divided into a header part and a control loop part. The
header part consist of all the function deffinitions. The header is only inserted once in the
urmahp marked this conversation as resolved.
Show resolved Hide resolved
program, while the control loop is inserted for each URCap node in the program tree.
urmahp marked this conversation as resolved.
Show resolved Hide resolved

To be able to distinguish between header and control loop, the header part of the script should be
encapsulated in:
```bash
# HEADER_BEGIN
Here goes the header code
# HEADER_END

# NODE_CONTROL_LOOP_BEGINS
Here goes the control loop code
# NODE_CONTROL_LOOP_ENDS
fmauch marked this conversation as resolved.
Show resolved Hide resolved
```
If its not possible to find either `# HEADER_BEGIN` or `# HEADER_END`, the script will not be
divided into two scripts and it will not be possible to have multiple URCap nodes in one program.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public class ExternalControlInstallationNodeContribution implements Installation
private static final String HOST_IP = "host_ip";
private static final String PORT_NR = "port_nr";
private static final String NAME = "name";
private String urScriptProgram = "";
private static final String DEFAULT_IP = "192.168.56.1";
private static final String DEFAULT_PORT = "50002";
private static final String DEFAULT_NAME = DEFAULT_IP;
private DataModel model;
private final ExternalControlInstallationNodeView view;
private final KeyboardInputFactory keyboardFactory;
private RequestProgram sender;
private boolean programRequested;

public ExternalControlInstallationNodeContribution(InstallationAPIProvider apiProvider,
ExternalControlInstallationNodeView view, DataModel model) {
Expand All @@ -65,8 +66,7 @@ public boolean isDefined() {

@Override
public void generateScript(ScriptWriter writer) {
// Make sure the program gets requested at least once
urScriptProgram = "";
programRequested = false;
}

// IP helper functions
Expand Down Expand Up @@ -168,11 +168,17 @@ public void onOk(String value) {
};
}

public String getUrScriptProgram() {
if (urScriptProgram == "") {
RequestProgram sender = new RequestProgram(getHostIP(), getCustomPort());
urScriptProgram = sender.sendCommand("request_program\n");
}
return urScriptProgram;
public String getControlLoop(ScriptWriter writer) {
if (programRequested == false) {
// Request and split the program
sender = new RequestProgram(getHostIP(), getCustomPort());
sender.requestAndSplitProgram();

// Append header to the ur program.
writer.appendRaw(sender.getHeader());

programRequested = true;
}
return sender.getControlLoop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public boolean isDefined() {

@Override
public void generateScript(ScriptWriter writer) {
String urScriptProgram = getInstallation().getUrScriptProgram();
writer.appendRaw(urScriptProgram);
String controlLoop = getInstallation().getControlLoop(writer);
writer.appendRaw(controlLoop);
}

private ExternalControlInstallationNodeContribution getInstallation() {
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/fzi/externalcontrol/impl/RequestProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,33 @@ public class RequestProgram {
private final String hostIp;
// custom port
private final int portNr;
// header of the requested program
private String header;
// control loop of the requested program
private String controlLoop;

/*
* Default constructor
*/
public RequestProgram(String hostIp, String portNr) {
this.hostIp = hostIp;
this.portNr = Integer.parseInt(portNr);
this.header = "";
this.controlLoop = "";
}

public void requestAndSplitProgram() {
String program = sendCommand("request_program\n");
// Split program into header and control loop
splitProgram(program);
}

public String getHeader() {
return this.header;
}

public String getControlLoop() {
return this.controlLoop;
}

public String sendCommand(String command) {
Expand Down Expand Up @@ -89,4 +109,36 @@ public String sendCommand(String command) {
}
return result;
}

public void splitProgram(String program) {
this.header = "";
this.controlLoop = "";
Boolean headerEndFound = false;
Boolean headerBeginFound = false;
String[] programArray = program.split("\n");

for (int it = 0; it < programArray.length; ++it) {
if (programArray[it].matches("(\\p{Zs}*)# HEADER_BEGIN(\\p{Zs}*)")) {
headerBeginFound = true;
this.header += programArray[it] + "\n";
}
else if (programArray[it].matches("(\\p{Zs}*)# HEADER_END(\\p{Zs}*)") && headerBeginFound) {
headerEndFound = true;
headerBeginFound = false;
this.header += programArray[it] + "\n";
}
else if (headerBeginFound) {
this.header += programArray[it] + "\n";
}
else if (headerEndFound) {
this.controlLoop += programArray[it] + "\n";
}
}

if (headerEndFound == false) {
System.out.println("Unable to find anchors defining header, the program will not be split");
this.header = "";
this.controlLoop = program;
}
}
}