SMACC2
Loading...
Searching...
No Matches
cb_sequence.cpp
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#include <functional>
22#include <rclcpp/rclcpp.hpp>
25
26namespace smacc2
27{
28namespace client_behaviors
29{
31
33{
34 // Starting a step creates a behavior and signal connections, which take the state machine's mutex.
35 // An exiting state holds that mutex while it joins this thread, so it is never waited on here.
36 std::unique_lock<std::recursive_mutex> lock(getStateMachine()->getMutex(), std::defer_lock);
37 while (!lock.try_lock())
38 {
39 if (this->isShutdownRequested()) return false;
40 rclcpp::sleep_for(std::chrono::milliseconds(1));
41 }
42 // Nor is a step started once ROS is shutting down and the behavior threads are being joined.
43 if (this->isShutdownRequested() || !rclcpp::ok()) return false;
44
45 RCLCPP_INFO(
46 getLogger(), "[SequenceNode %p] next onEntry: %ld", static_cast<const void *>(this),
47 sequenceNodes_.size());
48
49 auto first = sequenceNodes_.front();
50 auto onDelayedConfigureFn = first;
51
52 RCLCPP_INFO(
53 getLogger(), "[SequenceNode %p] Behavior on delayed sequence configure",
54 static_cast<const void *>(this));
55 bh_ = onDelayedConfigureFn();
56 std::string currentNodeName = bh_->getName();
57
58 RCLCPP_INFO(
59 getLogger(), "[SequenceNode %p] Subscribing OnSuccess", static_cast<const void *>(this));
60 this->conn_ = bh_->onSuccess(&CbSequence::onSubNodeSuccess, this);
61 this->conn2_ = bh_->onFailure(&CbSequence::onSubNodeAbort, this);
62
63 RCLCPP_INFO(
64 getLogger(), "[SequenceNode %p] subnode %s on entry", static_cast<const void *>(this),
65 currentNodeName.c_str());
66 bh_->executeOnEntry();
67 lock.unlock();
68 RCLCPP_INFO(
69 getLogger(), "[SequenceNode %p] subnode %s on entry finished", static_cast<const void *>(this),
70 currentNodeName.c_str());
71 bh_->waitOnEntryThread(false); // we do not request to finish to keep on subscriptions
72 RCLCPP_INFO(
73 getLogger(), "[SequenceNode %p] subnode %s thread finished", static_cast<const void *>(this),
74 currentNodeName.c_str());
75 return true;
76}
77
79{
80 bool started = this->recursiveConsumeNext();
81 while (started && !sequenceNodes_.empty())
82 {
83 bool is_shutdown_requested = this->isShutdownRequested();
84 if (is_shutdown_requested)
85 {
86 break;
87 }
88
89 rclcpp::sleep_for(std::chrono::milliseconds(200));
90 const auto & head = *bh_;
91 RCLCPP_INFO_THROTTLE(
92 getLogger(), *(getNode()->get_clock()), 1000,
93 "[CbSequence %p] Waiting for subnodes to finish %ld. Current head Behavior: %s ",
94 static_cast<const void *>(this), sequenceNodes_.size(), demangleType(&typeid(head)).c_str());
95
96 if (consume_)
97 {
98 this->conn_.disconnect();
99 this->conn2_.disconnect();
100
101 sequenceNodes_.pop_front();
102 consume_--;
103
104 if (sequenceNodes_.size() > 0)
105 {
106 started = this->recursiveConsumeNext();
107 }
108 }
109 }
110
111 if (sequenceNodes_.empty())
112 {
113 RCLCPP_INFO(
114 getLogger(), "[CbSequence %p] All subnodes finished", static_cast<const void *>(this));
115 this->postSuccessEvent();
116 }
117 else
118 {
119 RCLCPP_INFO(getLogger(), "[CbSequence %p] Aborting", static_cast<const void *>(this));
120 this->postFailureEvent();
121 }
122}
123
125{
126 RCLCPP_INFO(
127 getLogger(), "[CbSequence %p] Success NextCbSequence %ld", static_cast<const void *>(this),
128 sequenceNodes_.size());
129 consume_++;
130}
131
133{
134 RCLCPP_INFO(
135 getLogger(), "[CbSequence %p] Abort NextCbSequence %ld", static_cast<const void *>(this),
136 sequenceNodes_.size());
137 this->conn_.disconnect();
138 this->conn2_.disconnect();
139
140 this->postFailureEvent();
141 RCLCPP_INFO(
142 getLogger(), "[CbSequence %p] Abort NextCbSequence requesting for force finish",
143 static_cast<const void *>(this));
144 this->requestForceFinish();
145 consume_++;
146}
147
148} // namespace client_behaviors
149
150} // namespace smacc2
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...
smacc2::SmaccSignalConnection conn_
smacc2::SmaccSignalConnection conn2_
std::list< std::function< std::shared_ptr< smacc2::SmaccAsyncClientBehavior >()> > sequenceNodes_
std::shared_ptr< smacc2::SmaccAsyncClientBehavior > bh_
std::string demangleType(const std::type_info *tinfo)