SMACC2
Loading...
Searching...
No Matches
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)
72 {
74 event->msgData = msg;
75 this->postEvent(event);
76 };
77
78 this->postInitialMessageEvent = [=](auto msg)
79 {
81 event->msgData = msg;
82 this->postEvent(event);
83 };
84 }
85
86 void onInitialize() override
87 {
88 if (!initialized_)
89 {
90 firstMessage_ = true;
91
92 if (!queueSize) queueSize = 1;
93
94 RCLCPP_INFO_STREAM(
95 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << topicName_);
96
97 rclcpp::SensorDataQoS qos;
98 if (queueSize) qos.keep_last(*queueSize);
99
100 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg)
101 { this->messageCallback(*msg); };
102
103 sub_ = this->getNode()->template create_subscription<MessageType>(topicName_, qos, fn);
104 this->initialized_ = true;
105 }
106 }
107
108private:
109 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
112 std::string topicName_;
113
114 void messageCallback(const MessageType & msg)
115 {
116 if (firstMessage_)
117 {
120 firstMessage_ = false;
121 }
122
123 postMessageEvent(msg);
125 }
126};
127} // namespace components
128} // namespace smacc2
ISmaccStateMachine * getStateMachine()
virtual std::string getName() const
rclcpp::Logger getLogger() 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_