Skip to content

Commit

Permalink
[Build] Import dummy ROS2 publisher
Browse files Browse the repository at this point in the history
This is Work-in-Progress to integrate ROS2 code into the existing ROS1
code and make them buildable with both CMake and Colcon. The imported
code is from [1] and will be removed.

[1] https://github.com/ros2/examples/tree/eloquent/rclcpp/minimal_publisher

Signed-off-by: Wook Song <[email protected]>
  • Loading branch information
wooksong committed Oct 21, 2020
1 parent 3773151 commit c5d4341
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 85 deletions.
52 changes: 44 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)

PROJECT(nnstreamer-ros C CXX)

OPTION(TIZEN "Enable Tizen build mode" OFF)
OPTION(ENABLE_TEST "Enable tests" ON)

SET(ROS_DISTROS "kinetic" "melodic" "noextic" )
SET(ROS2_DISTROS "eloquent" "foxy")

SET(NNS_VERSION "1.6.0")
ADD_DEFINITIONS(-DNNS_VERSION="${NNS_VERSION}")

IF(NOT ROS_VERSION)
SET(ROS_VERSION "melodic")
ENDIF (NOT ROS_VERSION)

LIST(FIND ROS_DISTROS ${ROS_VERSION} ROS_DISTRO_IDX)
LIST(FIND ROS2_DISTROS ${ROS_VERSION} ROS2_DISTRO_IDX)

IF(${ROS_DISTRO_IDX} GREATER -1)
SET(WITH_ROS1 TRUE)
SET(WITH_ROS2 FALSE)
ADD_DEFINITIONS(-DWITH_ROS1)
ELSEIF(${ROS2_DISTRO_IDX} GREATER -1)
SET(WITH_ROS1 FALSE)
SET(WITH_ROS2 TRUE)
ADD_DEFINITIONS(-DWITH_ROS2)
ELSE()
message(FATAL_ERROR "Invalid or Not Supported ROS_DISTRO: ${ROS_VERSION}")
ENDIF(${ROS_DISTRO_IDX} GREATER -1)

IF(NOT ROS_BASE_PREFIX)
SET(ROS_BASE_PREFIX "/opt/ros/${ROS_VERSION}")
ENDIF(NOT ROS_BASE_PREFIX)
Expand All @@ -28,7 +46,10 @@ SET(ROS_INSTALL_INCLUDEDIR "include")

## Override CMake Variables
SET(CMAKE_PREFIX_PATH "${ROS_BASE_PREFIX}")
SET(CMAKE_INSTALL_PREFIX "${ROS_INSTALL_PREFIX}")
STRING(FIND ${CMAKE_INSTALL_PREFIX} ${CMAKE_CURRENT_SOURCE_DIR} BUILD_IN_WORKSPACE)
IF(BUILD_IN_WORKSPACE EQUAL -1)
SET(CMAKE_INSTALL_PREFIX ${ROS_INSTALL_PREFIX})
ENDIF(BUILD_IN_WORKSPACE EQUAL -1)
SET(CMAKE_INSTALL_LIBDIR "${ROS_INSTALL_LIBDIR}")
SET(CMAKE_INSTALL_BINDIR "${ROS_INSTALL_BINDIR}")
SET(CMAKE_INSTALL_INCLUDEDIR "${ROS_INSTALL_INCLUDEDIR}")
Expand All @@ -55,19 +76,34 @@ ENDIF(TIZEN)
PKG_CHECK_MODULES(PKGS_COMMON REQUIRED ${PKG_COMMON_MODULES})

INCLUDE_DIRECTORIES(
./common/include
${PROJECT_SOURCE_DIR}/include
${PKGS_COMMON_INCLUDE_DIRS}
${ROS_BASE_INCLUDE_DIR}
)

SET(c_flags -Wall -Werror -fPIC -g -std=c89)
SET(cxx_flags -Wall -Werror -fPIC -g -std=c++11)
# Default to C99
IF(NOT CMAKE_C_STANDARD)
SET(CMAKE_C_STANDARD 99)
ENDIF()

# Default to C++14
IF(NOT CMAKE_CXX_STANDARD)
SET(CMAKE_CXX_STANDARD 14)
ENDIF()

SET(EXTRA_CFLAGS -Wall -Werror -fPIC -g)
SET(EXTRA_CXXFLAGS -Wall -Werror -fPIC -g)

ADD_COMPILE_OPTIONS(
"$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
"$<$<COMPILE_LANGUAGE:C>:${EXTRA_CFLAGS}>"
"$<$<COMPILE_LANGUAGE:CXX>:${EXTRA_CXXFLAGS}>"
)

ADD_SUBDIRECTORY(common)
IF(WITH_ROS1)
ADD_SUBDIRECTORY(ros)
ELSEIF(WITH_ROS2)
ADD_SUBDIRECTORY(ros2)
ENDIF()
ADD_SUBDIRECTORY(gst)

IF(ENABLE_TEST)
Expand Down
70 changes: 0 additions & 70 deletions common/package.xml

This file was deleted.

