SMACC2
Loading...
Searching...
No Matches
smacc2::client_behaviors::CbSequence Class Reference

#include <cb_sequence.hpp>

Inheritance diagram for smacc2::client_behaviors::CbSequence:
Inheritance graph
Collaboration diagram for smacc2::client_behaviors::CbSequence:
Collaboration graph

Public Member Functions

 CbSequence ()
 
void onEntry () override
 
void onExit () override
 
template<typename TOrthogonal , typename TBehavior , typename... Args>
CbSequence * then (Args &&... args)
 
- Public Member Functions inherited from smacc2::SmaccAsyncClientBehavior
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
 SmaccAsyncClientBehavior ()=default
 
 ~SmaccAsyncClientBehavior () override
 
 SmaccAsyncClientBehavior (const SmaccAsyncClientBehavior &)=delete
 
SmaccAsyncClientBehavior & operator= (const SmaccAsyncClientBehavior &)=delete
 
 SmaccAsyncClientBehavior (SmaccAsyncClientBehavior &&)=delete
 
SmaccAsyncClientBehavior & operator= (SmaccAsyncClientBehavior &&)=delete
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onSuccess (TCallback callback, T *object)
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onFinished (TCallback callback, T *object)
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onFailure (TCallback callback, T *object)
 
void requestForceFinish ()
 
void executeOnEntry () override
 
void executeOnExit () override
 
void waitOnEntryThread (bool requestFinish)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onSuccess (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onFinished (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onFailure (TCallbackMethod callback, T *object)
 
- Public Member Functions inherited from smacc2::ISmaccClientBehavior
 ISmaccClientBehavior ()
 
virtual ~ISmaccClientBehavior ()
 
 ISmaccClientBehavior (const ISmaccClientBehavior &)=delete
 
ISmaccClientBehavior & operator= (const ISmaccClientBehavior &)=delete
 
 ISmaccClientBehavior (ISmaccClientBehavior &&)=delete
 
ISmaccClientBehavior & operator= (ISmaccClientBehavior &&)=delete
 
ISmaccStateMachine * getStateMachine ()
 
std::string getName () const
 
template<typename SmaccClientType >
void requiresClient (SmaccClientType *&storage)
 
template<typename SmaccComponentType >
void requiresComponent (SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename SmaccComponentType >
void requiresComponent (const std::string &name, SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 

Private Member Functions

void onSubNodeSuccess ()
 
void onSubNodeAbort ()
 
bool recursiveConsumeNext ()
 

Private Attributes

std::list< std::function< std::shared_ptr< smacc2::SmaccAsyncClientBehavior >()> > sequenceNodes_
 
smacc2::SmaccSignalConnection conn_
 
smacc2::SmaccSignalConnection conn2_
 
std::shared_ptr< smacc2::SmaccAsyncClientBehavior > bh_
 
std::atomic< int > consume_ {0}
 

Additional Inherited Members

- Protected Member Functions inherited from smacc2::SmaccAsyncClientBehavior
void postSuccessEvent ()
 
void postFailureEvent ()
 
void dispose () override
 
bool isShutdownRequested ()
 onEntry is executed in a new thread. However the current state cannot be left until the onEntry thread finishes. This flag can be checked from the onEntry thread to force finishing the thread.
 
- Protected Member Functions inherited from smacc2::ISmaccClientBehavior
virtual void runtimeConfigure ()
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 
ISmaccState * getCurrentState ()
 
void postInitializationFailureEvent (const std::string &what)
 
virtual rclcpp::Node::SharedPtr getNode () const
 
virtual rclcpp::Logger getLogger () const
 
std::string getTraceStateName () const
 
std::string getTraceOrthogonalName () const
 

Detailed Description

Definition at line 31 of file cb_sequence.hpp.

Constructor & Destructor Documentation

◆ CbSequence()

smacc2::client_behaviors::CbSequence::CbSequence ( )

Definition at line 30 of file cb_sequence.cpp.

30{}

Member Function Documentation

◆ onEntry()

void smacc2::client_behaviors::CbSequence::onEntry ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 78 of file cb_sequence.cpp.

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}
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)

References bh_, conn2_, conn_, consume_, smacc2::introspection::demangleType(), smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getNode(), smacc2::SmaccAsyncClientBehavior::isShutdownRequested(), smacc2::SmaccAsyncClientBehavior::postFailureEvent(), smacc2::SmaccAsyncClientBehavior::postSuccessEvent(), recursiveConsumeNext(), and sequenceNodes_.

Here is the call graph for this function:

◆ onExit()

void smacc2::client_behaviors::CbSequence::onExit ( )
inlineoverridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 38 of file cb_sequence.hpp.

38{ sequenceNodes_.clear(); }

References sequenceNodes_.

◆ onSubNodeAbort()

void smacc2::client_behaviors::CbSequence::onSubNodeAbort ( )
private

Definition at line 132 of file cb_sequence.cpp.

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}

References conn2_, conn_, consume_, smacc2::ISmaccClientBehavior::getLogger(), smacc2::SmaccAsyncClientBehavior::postFailureEvent(), smacc2::SmaccAsyncClientBehavior::requestForceFinish(), and sequenceNodes_.

Referenced by recursiveConsumeNext().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onSubNodeSuccess()

void smacc2::client_behaviors::CbSequence::onSubNodeSuccess ( )
private

Definition at line 124 of file cb_sequence.cpp.

125{
126 RCLCPP_INFO(
127 getLogger(), "[CbSequence %p] Success NextCbSequence %ld", static_cast<const void *>(this),
128 sequenceNodes_.size());
129 consume_++;
130}

References consume_, smacc2::ISmaccClientBehavior::getLogger(), and sequenceNodes_.

Referenced by recursiveConsumeNext().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ recursiveConsumeNext()

bool smacc2::client_behaviors::CbSequence::recursiveConsumeNext ( )
private

Definition at line 32 of file cb_sequence.cpp.

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}

