SMACC2
Loading...
Searching...
No Matches
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)
49 {
50 initialized_ = false;
51 topicName = topicname;
52 }
53
55
56 smacc2::SmaccSignal<void(const MessageType &)> onFirstMessageReceived_;
57 smacc2::SmaccSignal<void(const MessageType &)> onMessageReceived_;
58
59 std::function<void(const MessageType &)> postMessageEvent;
60 std::function<void(const MessageType &)> postInitialMessageEvent;
61
62 template <typename T>
63 boost::signals2::connection onMessageReceived(
64 void (T::*callback)(const MessageType &), T * object)
65 {
66 return this->getStateMachine()->createSignalConnection(onMessageReceived_, callback, object);
67 }
68
69 template <typename T>
70 boost::signals2::connection onFirstMessageReceived(
71 void (T::*callback)(const MessageType &), T * object)
72 {
74 onFirstMessageReceived_, callback, object);
75 }
76
77 template <typename TOrthogonal, typename TSourceObject>
79 {
80 // ros topic message received smacc event callback
81 this->postMessageEvent = [this](auto msg)
82 {
84 event->msgData = msg;
85 this->postEvent(event);
86 };
87
88 // initial ros topic message received smacc event callback
89 this->postInitialMessageEvent = [this](auto msg)
90 {
92 event->msgData = msg;
93 this->postEvent(event);
94 };
95 }
96
97protected:
98 void onInitialize() override
99 {
100 if (!initialized_)
101 {
102 firstMessage_ = true;
103
104 if (!queueSize) queueSize = 1;
105
106 if (!topicName)
107 {
108 RCLCPP_ERROR(getLogger(), "topic client with no topic name set. Skipping subscribing");
109 }
110 else
111 {
112 RCLCPP_INFO_STREAM(
113 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << *topicName);
114
115 rclcpp::SensorDataQoS qos;
116 if (queueSize) qos.keep_last(*queueSize);
117
118 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg)
119 { this->messageCallback(*msg); };
120 sub_ = getNode()->create_subscription<MessageType>(*topicName, qos, fn);
121 this->initialized_ = true;
122 }
123 }
124 }
125
126private:
127 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
130
131 void messageCallback(const MessageType & msg)
132 {
133 if (firstMessage_)
134 {
137 firstMessage_ = false;
138 }
139
140 postMessageEvent(msg);
142 }
143};
144} // namespace client_bases
145} // 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_