Commit e5788144 authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Fix ROS2 generated code

parent cc26d4ea
Pipeline #96867 passed with stages
in 2 minutes and 15 seconds
......@@ -17,7 +17,7 @@ public class LanguageUnitRosCMake {
"\n" +
"add_library(<name> <name>.cpp)\n" +
"set_target_properties(<name> PROPERTIES LINKER_LANGUAGE CXX)\n" +
"target_link_libraries(<name> <compName> <libraries>)\n" +
"target_link_libraries(<name> <compName> IAdapter_<compName> <libraries>)\n" +
"target_include_directories(<name> PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} <include_dirs>)\n" +
"<dependency>\n" +
"\n" +
......
......@@ -81,10 +81,10 @@ public class LanguageUnitRosCppAdapter {
.map(type -> "<" + type + ".h>")
.forEach(currentBluePrint::addAdditionalIncludeString);
}else{
currentBluePrint.addAdditionalIncludeString("<rclpp/rclpp.hpp>");
currentBluePrint.addAdditionalIncludeString("<rclcpp/rclcpp.hpp>");
String compName = NameHelper.getComponentNameTargetLanguage(componentSymbol.getFullName());
currentBluePrint.addAdditionalIncludeString("\"" + compName + ".hpp\"");
currentBluePrint.addAdditionalIncludeString("\"IAdapter_" + compName + ".hpp\"");
currentBluePrint.addAdditionalIncludeString("\"" + compName + ".h\"");
currentBluePrint.addAdditionalIncludeString("\"IAdapter_" + compName + ".h\"");
//Add each msg include exactly once
rosPorts.stream()
......@@ -94,6 +94,15 @@ public class LanguageUnitRosCppAdapter {
.map(RosConnectionSymbol::getTopicType)
.map(Optional::get)
.peek(topicType -> additionalPackages.add(NameHelper.getPackageOfMsgType(topicType)))
.map(type ->{
if(type.contains("/msg/")) {
String[] parts = type.split("/");
parts[parts.length - 1] = parts[parts.length - 1].substring(0, 1).toLowerCase() + parts[parts.length - 1].substring(1);
return String.join("/", parts);
}else{
return type;
}
})
.map(type -> "<" + type + ".hpp>")
.forEach(currentBluePrint::addAdditionalIncludeString);
}
......@@ -135,7 +144,7 @@ public class LanguageUnitRosCppAdapter {
publishers.keySet().forEach(var -> {
Method method = topicToMethod.get(publishers.get(var).getTopicName().get());
method.addInstruction(new PublishInstruction(var));
method.addInstruction(new PublishInstruction(var, ros2Mode));
currentBluePrint.addMethod(method);
});
}
......@@ -177,7 +186,7 @@ public class LanguageUnitRosCppAdapter {
if(!isRos2Mode()) {
initMethod.addInstruction(new TargetCodeInstruction("ros::spin();"));
}else{
initMethod.addInstruction(new TargetCodeInstruction("rclcpp::spin("+ classname + ");"));
initMethod.addInstruction(new TargetCodeInstruction("rclcpp::spin(node_handle);"));
}
currentBluePrint.addMethod(initMethod);
......@@ -206,7 +215,7 @@ public class LanguageUnitRosCppAdapter {
if(!isRos2Mode()) {
field.setTypeNameTargetLanguage("ros::Subscriber");
}else{
field.setTypeNameTargetLanguage("rclpp::Subscriber");
field.setTypeNameTargetLanguage("rclcpp::Subscription<" + getFullRosType(rosConnectionSymbol) + ">::SharedPtr");
}
field.setName(name);
uniqueSubFields.put(name, field);
......@@ -226,7 +235,7 @@ public class LanguageUnitRosCppAdapter {
if(!isRos2Mode()) {
field.setTypeNameTargetLanguage("ros::Publisher");
}else{
field.setTypeNameTargetLanguage("rclpp::Publisher");
field.setTypeNameTargetLanguage("rclcpp::Publisher<" + getFullRosType(rosConnectionSymbol) + ">::SharedPtr");
}
field.setName(name);
uniquePubFields.put(name, field);
......
......@@ -23,8 +23,9 @@ public class InitHelper {
}else{
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("int i = 1;"));
result.add(new TargetCodeInstruction("rclcpp::init(i, &tmp);"));
result.add(new TargetCodeInstruction("auto node_handle = rclcpp::Node::make_shared(\""+ classname +"\");"));
}
return result;
}
......
......@@ -5,7 +5,9 @@ import de.monticore.lang.monticar.generator.roscpp.util.Method;
import de.monticore.lang.monticar.generator.roscpp.util.Variable;
import de.monticore.lang.monticar.generator.roscpp.util.BluePrintCPP;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
public class PrinterHelper {
......
......@@ -5,7 +5,11 @@ import de.monticore.lang.monticar.generator.roscpp.util.Variable;
public class PublishInstruction extends TargetCodeInstruction {
public PublishInstruction(Variable publisher) {
this.instruction = publisher.getNameTargetLanguageFormat() + ".publish(tmpMsg);";
public PublishInstruction(Variable publisher, boolean ros2Mode) {
if(!ros2Mode) {
this.instruction = publisher.getNameTargetLanguageFormat() + ".publish(tmpMsg);";
}else{
this.instruction = publisher.getNameTargetLanguageFormat() + "->publish(tmpMsg);";
}
}
}
......@@ -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_subscription<" + fullRosType + ">(\"" + topicName + "\", " + callback + ");";
this.instruction = subscriber.getNameTargetLanguageFormat() + " = node_handle->create_subscription<" + fullRosType + ">(\"" + topicName + "\", std::bind(&" + className + "::" + callback + ", this, std::placeholders::_1));";
}
}
@Override
......
......@@ -23,7 +23,7 @@ public class Ros2Test extends AbstractSymtabTest{
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
RosToEmamTagSchema.registerTagTypes(taggingResolver);
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("tests.a.compA", EMAComponentInstanceSymbol.KIND).orElse(null);
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("tests.a.compB", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
GeneratorRosCpp generatorRosCpp = new GeneratorRosCpp();
......@@ -37,7 +37,7 @@ public class Ros2Test extends AbstractSymtabTest{
@Test
public void testGenerateCMakeRos2() throws IOException {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("tests.a.compA", EMAComponentInstanceSymbol.KIND).orElse(null);
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("tests.a.compB", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
GeneratorRosCpp generatorRosCpp = new GeneratorRosCpp();
......
......@@ -5,7 +5,7 @@ find_package(roscpp REQUIRED)
add_library(RosAdapter_tests_msg_basicStructComp RosAdapter_tests_msg_basicStructComp.cpp)
set_target_properties(RosAdapter_tests_msg_basicStructComp PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(RosAdapter_tests_msg_basicStructComp tests_msg_basicStructComp ${roscpp_LIBRARIES} ${struct_msgs_LIBRARIES})
target_link_libraries(RosAdapter_tests_msg_basicStructComp tests_msg_basicStructComp IAdapter_tests_msg_basicStructComp ${roscpp_LIBRARIES} ${struct_msgs_LIBRARIES})
target_include_directories(RosAdapter_tests_msg_basicStructComp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${roscpp_INCLUDE_DIRS} ${struct_msgs_INCLUDE_DIRS})
add_dependencies(RosAdapter_tests_msg_basicStructComp struct_msgs_generate_messages)
export(TARGETS RosAdapter_tests_msg_basicStructComp FILE RosAdapter_tests_msg_basicStructComp.cmake)
\ No newline at end of file
......@@ -7,7 +7,7 @@ find_package(rosgraph_msgs REQUIRED)
add_library(RosAdapter_tests_a_compA RosAdapter_tests_a_compA.cpp)
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} ${roscpp_LIBRARIES} ${rosgraph_msgs_LIBRARIES})
target_link_libraries(RosAdapter_tests_a_compA tests_a_compA IAdapter_tests_a_compA ${automated_driving_msgs_LIBRARIES} ${roscpp_LIBRARIES} ${rosgraph_msgs_LIBRARIES})
target_include_directories(RosAdapter_tests_a_compA PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${automated_driving_msgs_INCLUDE_DIRS} ${roscpp_INCLUDE_DIRS} ${rosgraph_msgs_INCLUDE_DIRS})
export(TARGETS RosAdapter_tests_a_compA FILE RosAdapter_tests_a_compA.cmake)
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
project (RosAdapter_tests_a_compA)
project (RosAdapter_tests_a_compB)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_library(RosAdapter_tests_a_compA RosAdapter_tests_a_compA.cxx)
set_target_properties(RosAdapter_tests_a_compA PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(RosAdapter_tests_a_compA tests_a_compA ${rclcpp_LIBRARIES} ${std_msgs_LIBRARIES} IAdapter_tests_a_compA)
target_include_directories(RosAdapter_tests_a_compA PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${std_msgs_INCLUDE_DIRS} ${rclcpp_INCLUDE_DIRS})
add_library(RosAdapter_tests_a_compB RosAdapter_tests_a_compB.cpp)
set_target_properties(RosAdapter_tests_a_compB PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(RosAdapter_tests_a_compB tests_a_compB IAdapter_tests_a_compB ${rclcpp_LIBRARIES} ${std_msgs_LIBRARIES})
target_include_directories(RosAdapter_tests_a_compB PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${rclcpp_INCLUDE_DIRS} ${std_msgs_INCLUDE_DIRS})
export(TARGETS RosAdapter_tests_a_compA FILE RosAdapter_tests_a_compA.cmake)
\ No newline at end of file
export(TARGETS RosAdapter_tests_a_compB FILE RosAdapter_tests_a_compB.cmake)
\ No newline at end of file
#include "RosAdapter_tests_a_compA.h"
\ No newline at end of file
#include "RosAdapter_tests_a_compB.h"
\ No newline at end of file
#pragma once
#include "IAdapter_tests_a_compA.h"
#include "tests_a_compA.h"
#include "IAdapter_tests_a_compB.h"
#include "tests_a_compB.h"
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/float64.hpp>
#include "rclcpp/rclcpp.hpp"
class RosAdapter_tests_a_compA: public IAdapter_tests_a_compA{
tests_a_compA* component;
class RosAdapter_tests_a_compB: public IAdapter_tests_a_compB{
tests_a_compB* component;
rclcpp::Subscription<std_msgs::msg::Float64>::SharedPtr _clockSubscriber;
rclcpp::Publisher<std_msgs::msg::Float64>::SharedPtr _echoPublisher;
public:
RosAdapter_tests_a_compA(){
RosAdapter_tests_a_compB(){
}
......@@ -17,15 +17,13 @@ class RosAdapter_tests_a_compA: public IAdapter_tests_a_compA{
component->rosIn = msg->data;
}
void init(tests_a_compA* comp){
void init(tests_a_compB* comp){
this->component = comp;
char* tmp = strdup("");
int i = 1;
rclcpp::init(i, &tmp);
auto node_handle = rclcpp::Node::make_shared("RosAdapter_tests_a_compA");
_clockSubscriber = node_handle->create_subscription<std_msgs::msg::Float64>("/clock", std::bind(&RosAdapter_tests_a_compA::_clockCallback, this, std::placeholders::_1));
auto node_handle = rclcpp::Node::make_shared("RosAdapter_tests_a_compB");
_clockSubscriber = node_handle->create_subscription<std_msgs::msg::Float64>("/clock", std::bind(&RosAdapter_tests_a_compB::_clockCallback, this, std::placeholders::_1));
_echoPublisher = node_handle->create_publisher<std_msgs::msg::Float64>("/echo");
rclcpp::spin(node_handle);
}
......@@ -40,4 +38,4 @@ class RosAdapter_tests_a_compA: public IAdapter_tests_a_compA{
publish_echoPublisher();
}
};
\ No newline at end of file
};
......@@ -5,7 +5,7 @@ find_package(roscpp REQUIRED)
add_library(RosAdapter_test_instanceArrayComp_basicPorts_1_ RosAdapter_test_instanceArrayComp_basicPorts_1_.cpp)
set_target_properties(RosAdapter_test_instanceArrayComp_basicPorts_1_ PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(RosAdapter_test_instanceArrayComp_basicPorts_1_ test_instanceArrayComp_basicPorts_1_ ${roscpp_LIBRARIES})
target_link_libraries(RosAdapter_test_instanceArrayComp_basicPorts_1_ test_instanceArrayComp_basicPorts_1_ IAdapter_test_instanceArrayComp_basicPorts_1_ ${roscpp_LIBRARIES})
target_include_directories(RosAdapter_test_instanceArrayComp_basicPorts_1_ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${roscpp_INCLUDE_DIRS})
......
package tests.a;
component CompB{
port in Q rosIn;
port out Q rosOut;
implementation Math{
rosOut = rosIn;
}
}
\ No newline at end of file
package tests.a;
conforms to de.monticore.lang.monticar.generator.roscpp.RosToEmamTagSchema;
tags EchoRos2 {
tag compB.rosIn with RosConnection = {topic = (/clock, std_msgs/msg/Float64), msgField = data};
tag compB.rosOut with RosConnection = {topic = (/echo, std_msgs/msg/Float64), msgField = data};
}
\ 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