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

feat: add support for generic subscriptions #144

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
"/home/ros2/ws/src/modulo_utils/include",
"/home/ros2/ws/src/modulo_core/include",
"/home/ros2/ws/src/modulo_components/include",
"/home/ros2/ws/src/modulo_controllers/include"
"/home/ros2/ws/src/modulo_controllers/include",
"/usr/include"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
"cppStandard": "c++20",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
"-std=c++20",
],
}
],
"version": 4
Expand Down
18 changes: 16 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
"editor.rulers": [
120
],
"autopep8.args": ["--max-line-length", "120", "--experimental"],
"pylint.args": ["--generate-members", "--max-line-length", "120", "-d", "C0114", "-d", "C0115", "-d", "C0116"]
"autopep8.args": [
"--max-line-length",
"120",
"--experimental"
],
"pylint.args": [
"--generate-members",
"--max-line-length",
"120",
"-d",
"C0114",
"-d",
"C0115",
"-d",
"C0116"
],
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Release Versions:
- release: use updated base image (#139)
- fix(controllers): remove duplicate function (#140)
- chore: format repository (#142)
- feat: add support for generic inputs/outputs (#133)

## 4.2.2

Expand Down
6 changes: 3 additions & 3 deletions source/modulo_components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()

# default to C++17
# default to C++20
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Expand Down Expand Up @@ -60,4 +60,4 @@ if(BUILD_TESTING)

endif()

ament_auto_package()
ament_auto_package()
25 changes: 19 additions & 6 deletions source/modulo_components/include/modulo_components/Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include <rclcpp/node.hpp>

#include "modulo_components/ComponentInterface.hpp"
#include "modulo_core/concepts.hpp"

namespace modulo_components {

using namespace modulo_core::concepts;

/**
* @class Component
* @brief A wrapper for rclcpp::Node to simplify application composition through unified component interfaces.
Expand Down Expand Up @@ -107,6 +110,14 @@ inline void Component::add_output(
this->get_logger(), "Adding output '" << parsed_signal_name << "' with topic name '" << topic_name << "'.");
auto message_pair = this->outputs_.at(parsed_signal_name)->get_message_pair();
switch (message_pair->get_type()) {
case MessageType::ENCODED_STATE: {
auto publisher = this->create_publisher<modulo_core::EncodedState>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) =
std::make_shared<PublisherHandler<rclcpp::Publisher<modulo_core::EncodedState>, modulo_core::EncodedState>>(
PublisherType::PUBLISHER, publisher)
->create_publisher_interface(message_pair);
break;
}
case MessageType::BOOL: {
auto publisher = this->create_publisher<std_msgs::msg::Bool>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) =
Expand Down Expand Up @@ -147,17 +158,19 @@ inline void Component::add_output(
->create_publisher_interface(message_pair);
break;
}
case MessageType::ENCODED_STATE: {
auto publisher = this->create_publisher<modulo_core::EncodedState>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) =
std::make_shared<PublisherHandler<rclcpp::Publisher<modulo_core::EncodedState>, modulo_core::EncodedState>>(
PublisherType::PUBLISHER, publisher)
->create_publisher_interface(message_pair);
case MessageType::CUSTOM_MESSAGE: {
if constexpr (CustomDataT<DataT>) {
auto publisher = this->create_publisher<DataT>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) =
std::make_shared<PublisherHandler<rclcpp::Publisher<DataT>, DataT>>(PublisherType::PUBLISHER, publisher)
->create_publisher_interface(message_pair);
}
break;
}
}
} catch (const std::exception& ex) {
RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to add output '" << signal_name << "': " << ex.what());
}
}

}// namespace modulo_components
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,14 @@ inline void ComponentInterface::add_input(
subscription_interface = subscription_handler->create_subscription_interface(subscription);
break;
}
case MessageType::CUSTOM_MESSAGE: {
auto subscription_handler = std::make_shared<SubscriptionHandler<DataT>>(message_pair);
auto subscription = rclcpp::create_subscription<DataT>(
this->node_parameters_, this->node_topics_, topic_name, this->qos_,
subscription_handler->get_callback(user_callback));
subscription_interface = subscription_handler->create_subscription_interface(subscription);
break;
}
}
this->inputs_.insert_or_assign(parsed_signal_name, subscription_interface);
} catch (const std::exception& ex) {
Expand Down
1 change: 0 additions & 1 deletion source/modulo_components/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<depend>modulo_interfaces</depend>
<depend>modulo_utils</depend>
<depend>tf2_ros</depend>

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

Expand Down
2 changes: 2 additions & 0 deletions source/modulo_components/src/LifecycleComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ bool LifecycleComponent::configure_outputs() {
->create_publisher_interface(message_pair);
break;
}
default:
break;
}
} catch (const modulo_core::exceptions::CoreException& ex) {
success = false;
Expand Down
21 changes: 21 additions & 0 deletions source/modulo_components/test/cpp/test_component.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <gtest/gtest.h>

#include <geometry_msgs/msg/twist.hpp>
#include <rclcpp/node_options.hpp>
#include <sensor_msgs/msg/image.hpp>

#include <modulo_core/EncodedState.hpp>

Expand Down Expand Up @@ -70,5 +72,24 @@ TEST_F(ComponentTest, AddRemoveOutput) {
EXPECT_NO_THROW(component_->publish_output("8_teEsTt_#1@3"));
EXPECT_NO_THROW(component_->publish_output("test_13"));
EXPECT_THROW(component_->publish_output(""), modulo_core::exceptions::CoreException);

auto std_msg_data = std::make_shared<std_msgs::msg::String>();
std_msg_data->data = "foo";
component_->add_output("custom_msg_test", std_msg_data);
EXPECT_TRUE(component_->outputs_.find("custom_msg_test") != component_->outputs_.end());
EXPECT_NO_THROW(component_->outputs_.at("custom_msg_test")->publish());
EXPECT_THROW(component_->publish_output("custom_msg_test"), modulo_core::exceptions::CoreException);

auto geometry_msg_data = std::make_shared<geometry_msgs::msg::Twist>();
component_->add_output("geometry_msg_test", geometry_msg_data);
EXPECT_TRUE(component_->outputs_.find("geometry_msg_test") != component_->outputs_.end());
EXPECT_NO_THROW(component_->outputs_.at("geometry_msg_test")->publish());
EXPECT_THROW(component_->publish_output("geometry_msg_test"), modulo_core::exceptions::CoreException);

auto sensor_msg_data = std::make_shared<sensor_msgs::msg::Image>();
component_->add_output("sensor_msg_test", sensor_msg_data);
EXPECT_TRUE(component_->outputs_.find("sensor_msg_test") != component_->outputs_.end());
EXPECT_NO_THROW(component_->outputs_.at("sensor_msg_test")->publish());
EXPECT_THROW(component_->publish_output("sensor_msg_test"), modulo_core::exceptions::CoreException);
}
}// namespace modulo_components
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "test_modulo_components/component_public_interfaces.hpp"

#include <sensor_msgs/msg/image.hpp>

namespace modulo_components {

using namespace std::chrono_literals;
Expand Down Expand Up @@ -107,6 +109,13 @@ TYPED_TEST(ComponentInterfaceTest, AddRemoveInput) {
// remove input
this->component_->remove_input("test_13");
EXPECT_TRUE(this->component_->inputs_.find("test_13") == this->component_->inputs_.end());

auto sensor_msg_data = std::make_shared<sensor_msgs::msg::Image>();
sensor_msg_data->height = 480;
EXPECT_NO_THROW(this->component_->add_input("sensor_msg_data", sensor_msg_data));
EXPECT_FALSE(this->component_->inputs_.find("sensor_msg_data") == this->component_->inputs_.end());
// EXPECT_EQ(
// this->component_->template get_parameter_value<sensor_msgs::msg::Image>("sensor_msg_data"), *sensor_msg_data);
}

TYPED_TEST(ComponentInterfaceTest, AddInputWithUserCallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "test_modulo_components/component_public_interfaces.hpp"

#include <sensor_msgs/msg/image.hpp>

namespace modulo_components {

template<class NodeT>
Expand Down
8 changes: 4 additions & 4 deletions source/modulo_controllers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ if (NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif ()

# default to C++17
if (NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif ()
# default to C++20
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
endif()

if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
Expand Down
4 changes: 2 additions & 2 deletions source/modulo_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()

# default to C++17
# default to C++20
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Expand Down
Loading
Loading