Skip to content

Commit 8dbbac9

Browse files
committed
unify the ros1 cpp code generator; it should also create a proper ROS package to hold the generated code
1 parent 76dc766 commit 8dbbac9

5 files changed

Lines changed: 145 additions & 12 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.0.2)
2+
project(test_pkg)
3+
4+
find_package(catkin REQUIRED COMPONENTS roscpp std_srvs sensor_msgs )
5+
6+
catkin_package(
7+
CATKIN_DEPENDS roscpp std_srvs sensor_msgs
8+
)
9+
10+
### Build ###
11+
12+
include_directories(${catkin_INCLUDE_DIRS})
13+
14+
add_executable(test_node src/test_node.cpp)
15+
add_dependencies(test_node ${catkin_EXPORTED_TARGETS})
16+
target_link_libraries(test_node ${catkin_LIBRARIES})
17+
18+
### Install ###
19+
install(TARGETS test_node
20+
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
21+
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
22+
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
23+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<?xml-model
3+
href="http://download.ros.org/schema/package_format3.xsd"
4+
schematypens="http://www.w3.org/2001/XMLSchema"?>
5+
<package format="3">
6+
<name>test_pkg</name>
7+
<version>0.0.0</version>
8+
<description>This package contains the implementation of the node test_node</description>
9+
<maintainer email="jane.doe@example.com">Jane Doe</maintainer>
10+
<author email="jane.doe@example.com">Jane Doe</author>
11+
<license>Apache 2.0</license>
12+
13+
<buildtool_depend>catkin</buildtool_depend>
14+
15+
<depend>boost</depend>
16+
<depend>roscpp</depend>
17+
<depend>std_srvs</depend>
18+
<depend>sensor_msgs</depend>
19+
20+
</package>

plugins/de.fraunhofer.ipa.ros.xtext.tests/resources/rosgenerator/test_node.cpp renamed to plugins/de.fraunhofer.ipa.ros.xtext.tests/resources/rosgenerator/test_pkg/src/test_node.cpp

File renamed without changes.

plugins/de.fraunhofer.ipa.ros.xtext.tests/src/de/fraunhofer/ipa/ros/tests/RosGeneratorTest.xtend

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,20 @@ class RosGeneratorTest {
4343

4444
val resourceSet = rosTestingUtils.getMessagesResourceSet
4545
val fileContent = new String(Files.readAllBytes(Paths.get(RESOURCES_BASE_DIR, 'test.ros')))
46-
4746
val model = parseHelper.parse(fileContent, resourceSet)
4847

4948
val fsa = new InMemoryFileSystemAccess
5049
rosGenerator.doGenerate(model.eResource, fsa, new GeneratorContext)
51-
52-
Assert.assertTrue(fsa.textFiles.containsKey(CustomOutputProvider::DEFAULT_OUTPUT + "test_node.cpp"))
50+
Assert.assertTrue(fsa.textFiles.containsKey(CustomOutputProvider::DEFAULT_OUTPUT + "test_pkg/package.xml"))
51+
Assert.assertEquals(
52+
new String(Files.readAllBytes(Paths.get(RESOURCES_BASE_DIR, 'rosgenerator/test_pkg/src/','test_node.cpp'))).trim,
53+
fsa.textFiles.get(CustomOutputProvider::DEFAULT_OUTPUT + "test_pkg/src/test_node.cpp").toString.trim)
5354
Assert.assertEquals(
54-
new String(Files.readAllBytes(Paths.get(RESOURCES_BASE_DIR, 'rosgenerator', 'test_node.cpp'))).trim,
55-
fsa.textFiles.get(CustomOutputProvider::DEFAULT_OUTPUT + "test_node.cpp").toString.trim)
55+
new String(Files.readAllBytes(Paths.get(RESOURCES_BASE_DIR, 'rosgenerator/test_pkg/','CMakeLists.txt'))).trim,
56+
fsa.textFiles.get(CustomOutputProvider::DEFAULT_OUTPUT + "test_pkg/CMakeLists.txt").toString.trim)
57+
Assert.assertEquals(
58+
new String(Files.readAllBytes(Paths.get(RESOURCES_BASE_DIR, 'rosgenerator/test_pkg/','package.xml'))).trim,
59+
fsa.textFiles.get(CustomOutputProvider::DEFAULT_OUTPUT + "test_pkg/package.xml").toString.trim)
5660
}
5761

5862
@Test

plugins/de.fraunhofer.ipa.roscode.generator/src/de/fraunhofer/ipa/roscode/generator/RosCodeGenerator.xtend

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package de.fraunhofer.ipa.roscode.generator
22

3+
import java.util.ArrayList
4+
import java.util.HashSet
5+
import java.util.List
6+
import java.util.Set
37
import org.eclipse.emf.ecore.resource.Resource
48
import org.eclipse.xtext.generator.AbstractGenerator
59
import org.eclipse.xtext.generator.IFileSystemAccess2
610
import org.eclipse.xtext.generator.IGeneratorContext
11+
import ros.Node
12+
import ros.Package
713
import org.eclipse.xtext.generator.IOutputConfigurationProvider
814
import org.eclipse.xtext.generator.OutputConfiguration
9-
import java.util.Set
10-
import ros.*
15+
import ros.Publisher
16+
import ros.Subscriber
17+
import ros.ServiceServer
18+
import ros.ServiceClient
1119

