Skip to content

Commit 1deda5d

Browse files
committed
Created sklethon for the launch file generator for ROS2
1 parent ccf8141 commit 1deda5d

2 files changed

Lines changed: 190 additions & 6 deletions

File tree

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package de.fraunhofer.ipa.rossystem.generator
2+
3+
import system.RosNode
4+
import ros.RosPackage
5+
import ros.Artifact
6+
import system.Rossystem
7+
8+
class LaunchFileCompiler_ROS2 {
9+
10+
11+
def compile_toROS2launch(Rossystem system) '''
12+
from launch import LaunchDescription
13+
from launch_ros.actions import Node
14+
15+
def generate_launch_description():
16+
ld = LaunchDescription()
17+
18+
«FOR component:system.components»«IF component.eClass=="RosNode"»
19+
«(component as RosNode).name» = Node(
20+
package="«((component as RosNode).from.eContainer.eContainer as RosPackage).name»",
21+
executable="«((component as RosNode).from.eContainer as Artifact).name»",
22+
name="«(component as RosNode).name»"
23+
)
24+
«ENDIF»
25+
«ENDFOR»
26+
27+
«FOR component:system.components»«IF component.eClass=="RosNode"»
28+
ld.add_action(«(component as RosNode).name»)
29+
«ENDIF»
30+
«ENDFOR»
31+
32+
return ld
33+
'''
34+
35+
// def void compile_list_of_ROS2components(RosSystem system, ComponentStack stack) {
36+
// components_tmp_.clear;
37+
// Ros2components.clear;
38+
// if (stack === null){
39+
// components_tmp_ = system.rosComponent;
40+
// } else {
41+
// components_tmp_ = stack.rosComponent;
42+
// }
43+
// for(ComponentInterface component:components_tmp_){
44+
// if (component.compile_pkg_type.toString.contains("AmentPackage")){
45+
// Ros2components.add(component);
46+
// }
47+
// }
48+
// }
49+
//
50+
// def check_ns(ComponentInterface component){
51+
// if (component.hasNS){
52+
// return component.get_ns();
53+
// }else {
54+
// return "";
55+
// }
56+
// }
57+
//
58+
// def List<String> InterfaceDef(String name, String type){
59+
// ListInterfaceDef = new ArrayList()
60+
// ListInterfaceDef.add(name.replace("/","_"))
61+
// ListInterfaceDef.add(name)
62+
// ListInterfaceDef.add(type)
63+
// return ListInterfaceDef
64+
// }
65+
//
66+
// def boolean hasNS(ComponentInterface component){
67+
// if(!component.nameSpace.nullOrEmpty){
68+
// return true;
69+
// }else{
70+
// return false
71+
// }
72+
// }
73+
// def String get_ns(ComponentInterface component){
74+
// return component.nameSpace.replaceFirst("/","");
75+
// }
76+
//
77+
// def String compile_remappings_str(ComponentInterface component) {
78+
// var remap_str = "";
79+
// val NS = component.check_ns();
80+
// for (rosPublisher : component.rospublisher) {
81+
// if (!((prefix(NS)+rosPublisher.name).equals(compile_topic_name(rosPublisher.publisher, NS)))) {
82+
// remap_str += "\t(\"" + rosPublisher.publisher.name + "\", \"" + rosPublisher.name + "\"),\n";
83+
// }
84+
// }
85+
// for (rosSubscriber : component.rossubscriber) {
86+
// if (!((prefix(NS)+rosSubscriber.name).equals(compile_topic_name(rosSubscriber.subscriber, NS)))) {
87+
// remap_str += "\t(\"" + rosSubscriber.subscriber.name + "\", \"" + rosSubscriber.name + "\"),\n";
88+
// }
89+
// }
90+
// for (rosServiceServer : component.rosserviceserver) {
91+
// if (!((prefix(NS)+rosServiceServer.name).equals(compile_service_name(rosServiceServer.srvserver, NS)))) {
92+
// remap_str += "\t(\"" + rosServiceServer.srvserver.name + "\", \"" + rosServiceServer.name + "\"),\n";
93+
// }
94+
// }
95+
// for (rosServiceClient : component.rosserviceclient) {
96+
// if (!((prefix(NS)+rosServiceClient.name).equals(compile_service_name(rosServiceClient.srvclient, NS)))) {
97+
// remap_str += "\t(\"" + rosServiceClient.srvclient.name + "\", \"" + rosServiceClient.name + "\"),\n";
98+
// }
99+
// }
100+
// for (rosActionServer : component.rosactionserver) {
101+
// if (!((prefix(NS)+rosActionServer.name).equals(compile_action_name(rosActionServer.actserver, NS)))) {
102+
// remap_str += "\t(\"" + rosActionServer.actserver.name + "\", \"" + rosActionServer.name + "\"),\n";
103+
// }
104+
// }
105+
// for (rosActionClient : component.rosactionclient) {
106+
// if (!((prefix(NS)+rosActionClient.name).equals(compile_action_name(rosActionClient.actclient, NS)))) {
107+
// remap_str += "\t(\"" + rosActionClient.actclient.name + "\", \"" + rosActionClient.name + "\"),\n";
108+
// }
109+
// }
110+
// if (!remap_str.empty) {
111+
// remap_str = ",\nremappings=[\n" + remap_str.substring(0,remap_str.length-2) + "]\n";
112+
// }
113+
// return remap_str;
114+
// }
115+
//
116+
// def String compile_parameters_str(EList<RosParameter> rosParameters) {
117+
// param_count = rosParameters.length;
118+
// var param_str = "";
119+
// for (rosParameter : rosParameters) {
120+
// val param_count=param_count--;
121+
// if (rosParameter.parameter.type instanceof ParameterStructTypeImpl) {
122+
// param_str += compile_struct_str(rosParameter.value, rosParameter.parameter.name);
123+
// } else {
124+
// param_str += "{ \"" + rosParameter.parameter.name + "\" : " + get_param_value(rosParameter.value, rosParameter.parameter.name);
125+
// }
126+
// if (param_count > 1){
127+
// param_str +=" },\n"
128+
// } else {
129+
// param_str +=" }\n";
130+
// }
131+
// }
132+
// return param_str;
133+
// }
134+
//
135+
// def String compile_struct_str(ParameterValue value, String name) {
136+
// var param_str = "";
137+
// var elem_count = (value as ParameterSequenceImpl).eContents.length;
138+
//
139+
// for (elem : ((value as ParameterSequenceImpl).eContents)) {
140+
// var member = ((elem as ParameterStructImpl).eContents.get(0) as ParameterStructMemberImpl);
141+
// val param_val = get_param_value(member.getValue(), name + "/" + member.getName());
142+
// if (param_val.startsWith("{")) {
143+
// param_str += param_val;
144+
// } else {
145+
// param_str += "{ \"" + name + "/" + member.getName() + "\" : " + param_val;
146+
// }
147+
// elem_count--;
148+
// if (elem_count > 0){
149+
// param_str +=" },\n"
150+
// }
151+
// }
152+
// return param_str;
153+
// }
154+
//
155+
// def String get_param_value(ParameterValue value, String name) {
156+
// var param_val = "";
157+
// if (value instanceof ParameterStringImpl) {
158+
// param_val = "\"" + (value as ParameterStringImpl).getValue() + "\"";
159+
// } else if (value instanceof ParameterIntegerImpl) {
160+
// param_val = (value as ParameterIntegerImpl).getValue().toString;
161+
// } else if (value instanceof ParameterDoubleImpl) {
162+
// param_val = (value as ParameterDoubleImpl).getValue().toString;
163+
// } else if (value instanceof ParameterBooleanImpl) {
164+
// param_val = (value as ParameterBooleanImpl).isValue().toString;
165+
// } else if (value instanceof ParameterSequenceImpl) {
166+
// var elem_count = (value as ParameterSequenceImpl).eContents.length;
167+
// if ((value as ParameterSequenceImpl).eContents.get(0) instanceof ParameterStructImpl) {
168+
// param_val = compile_struct_str(value, name);
169+
// } else {
170+
// param_val += "[";
171+
// for (elem : (value as ParameterSequenceImpl).eContents) {
172+
// param_val += get_param_value(elem as ParameterValue, name);
173+
// elem_count--;
174+
// if (elem_count > 0){
175+
// param_val +=", "
176+
// }
177+
// }
178+
// param_val += "]";
179+
// }
180+
// }
181+
// return param_val;
182+
// }
183+
}

plugins/de.fraunhofer.ipa.rossystem.xtext/src/de/fraunhofer/ipa/rossystem/generator/RosSystemGenerator.xtend

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import org.eclipse.emf.ecore.resource.Resource
77
import org.eclipse.xtext.generator.AbstractGenerator
88
import org.eclipse.xtext.generator.IFileSystemAccess2
99
import org.eclipse.xtext.generator.IGeneratorContext
10+
import com.google.inject.Inject
11+
import system.Rossystem
1012

1113
/**
1214
* Generates code from your model files on save.
1315
*
1416
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
1517
*/
1618
class RosSystemGenerator extends AbstractGenerator {
17-
19+
@Inject extension LaunchFileCompiler_ROS2
20+
1821
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
19-
// fsa.generateFile('greetings.txt', 'People to greet: ' +
20-
// resource.allContents
21-
// .filter(Greeting)
22-
// .map[name]
23-
// .join(', '))
22+
for (system : resource.allContents.toIterable.filter(Rossystem)){
23+
fsa.generateFile(system.getName().toLowerCase+"_ros2/launch/"+system.getName()+".launch.py",compile_toROS2launch(system).toString().replace("\t"," "))
2424
}
2525
}
26+
}

0 commit comments

Comments
 (0)