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) { 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)
78 {
80 event->msgData = msg;
81 this->postEvent(event);
82 };
83
84 // initial ros topic message received smacc event callback
85 this->postInitialMessageEvent = [this](auto msg)
86 {
88 event->msgData = msg;
89 this->postEvent(event);
90 };
91 }
92
93protected:
94 void onInitialize() override
95 {
96 if (!initialized_)
97 {
98 firstMessage_ = true;
99
100 if (!queueSize) queueSize = 1;
101
102 if (!topicName)
103 {
104 RCLCPP_ERROR(getLogger(), "topic client with no topic name set. Skipping subscribing");
105 }
106 else
107 {
108 RCLCPP_INFO_STREAM(
109 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << *topicName);
110
111 rclcpp::SensorDataQoS qos;
112 if (queueSize) qos.keep_last(*queueSize);
113
114 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg)
115 { this->messageCallback(*msg); };
116 sub_ = getNode()->create_subscription<MessageType>(*topicName, qos, fn);
117 this->initialized_ = true;
118 }
119 }
120 }
121
122private:
123 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
126
127 void messageCallback(const MessageType & msg)
128 {
129 if (firstMessage_)
130 {
133 firstMessage_ = false;
134 }
135
136 postMessageEvent(msg);
138 }
139};
140} // namespace client_bases
141} // 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_