SMACC2
Loading...
Searching...
No Matches
cp_topic_subscriber.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft 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/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22#include <optional>
24#include <smacc2/component.hpp>
26
27namespace smacc2
28{
29namespace client_core_components
30{
31using namespace smacc2::default_events;
32
33template <typename MessageType>
35{
36public:
37 std::optional<int> queueSize;
38
39 typedef MessageType TMessageType;
40
42
43 CpTopicSubscriber(std::string topicname)
44 {
45 topicName_ = topicname;
46 initialized_ = false;
47 }
48
49 virtual ~CpTopicSubscriber() {}
50
51 std::function<void(const MessageType &)> postMessageEvent;
52 std::function<void(const MessageType &)> postInitialMessageEvent;
53
54 smacc2::SmaccSignal<void(const MessageType &)> onFirstMessageReceived_;
55 smacc2::SmaccSignal<void(const MessageType &)> onMessageReceived_;
56
57 // signal subscription method. This signal will be triggered when the first message is received
58 template <typename T>
60 void (T::*callback)(const MessageType &), T * object)
61 {
62 return this->getStateMachine()->createSignalConnection(onMessageReceived_, callback, object);
63 }
64
65 // signal subscription method. This signal will be triggered when the first message is received
66 template <typename T>
68 void (T::*callback)(const MessageType &), T * object)
69 {
71 onFirstMessageReceived_, callback, object);
72 }
73
74 template <typename TOrthogonal, typename TSourceObject>
76 {
77 this->postMessageEvent = [=](auto msg)
78 {
80 event->msgData = msg;
81 this->postEvent(event);
82 };
83
84 this->postInitialMessageEvent = [=](auto msg)
85 {
87 event->msgData = msg;
88 this->postEvent(event);
89 };
90 }
91
92 void onInitialize() override
93 {
94 if (!initialized_)
95 {
96 firstMessage_ = true;
97
98 if (!queueSize) queueSize = 1;
99
100 RCLCPP_INFO_STREAM(
101 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << topicName_);
102
103 rclcpp::SensorDataQoS qos;
104 if (queueSize) qos.keep_last(*queueSize);
105
106 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg)
107 { this->messageCallback(*msg); };
108
109 sub_ = this->getNode()->template create_subscription<MessageType>(topicName_, qos, fn);
110 this->initialized_ = true;
111 }
112 }
113
114private:
115 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
118 std::string topicName_;
119
120 void messageCallback(const MessageType & msg)
121 {
122 if (firstMessage_)
123 {
126 firstMessage_ = false;
127 }
128
129 postMessageEvent(msg);
131 }
132};
133} // namespace client_core_components
134} // namespace smacc2
ISmaccStateMachine * getStateMachine()
virtual std::string getName() const
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
smacc2::SmaccSignal< void(const MessageType &)> onFirstMessageReceived_
smacc2::SmaccSignal< void(const MessageType &)> onMessageReceived_
std::function< void(const MessageType &)> postInitialMessageEvent
smacc2::SmaccSignalConnection onFirstMessageReceived(void(T::*callback)(const MessageType &), T *object)
rclcpp::Subscription< MessageType >::SharedPtr sub_
std::function< void(const MessageType &)> postMessageEvent
smacc2::SmaccSignalConnection onMessageReceived(void(T::*callback)(const MessageType &), T *object)
boost::signals2::connection SmaccSignalConnection