Skip to content

Commit dce9e2e

Browse files
urmahpmahpFelix Exner
authored
Split the urscript into a header and a control loop. (#14)
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]>
1 parent e640cb5 commit dce9e2e

File tree

4 files changed

+88
-11
lines changed

4 files changed

+88
-11
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ To enable external control of a UR robot from a remote PC, this URcap must be in
1616
* Add this URcap to a program by selecting it from the side menu under the tab _URcap_.
1717
* Execute the program by pressing the _play_ button in the _Program_ tab of Polyscope.
1818

19+
### Multiple URCap nodes
20+
To use this URCap node multiple times in a ur program, the control script is divided into two
21+
scripts. After receiving the script, it is divided into a header part and a control loop part. The
22+
header part consist of all the function deffinitions. The header is only inserted once in the
23+
program, while the control loop is inserted for each URCap node in the program tree.
24+
25+
To be able to distinguish between header and control loop, the header part of the script should be
26+
encapsulated in:
27+
```bash
28+
# HEADER_BEGIN
29+
Here goes the header code
30+
# HEADER_END
31+
32+
# NODE_CONTROL_LOOP_BEGINS
33+
Here goes the control loop code
34+
# NODE_CONTROL_LOOP_ENDS
35+
```
36+
If its not possible to find either `# HEADER_BEGIN` or `# HEADER_END`, the script will not be
37+
divided into two scripts and it will not be possible to have multiple URCap nodes in one program.

src/main/java/com/fzi/externalcontrol/impl/ExternalControlInstallationNodeContribution.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ public class ExternalControlInstallationNodeContribution implements Installation
3737
private static final String HOST_IP = "host_ip";
3838
private static final String PORT_NR = "port_nr";
3939
private static final String NAME = "name";
40-
private String urScriptProgram = "";
4140
private static final String DEFAULT_IP = "192.168.56.1";
4241
private static final String DEFAULT_PORT = "50002";
4342
private static final String DEFAULT_NAME = DEFAULT_IP;
4443
private DataModel model;
4544
private final ExternalControlInstallationNodeView view;
4645
private final KeyboardInputFactory keyboardFactory;
46+
private RequestProgram sender;
47+
private boolean programRequested;
4748

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

6667
@Override
6768
public void generateScript(ScriptWriter writer) {
68-
// Make sure the program gets requested at least once
69-
urScriptProgram = "";
69+
programRequested = false;
7070
}
7171

7272
// IP helper functions
@@ -168,11 +168,17 @@ public void onOk(String value) {
168168
};
169169
}
170170

171-
public String getUrScriptProgram() {
172-
if (urScriptProgram == "") {
173-
RequestProgram sender = new RequestProgram(getHostIP(), getCustomPort());
174-
urScriptProgram = sender.sendCommand("request_program\n");
175-
}
176-
return urScriptProgram;
171+
public String getControlLoop(ScriptWriter writer) {
172+
if (programRequested == false) {
173+
// Request and split the program
174+
sender = new RequestProgram(getHostIP(), getCustomPort());
175+
sender.requestAndSplitProgram();
176+
177+
// Append header to the ur program.
178+
writer.appendRaw(sender.getHeader());
179+
180+
programRequested = true;
181+
}
182+
return sender.getControlLoop();
177183
}
178184
}

src/main/java/com/fzi/externalcontrol/impl/ExternalControlProgramNodeContribution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public boolean isDefined() {
7979

8080
@Override
8181
public void generateScript(ScriptWriter writer) {
82-
String urScriptProgram = getInstallation().getUrScriptProgram();
83-
writer.appendRaw(urScriptProgram);
82+
String controlLoop = getInstallation().getControlLoop(writer);
83+
writer.appendRaw(controlLoop);
8484
}
8585

8686
private ExternalControlInstallationNodeContribution getInstallation() {

src/main/java/com/fzi/externalcontrol/impl/RequestProgram.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,33 @@ public class RequestProgram {
3737
private final String hostIp;
3838
// custom port
3939
private final int portNr;
40+
// header of the requested program
41+
private String header;
42+
// control loop of the requested program
43+
private String controlLoop;
4044

4145
/*
4246
* Default constructor
4347
*/
4448
public RequestProgram(String hostIp, String portNr) {
4549
this.hostIp = hostIp;
4650
this.portNr = Integer.parseInt(portNr);
51+
this.header = "";
52+
this.controlLoop = "";
53+
}
54+
55+
public void requestAndSplitProgram() {
56+
String program = sendCommand("request_program\n");
57+
// Split program into header and control loop
58+
splitProgram(program);
59+
}
60+
61+
public String getHeader() {
62+
return this.header;
63+
}
64+
65+
public String getControlLoop() {
66+
return this.controlLoop;
4767
}
4868

4969
public String sendCommand(String command) {
@@ -89,4 +109,36 @@ public String sendCommand(String command) {
89109
}
90110
return result;
91111
}
112+
113+
public void splitProgram(String program) {
114+
this.header = "";
115+
this.controlLoop = "";
116+
Boolean headerEndFound = false;
117+
Boolean headerBeginFound = false;
118+
String[] programArray = program.split("\n");
119+
120+
for (int it = 0; it < programArray.length; ++it) {
121+
if (programArray[it].matches("(\\p{Zs}*)# HEADER_BEGIN(\\p{Zs}*)")) {
122+
headerBeginFound = true;
123+
this.header += programArray[it] + "\n";
124+
}
125+
else if (programArray[it].matches("(\\p{Zs}*)# HEADER_END(\\p{Zs}*)") && headerBeginFound) {
126+
headerEndFound = true;
127+
headerBeginFound = false;
128+
this.header += programArray[it] + "\n";
129+
}
130+
else if (headerBeginFound) {
131+
this.header += programArray[it] + "\n";
132+
}
133+
else if (headerEndFound) {
134+
this.controlLoop += programArray[it] + "\n";
135+
}
136+
}
137+
138+
if (headerEndFound == false) {
139+
System.out.println("Unable to find anchors defining header, the program will not be split");
140+
this.header = "";
141+
this.controlLoop = program;
142+
}
143+
}
92144
}

0 commit comments

Comments
 (0)