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

Refactor component interface using node interfaces #62

Merged
merged 5 commits into from
Oct 24, 2023
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Release Versions:
- [2.1.1](#211)
- [2.1.0](#210)

## Upcoming changes (in development)

- Remove callback group for topics and services (#61)
- Avoid conflict with get_parameter (#42)
- Revise ComponentInterface by moving implementations to source file (#39)
- Update component classes to inherit from new ComponentInterface (#38)
- Refactor ComponentInterface to use node interfaces (#33)

## 3.2.0

### October 23, 2023
Expand Down
1 change: 1 addition & 0 deletions source/modulo_components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ament_auto_find_build_dependencies()
include_directories(include)

ament_auto_add_library(${PROJECT_NAME} SHARED
${PROJECT_SOURCE_DIR}/src/ComponentInterface.cpp
${PROJECT_SOURCE_DIR}/src/Component.cpp
${PROJECT_SOURCE_DIR}/src/LifecycleComponent.cpp)

Expand Down
42 changes: 24 additions & 18 deletions source/modulo_components/include/modulo_components/Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#include <rclcpp/node.hpp>

#include "modulo_components/ComponentInterface.hpp"
#include "modulo_components/utilities/utilities.hpp"
#include "modulo_core/EncodedState.hpp"
#include "modulo_core/exceptions/CoreException.hpp"

namespace modulo_components {

Expand All @@ -23,7 +20,7 @@ namespace modulo_components {
* constructor.
* @see LifecycleComponent for a state-based composition alternative
*/
class Component : public ComponentInterface<rclcpp::Node> {
class Component : public rclcpp::Node, public ComponentInterface {
public:
friend class ComponentPublicInterface;

Expand All @@ -40,6 +37,11 @@ class Component : public ComponentInterface<rclcpp::Node> {
virtual ~Component() = default;

protected:
/**
* @copydoc ComponentInterface::get_parameter
*/
[[nodiscard]] std::shared_ptr<state_representation::ParameterInterface> get_parameter(const std::string& name) const;

/**
* @brief Start the execution thread.
*/
Expand Down Expand Up @@ -79,13 +81,16 @@ class Component : public ComponentInterface<rclcpp::Node> {
void on_execute();

// TODO hide ROS methods
using ComponentInterface<rclcpp::Node>::create_output;
using ComponentInterface<rclcpp::Node>::inputs_;
using ComponentInterface<rclcpp::Node>::outputs_;
using ComponentInterface<rclcpp::Node>::qos_;
using ComponentInterface<rclcpp::Node>::publish_predicates;
using ComponentInterface<rclcpp::Node>::publish_outputs;
using ComponentInterface<rclcpp::Node>::evaluate_periodic_callbacks;
using ComponentInterface::get_parameter;
using ComponentInterface::create_output;
using ComponentInterface::inputs_;
using ComponentInterface::outputs_;
using ComponentInterface::periodic_outputs_;
using ComponentInterface::publish_predicate;
using ComponentInterface::publish_predicates;
using ComponentInterface::publish_outputs;
using ComponentInterface::evaluate_periodic_callbacks;
using rclcpp::Node::get_parameter;

std::thread execute_thread_; ///< The execution thread of the component
bool started_; ///< Flag that indicates if execution has started or not
Expand All @@ -98,50 +103,51 @@ inline void Component::add_output(
) {
using namespace modulo_core::communication;
try {
auto parsed_signal_name = this->create_output(signal_name, data, default_topic, fixed_topic, publish_on_step);
auto parsed_signal_name =
this->create_output(PublisherType::PUBLISHER, signal_name, data, default_topic, fixed_topic, publish_on_step);
auto topic_name = this->get_parameter_value<std::string>(parsed_signal_name + "_topic");
RCLCPP_DEBUG_STREAM(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::BOOL: {
auto publisher = this->create_publisher<std_msgs::msg::Bool>(topic_name, this->qos_);
auto publisher = this->create_publisher<std_msgs::msg::Bool>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) =
std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Bool>, std_msgs::msg::Bool>>(
PublisherType::PUBLISHER, publisher)->create_publisher_interface(message_pair);
break;
}
case MessageType::FLOAT64: {
auto publisher = this->create_publisher<std_msgs::msg::Float64>(topic_name, this->qos_);
auto publisher = this->create_publisher<std_msgs::msg::Float64>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) =
std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Float64>, std_msgs::msg::Float64>>(
PublisherType::PUBLISHER, publisher)->create_publisher_interface(message_pair);
break;
}
case MessageType::FLOAT64_MULTI_ARRAY: {
auto publisher = this->create_publisher<std_msgs::msg::Float64MultiArray>(topic_name, this->qos_);
auto publisher = this->create_publisher<std_msgs::msg::Float64MultiArray>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) = std::make_shared<
PublisherHandler<
rclcpp::Publisher<std_msgs::msg::Float64MultiArray>, std_msgs::msg::Float64MultiArray>>(
PublisherType::PUBLISHER, publisher)->create_publisher_interface(message_pair);
break;
}
case MessageType::INT32: {
auto publisher = this->create_publisher<std_msgs::msg::Int32>(topic_name, this->qos_);
auto publisher = this->create_publisher<std_msgs::msg::Int32>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) =
std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Int32>, std_msgs::msg::Int32>>(
PublisherType::PUBLISHER, publisher)->create_publisher_interface(message_pair);
break;
}
case MessageType::STRING: {
auto publisher = this->create_publisher<std_msgs::msg::String>(topic_name, this->qos_);
auto publisher = this->create_publisher<std_msgs::msg::String>(topic_name, this->get_qos());
this->outputs_.at(parsed_signal_name) =
std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::String>, std_msgs::msg::String>>(
PublisherType::PUBLISHER, publisher)->create_publisher_interface(message_pair);
break;
}
case MessageType::ENCODED_STATE: {
auto publisher = this->create_publisher<modulo_core::EncodedState>(topic_name, this->qos_);
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);
Expand Down
Loading