6 changes: 4 additions & 2 deletions gst/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
ADD_SUBDIRECTORY(tensor_ros_sink)
ADD_SUBDIRECTORY(tensor_ros_src)
IF(WITH_ROS1)
ADD_SUBDIRECTORY(tensor_ros_sink)
ADD_SUBDIRECTORY(tensor_ros_src)
ENDIF(WITH_ROS1)
File renamed without changes.
File renamed without changes.
5 changes: 2 additions & 3 deletions common/CMakeLists.txt → ros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,17 @@ generate_messages(
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS include
LIBRARIES nns_ros_bridge
CATKIN_DEPENDS roscpp rosbag std_msgs message_runtime
DEPENDS ${PKGS_COMMON}
)

SET(CXX_SRC_FILES_NNS_ROS_PUBLISHER
./node/nns_ros_publisher.cc
./node/nns_ros_publisher.cc
)

SET(CXX_SRC_FILES_NNS_ROS_SRC
./node/nns_ros_subscriber.cc
./node/nns_ros_subscriber.cc
)

###########
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions ros/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<package format="2">
<name>nns_ros_bridge</name>
<version>0.0.1</version>
<description>A bridge for ROS support within NNStreamer</description>
<maintainer email="[email protected]">Wook Song</maintainer>
<author email="[email protected]">Wook Song</author>

<license>LGPLv2.1</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rosbag</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rosbag</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>

<export>
</export>
</package>
62 changes: 62 additions & 0 deletions ros2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
PROJECT(nns_ros2_bridge C CXX)

IF(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
ENDIF()

FIND_PROGRAM(PYTHON_EXECUTABLE python3 DOC "python interpreter")
IF(PYTHON_EXECUTABLE)
EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import platform; ver = platform.python_version_tuple(); print(ver[0] + '.' + ver[1])"
OUTPUT_VARIABLE PYTHON_VER
RESULT_VARIABLE PYTHON_VER_NOT_FOUND
OUTPUT_STRIP_TRAILING_WHITESPACE)
ENDIF(PYTHON_EXECUTABLE)

IF(PYTHON_VER_NOT_FOUND)
MESSAGE(FATAL_ERROR "Failed to find python3 required by ament_cmake")
ENDIF(PYTHON_VER_NOT_FOUND)

IF(EXISTS ${CMAKE_PREFIX_PATH}/lib/python${PYTHON_VER}/site-packages)
LIST(APPEND PYTHONPATH_CANDIDATES "${CMAKE_PREFIX_PATH}/lib/python${PYTHON_VER}")
ENDIF(EXISTS ${CMAKE_PREFIX_PATH}/lib/python${PYTHON_VER}/site-packages)

IF(EXISTS ${CMAKE_PREFIX_PATH}/lib/python3/site-packages)
LIST(APPEND PYTHONPATH_CANDIDATES "${CMAKE_PREFIX_PATH}/lib/python3")
ENDIF(EXISTS ${CMAKE_PREFIX_PATH}/lib/python3/site-packages)

LIST(LENGTH PYTHONPATH_CANDIDATES LEN_PYTHONPATH_CANDIDATES)
IF(${LEN_PYTHONPATH_CANDIDATES} LESS "1")
MESSAGE(FATAL_ERROR "Failed to find python3-related path in ${CMAKE_PREFIX_PATH}/lib")
ENDIF(${LEN_PYTHONPATH_CANDIDATES} LESS "1")

FOREACH(pythonpath ${PYTHONPATH_CANDIDATES})
SET(ENV{PYTHONPATH} ${pythonpath}/site-packages:$ENV{PYTHONPATH})
ENDFOREACH()

# find dependencies
FIND_PACKAGE(ament_cmake REQUIRED)
FIND_PACKAGE(rclcpp REQUIRED)
FIND_PACKAGE(std_msgs REQUIRED)

ADD_EXECUTABLE(publisher_member_function node/member_function.cpp)
AMENT_TARGET_DEPENDENCIES(publisher_member_function rclcpp std_msgs)

INSTALL(TARGETS
publisher_member_function
RUNTIME DESTINATION ${ROS_INSTALL_BINDIR}
LIBRARY DESTINATION ${ROS_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${ROS_INSTALL_LIBDIR}
)

IF(BUILD_TESTING)
FIND_PACKAGE(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
AMENT_LINT_AUTO_FIND_TEST_DEPENDENCIES()
ENDIF()

AMENT_PACKAGE()
56 changes: 56 additions & 0 deletions ros2/node/member_function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <chrono>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */

class MinimalPublisher : public rclcpp::Node
{
public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalPublisher::timer_callback, this));
}

private:
void timer_callback()
{
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
size_t count_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}
24 changes: 24 additions & 0 deletions ros2/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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>nns_ros2_bridge</name>
<version>0.0.1</version>
<description>A bridge for ROS2 support within NNStreamer</description>
<maintainer email="[email protected]">wook</maintainer>
<author>Wook Song</author>
<license>LGPLv2.1</license>

<buildtool_depend>ament_cmake</buildtool_depend>
<build_depend>rclcpp</build_depend>
<build_depend>std_msgs</build_depend>

<exec_depend>rclcpp</exec_depend>
<exec_depend>std_msgs</exec_depend>

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

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
6 changes: 4 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
ADD_SUBDIRECTORY(tensor_ros_sink)
ADD_SUBDIRECTORY(tensor_ros_src)
IF(WITH_ROS1)
ADD_SUBDIRECTORY(tensor_ros_sink)
ADD_SUBDIRECTORY(tensor_ros_src)
ENDIF(WITH_ROS1)

0 comments on commit c5d4341

Please sign in to comment.