Commit 813ffcc8 authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Fix generation when message fields and structs are used

parent bd15dd7c
Pipeline #157345 passed with stages
in 3 minutes and 30 seconds
......@@ -9,7 +9,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-roscpp-generator</artifactId>
<version>0.1.4-SNAPSHOT</version>
<version>0.1.5-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
......
......@@ -14,7 +14,7 @@ public class SetStructMsgInstruction{
private SetStructMsgInstruction() {
}
public static String getInstruction(EMAPortSymbol portSymbol, RosMsg rosMsg) {
public static String getInstruction(EMAPortSymbol portSymbol, RosMsg rosMsg, String fieldPrefix) {
String inst;
if (rosMsg.getName().startsWith("std_msgs/")) {
if (rosMsg.getName().endsWith("MultiArray")) {
......@@ -26,7 +26,7 @@ public class SetStructMsgInstruction{
dataSize += (i == 0 ? "" : " * ") + dimSizes.get(i);
}
inst = "tmpMsg.data.resize(" + dataSize + ");\n";
inst = "tmpMsg" + fieldPrefix + ".data.resize(" + dataSize + ");\n";
inst += "int counter = 0;\n";
String indexString = "";
for (int i = 0; i < dimSizes.size(); i++) {
......@@ -35,7 +35,7 @@ public class SetStructMsgInstruction{
inst += "for(int " + curInd + " = 0; " + curInd + " < " + dimSizes.get(i) + "; " + curInd + "++){\n";
}
inst += "tmpMsg.data[counter] = (component->" + NameHelper.getPortNameTargetLanguage(portSymbol) + ")(" + indexString + ")";
inst += "tmpMsg" + fieldPrefix + ".data[counter] = (component->" + NameHelper.getPortNameTargetLanguage(portSymbol) + ")(" + indexString + ")";
//TODO: check type not name
if (rosMsg.getName().equals("std_msgs/ByteMultiArray")) {
//is a bool msg
......@@ -51,13 +51,13 @@ public class SetStructMsgInstruction{
} else {
inst = NameHelper.getAllFieldNames(rosMsg).stream()
.map(field -> "tmpMsg." + field + " = component->" + portSymbol.getName() + ";")
.map(field -> "tmpMsg." + fieldPrefix + field + " = component->" + portSymbol.getName() + ";")
.sorted()
.collect(Collectors.joining("\n"));
}
} else {
inst = NameHelper.getAllFieldNames(rosMsg).stream()
.map(field -> "tmpMsg." + field + " = component->" + portSymbol.getName() + "." + field + ";")
.map(field -> "tmpMsg." + fieldPrefix + field + " = component->" + portSymbol.getName() + "." + field + ";")
.sorted()
.collect(Collectors.joining("\n"));
}
......
......@@ -18,7 +18,7 @@ public class SetStructPortInstruction{
private SetStructPortInstruction() {
}
public static String getInstruction(EMAPortSymbol port, RosMsg rosMsg) {
public static String getInstruction(EMAPortSymbol port, RosMsg rosMsg, String fieldPrefix) {
String inst;
if (rosMsg.getName().startsWith("std_msgs/")) {
if (rosMsg.getName().endsWith("MultiArray")) {
......@@ -34,7 +34,7 @@ public class SetStructPortInstruction{
inst += "for(int " + curInd + " = 0; " + curInd + " < " + dimSizes.get(i) + "; " + curInd + "++){\n";
}
inst += "(component->" + NameHelper.getPortNameTargetLanguage(port) + ")(" + indexString + ") = msg->data[counter]";
inst += "(component->" + NameHelper.getPortNameTargetLanguage(port) + ")(" + indexString + ") = msg->" + fieldPrefix + "data[counter]";
//TODO: check type not name
if (rosMsg.getName().equals("std_msgs/ByteMultiArray")) {
//is a bool msg
......@@ -50,7 +50,7 @@ public class SetStructPortInstruction{
} else {
inst = NameHelper.getAllFieldNames(rosMsg).stream()
.map(field -> "component->" + NameHelper.getPortNameTargetLanguage(port) + " = msg->" + field + ";")
.map(field -> "component->" + NameHelper.getPortNameTargetLanguage(port) + " = msg->" + fieldPrefix + field + ";")
.sorted()
.collect(Collectors.joining("\n"));
}
......@@ -86,7 +86,7 @@ public class SetStructPortInstruction{
}
inst = structFieldNames.stream()
.map(field -> "component->" + NameHelper.getPortNameTargetLanguage(port) + "." + field + " = msg->" + structToMsgField.get(field) + ";")
.map(field -> "component->" + NameHelper.getPortNameTargetLanguage(port) + "." + field + " = msg->" + fieldPrefix + structToMsgField.get(field) + ";")
.sorted()
.collect(Collectors.joining("\n"));
}
......
......@@ -4,9 +4,14 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.cncModel
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosConnectionSymbol;
import de.monticore.lang.monticar.generator.roscpp.helper.NameHelper;
import de.monticore.lang.monticar.generator.rosmsg.GeneratorRosMsg;
import de.monticore.lang.monticar.generator.rosmsg.RosField;
import de.monticore.lang.monticar.generator.rosmsg.RosMsg;
import de.monticore.lang.monticar.struct._symboltable.StructSymbol;
import de.monticore.lang.monticar.ts.MCTypeSymbol;
import de.se_rwth.commons.logging.Log;
import java.util.Arrays;
import java.util.stream.Collectors;
public abstract class RosInterface {
protected EMAPortSymbol port;
......@@ -14,7 +19,30 @@ public abstract class RosInterface {
public RosMsg getRosMsg() {
String packageName = Arrays.stream(getTopicType().split("/")).findFirst().get();
return GeneratorRosMsg.getRosType(packageName, this.getPort().getTypeReference(), false);
RosMsg res = GeneratorRosMsg.getRosType(packageName, this.getPort().getTypeReference(), false);
if(rosConnectionSymbol.getMsgField().isPresent()){
String msgField = rosConnectionSymbol.getMsgField().get();
String[] parts = msgField.split("\\.");
String path = "";
for(String p : parts){
path += (path.length() == 0 ? "." : "") + p;
boolean found = false;
for(RosField field : res.getFields()){
if (field.getType() instanceof RosMsg)
if (field.getName().equals(msgField)) {
res = (RosMsg) field.getType();
found = true;
}else{
Log.error("Found field " + path + ", but it has a primitive type instead of a message type!");
}
}
if(!found){
Log.error("Can not find field " + path + "!");
}
}
}
return res;
}
public RosMsg getRos2Msg() {
......@@ -66,4 +94,12 @@ public abstract class RosInterface {
return NameHelper.msgTypeToSnakecase(getTopicType()) + ".hpp";
}
public boolean isStructInterface(){
if(!port.getTypeReference().existsReferencedSymbol()){
return false;
}
return port.getTypeReference().getReferencedSymbol() instanceof StructSymbol;
}
}
......@@ -31,11 +31,13 @@ public class RosPublisher extends RosInterface {
@Override
public String getRosSetStructInstruction() {
return SetStructMsgInstruction.getInstruction(getPort(), getRosMsg());
String fieldPrefix = this.rosConnectionSymbol.getMsgField().map(msgfield -> msgfield + ".").orElse("");
return SetStructMsgInstruction.getInstruction(getPort(), getRosMsg(), fieldPrefix);
}
@Override
public String getRos2SetStructInstruction() {
return SetStructMsgInstruction.getInstruction(getPort(), getRos2Msg());
String fieldPrefix = this.rosConnectionSymbol.getMsgField().map(msgfield -> msgfield + ".").orElse("");
return SetStructMsgInstruction.getInstruction(getPort(), getRos2Msg(),fieldPrefix);
}
}
......@@ -31,11 +31,13 @@ public class RosSubscriber extends RosInterface {
@Override
public String getRosSetStructInstruction() {
return SetStructPortInstruction.getInstruction(getPort(), getRosMsg());
String fieldPrefix = this.rosConnectionSymbol.getMsgField().map(msgfield -> msgfield + ".").orElse("");
return SetStructPortInstruction.getInstruction(getPort(), getRosMsg(), fieldPrefix);
}
@Override
public String getRos2SetStructInstruction() {
return SetStructPortInstruction.getInstruction(getPort(), getRos2Msg());
String fieldPrefix = this.rosConnectionSymbol.getMsgField().map(msgfield -> msgfield + ".").orElse("");
return SetStructPortInstruction.getInstruction(getPort(), getRos2Msg(), fieldPrefix);
}
}
......@@ -38,7 +38,7 @@
<#macro callback sub>
void ${sub.getMethodName()}(const ${sub.getTypeNameInTargetLanguage()}::SharedPtr msg){
<#if sub.getRosConnectionSymbol().getMsgField().isPresent()>
<#if !sub.isStructInterface() && sub.getRosConnectionSymbol().getMsgField().isPresent()>
component->${sub.getPortNameInTargetLanguage()} = msg->${sub.getRosConnectionSymbol().getMsgField().get()};
<#else>
${sub.getRos2SetStructInstruction()}
......@@ -50,7 +50,7 @@
<#macro publish pub>
void publish${pub.getNameInTargetLanguage()}(){
${pub.getTypeNameInTargetLanguage()} tmpMsg;
<#if pub.getRosConnectionSymbol().getMsgField().isPresent()>
<#if !pub.isStructInterface() && pub.getRosConnectionSymbol().getMsgField().isPresent()>
tmpMsg.${pub.getRosConnectionSymbol().getMsgField().get()} = component->${pub.getPortNameInTargetLanguage()};
<#else>
${pub.getRos2SetStructInstruction()}
......
......@@ -37,7 +37,7 @@ ros::Publisher ${name};
<#macro callback sub>
void ${sub.getMethodName()}(const ${sub.getTypeNameInTargetLanguage()}::ConstPtr& msg){
<#if sub.getRosConnectionSymbol().getMsgField().isPresent()>
<#if !sub.isStructInterface() && sub.getRosConnectionSymbol().getMsgField().isPresent()>
component->${sub.getPortNameInTargetLanguage()} = msg->${sub.getRosConnectionSymbol().getMsgField().get()};
<#else>
${sub.getRosSetStructInstruction()}
......@@ -49,7 +49,7 @@ void ${sub.getMethodName()}(const ${sub.getTypeNameInTargetLanguage()}::ConstPtr
<#macro publish pub>
void publish${pub.getNameInTargetLanguage()}(){
${pub.getTypeNameInTargetLanguage()} tmpMsg;
<#if pub.getRosConnectionSymbol().getMsgField().isPresent()>
<#if !pub.isStructInterface() && pub.getRosConnectionSymbol().getMsgField().isPresent()>
tmpMsg.${pub.getRosConnectionSymbol().getMsgField().get()} = component->${pub.getPortNameInTargetLanguage()};
<#else>
${pub.getRosSetStructInstruction()}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment