Skip to content

Commit 58d5667

Browse files
author
Javier Moreno
committed
Refs #3637 - Translate demo to rclcpp. Traslate int32 pub/sub demos (works ok).
1 parent bb62736 commit 58d5667

6 files changed

Lines changed: 111 additions & 1 deletion

File tree

Cpp/RAD0_control/README.md

Whitespace-only changes.

Cpp/int32_publisher/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include <chrono>
16+
#include <cstdio>
1617
#include "rclcpp/rclcpp.hpp"
1718
#include "std_msgs/msg/int32.hpp"
1819
using namespace std::chrono_literals;
@@ -32,6 +33,7 @@ class int32_publisher_node : public rclcpp::Node
3233
{
3334
count_.data++;
3435
publisher_->publish(count_);
36+
std::cout << count_.data << std::endl;
3537
}
3638

3739
rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr publisher_;

Cpp/int32_publisher/package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package format="2">
44
<name>int32_publisher_cpp</name>
55
<version>0.0.1</version>
6-
<description>Real apliacion demostration.</description>
6+
<description>Example of a publisher using int32</description>
77
<maintainer email="javiermoreno@eprosima.com">Javier Moreno</maintainer>
88
<license>Apache License 2.0</license>
99
<author>Javier Moreno</author>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
set(CMAKE_C_CLANG_TIDY clang-tidy -checks=*)
3+
project(int32_subscriber_cpp)
4+
5+
# Default to C++14
6+
if(NOT CMAKE_CXX_STANDARD)
7+
set(CMAKE_CXX_STANDARD 14)
8+
endif()
9+
10+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
11+
add_compile_options(-Wall -Wextra -Wpedantic)
12+
endif()
13+
14+
find_package(ament_cmake REQUIRED)
15+
find_package(rclcpp QUIET)
16+
find_package(std_msgs REQUIRED)
17+
18+
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
19+
find_package (Threads REQUIRED)
20+
21+
22+
# Do not compile package if rclcpp is not found
23+
if (NOT rclcpp_FOUND)
24+
message(WARNING "${PROJECT_NAME} will be ignored due to rclcpp is not installed")
25+
ament_package()
26+
return()
27+
endif()
28+
29+
30+
add_executable(${PROJECT_NAME} main.cpp)
31+
ament_target_dependencies(${PROJECT_NAME} rclcpp std_msgs)
32+
33+
target_link_libraries(${PROJECT_NAME} Threads::Threads)
34+
35+
install(TARGETS
36+
${PROJECT_NAME}
37+
DESTINATION lib/${PROJECT_NAME}
38+
)
39+
40+
ament_package()

Cpp/int32_subscriber/main.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <cstdio>
16+
17+
#include "rclcpp/rclcpp.hpp"
18+
#include "std_msgs/msg/int32.hpp"
19+
using std::placeholders::_1;
20+
21+
class int32_subscriber_node : public rclcpp::Node
22+
{
23+
public:
24+
int32_subscriber_node() : Node("int32_subscriber_cpp")
25+
{
26+
subscription_ = this->create_subscription<std_msgs::msg::Int32>("std_msgs_msg_Int32",
27+
std::bind(&int32_subscriber_node::topic_callback, this, _1));
28+
}
29+
30+
private:
31+
rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr subscription_;
32+
33+
void topic_callback(const std_msgs::msg::Int32::SharedPtr msg)
34+
{
35+
std::cout << "I heard: " << msg->data << std::endl;
36+
}
37+
};
38+
39+
int main(int argc, char * argv[])
40+
{
41+
rclcpp::init(argc, argv);
42+
rclcpp::spin(std::make_shared<int32_subscriber_node>());
43+
rclcpp::shutdown();
44+
return 0;
45+
}

Cpp/int32_subscriber/package.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="2">
4+
<name>int32_subscriber_cpp</name>
5+
<version>0.0.1</version>
6+
<description>Example of a subscriber using int32</description>
7+
<maintainer email="javiermoreno@eprosima.com">Javier Moreno</maintainer>
8+
<license>Apache License 2.0</license>
9+
<author>Javier Moreno</author>
10+
11+
<buildtool_depend>ament_cmake</buildtool_depend>
12+
13+
<build_depend>rclcpp</build_depend>
14+
<build_depend>std_msgs</build_depend>
15+
16+
<exec_depend>rclcpp</exec_depend>
17+
<exec_depend>std_msgs</exec_depend>
18+
19+
<export>
20+
<build_type>ament_cmake</build_type>
21+
</export>
22+
</package>
23+

0 commit comments

Comments
 (0)