CORESENSE4home_Architecture
Loading...
Searching...
No Matches
Afferent.hpp
Go to the documentation of this file.
1// Copyright 2024 Intelligent Robotics Lab
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
16#ifndef CS4HOME_CORE__AFFERENT_HPP_
17#define CS4HOME_CORE__AFFERENT_HPP_
18
19#include <memory>
20#include <utility>
21#include <queue>
22#include <vector>
23#include <string>
24
25#include "rclcpp_lifecycle/lifecycle_node.hpp"
26#include "rclcpp/rclcpp.hpp"
27#include "rclcpp/macros.hpp"
28#include "rclcpp/serialization.hpp"
29#include "rclcpp/create_generic_subscription.hpp"
30
31namespace cs4home_core
32{
33
40{
41public:
42 RCLCPP_SMART_PTR_DEFINITIONS(Afferent)
43
44
49
54 explicit Afferent(rclcpp_lifecycle::LifecycleNode::SharedPtr parent);
55
60 virtual bool configure() = 0;
61
68 void set_mode(
70 std::function<void(std::unique_ptr<rclcpp::SerializedMessage>)> cb = nullptr);
71
72
78
83 void set_max_queue_size(size_t size) {max_queue_size_ = size;}
84
90
97 template<class MessageT> std::unique_ptr<MessageT> get_msg(
98 std::unique_ptr<rclcpp::SerializedMessage> msg)
99 {
100 auto typed_msg = std::make_unique<MessageT>();
101 rclcpp::Serialization<MessageT> serializer;
102 serializer.deserialize_message(msg.get(), typed_msg.get());
103
104 return std::move(typed_msg);
105 }
106
112 template<class MessageT> std::unique_ptr<MessageT> get_msg()
113 {
114 if (msg_queue_.empty()) {
115 return {};
116 }
117
118 std::unique_ptr<rclcpp::SerializedMessage> msg = std::move(msg_queue_.front());
119 msg_queue_.pop();
120
121 return get_msg<MessageT>(std::move(msg));
122 }
123
124protected:
126 rclcpp_lifecycle::LifecycleNode::SharedPtr parent_;
128 std::vector<std::shared_ptr<rclcpp::GenericSubscription>> subs_;
129
131
133 const size_t MAX_DEFAULT_QUEUE_SIZE = 100;
137 std::queue<std::unique_ptr<rclcpp::SerializedMessage>> msg_queue_;
138
140 std::function<void(std::unique_ptr<rclcpp::SerializedMessage>)> callback_;
141
142
149 bool create_subscriber(const std::string & topic, const std::string & type);
150};
151
152} // namespace cs4home_core
153
154#endif // CS4HOME_CORE__AFFERENT_HPP_
const size_t MAX_DEFAULT_QUEUE_SIZE
Default maximum queue size.
Definition Afferent.hpp:133
bool create_subscriber(const std::string &topic, const std::string &type)
Creates a subscriber for a specific topic and message type.
Definition Afferent.cpp:71
void set_max_queue_size(size_t size)
Sets the maximum queue size for storing messages.
Definition Afferent.hpp:83
std::function< void(std::unique_ptr< rclcpp::SerializedMessage >)> callback_
Callback for serialized messages.
Definition Afferent.hpp:140
EfferentProcessMode get_mode()
Gets the current processing mode.
Definition Afferent.hpp:77
EfferentProcessMode mode_
Current processing mode.
Definition Afferent.hpp:130
Afferent(rclcpp_lifecycle::LifecycleNode::SharedPtr parent)
Constructor for the Afferent class.
Definition Afferent.cpp:27
std::unique_ptr< MessageT > get_msg(std::unique_ptr< rclcpp::SerializedMessage > msg)
Converts a serialized message to a typed message.
Definition Afferent.hpp:97
size_t max_queue_size_
Maximum queue size.
Definition Afferent.hpp:135
std::vector< std::shared_ptr< rclcpp::GenericSubscription > > subs_
List of subscriptions.
Definition Afferent.hpp:128
virtual bool configure()=0
Configures the afferent component; intended for subclass implementation.
rclcpp_lifecycle::LifecycleNode::SharedPtr parent_
Shared pointer to the parent node.
Definition Afferent.hpp:126
std::queue< std::unique_ptr< rclcpp::SerializedMessage > > msg_queue_
Queue for serialized messages.
Definition Afferent.hpp:137
void set_mode(EfferentProcessMode mode, std::function< void(std::unique_ptr< rclcpp::SerializedMessage >)> cb=nullptr)
Sets the processing mode and an optional callback function.
Definition Afferent.cpp:42
EfferentProcessMode
Defines processing modes for serialized message handling.
Definition Afferent.hpp:48
@ ONDEMAND
Definition Afferent.hpp:48
@ CALLBACK
Definition Afferent.hpp:48
size_t get_max_queue_size()
Gets the maximum queue size.
Definition Afferent.hpp:89
std::unique_ptr< MessageT > get_msg()
Retrieves the next message from the queue, if available.
Definition Afferent.hpp:112
Definition Afferent.hpp:32