From 7d06593253ffd6620ede447a1559a3aae2eb19b0 Mon Sep 17 00:00:00 2001 From: Bilal Gill Date: Wed, 13 Jul 2022 16:21:52 -0400 Subject: [PATCH 1/3] Initial commit of port of naive to ros 2 --- ros2/naive/CMakeLists.txt | 34 ++++++++++++++++++++++++ ros2/naive/include/naive/incrementer.hpp | 16 +++++++++++ ros2/naive/package.xml | 21 +++++++++++++++ ros2/naive/src/incrementer.cpp | 15 +++++++++++ ros2/naive/src/node.cpp | 19 +++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 ros2/naive/CMakeLists.txt create mode 100644 ros2/naive/include/naive/incrementer.hpp create mode 100644 ros2/naive/package.xml create mode 100644 ros2/naive/src/incrementer.cpp create mode 100644 ros2/naive/src/node.cpp diff --git a/ros2/naive/CMakeLists.txt b/ros2/naive/CMakeLists.txt new file mode 100644 index 0000000..60cf1cd --- /dev/null +++ b/ros2/naive/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.8) +project(naive) + +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() + +# find dependencies +find_package(ament_cmake REQUIRED) +find_package(rclcpp REQUIRED) +find_package(std_msgs REQUIRED) + +add_executable(naive src/node.cpp src/incrementer.cpp) +target_include_directories(naive PUBLIC include) +ament_target_dependencies(naive rclcpp std_msgs) + +install(TARGETS + naive + DESTINATION lib/${PROJECT_NAME} +) + +if(BUILD_TESTING) + find_package(ament_lint_auto REQUIRED) + # the following line skips the linter which checks for copyrights + # comment the line when a copyright and license is added to all source files + set(ament_cmake_copyright_FOUND TRUE) + # the following line skips cpplint (only works in a git repo) + # comment the line when this package is in a git repo and when + # a copyright and license is added to all source files + set(ament_cmake_cpplint_FOUND TRUE) + ament_lint_auto_find_test_dependencies() +endif() + +ament_package() diff --git a/ros2/naive/include/naive/incrementer.hpp b/ros2/naive/include/naive/incrementer.hpp new file mode 100644 index 0000000..01e8c4c --- /dev/null +++ b/ros2/naive/include/naive/incrementer.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/int64.hpp" + +class Incrementer { +public: + Incrementer(std::shared_ptr node, const std::string& in_topic, const std::string& out_topic); +private: + static constexpr int QUEUE_SIZE = 10; + std::shared_ptr node_; + std::shared_ptr> pub_; + std::shared_ptr> sub_; +}; \ No newline at end of file diff --git a/ros2/naive/package.xml b/ros2/naive/package.xml new file mode 100644 index 0000000..c84f5e2 --- /dev/null +++ b/ros2/naive/package.xml @@ -0,0 +1,21 @@ + + + + naive + 0.0.0 + TODO: Package description + bilal + TODO: License declaration + + ament_cmake + + rclcpp + std_msgs + + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/ros2/naive/src/incrementer.cpp b/ros2/naive/src/incrementer.cpp new file mode 100644 index 0000000..6b7ee8a --- /dev/null +++ b/ros2/naive/src/incrementer.cpp @@ -0,0 +1,15 @@ +#include "naive/incrementer.hpp" + +#include +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/int64.hpp" + +Incrementer::Incrementer(std::shared_ptr node, const std::string& in_topic, const std::string& out_topic) +: node_{std::move(node)}, + pub_{node_->create_publisher(out_topic,QUEUE_SIZE)}, + sub_{node_->create_subscription(in_topic, QUEUE_SIZE, [this](const std_msgs::msg::Int64::UniquePtr msg) + { + std_msgs::msg::Int64 incremented; + incremented.data = msg->data + 1; + pub_->publish(incremented); + })} {} \ No newline at end of file diff --git a/ros2/naive/src/node.cpp b/ros2/naive/src/node.cpp new file mode 100644 index 0000000..3c5e991 --- /dev/null +++ b/ros2/naive/src/node.cpp @@ -0,0 +1,19 @@ +#include "naive/incrementer.hpp" + +#include "rclcpp/rclcpp.hpp" + +int main(int argc, char **argv) { + + rclcpp::init(argc,argv); + + auto const node = std::make_shared("incrementer"); + + Incrementer incrementer{node, "/in", "/out"}; + + rclcpp::spin(node); + + rclcpp::shutdown(); + + return 0; + +} \ No newline at end of file From 59e9371981d2f7009549e8d42e149e38a5f2ae69 Mon Sep 17 00:00:00 2001 From: Bilal Gill Date: Fri, 15 Jul 2022 09:42:17 -0400 Subject: [PATCH 2/3] Updated documentation, comments, and style based on feedback --- ros2/naive/CMakeLists.txt | 8 ++-- ros2/naive/include/naive/incrementer.hpp | 49 +++++++++++++++++++++--- ros2/naive/package.xml | 4 +- ros2/naive/src/incrementer.cpp | 26 +++++++------ ros2/naive/src/node.cpp | 10 +---- 5 files changed, 67 insertions(+), 30 deletions(-) diff --git a/ros2/naive/CMakeLists.txt b/ros2/naive/CMakeLists.txt index 60cf1cd..f53d608 100644 --- a/ros2/naive/CMakeLists.txt +++ b/ros2/naive/CMakeLists.txt @@ -1,8 +1,8 @@ -cmake_minimum_required(VERSION 3.8) -project(naive) +cmake_minimum_required(VERSION 3.16) +project(naive CXX) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(-Wall -Wextra -Wpedantic) + add_compile_options(-Wall -Wextra -Wpedantic -Werror) endif() # find dependencies @@ -11,7 +11,7 @@ find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) add_executable(naive src/node.cpp src/incrementer.cpp) -target_include_directories(naive PUBLIC include) +target_include_directories(naive PRIVATE include) ament_target_dependencies(naive rclcpp std_msgs) install(TARGETS diff --git a/ros2/naive/include/naive/incrementer.hpp b/ros2/naive/include/naive/incrementer.hpp index 01e8c4c..a93f7c0 100644 --- a/ros2/naive/include/naive/incrementer.hpp +++ b/ros2/naive/include/naive/incrementer.hpp @@ -1,16 +1,55 @@ +/********************************************************************* + * Software License Agreement (BSD License) + * + * Copyright (c) 2022, PickNik, LLC. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of PickNik nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + *********************************************************************/ #pragma once #include #include -#include "rclcpp/rclcpp.hpp" -#include "std_msgs/msg/int64.hpp" +#include +#include class Incrementer { public: - Incrementer(std::shared_ptr node, const std::string& in_topic, const std::string& out_topic); + /** + * @brief Incrementer constructor + * + * @param[in] node Shared pointer to a ROS node + * @param[in] in_topic ROS topic to the Incrementer subscribes to + * @param[in] out_topic ROS topic that the Incrementer publishes to + */ + Incrementer(std::shared_ptr node, std::string const& in_topic, std::string const& out_topic); private: - static constexpr int QUEUE_SIZE = 10; std::shared_ptr node_; std::shared_ptr> pub_; std::shared_ptr> sub_; -}; \ No newline at end of file +}; diff --git a/ros2/naive/package.xml b/ros2/naive/package.xml index c84f5e2..b627d1e 100644 --- a/ros2/naive/package.xml +++ b/ros2/naive/package.xml @@ -3,9 +3,9 @@ naive 0.0.0 - TODO: Package description + Reference pub/sub example bilal - TODO: License declaration + MIT ament_cmake diff --git a/ros2/naive/src/incrementer.cpp b/ros2/naive/src/incrementer.cpp index 6b7ee8a..3388713 100644 --- a/ros2/naive/src/incrementer.cpp +++ b/ros2/naive/src/incrementer.cpp @@ -1,15 +1,19 @@ #include "naive/incrementer.hpp" #include -#include "rclcpp/rclcpp.hpp" -#include "std_msgs/msg/int64.hpp" +#include +#include -Incrementer::Incrementer(std::shared_ptr node, const std::string& in_topic, const std::string& out_topic) -: node_{std::move(node)}, - pub_{node_->create_publisher(out_topic,QUEUE_SIZE)}, - sub_{node_->create_subscription(in_topic, QUEUE_SIZE, [this](const std_msgs::msg::Int64::UniquePtr msg) - { - std_msgs::msg::Int64 incremented; - incremented.data = msg->data + 1; - pub_->publish(incremented); - })} {} \ No newline at end of file +namespace { + constexpr auto queue_size = 10; +} + +Incrementer::Incrementer(std::shared_ptr node, std::string const& in_topic, std::string const& out_topic) +: node_{std::move(node)}, + pub_{node_->create_publisher(out_topic,queue_size)}, + sub_{node_->create_subscription(in_topic, queue_size, + [this](std_msgs::msg::Int64::UniquePtr const msg) { + std_msgs::msg::Int64 incremented; + incremented.data = msg->data + 1; + pub_->publish(incremented); + })} {} diff --git a/ros2/naive/src/node.cpp b/ros2/naive/src/node.cpp index 3c5e991..84c0f66 100644 --- a/ros2/naive/src/node.cpp +++ b/ros2/naive/src/node.cpp @@ -1,19 +1,13 @@ #include "naive/incrementer.hpp" - -#include "rclcpp/rclcpp.hpp" +#include int main(int argc, char **argv) { rclcpp::init(argc,argv); - auto const node = std::make_shared("incrementer"); - Incrementer incrementer{node, "/in", "/out"}; - rclcpp::spin(node); - rclcpp::shutdown(); return 0; - -} \ No newline at end of file +} From af282e1c228f76a14289ff8c5945aee75f25fb3e Mon Sep 17 00:00:00 2001 From: Griswald Brooks Date: Wed, 14 Sep 2022 15:04:43 -0700 Subject: [PATCH 3/3] Update ros2/naive/src/node.cpp --- ros2/naive/src/node.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/ros2/naive/src/node.cpp b/ros2/naive/src/node.cpp index 84c0f66..266c1d1 100644 --- a/ros2/naive/src/node.cpp +++ b/ros2/naive/src/node.cpp @@ -9,5 +9,4 @@ int main(int argc, char **argv) { rclcpp::spin(node); rclcpp::shutdown(); - return 0; }