Commit a2cca7c9 authored by Markus Philipp Bauer's avatar Markus Philipp Bauer
Browse files

ros 2 base changes

parent 90c075f0
......@@ -113,7 +113,7 @@ public class GeneratorRosCpp {
if (generateCMake) {
LanguageUnitRosCMake languageUnitRosCMake = new LanguageUnitRosCMake();
res.add(languageUnitRosCMake.generate(component, languageUnitRosCppAdapter.getAdditionalPackages()));
res.add(languageUnitRosCMake.generate(component, languageUnitRosCppAdapter.getAdditionalPackages(),isRos2Mode()));
}
res.add(apdapter);
......
......@@ -23,13 +23,17 @@ public class LanguageUnitRosCMake {
"\n" +
"export(TARGETS <name> FILE <name>.cmake)";
FileContent generate(EMAComponentInstanceSymbol componentInstanceSymbol, List<String> additionalPackages) {
FileContent generate(EMAComponentInstanceSymbol componentInstanceSymbol, List<String> additionalPackages, boolean isRos2Mode) {
FileContent fileContent = new FileContent();
fileContent.setFileName("CMakeLists.txt");
List<String> allPackages = new ArrayList<>();
allPackages.addAll(additionalPackages);
allPackages.add("roscpp");
if(!isRos2Mode) {
allPackages.add("roscpp");
}else{
allPackages.add("rclcpp");
}
List<String> distinctSortedPackages = allPackages.stream()
.distinct()
......
......@@ -64,21 +64,39 @@ public class LanguageUnitRosCppAdapter {
}
private void generateIncludes(EMAComponentInstanceSymbol componentSymbol, List<EMAPortSymbol> rosPorts, BluePrintCPP currentBluePrint) {
currentBluePrint.addAdditionalIncludeString("<ros/ros.h>");
String compName = NameHelper.getComponentNameTargetLanguage(componentSymbol.getFullName());
currentBluePrint.addAdditionalIncludeString("\"" + compName + ".h\"");
currentBluePrint.addAdditionalIncludeString("\"IAdapter_" + compName + ".h\"");
//Add each msg include exactly once
rosPorts.stream()
.map(EMAPortSymbol::getMiddlewareSymbol)
.map(Optional::get)
.map(mws -> (RosConnectionSymbol) mws)
.map(RosConnectionSymbol::getTopicType)
.map(Optional::get)
.peek(topicType -> additionalPackages.add(NameHelper.getPackageOfMsgType(topicType)))
.map(type -> "<" + type + ".h>")
.forEach(currentBluePrint::addAdditionalIncludeString);
if(!isRos2Mode()) {
currentBluePrint.addAdditionalIncludeString("<ros/ros.h>");
String compName = NameHelper.getComponentNameTargetLanguage(componentSymbol.getFullName());
currentBluePrint.addAdditionalIncludeString("\"" + compName + ".h\"");
currentBluePrint.addAdditionalIncludeString("\"IAdapter_" + compName + ".h\"");
//Add each msg include exactly once
rosPorts.stream()
.map(EMAPortSymbol::getMiddlewareSymbol)
.map(Optional::get)
.map(mws -> (RosConnectionSymbol) mws)
.map(RosConnectionSymbol::getTopicType)
.map(Optional::get)
.peek(topicType -> additionalPackages.add(NameHelper.getPackageOfMsgType(topicType)))
.map(type -> "<" + type + ".h>")
.forEach(currentBluePrint::addAdditionalIncludeString);
}else{
currentBluePrint.addAdditionalIncludeString("<rclpp/rclpp.hpp>");
String compName = NameHelper.getComponentNameTargetLanguage(componentSymbol.getFullName());
currentBluePrint.addAdditionalIncludeString("\"" + compName + ".hpp\"");
currentBluePrint.addAdditionalIncludeString("\"IAdapter_" + compName + ".hpp\"");
//Add each msg include exactly once
rosPorts.stream()
.map(EMAPortSymbol::getMiddlewareSymbol)
.map(Optional::get)
.map(mws -> (RosConnectionSymbol) mws)
.map(RosConnectionSymbol::getTopicType)
.map(Optional::get)
.peek(topicType -> additionalPackages.add(NameHelper.getPackageOfMsgType(topicType)))
.map(type -> "<" + type + ".hpp>")
.forEach(currentBluePrint::addAdditionalIncludeString);
}
msgConverts.stream()
.map(MsgConverter::getAdditionalIncludes)
......@@ -146,18 +164,21 @@ public class LanguageUnitRosCppAdapter {
//subs
subscribers.keySet().stream()
.map(var -> new SubscribeInstruction(classname, var, subscribers.get(var).getTopicName().get(), getTopicNameTargetLanguage(subscribers.get(var).getTopicName().get()) + "Callback", ros2Mode, getFullRosType(subscribers.get(var))))
.map(var -> new SubscribeInstruction(classname, var, subscribers.get(var).getTopicName().get(), getTopicNameTargetLanguage(subscribers.get(var).getTopicName().get()) + "Callback", isRos2Mode(), 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(),ros2Mode))
.map(var -> new AdvertiseInstruction(var, getFullRosType(publishers.get(var)), publishers.get(var).getTopicName().get(),isRos2Mode()))
.distinct()
.sorted(Comparator.comparing(AdvertiseInstruction::getTargetLanguageInstruction))
.forEach(initMethod::addInstruction);
initMethod.addInstruction(new TargetCodeInstruction("ros::spin();"));
if(!isRos2Mode()) {
initMethod.addInstruction(new TargetCodeInstruction("ros::spin();"));
}else{
initMethod.addInstruction(new TargetCodeInstruction("rclcpp::spin("+ classname + ");"));
}
currentBluePrint.addMethod(initMethod);
}
......@@ -182,7 +203,11 @@ public class LanguageUnitRosCppAdapter {
String name = getTopicNameTargetLanguage(rosConnectionSymbol.getTopicName().get()).toLowerCase() + "Subscriber";
if (!uniqueSubFields.containsKey(name)) {
Variable field = new Variable();
field.setTypeNameTargetLanguage("ros::Subscriber");
if(!isRos2Mode()) {
field.setTypeNameTargetLanguage("ros::Subscriber");
}else{
field.setTypeNameTargetLanguage("rclpp::Subscriber");
}
field.setName(name);
uniqueSubFields.put(name, field);
currBluePrint.addVariable(field);
......@@ -198,7 +223,11 @@ public class LanguageUnitRosCppAdapter {
String name = getTopicNameTargetLanguage(rosConnectionSymbol.getTopicName().get()).toLowerCase() + "Publisher";
if (!uniquePubFields.containsKey(name)) {
Variable field = new Variable();
field.setTypeNameTargetLanguage("ros::Publisher");
if(!isRos2Mode()) {
field.setTypeNameTargetLanguage("ros::Publisher");
}else{
field.setTypeNameTargetLanguage("rclpp::Publisher");
}
field.setName(name);
uniquePubFields.put(name, field);
currBluePrint.addVariable(field);
......@@ -241,7 +270,12 @@ public class LanguageUnitRosCppAdapter {
Method method = new Method(getTopicNameTargetLanguage(rosCon.getTopicName().get()) + "Callback", "void");
Variable tmpParam = new Variable();
tmpParam.setName("msg");
tmpParam.setTypeNameTargetLanguage("const " + getFullRosType(rosCon) + "::ConstPtr&");
if(!isRos2Mode())
{
tmpParam.setTypeNameTargetLanguage("const " + getFullRosType(rosCon) + "::ConstPtr&");
}else{
tmpParam.setTypeNameTargetLanguage("const " + getFullRosType(rosCon) + "::SharedPtr");
}
method.addParameter(tmpParam);
uniqueMethods.put(rosCon.getTopicName().get(), method);
}
......
......@@ -21,7 +21,10 @@ public class InitHelper {
result.add(new TargetCodeInstruction("ros::init(i, &tmp, \"" + classname + "_node\");"));
result.add(new TargetCodeInstruction("ros::NodeHandle node_handle = ros::NodeHandle();"));
}else{
//TODO: fill
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("rclcpp::init(i, &tmp);"));
}
return result;
}
......
......@@ -12,7 +12,7 @@ public class SubscribeInstruction extends TargetCodeInstruction {
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 + ");";
this.instruction = subscriber.getNameTargetLanguageFormat() + " = node_handle->create_subscription<" + fullRosType + ">(\"" + topicName + "\", " + callback + ");";
}
}
@Override
......
......@@ -10,14 +10,16 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class Ros2Test extends AbstractSymtabTest{
//TODO: change resources/results/echoRos2 to ros2
@Test
public void echoCompTest() throws IOException {
public void echoCMakeRos2() throws IOException {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
RosToEmamTagSchema.registerTagTypes(taggingResolver);
......@@ -25,12 +27,31 @@ public class Ros2Test extends AbstractSymtabTest{
assertNotNull(componentInstanceSymbol);
GeneratorRosCpp generatorRosCpp = new GeneratorRosCpp();
generatorRosCpp.setGenerationTargetPath("./target/generated-sources-roscpp/echoRos2/");
generatorRosCpp.setGenerationTargetPath("./target/generated-sources-rclcpp/echoCMakeRos2/");
generatorRosCpp.setGenerateCMake(true);
generatorRosCpp.setRos2Mode(true);
List<File> files = TagHelper.resolveAndGenerate(generatorRosCpp, taggingResolver, componentInstanceSymbol);
testFilesAreEqual(files, "echoRos2/");
testFilesAreEqual(files, "echoCMakeRos2/");
}
@Test
public void testGenerateCMakeRos2() throws IOException {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("tests.a.compA", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
GeneratorRosCpp generatorRosCpp = new GeneratorRosCpp();
generatorRosCpp.setGenerationTargetPath("./target/generated-sources-rclcpp/CMakeRos2/");
generatorRosCpp.setGenerateCMake(true);
generatorRosCpp.setRos2Mode(true);
List<File> files = generatorRosCpp.generateFiles(componentInstanceSymbol, taggingResolver);
List<String> fileNames = files.stream()
.map(File::getName)
.collect(Collectors.toList());
assertTrue(fileNames.contains("CMakeLists.txt"));
}
}
cmake_minimum_required(VERSION 3.5)
project (RosAdapter_tests_a_compA)
find_package(automated_driving_msgs REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rosgraph_msgs REQUIRED)
add_library(RosAdapter_tests_a_compA RosAdapter_tests_a_compA.h)
set_target_properties(RosAdapter_tests_a_compA PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(RosAdapter_tests_a_compA tests_a_compA ${automated_driving_msgs_LIBRARIES} ${rclcpp_LIBRARIES} ${rosgraph_msgs_LIBRARIES})
target_include_directories(RosAdapter_tests_a_compA PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${automated_driving_msgs_INCLUDE_DIRS} ${rclcpp_INCLUDE_DIRS} ${rosgraph_msgs_INCLUDE_DIRS})
export(TARGETS RosAdapter_tests_a_compA FILE RosAdapter_tests_a_compA.cmake)
\ No newline at end of file
#pragma once
#include "IAdapter_tests_a_compA.hpp"
#include "tests_a_compA.hpp"
#include <automated_driving_msgs/StampedFloat64.hpp>
#include <rclpp/rclpp.hpp>
#include <rosgraph_msgs/Clock.hpp>
class RosAdapter_tests_a_compA: public IAdapter_tests_a_compA{
tests_a_compA* component;
rclpp::Subscriber _clockSubscriber;
rclpp::Publisher _echoPublisher;
public:
RosAdapter_tests_a_compA(){
}
void _clockCallback(const rosgraph_msgs::Clock::SharedPtr msg){
component->rosIn = msg->clock.toSec();
}
void init(tests_a_compA* comp){
this->component = comp;
char* tmp = strdup("");
int i = 0;
rclcpp::init(i, &tmp);
_clockSubscriber = node_handle->create_subscription<rosgraph_msgs::Clock>("/clock", _clockCallback);
_echoPublisher = node_handle->create_publisher<automated_driving_msgs::StampedFloat64>("/echo");
rclcpp::spin(RosAdapter_tests_a_compA);
}
void publish_echoPublisher(){
automated_driving_msgs::StampedFloat64 tmpMsg;
tmpMsg.data = component->rosOut;
_echoPublisher.publish(tmpMsg);
}
void tick(){
publish_echoPublisher();
}
};
\ No newline at end of file
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