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 std::function<void(const MessageType &)> postMessageEvent;
48 std::function<void(const MessageType &)> postInitialMessageEvent;
49
50 smacc2::SmaccSignal<void(const MessageType &)> onFirstMessageReceived_;
51 smacc2::SmaccSignal<void(const MessageType &)> onMessageReceived_;
52
53 // signal subscription method. This signal will be triggered when the first message is received
54 template <typename T>
55 boost::signals2::connection onMessageReceived(
56 void (T::*callback)(const MessageType &), T * object)
57 {
58 return this->getStateMachine()->createSignalConnection(onMessageReceived_, callback, object);
59 }
60
61 // signal subscription method. This signal will be triggered when the first message is received
62 template <typename T>
63 boost::signals2::connection onFirstMessageReceived(
64 void (T::*callback)(const MessageType &), T * object)
65 {
67 onFirstMessageReceived_, callback, object);
68 }
69
70 template <typename TOrthogonal, typename TSourceObject>
72 {
73 this->postMessageEvent = [=](auto msg)
74 {
76 event->msgData = msg;
77 this->postEvent(event);
78 };
79
80 this->postInitialMessageEvent = [=](auto msg)
81 {
83 event->msgData = msg;
84 this->postEvent(event);
85 };
86 }
87
88 void onInitialize() override
89 {
90 if (!initialized_)
91 {
92 firstMessage_ = true;
93
94 if (!queueSize) queueSize = 1;
95
97 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << topicName_);
98
99 rclcpp::SensorDataQoS qos;
100 if (queueSize) qos.keep_last(*queueSize);
101
102 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg)
103 { this->messageCallback(*msg); };
104
106 this->initialized_ = true;
107 }
108 }
109
110private:
111 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
114 std::string topicName_;
115
117 {
118 if (firstMessage_)
119 {
122 firstMessage_ = false;
123 }
124
127 }
128};
129} // namespace components
130} // 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_