References bh_, conn2_, conn_, smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getStateMachine(), smacc2::SmaccAsyncClientBehavior::isShutdownRequested(), onSubNodeAbort(), onSubNodeSuccess(), and sequenceNodes_.

Referenced by onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ then()

template<typename TOrthogonal , typename TBehavior , typename... Args>
CbSequence * smacc2::client_behaviors::CbSequence::then ( Args &&... args)
inline

Definition at line 41 of file cb_sequence.hpp.

42 {
43 std::function<std::shared_ptr<smacc2::SmaccAsyncClientBehavior>()> delayedCBFactoryFn =
44 [this, args...]()
45 {
46 RCLCPP_INFO(
47 getLogger(), "[CbSequence] then creating new sub behavior %s ",
49 auto createdBh = std::make_shared<TBehavior>(args...);
50
51 this->getCurrentState()->getOrthogonal<TOrthogonal>()->addClientBehavior(createdBh);
53
54 return createdBh;
55 };
56
57 sequenceNodes_.push_back(delayedCBFactoryFn);
58
59 return this;
60 }
TOrthogonal * getOrthogonal()
std::string demangleSymbol()

References smacc2::introspection::demangleSymbol(), smacc2::ISmaccClientBehavior::getCurrentState(), smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccState::getOrthogonal(), smacc2::SmaccAsyncClientBehavior::onStateOrthogonalAllocation(), and sequenceNodes_.

Here is the call graph for this function:

Member Data Documentation

◆ bh_

std::shared_ptr<smacc2::SmaccAsyncClientBehavior> smacc2::client_behaviors::CbSequence::bh_
private

Definition at line 72 of file cb_sequence.hpp.

Referenced by onEntry(), and recursiveConsumeNext().

◆ conn2_

smacc2::SmaccSignalConnection smacc2::client_behaviors::CbSequence::conn2_
private

Definition at line 70 of file cb_sequence.hpp.

Referenced by onEntry(), onSubNodeAbort(), and recursiveConsumeNext().

◆ conn_

smacc2::SmaccSignalConnection smacc2::client_behaviors::CbSequence::conn_
private

Definition at line 69 of file cb_sequence.hpp.

Referenced by onEntry(), onSubNodeAbort(), and recursiveConsumeNext().

◆ consume_

std::atomic<int> smacc2::client_behaviors::CbSequence::consume_ {0}
private

Definition at line 73 of file cb_sequence.hpp.

73{0};

Referenced by onEntry(), onSubNodeAbort(), and onSubNodeSuccess().

◆ sequenceNodes_

std::list<std::function<std::shared_ptr<smacc2::SmaccAsyncClientBehavior>()> > smacc2::client_behaviors::CbSequence::sequenceNodes_
private

The documentation for this class was generated from the following files: