SMACC2
Loading...
Searching...
No Matches
smacc_subscriber_client.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft 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(std::move(topicname))
49 {
50 initialized_ = false;
51 }
52
54
55 smacc2::SmaccSignal<void(const MessageType &)> onFirstMessageReceived_;
56 smacc2::SmaccSignal<void(const MessageType &)> onMessageReceived_;
57
58 std::function<void(const MessageType &)> postMessageEvent;
59 std::function<void(const MessageType &)> postInitialMessageEvent;
60
61 template <typename T>
63 void (T::*callback)(const MessageType &), T * object)
64 {
65 return this->getStateMachine()->createSignalConnection(onMessageReceived_, callback, object);
66 }
67
68 template <typename T>
70 void (T::*callback)(const MessageType &), T * object)
71 {
73 onFirstMessageReceived_, callback, object);
74 }
75
76 template <typename TOrthogonal, typename TSourceObject>
78 {
79 // ros topic message received smacc event callback
80 this->postMessageEvent = [this](auto msg)
81 {
83 event->msgData = msg;
84 this->postEvent(event);
85 };
86
87 // initial ros topic message received smacc event callback
88 this->postInitialMessageEvent = [this](auto msg)
89 {
91 event->msgData = msg;
92 this->postEvent(event);
93 };
94 }
95
96protected:
97 void onInitialize() override
98 {
99 if (!initialized_)
100 {
101 firstMessage_ = true;
102
103 if (!queueSize) queueSize = 1;
104
105 if (!topicName)
106 {
107 RCLCPP_ERROR(getLogger(), "topic client with no topic name set. Skipping subscribing");
108 }
109 else
110 {
111 RCLCPP_INFO_STREAM(
112 getLogger(), "[" << this->getName() << "] Subscribing to topic: " << *topicName);
113
114 rclcpp::SensorDataQoS qos;
115 if (queueSize) qos.keep_last(*queueSize);
116
117 std::function<void(typename MessageType::SharedPtr)> fn = [this](auto msg)
118 { this->messageCallback(*msg); };
119 sub_ = getNode()->template create_subscription<MessageType>(*topicName, qos, fn);
120 this->initialized_ = true;
121 }
122 }
123 }
124
125private:
126 typename rclcpp::Subscription<MessageType>::SharedPtr sub_;
127 bool firstMessage_ = true;
129
130 void messageCallback(const MessageType & msg)
131 {
132 if (firstMessage_)
133 {
136 firstMessage_ = false;
137 }
138
139 postMessageEvent(msg);
141 }
142};
143} // namespace client_bases
144} // namespace smacc2
rclcpp::Node::SharedPtr getNode()
Definition client.cpp:60
ISmaccStateMachine * getStateMachine()
virtual std::string getName() const
Definition client.cpp:42
rclcpp::Logger getLogger()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
rclcpp::Subscription< MessageType >::SharedPtr sub_
std::function< void(const MessageType &)> postMessageEvent
std::function< void(const MessageType &)> postInitialMessageEvent
smacc2::SmaccSignalConnection onFirstMessageReceived(void(T::*callback)(const MessageType &), T *object)
smacc2::SmaccSignal< void(const MessageType &)> onMessageReceived_
smacc2::SmaccSignal< void(const MessageType &)> onFirstMessageReceived_
smacc2::SmaccSignalConnection onMessageReceived(void(T::*callback)(const MessageType &), T *object)
boost::signals2::connection SmaccSignalConnection
boost::intrusive_ptr< EventType > makeEvent(Args &&... args)