SMACC2
Loading...
Searching...
No Matches
cb_sequence.cpp
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#include <functional>
22#include <rclcpp/rclcpp.hpp>
25
26namespace smacc2
27{
28namespace client_behaviors
29{
31
33{
34 RCLCPP_INFO(
35 getLogger(), "[SequenceNode %ld] next onEntry: %ld", (long)this, sequenceNodes_.size());
36
37 auto first = sequenceNodes_.front();
38 auto onDelayedConfigureFn = first;
39
40 RCLCPP_INFO(getLogger(), "[SequenceNode %ld] Behavior on delayed sequence configure", (long)this);
41 bh_ = onDelayedConfigureFn();
42 std::string currentNodeName = bh_->getName();
43
44 RCLCPP_INFO(getLogger(), "[SequenceNode %ld] Subscribing OnSuccess", (long)this);
45 this->conn_ = bh_->onSuccess(&CbSequence::onSubNodeSuccess, this);
46 this->conn2_ = bh_->onFailure(&CbSequence::onSubNodeAbort, this);
47
48 RCLCPP_INFO(
49 getLogger(), "[SequenceNode %ld] subnode %s on entry", (long)this, currentNodeName.c_str());
50 bh_->executeOnEntry();
51 RCLCPP_INFO(
52 getLogger(), "[SequenceNode %ld] subnode %s on entry finished", (long)this,
53 currentNodeName.c_str());
54 bh_->waitOnEntryThread(false); // we do not request to finish to keep on subscriptions
55 RCLCPP_INFO(
56 getLogger(), "[SequenceNode %ld] subnode %s thread finished", (long)this,
57 currentNodeName.c_str());
58}
59
61{
63 while (!sequenceNodes_.empty())
64 {
65 bool is_shutdown_requested = this->isShutdownRequested();
66 if (is_shutdown_requested)
67 {
68 break;
69 }
70
71 rclcpp::sleep_for(std::chrono::milliseconds(200));
72 RCLCPP_INFO_THROTTLE(
73 getLogger(), *(getNode()->get_clock()), 1000,
74 "[CbSequence %ld] Waiting for subnodes to finish %ld. Current head Behavior: %s ", (long)this,
75 sequenceNodes_.size(), demangleType(&typeid(*bh_)).c_str());
76
77 if (consume_)
78 {
79 // bh_->waitOnEntryThread();
80 this->conn_.disconnect();
81 this->conn2_.disconnect();
82
83 sequenceNodes_.pop_front();
84 consume_--;
85
86 if (sequenceNodes_.size() > 0)
87 {
89 }
90 }
91 }
92
93 if (sequenceNodes_.empty())
94 {
95 RCLCPP_INFO(getLogger(), "[CbSequence %ld] All subnodes finished", (long)this);
96 this->postSuccessEvent();
97 }
98 else
99 {
100 RCLCPP_INFO(getLogger(), "[CbSequence %ld] Aborting", (long)this);
101 this->postFailureEvent();
102 }
103}
104
106{
107 RCLCPP_INFO(
108 getLogger(), "[CbSequence %ld] Success NextCbSequence %ld", (long)this, sequenceNodes_.size());
109 consume_++;
110}
111
113{
114 RCLCPP_INFO(
115 getLogger(), "[CbSequence %ld] Abort NextCbSequence %ld", (long)this, sequenceNodes_.size());
116 // bh_->waitOnEntryThread();
117 this->conn_.disconnect();
118 this->conn2_.disconnect();
119
120 this->postFailureEvent();
121 RCLCPP_INFO(
122 getLogger(), "[CbSequence %ld] Abort NextCbSequence requesting for force finish", (long)this);
123 this->requestForceFinish();
124 consume_++;
125}
126
127} // namespace client_behaviors
128
129} // namespace smacc2
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
boost::signals2::connection conn_
Definition: cb_sequence.hpp:85
boost::signals2::connection conn2_
Definition: cb_sequence.hpp:86
std::list< std::function< std::shared_ptr< smacc2::SmaccAsyncClientBehavior >()> > sequenceNodes_
Definition: cb_sequence.hpp:84
std::shared_ptr< smacc2::SmaccAsyncClientBehavior > bh_
Definition: cb_sequence.hpp:88
std::string demangleType(const std::type_info *tinfo)