1220
class CustomOutputProvider implements IOutputConfigurationProvider {
1321
public final static String DEFAULT_OUTPUT = "DEFAULT_OUTPUT"
@@ -34,17 +42,79 @@ class RosCodeGenerator extends AbstractGenerator {
3442

3543

3644
String resourcepath
45+
Node node
46+
List<String> PkgsList
47+
Set<String> set
48+
3749

3850
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
3951
resourcepath = resource.URI.toString();
4052
if (! resourcepath.contains("/ros-input")) {
41-
for (node : resource.allContents.toIterable.filter(Node)){
42-
fsa.generateFile(node.getName()+".cpp",node.compile)
53+
for (pkg : resource.allContents.toIterable.filter(Package)){
54+
fsa.generateFile(pkg.getName().toLowerCase+"/package.xml",pkg.compile_package_xml)
55+
fsa.generateFile(pkg.getName().toLowerCase+"/CMakeLists.txt",pkg.compile_CMakeLists)
56+
for (art : pkg.artifact){
57+
node = art.node
58+
fsa.generateFile(pkg.getName().toLowerCase+"/src/"+node.name+".cpp",node.compile_node)
59+
60+
}
61+
}
4362
}
44-
}
4563
}
4664

47-
def compile(Node node) '''
65+
def compile_package_xml(Package pkg)'''
66+
<?xml version="1.0"?>
67+
<?xml-model
68+
href="http://download.ros.org/schema/package_format3.xsd"
69+
schematypens="http://www.w3.org/2001/XMLSchema"?>
70+
<package format="3">
71+
<name>«pkg.name»</name>
72+
<version>0.0.0</version>
73+
<description>This package contains the implementation of the node «pkg.artifact.get(0).node.name»</description>
74+
<maintainer email="jane.doe@example.com">Jane Doe</maintainer>
75+
<author email="jane.doe@example.com">Jane Doe</author>
76+
<license>Apache 2.0</license>
77+
78+
<buildtool_depend>catkin</buildtool_depend>
79+
80+
<depend>boost</depend>
81+
<depend>roscpp</depend>
82+
«FOR depend_pkg:pkg.getPkgDependencies»
83+
<depend>«depend_pkg»</depend>
84+
«ENDFOR»
85+
86+
</package>
87+
'''
88+
89+
def compile_CMakeLists(Package pkg)'''
90+
cmake_minimum_required(VERSION 3.0.2)
91+
projectpkg.name»)
92+
93+
find_package(catkin REQUIRED COMPONENTS roscpp «FOR depend_pkg:pkg.getPkgDependencies»«depend_pkg» «ENDFOR»)
94+
95+
catkin_package(
96+
CATKIN_DEPENDS roscpp «FOR depend_pkg:pkg.getPkgDependencies»«depend_pkg» «ENDFOR»
97+
)
98+
99+
### Build ###
100+
101+
include_directories(${catkin_INCLUDE_DIRS})
102+
103+
«FOR art:pkg.artifact»
104+
add_executableart.name» srcart.node.name».cpp)
105+
add_dependenciesart.name» ${catkin_EXPORTED_TARGETS})
106+
target_link_librariesart.name» ${catkin_LIBRARIES})
107+
108+
«ENDFOR»
109+
### Install ###
110+
install(TARGETS «FOR art:pkg.artifact»«art.name»«ENDFOR»
111+
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
112+
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
113+
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
114+
)
115+
'''
116+
117+
def compile_node(Node node) '''
48118
#include <ros/ros.h>
49119
«FOR pub : node.publisher»
50120
#include <«pub.message.package.name»/«pub.message.name».h>
@@ -102,4 +172,20 @@ def compile(ServiceServer srvserver)
102172
def compile(ServiceClient srvclient)
103173
''' ros::ServiceClient «srvclient.name» = n.serviceClient<«srvclient.service.package.name»::«srvclient.service.name»>("«srvclient.name»");'''
104174
105-
}
175+
176+
def List<String> getPkgDependencies(Package pkg){
177+
set=new HashSet<String>()
178+
PkgsList = new ArrayList()
179+
for (art:pkg.artifact){
180+
node=art.node
181+
for (pub:node.publisher){set.add(pub.message.package.name)}
182+
for (sub:node.subscriber){set.add(sub.message.package.name)}
183+
for (srvserver:node.serviceserver){set.add(srvserver.service.package.name)}
184+
for (srvclient:node.serviceclient){set.add(srvclient.service.package.name)}
185+
}
186+
PkgsList.addAll(set)
187+
return PkgsList
188+
}
189+
190+
}
191+

0 commit comments

Comments
 (0)