Commit 90c075f0 authored by Markus Philipp Bauer's avatar Markus Philipp Bauer
Browse files

ros 2 base changes

parent 8e95f43a
Pipeline #88806 failed with stages
in 1 minute and 2 seconds
......@@ -23,6 +23,15 @@ public class GeneratorRosCpp {
private String generationTargetPath;
private boolean generateCMake = false;
private boolean ros2Mode = false;
public boolean isRos2Mode() {
return ros2Mode;
}
public void setRos2Mode(boolean ros2Mode) {
this.ros2Mode = ros2Mode;
}
public void setGenerateCMake(boolean generateCMake) {
this.generateCMake = generateCMake;
......@@ -81,6 +90,7 @@ public class GeneratorRosCpp {
FileContent apdapter = new FileContent();
LanguageUnitRosCppAdapter languageUnitRosCppAdapter = new LanguageUnitRosCppAdapter();
languageUnitRosCppAdapter.setRos2Mode(this.isRos2Mode());
Optional<BluePrintCPP> currentBluePrint = languageUnitRosCppAdapter.generateBluePrint(component);
if(currentBluePrint.isPresent()) {
......
......@@ -3,6 +3,7 @@ package de.monticore.lang.monticar.generator.roscpp;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.cncModel.EMAPortSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosConnectionSymbol;
import de.monticore.lang.monticar.generator.roscpp.helper.InitHelper;
import de.monticore.lang.monticar.generator.roscpp.util.*;
import de.monticore.lang.monticar.generator.roscpp.helper.NameHelper;
import de.monticore.lang.monticar.generator.roscpp.helper.TagHelper;
......@@ -23,6 +24,15 @@ public class LanguageUnitRosCppAdapter {
private List<MsgConverter> msgConverts = new ArrayList<>();
private List<String> additionalPackages = new ArrayList<>();
private Map<RosMsg, MCTypeReference<? extends MCTypeSymbol>> usedRosMsgs = new HashMap<>();
private boolean ros2Mode = false;
public boolean isRos2Mode() {
return ros2Mode;
}
public void setRos2Mode(boolean ros2Mode) {
this.ros2Mode = ros2Mode;
}
public List<String> getAdditionalPackages() {
return additionalPackages;
......@@ -132,21 +142,17 @@ public class LanguageUnitRosCppAdapter {
compPointer.setName("comp");
initMethod.addParameter(compPointer);
initMethod.addInstruction(new TargetCodeInstruction("this->component = comp;"));
initMethod.addInstruction(new TargetCodeInstruction("char* tmp = strdup(\"\");"));
initMethod.addInstruction(new TargetCodeInstruction("int i = 0;"));
initMethod.addInstruction(new TargetCodeInstruction("ros::init(i, &tmp, \"" + classname + "_node\");"));
initMethod.addInstruction(new TargetCodeInstruction("ros::NodeHandle node_handle = ros::NodeHandle();"));
InitHelper.getInitInstructions(classname,isRos2Mode()).forEach(initMethod::addInstruction);
//subs
subscribers.keySet().stream()
.map(var -> new SubscribeInstruction(classname, var, subscribers.get(var).getTopicName().get(), getTopicNameTargetLanguage(subscribers.get(var).getTopicName().get()) + "Callback"))
.map(var -> new SubscribeInstruction(classname, var, subscribers.get(var).getTopicName().get(), getTopicNameTargetLanguage(subscribers.get(var).getTopicName().get()) + "Callback", ros2Mode, getFullRosType(subscribers.get(var))))
.distinct()
.sorted(Comparator.comparing(SubscribeInstruction::getTargetLanguageInstruction))
.forEach(initMethod::addInstruction);
publishers.keySet().stream()
.map(var -> new AdvertiseInstruction(var, getFullRosType(publishers.get(var)), publishers.get(var).getTopicName().get()))
.map(var -> new AdvertiseInstruction(var, getFullRosType(publishers.get(var)), publishers.get(var).getTopicName().get(),ros2Mode))
.distinct()
.sorted(Comparator.comparing(AdvertiseInstruction::getTargetLanguageInstruction))
.forEach(initMethod::addInstruction);
......
package de.monticore.lang.monticar.generator.roscpp.helper;
import de.monticore.lang.monticar.generator.roscpp.util.Instruction;
import de.monticore.lang.monticar.generator.roscpp.util.TargetCodeInstruction;
import java.util.ArrayList;
import java.util.List;
public class InitHelper {
private InitHelper(){
}
public static List<Instruction> getInitInstructions(String classname, boolean isRos2){
List<Instruction> result = new ArrayList<>();
if (!isRos2){
result.add(new TargetCodeInstruction("this->component = comp;"));
result.add(new TargetCodeInstruction("char* tmp = strdup(\"\");"));
result.add(new TargetCodeInstruction("int i = 0;"));
result.add(new TargetCodeInstruction("ros::init(i, &tmp, \"" + classname + "_node\");"));
result.add(new TargetCodeInstruction("ros::NodeHandle node_handle = ros::NodeHandle();"));
}else{
//TODO: fill
}
return result;
}
}
......@@ -8,10 +8,13 @@ import java.util.Objects;
public class AdvertiseInstruction extends TargetCodeInstruction {
private static final int MSG_QUEUE_SIZE = 5;
public AdvertiseInstruction(Variable publisher, String fullRosType, String topicName) {
this.instruction = publisher.getNameTargetLanguageFormat() + " = node_handle.advertise<" + fullRosType + ">(\"" + topicName + "\"," + MSG_QUEUE_SIZE + ");";
public AdvertiseInstruction(Variable publisher, String fullRosType, String topicName, boolean isRos2) {
if (!isRos2) {
this.instruction = publisher.getNameTargetLanguageFormat() + " = node_handle.advertise<" + fullRosType + ">(\"" + topicName + "\"," + MSG_QUEUE_SIZE + ");";
}else{
this.instruction = publisher.getNameTargetLanguageFormat() + " = node_handle->create_publisher<" + fullRosType + ">(\"" + topicName + "\");";
}
}
@Override
public boolean equals(Object other) {
if (!(other instanceof AdvertiseInstruction)) return false;
......
......@@ -8,10 +8,13 @@ import java.util.Objects;
public class SubscribeInstruction extends TargetCodeInstruction {
private static final int MSG_QUEUE_SIZE = 5;
public SubscribeInstruction(String className, Variable subscriber, String topicName, String callback) {
this.instruction = subscriber.getNameTargetLanguageFormat() + " = node_handle.subscribe(\"" + topicName + "\" ," + MSG_QUEUE_SIZE + ",&" + className + "::" + callback + ", this, ros::TransportHints().tcpNoDelay());";
public SubscribeInstruction(String className, Variable subscriber, String topicName, String callback, boolean isRos2, String fullRosType) {
if (!isRos2) {
this.instruction = subscriber.getNameTargetLanguageFormat() + " = node_handle.subscribe(\"" + topicName + "\" ," + MSG_QUEUE_SIZE + ",&" + className + "::" + callback + ", this, ros::TransportHints().tcpNoDelay());";
} else {
this.instruction = subscriber.getNameTargetLanguageFormat() + " = node_handle->create_subscribe<" + fullRosType + ">(" + topicName + ", " + callback + ");";
}
}
@Override
public boolean equals(Object other) {
if (!(other instanceof SubscribeInstruction)) return false;
......
package de.monticore.lang.monticar.generator.roscpp;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosToEmamTagSchema;
import de.monticore.lang.monticar.generator.roscpp.helper.TagHelper;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertNotNull;
public class Ros2Test extends AbstractSymtabTest{
//TODO: change resources/results/echoRos2 to ros2
@Test
public void echoCompTest() throws IOException {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
RosToEmamTagSchema.registerTagTypes(taggingResolver);
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("tests.a.compA", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
GeneratorRosCpp generatorRosCpp = new GeneratorRosCpp();
generatorRosCpp.setGenerationTargetPath("./target/generated-sources-roscpp/echoRos2/");
generatorRosCpp.setGenerateCMake(true);
generatorRosCpp.setRos2Mode(true);
List<File> files = TagHelper.resolveAndGenerate(generatorRosCpp, taggingResolver, componentInstanceSymbol);
testFilesAreEqual(files, "echoRos2/");
}
}
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