Skip to content

Commit

Permalink
update ros2 coe generator to create the ROS package complete
Browse files Browse the repository at this point in the history
  • Loading branch information
ipa-nhg committed May 17, 2021
1 parent 1c8a918 commit 05d1ecd
Showing 1 changed file with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,104 @@ import org.eclipse.xtext.generator.IOutputConfigurationProvider
import org.eclipse.xtext.generator.OutputConfiguration
import java.util.Set
import ros.*


import java.util.List
import java.util.ArrayList
import java.util.HashSet

/**
* Generates code from your model files on save.
*
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
*/
class Ros2CodeGenerator extends AbstractGenerator {


String resourcepath
String import_msgs
int char_i
Node node
List<String> PkgsList
Set<String> set


override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
resourcepath = resource.URI.toString();
if (! resourcepath.contains("/ros-input")) {
for (node : resource.allContents.toIterable.filter(Node)){
fsa.generateFile(node.getName()+".cpp",node.compile)
for (pkg : resource.allContents.toIterable.filter(Package)){
fsa.generateFile(pkg.getName().toLowerCase+"/package.xml",pkg.compile_package_xml)
fsa.generateFile(pkg.getName().toLowerCase+"/CMakeLists.txt",pkg.compile_CMakeLists)
for (art : pkg.artifact){
node = art.node
fsa.generateFile(pkg.getName().toLowerCase+"/src/"+node.name+".cpp",node.compile_node)

}
}
}
}
}

def compile(Node node) '''

def compile_package_xml(Package pkg)'''
<?xml version="1.0"?>
<?xml-model
href="http://download.ros.org/schema/package_format3.xsd"
schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>«pkg.name»</name>
<version>0.0.0</version>
<description>This package contains the implementation of the node «pkg.artifact.get(0).node.name»</description>
<maintainer email="[email protected]">Jane Doe</maintainer>
<author email="[email protected]">Jane Doe</author>
<license>Apache 2.0</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>boost</depend>
<depend>rclcpp</depend>
«FOR depend_pkg:pkg.getPkgDependencies»
<depend>«depend_pkg»</depend>
«ENDFOR»

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
'''

def compile_CMakeLists(Package pkg)'''
cmake_minimum_required(VERSION 3.5)
projectpkg.name»)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
find_package(Boost REQUIRED)
find_package(rclcpp REQUIRED)
«FOR depend_pkg:pkg.getPkgDependencies»
find_packagedepend_pkg» REQUIRED)
«ENDFOR»

«FOR art:pkg.artifact»
add_executableart.name» srcart.node.name».cpp)
ament_target_dependenciesart.name» «FOR depend_pkg:pkg.getPkgDependencies»«depend_pkg» «ENDFOR»)

install(TARGETS
«art.name»
DESTINATION lib/${PROJECT_NAME})
«ENDFOR»

ament_package()
'''

def compile_node(Node node) '''
#include <chrono>
#include <cstdio>
#include <memory>
Expand Down Expand Up @@ -149,6 +222,20 @@ int main(int argc, char * argv[])
}
'''
def List<String> getPkgDependencies(Package pkg){
set=new HashSet<String>()
PkgsList = new ArrayList()
for (art:pkg.artifact){
node=art.node
for (pub:node.publisher){set.add(pub.message.package.name)}
for (sub:node.subscriber){set.add(sub.message.package.name)}
for (srvserver:node.serviceserver){set.add(srvserver.service.package.name)}
for (srvclient:node.serviceclient){set.add(srvclient.service.package.name)}
}
PkgsList.addAll(set)
return PkgsList
}
def String check_message_include(String message_name){
import_msgs = message_name.toFirstLower;
for (char_i =0; char_i < import_msgs.length; char_i++ ){
Expand Down

0 comments on commit 05d1ecd

Please sign in to comment.