Skip to content

Commit 90ef71a

Browse files
author
Javier Moreno
committed
Added demo ROS2
1 parent 7f1b182 commit 90ef71a

5 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎external_node/CMakeLists.txt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(external_node)
3+
4+
# Default to C++14
5+
if(NOT CMAKE_CXX_STANDARD)
6+
set(CMAKE_CXX_STANDARD 14)
7+
endif()
8+
9+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
10+
add_compile_options(-Wall -Wextra -Wpedantic)
11+
endif()
12+
13+
find_package(ament_cmake REQUIRED)
14+
find_package(rclcpp REQUIRED)
15+
find_package(std_msgs REQUIRED)
16+
17+
add_executable(external_node main.cpp)
18+
ament_target_dependencies(external_node rclcpp std_msgs)
19+
20+
install(TARGETS
21+
external_node
22+
DESTINATION lib/${PROJECT_NAME}
23+
)
24+
25+
ament_package()

‎external_node/COLCON_IGNORE‎

Whitespace-only changes.

‎external_node/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Minimal publisher examples
2+
3+
This package contains a few different strategies for creating short nodes which blast out messages.
4+
The `talker_timer_lambda` and `talker_timer_member_function` recipes create subclasses of `rclcpp::Node` and set up an `rclcpp::timer` to periodically call functions which publish messages.
5+
The `talker_without_subclass` recipe instead instantiates a `rclcpp::Node` object *without* subclassing it, which works, but is not compatible with composition, and thus is no longer the recommended style for ROS 2 coding.

‎external_node/main.cpp‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2016 Open Source Robotics Foundation, Inc.
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 "rclcpp/rclcpp.hpp"
16+
#include "std_msgs/msg/float64.hpp"
17+
#include "std_msgs/msg/string.hpp"
18+
using std::placeholders::_1;
19+
20+
class ExternalNode : public rclcpp::Node
21+
{
22+
public:
23+
ExternalNode() : Node("external_node")
24+
{
25+
power_publisher_ = this->create_publisher<std_msgs::msg::Float64>("engine_power");
26+
27+
altitude_subscription_ = this->create_subscription<std_msgs::msg::String>("status_report", std::bind(&ExternalNode::topic_callback, this, _1));
28+
}
29+
30+
private:
31+
rclcpp::Publisher<std_msgs::msg::Float64>::SharedPtr power_publisher_;
32+
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr altitude_subscription_;
33+
34+
void topic_callback(const std_msgs::msg::String::SharedPtr msg)
35+
{
36+
if (strcmp(msg->data.c_str(), "Failure!!") == 0)
37+
{
38+
//std_msgs__msg__Float64 message;
39+
auto message = std_msgs::msg::Float64();
40+
message.data = 10;
41+
this->power_publisher_->publish(message);
42+
}
43+
else if (strcmp(msg->data.c_str(), "Failure!!") == 0)
44+
{
45+
auto message = std_msgs::msg::Float64();
46+
message.data = 5;
47+
this->power_publisher_->publish(message);
48+
}
49+
else
50+
{
51+
RCLCPP_INFO(this->get_logger(), "Unkwnow status: '%s'", msg->data.c_str())
52+
}
53+
}
54+
};
55+
56+
int main(int argc, char * argv[])
57+
{
58+
rclcpp::init(argc, argv);
59+
rclcpp::spin(std::make_shared<ExternalNode>());
60+
rclcpp::shutdown();
61+
return 0;
62+
}

‎external_node/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>external_node</name>
5+
<version>0.0.1</version>
6+
<description>Demo. Node running on ros2</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)