SMACC2
Loading...
Searching...
No Matches
cb_wait_topic_message.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 <functional>
24#include <memory>
25#include <rclcpp/rclcpp.hpp>
28
29namespace smacc2
30{
31namespace client_behaviors
32{
33using namespace std::chrono_literals;
34
35// Asynchronous behavior that waits to a topic message to send EvCbSuccess event
36// a guard function can be set to use conditions on the contents
37template <typename TMessage>
39{
40public:
42 const char * topicname, std::function<bool(const TMessage &)> guardFunction = nullptr)
43 : guardFn_(std::move(guardFunction)), topicname_(topicname)
44 {
45 }
46
48
49 // On the state machine thread, before onEntry: the slot goes through the state-lifetime signal
50 // map, so dispose finalizes it and waits for a callback in flight before this behavior dies.
51 void runtimeConfigure() override
52 {
53 signal_ = std::make_shared<MessageSignal>();
56 }
57
58 void onEntry() override
59 {
60 rclcpp::SensorDataQoS qos;
61
62 RCLCPP_INFO_STREAM(
63 getLogger(), "[CbWaitTopicMessage] waiting message from topic: "
64 << topicname_ << "[" << demangledTypeName<TMessage>() << "]");
65
66 // The subscription callback co-owns the signal and touches nothing of this behavior; after
67 // dispose the signal has no slot and a late message does nothing.
68 auto signal = signal_;
69 std::function<void(typename TMessage::SharedPtr)> fn = [signal](auto msg) { (*signal)(*msg); };
70
71 auto nh = getNode();
72 sub_ = nh->template create_subscription<TMessage>(topicname_, qos, fn);
73 }
74
75 void onMessageReceived(const TMessage & msg)
76 {
77 if (!success && (guardFn_ == nullptr || guardFn_(msg)))
78 {
79 RCLCPP_INFO(getLogger(), "[CbWaitTopicMessage] message received.");
80 success = true;
81 this->postSuccessEvent();
82 }
83 }
84
85protected:
86 using MessageSignal = smacc2::SmaccSignal<void(const TMessage &)>;
87
88 bool success = false;
89 std::shared_ptr<MessageSignal> signal_;
90 typename rclcpp::Subscription<TMessage>::SharedPtr sub_;
91 std::function<bool(const TMessage &)> guardFn_;
92 std::string topicname_;
93};
94} // namespace client_behaviors
95} // namespace smacc2
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
CbWaitTopicMessage(const char *topicname, std::function< bool(const TMessage &)> guardFunction=nullptr)
std::function< bool(const TMessage &)> guardFn_
rclcpp::Subscription< TMessage >::SharedPtr sub_
std::string demangledTypeName()