blob: 217bdb9b920ade66bfcedf23079d88313ec3a57c [file] [log] [blame]
/**
* Copyright (c) 2020-2021 Robert Bosch GmbH.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
*/
package org.eclipse.app4mc.slg.ros2.transformers
import com.google.inject.Inject
import com.google.inject.Singleton
import java.util.HashSet
import java.util.Set
import org.eclipse.app4mc.amalthea.model.ActivityGraphItem
import org.eclipse.app4mc.amalthea.model.Amalthea
import org.eclipse.app4mc.amalthea.model.BooleanObject
import org.eclipse.app4mc.amalthea.model.ChannelSend
import org.eclipse.app4mc.amalthea.model.InterProcessStimulus
import org.eclipse.app4mc.amalthea.model.InterProcessTrigger
import org.eclipse.app4mc.amalthea.model.LabelAccess
import org.eclipse.app4mc.amalthea.model.RunnableCall
import org.eclipse.app4mc.amalthea.model.Tag
import org.eclipse.app4mc.amalthea.model.Task
import org.eclipse.app4mc.amalthea.model.Ticks
import org.eclipse.app4mc.slg.commons.m2t.transformers.AmaltheaModel2TextTransformer
import org.eclipse.app4mc.slg.commons.m2t.transformers.TicksUtilsTransformer
import org.eclipse.app4mc.slg.ros2.generators.RosAmlTranslationUnit
import org.eclipse.app4mc.transformation.util.OutputBuffer
@Singleton
class RosModel2TextTransformer extends AmaltheaModel2TextTransformer {
@Inject OutputBuffer outputBuffer
@Inject RosTagTransformer tagTransformer
@Inject TicksUtilsTransformer ticksUtilsTransformer
@Inject RosLabelTransformer labelTransformer
var label = false;
var ticks = false;
var interprocess = false;
var channelSend = false;
var performance_measurement = false;
override void transform(Amalthea model, String outputFolder) {
this.outputBuffer.initialize(outputFolder)
this.outputBuffer.configureFiletype("SRC", ".cpp", "// This code is auto-generated\n\n", null);
this.outputBuffer.configureFiletype("INC", ".h", "// This code is auto-generated\n\n", null);
this.outputBuffer.configureFiletype("OTHER", "", "", "");
var Set<String> services = new HashSet<String>()
var Set<String> nodes = new HashSet<String>()
model?.commonElements?.tags.forEach [ tag |
// instance to know which node published a message
tagTransformer.transform(tag, model)
]
/* extract information needed for top-level CMake file */
/* find runnables that use service files and use the tag to set the dependencies */
for (Task t : model?.swModel?.tasks) {
var stimuli = t.stimuli.get(0)
for (Tag tg : t.tags) {
nodes.add(tg.name)
}
// check if task has interprocess stimulus, if so task is service callback
if (stimuli instanceof InterProcessStimulus) {
services.add(stimuli.name + '_service')
} else {
// search for InterProcessTrigger
for (ActivityGraphItem r : t.activityGraph.items) {
if (r instanceof RunnableCall) {
if ((r?.runnable?.customProperties?.get('measure_performance') as BooleanObject)?.value) {
performance_measurement = true
}
for (ActivityGraphItem aci : r.runnable.activityGraph.items) {
if (aci instanceof LabelAccess) {
label = true;
}
if (aci instanceof Ticks) {
ticks = true;
}
if (aci instanceof InterProcessTrigger) {
interprocess = true;
}
if (aci instanceof ChannelSend) {
channelSend = true;
}
}
}
}
}
}
// create CMake files
// TransformerTicksUtils
ticksUtilsTransformer.createCMake()
// TransformerLabel
labelTransformer.createCMake()
// create aml if neccessary --> Side effect !!!
new RosAmlTranslationUnit(outputBuffer);
// tasks, containing only runnables
outputBuffer.appendTo("OTHER", 'CMakeLists.txt', toCmake(nodes, services).toString)
// create package.xml for ROS environment
outputBuffer.appendTo("OTHER", 'package.xml', toPackageXml(services).toString())
// create build script
outputBuffer.appendTo("OTHER", 'build.sh', toBuildScript(services).toString())
// create launch file
outputBuffer.appendTo("OTHER", 'launch.py', toLaunchFile(nodes).toString())
outputBuffer.finish
}
def toLaunchFile(Set<String> nodes) '''
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
«FOR node : nodes»
Node(
package='amalthea_ros_model',
node_namespace='',
node_executable='«node»',
),
«ENDFOR»
])
'''
def toBuildScript(Set<String> services) '''
«FOR service : services»
#building service «service»
cd services/«service»
colcon build
. install/setup.bash
cd ../..
«ENDFOR»
colcon build
. install/setup.bash
'''
// TODO: define attributes via ConfigModel
def toPackageXml(Set<String> services) '''
<?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>amalthea_ros_model</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="fixedterm.Patrick.Will@de.bosch.com">wlp8fe</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
«FOR service : services»
<depend>«service»</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 toCmake(Set<String> tags, Set<String> services) '''
# CMakeLists for Nodes
cmake_minimum_required(VERSION 3.5)
project(amalthea_ros_model)
# 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(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
«FOR service : services»
find_package(«service» REQUIRED)
«ENDFOR»
«IF channelSend»
add_library(CHANNELSEND_UTILS STATIC
synthetic_gen/channelSendUtils/_src/channelSendUtils.cpp
)
ament_target_dependencies(CHANNELSEND_UTILS rclcpp std_msgs)
target_include_directories(CHANNELSEND_UTILS
PUBLIC synthetic_gen/channelSendUtils/_inc/
)
«ENDIF»
«««Only to compile if there are InterProcessTriggers»»»
«IF interprocess»
add_library(INTERPROCESSTRIGGER_UTIL STATIC
synthetic_gen/interProcessTriggerUtils/_src/interProcessTriggerUtils.cpp
)
ament_target_dependencies(INTERPROCESSTRIGGER_UTIL rclcpp «FOR service : services»«service» «ENDFOR»)
target_include_directories(INTERPROCESSTRIGGER_UTIL
PUBLIC synthetic_gen/interProcessTriggerUtils/_inc
)
«ENDIF»
«IF performance_measurement»
add_library(AML_LIB STATIC
utils/aml/src/aml.cpp
)
target_include_directories(AML_LIB
PUBLIC utils/aml/inc/
)
«ENDIF»
«IF label»
add_subdirectory (synthetic_gen/labels)
«ENDIF»
«IF ticks»
add_subdirectory (synthetic_gen/ticksUtils)
«ENDIF»
# RUNNABLES_LIB ################################################################
####
add_library(RUNNABLES_LIB STATIC
synthetic_gen/runnables/_src/runnables.cpp
)
target_include_directories(RUNNABLES_LIB
PUBLIC synthetic_gen/runnables/_inc
)
target_include_directories(RUNNABLES_LIB
PUBLIC
«IF ticks»
synthetic_gen/ticksUtils/_inc
«ENDIF»
«IF label»
synthetic_gen/labels/_inc
«ENDIF»
«IF channelSend»
synthetic_gen/channelSendUtils/_inc
«ENDIF»
«IF interprocess»
synthetic_gen/interProcessTriggerUtils/_inc
«ENDIF»
«IF performance_measurement»
utils/aml/inc
«ENDIF»
)
target_link_libraries(RUNNABLES_LIB
PRIVATE «IF performance_measurement»AML_LIB«ENDIF» «IF label»LABELS_LIB«ENDIF» «IF ticks»TICKS_UTILS«ENDIF» «IF channelSend»CHANNELSEND_UTILS«ENDIF» «IF interprocess»INTERPROCESSTRIGGER_UTIL«ENDIF»
)
«FOR tag : tags»
add_executable(«tag» «tag»/_src/«tag».cpp)
target_link_libraries(«tag» «IF label»LABELS_LIB«ENDIF» RUNNABLES_LIB)
ament_target_dependencies(«tag» rclcpp std_msgs «FOR service : services»«service» «ENDFOR»)
«ENDFOR»
install(TARGETS
«FOR tag : tags»
«tag»
«ENDFOR»
DESTINATION lib/${PROJECT_NAME})
ament_package()
'''
}