Skip to content

Commit 1d923df

Browse files
author
Javier Moreno
committed
Added complex examples and RAD0 ros2 node
1 parent 462c219 commit 1d923df

16 files changed

Lines changed: 213 additions & 59 deletions

File tree

‎RAD0_control/CMakeLists.txt‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(rad0_control)
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 QUIET)
15+
find_package(std_msgs REQUIRED)
16+
17+
18+
# Do not compile package if rclcpp is not found
19+
if (NOT rclcpp_FOUND)
20+
message(WARNING "${PROJECT_NAME} will be ignored due to rclcpp is not installed")
21+
return()
22+
endif()
23+
24+
25+
add_executable(${PROJECT_NAME} main.cpp)
26+
ament_target_dependencies(${PROJECT_NAME} rclcpp std_msgs)
27+
28+
install(TARGETS
29+
${PROJECT_NAME}
30+
DESTINATION lib/${PROJECT_NAME}
31+
)
32+
33+
ament_package()

‎RAD0_control/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.

‎RAD0_control/main.cpp‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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/int32.hpp"
18+
#include "std_msgs/msg/string.hpp"
19+
using std::placeholders::_1;
20+
21+
class ExternalNode : public rclcpp::Node
22+
{
23+
public:
24+
ExternalNode() : Node("control")
25+
{
26+
engine_power_publisher_ = this->create_publisher<std_msgs::msg::Int32>("std_msgs_msg_Int32");
27+
warn_publisher_ = this->create_publisher<std_msgs::msg::String>("std_msgs_msg_String");
28+
29+
altitude_subscription_ = this->create_subscription<std_msgs::msg::Float64>("std_msgs_msg_Float64", std::bind(&ExternalNode::topic_callback, this, _1));
30+
}
31+
32+
private:
33+
rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr engine_power_publisher_;
34+
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr warn_publisher_;
35+
rclcpp::Subscription<std_msgs::msg::Float64>::SharedPtr altitude_subscription_;
36+
37+
void topic_callback(const std_msgs::msg::Float64::SharedPtr msg)
38+
{
39+
// Warning
40+
if (msg->data <= 500)
41+
{
42+
{
43+
auto message = std_msgs::msg::String();
44+
message.data = "Failure!!";
45+
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str())
46+
this->warn_publisher_->publish(message);
47+
}
48+
49+
{
50+
auto message = std_msgs::msg::Int32();
51+
message.data = 10;
52+
this->engine_power_publisher_->publish(message);
53+
}
54+
55+
}
56+
57+
// Failure
58+
else if (msg->data <= 1000)
59+
{
60+
{
61+
auto message = std_msgs::msg::String();
62+
message.data = "Warning!!";
63+
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str())
64+
this->warn_publisher_->publish(message);
65+
}
66+
67+
{
68+
auto message = std_msgs::msg::Int32();
69+
message.data = 5;
70+
this->engine_power_publisher_->publish(message);
71+
}
72+
73+
}
74+
}
75+
};
76+
77+
int main(int argc, char * argv[])
78+
{
79+
rclcpp::init(argc, argv);
80+
rclcpp::spin(std::make_shared<ExternalNode>());
81+
rclcpp::shutdown();
82+
return 0;
83+
}

‎RAD0_control/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>rad0_control</name>
5+
<version>0.0.1</version>
6+
<description>Real apliacion demostration.</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+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.5)
22

3-
project(example_custom_msgs)
3+
project(complex_msgs)
44

55
# Default to C++14
66
if(NOT CMAKE_CXX_STANDARD)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0"?>
22
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
33
<package format="3">
4-
<name>example_custom_msgs</name>
4+
<name>complex_msgs</name>
55
<version>1.0.0</version>
6-
<description>Custom generated msg.</description>
6+
<description>Example of a complex msg.</description>
77
<maintainer email="javiermoreno@eproxima.com">Javier Moreno</maintainer>
88
<license>Apache License 2.0</license>
99

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=*)
3+
project(complex_msg_publisher)
4+
5+
find_package(ament_cmake REQUIRED)
6+
find_package(rclc QUIET)
7+
find_package(complex_msgs REQUIRED)
8+
9+
10+
# Do not compile package if rclcpp is not found
11+
if (NOT rclc_FOUND)
12+
message(WARNING "${PROJECT_NAME} will be ignored due to rclc is not installed")
13+
return()
14+
endif()
15+
16+
17+
add_executable(${PROJECT_NAME} main.c)
18+
ament_target_dependencies(${PROJECT_NAME} rclc complex_msgs)
19+
20+
install(TARGETS
21+
${PROJECT_NAME}
22+
DESTINATION lib/${PROJECT_NAME}
23+
)
24+
25+
target_link_libraries(${PROJECT_NAME} micrortps_client microcdr)
26+
27+
ament_package()
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <rclc/rclc.h>
2-
#include <example_custom_msgs/msg/nested_msg_test.h>
2+
#include <complex_msgs/msg/nested_msg_test.h>
33

44
#include <stdio.h>
55

@@ -10,9 +10,9 @@ int main(int argc, char* argv[])
1010
rclc_init(0, NULL);
1111
rclc_node_t* node = rclc_create_node("publisher_node", "");
1212
rclc_publisher_t* publisher =
13-
rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(example_custom_msgs, msg, NestedMsgTest), "example_custom_msgs_msg_NestedMsgTest", 1);
13+
rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(complex_msgs, msg, NestedMsgTest), "complex_msgs_msg_NestedMsgTest", 1);
1414

15-
example_custom_msgs__msg__NestedMsgTest msg;
15+
complex_msgs__msg__NestedMsgTest msg;
1616
char Buff1[30];
1717
msg.data14.data1.data = Buff1;
1818
msg.data14.data1.size = 0;

0 commit comments

Comments
 (0)