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) : initialized_(false), topicName_(std::move(topicname))
44 {
45 }
46
47 // For a topic that must not lose messages, such as discrete commands. Without a QoS the
48 // subscription is best effort with a depth of queueSize (1 unless set), which suits sensor streams.
49 CpTopicSubscriber(std::string topicname, const rclcpp::QoS & qos)
50 : qos_(qos), initialized_(false), topicName_(std::move(topicname))
51 {
52 }
53
54 virtual ~CpTopicSubscriber() {}
55
56 std::function<void(const MessageType &)> postMessageEvent;
57 std::function<void(const MessageType &)> postInitialMessageEvent;
58
59 smacc2::SmaccSignal<void(const MessageType &)> onFirstMessageReceived_;
60 smacc2::SmaccSignal<void(const MessageType &)> onMessageReceived_;
61
62 // signal subscription method. This signal will be triggered when the first message is received
63 template <typename T>
65 void (T::*callback)(const MessageType &), T * object)
66 {
67 return this->getStateMachine()->createSignalConnection(onMessageReceived_, callback, object);
68 }
69
70 // signal subscription method. This signal will be triggered when the first message is received
71 template <typename T>
73 void (T::*callback)(const MessageType &), T * object)
74 {
76 onFirstMessageReceived_, callback, object);
77 }
78
79 template <typename TOrthogonal, typename TSourceObject>
81 {
82 this->postMessageEvent = [=](auto msg)
83 {
85 event->msgData = msg;
86 this->postEvent(event);
87 };
88
89 this->postInitialMessageEvent = [=](auto msg)
90 {
91 auto event =
93 event->msgData = msg;
94 this->postEvent(event);
95 };
96 }
97
98 void onInitialize() override
99 {
100 if (!initialized_)
101 {
102 firstMessage_ = true;
103
104 RCLCPP_INFO_STREAM(
105 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << topicName_);
106
107 rclcpp::QoS qos = rclcpp::SensorDataQoS();
108 if (qos_)
109 {
110 qos = *qos_;
111 if (queueSize) qos.keep_last(*queueSize);
112 }
113 else
114 {
115 if (!queueSize) queueSize = 1;
116 qos.keep_last(*queueSize);
117 }
118
119 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg)
120 { this->messageCallback(*msg); };
121
122 sub_ = this->getNode()->template create_subscription<MessageType>(topicName_, qos, fn);
123 this->initialized_ = true;
124 }
125 }
126
127private:
128 std::optional<rclcpp::QoS> qos_;
129 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
130 bool firstMessage_ = true;
132 std::string topicName_;
133
134 void messageCallback(const MessageType & msg)
135 {
136 if (firstMessage_)
137 {
140 firstMessage_ = false;
141 }
142
143 postMessageEvent(msg);
145 }
146};
147} // namespace client_core_components
148} // 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_
CpTopicSubscriber(std::string topicname, const rclcpp::QoS &qos)
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
boost::intrusive_ptr< EventType > makeEvent(Args &&... args)