Skip to content

Commit

Permalink
Split the urscript into a header and a control loop. (#14)
Browse files Browse the repository at this point in the history
The header is only appended to the UR program once, while the control loop is appended for each urcap node.
This enables the possibility to have multiple urcap nodes in one program.

Header and control loop are defined by the anchors

# HEADER_BEGIN
<header code>
# HEADER_END
<control loop code>

Co-authored-by: mahp <[email protected]>
Co-authored-by: Felix Exner <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2021
1 parent e640cb5 commit dce9e2e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
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
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
program, while the control loop is inserted for each URCap node in the program tree.

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
```
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;
}
}
}

0 comments on commit dce9e2e

Please sign in to comment.