SMACC2
cp_topic_subscriber.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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 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) { topicName_ = topicname; }
44
45 virtual ~CpTopicSubscriber() {}
46
47 smacc2::SmaccSignal<void(const MessageType &)> onFirstMessageReceived_;
48 smacc2::SmaccSignal<void(const MessageType &)> onMessageReceived_;
49
50 std::function<void(const MessageType &)> postMessageEvent;
51 std::function<void(const MessageType &)> postInitialMessageEvent;
52
53 template <typename T>
54 boost::signals2::connection onMessageReceived(
55 void (T::*callback)(const MessageType &), T * object)
56 {
57 return this->getStateMachine()->createSignalConnection(onMessageReceived_, callback, object);
58 }
59
60 template <typename T>
61 boost::signals2::connection onFirstMessageReceived(
62 void (T::*callback)(const MessageType &), T * object)
63 {
65 onFirstMessageReceived_, callback, object);
66 }
67
68 template <typename TOrthogonal, typename TSourceObject>
70 {
71 this->postMessageEvent = [=](auto msg) {
73 event->msgData = msg;
74 this->postEvent(event);
75 };
76
77 this->postInitialMessageEvent = [=](auto msg) {
79 event->msgData = msg;
80 this->postEvent(event);
81 };
82 }
83
84 void onInitialize() override
85 {
86 if (!initialized_)
87 {
88 firstMessage_ = true;
89
90 if (!queueSize) queueSize = 1;
91
92 RCLCPP_INFO_STREAM(
93 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << topicName_);
94
95 rclcpp::SensorDataQoS qos;
96 if (queueSize) qos.keep_last(*queueSize);
97
98 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg) {
99 this->messageCallback(*msg);
100 };
101
102 sub_ = this->getNode()->template create_subscription<MessageType>(topicName_, qos, fn);
103 this->initialized_ = true;
104 }
105 }
106
107private:
108 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
111 std::string topicName_;
112
113 void messageCallback(const MessageType & msg)
114 {
115 if (firstMessage_)
116 {
119 firstMessage_ = false;
120 }
121
122 postMessageEvent(msg);
124 }
125};
126} // namespace components
127} // namespace smacc2
ISmaccStateMachine * getStateMachine()
rclcpp::Logger getLogger()
virtual std::string getName() const
rclcpp::Node::SharedPtr getNode()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
rclcpp::Subscription< MessageType >::SharedPtr sub_
std::function< void(const MessageType &)> postMessageEvent
boost::signals2::connection onMessageReceived(void(T::*callback)(const MessageType &), T *object)
std::function< void(const MessageType &)> postInitialMessageEvent
void messageCallback(const MessageType &msg)
boost::signals2::connection onFirstMessageReceived(void(T::*callback)(const MessageType &), T *object)
smacc2::SmaccSignal< void(const MessageType &)> onFirstMessageReceived_
smacc2::SmaccSignal< void(const MessageType &)> onMessageReceived_
void callback(const image_tools::ROSCvMatContainer &img)