Skip to content

Commit

Permalink
Initial commit of port of naive to ros 2 (#8)
Browse files Browse the repository at this point in the history
* Initial commit of port of naive to ros 2

* Updated documentation, comments, and style based on feedback

* Update ros2/naive/src/node.cpp

Co-authored-by: Griswald Brooks <griswald.brooks@picknik.ai>
  • Loading branch information
bgill92 and griswaldbrooks authored Sep 14, 2022
1 parent edc27e6 commit f4ed9be
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
34 changes: 34 additions & 0 deletions ros2/naive/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 -Werror)
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 PRIVATE 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()
55 changes: 55 additions & 0 deletions ros2/naive/include/naive/incrementer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +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 <string>
#include <memory>
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/int64.hpp>

class Incrementer {
public:
/**
* @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<rclcpp::Node> node, std::string const& in_topic, std::string const& out_topic);
private:
std::shared_ptr<rclcpp::Node> node_;
std::shared_ptr<rclcpp::Publisher<std_msgs::msg::Int64>> pub_;
std::shared_ptr<rclcpp::Subscription<std_msgs::msg::Int64>> sub_;
};
21 changes: 21 additions & 0 deletions ros2/naive/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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>naive</name>
<version>0.0.0</version>
<description>Reference pub/sub example</description>
<maintainer email="bilal.gill@picknik.ai">bilal</maintainer>
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>rclcpp</depend>
<depend>std_msgs</depend>

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

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
19 changes: 19 additions & 0 deletions ros2/naive/src/incrementer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "naive/incrementer.hpp"

#include <memory>
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/int64.hpp>

namespace {
constexpr auto queue_size = 10;
}

Incrementer::Incrementer(std::shared_ptr<rclcpp::Node> node, std::string const& in_topic, std::string const& out_topic)
: node_{std::move(node)},
pub_{node_->create_publisher<std_msgs::msg::Int64>(out_topic,queue_size)},
sub_{node_->create_subscription<std_msgs::msg::Int64>(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);
})} {}
12 changes: 12 additions & 0 deletions ros2/naive/src/node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "naive/incrementer.hpp"
#include <rclcpp/rclcpp.hpp>

int main(int argc, char **argv) {

rclcpp::init(argc,argv);
auto const node = std::make_shared<rclcpp::Node>("incrementer");
Incrementer incrementer{node, "/in", "/out"};
rclcpp::spin(node);
rclcpp::shutdown();

}

0 comments on commit f4ed9be

Please sign in to comment.