SMACC2
smacc_subscriber_client.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
23#include <optional>
27
28namespace smacc2
29{
30namespace client_bases
31{
32using namespace smacc2::default_events;
33
34// This client class warps a ros subscriber and publishes two kind of
35// smacc events: EvTopicMessage (always a ros topic message is received)
36// and EvTopicInitialMessage (only once)
37template <typename MessageType>
39{
40public:
41 std::optional<std::string> topicName;
42 std::optional<int> queueSize;
43
44 typedef MessageType TMessageType;
45
47
48 SmaccSubscriberClient(std::string topicname) { topicName = topicname; }
49
51
52 smacc2::SmaccSignal<void(const MessageType &)> onFirstMessageReceived_;
53 smacc2::SmaccSignal<void(const MessageType &)> onMessageReceived_;
54
55 std::function<void(const MessageType &)> postMessageEvent;
56 std::function<void(const MessageType &)> postInitialMessageEvent;
57
58 template <typename T>
59 boost::signals2::connection onMessageReceived(
60 void (T::*callback)(const MessageType &), T * object)
61 {
62 return this->getStateMachine()->createSignalConnection(onMessageReceived_, callback, object);
63 }
64
65 template <typename T>
66 boost::signals2::connection onFirstMessageReceived(
67 void (T::*callback)(const MessageType &), T * object)
68 {
70 onFirstMessageReceived_, callback, object);
71 }
72
73 template <typename TOrthogonal, typename TSourceObject>
75 {
76 // ros topic message received smacc event callback
77 this->postMessageEvent = [this](auto msg) {
79 event->msgData = msg;
80 this->postEvent(event);
81 };
82
83 // initial ros topic message received smacc event callback
84 this->postInitialMessageEvent = [this](auto msg) {
86 event->msgData = msg;
87 this->postEvent(event);
88 };
89 }
90
91protected:
92 void onInitialize() override
93 {
94 if (!initialized_)
95 {
96 firstMessage_ = true;
97
98 if (!queueSize) queueSize = 1;
99
100 if (!topicName)
101 {
102 RCLCPP_ERROR(getLogger(), "topic client with no topic name set. Skipping subscribing");
103 }
104 else
105 {
106 RCLCPP_INFO_STREAM(
107 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << *topicName);
108
109 rclcpp::SensorDataQoS qos;
110 if (queueSize) qos.keep_last(*queueSize);
111
112 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg) {
113 this->messageCallback(*msg);
114 };
115 sub_ = getNode()->create_subscription<MessageType>(*topicName, qos, fn);
116 this->initialized_ = true;
117 }
118 }
119 }
120
121private:
122 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
125
126 void messageCallback(const MessageType & msg)
127 {
128 if (firstMessage_)
129 {
132 firstMessage_ = false;
133 }
134
135 postMessageEvent(msg);
137 }
138};
139} // namespace client_bases
140} // namespace smacc2
rclcpp::Node::SharedPtr getNode()
Definition: client.cpp:60
ISmaccStateMachine * getStateMachine()
virtual std::string getName() const
Definition: client.cpp:42
rclcpp::Logger getLogger()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
rclcpp::Subscription< MessageType >::SharedPtr sub_
boost::signals2::connection onMessageReceived(void(T::*callback)(const MessageType &), T *object)
std::function< void(const MessageType &)> postMessageEvent
boost::signals2::connection onFirstMessageReceived(void(T::*callback)(const MessageType &), T *object)
std::function< void(const MessageType &)> postInitialMessageEvent
smacc2::SmaccSignal< void(const MessageType &)> onMessageReceived_
smacc2::SmaccSignal< void(const MessageType &)> onFirstMessageReceived_
void callback(const image_tools::ROSCvMatContainer &img)