Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit of port of naive to ros 2 #8

Merged
merged 3 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

bgill92 marked this conversation as resolved.
Show resolved Hide resolved
#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);
griswaldbrooks marked this conversation as resolved.
Show resolved Hide resolved
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="[email protected]">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);
})} {}
13 changes: 13 additions & 0 deletions ros2/naive/src/node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "naive/incrementer.hpp"
#include <rclcpp/rclcpp.hpp>

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

griswaldbrooks marked this conversation as resolved.
Show resolved Hide resolved
rclcpp::init(argc,argv);
auto const node = std::make_shared<rclcpp::Node>("incrementer");
griswaldbrooks marked this conversation as resolved.
Show resolved Hide resolved
Incrementer incrementer{node, "/in", "/out"};
rclcpp::spin(node);
rclcpp::shutdown();

return 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth, this is unnecessary. It's specified that if a program reaches the end, it will always return 0 so it's safe to remove this.

griswaldbrooks marked this conversation as resolved.
Show resolved Hide resolved
}