SMACC2
Loading...
Searching...
No Matches
smacc2::SmaccAsyncClientBehavior Class Reference

#include <smacc_asynchronous_client_behavior.hpp>

Inheritance diagram for smacc2::SmaccAsyncClientBehavior:
Inheritance graph
Collaboration diagram for smacc2::SmaccAsyncClientBehavior:
Collaboration graph

Public Member Functions

template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
virtual ~SmaccAsyncClientBehavior ()
 
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 ()
 
ISmaccStateMachinegetStateMachine ()
 
std::string getName () const
 
template<typename SmaccClientType >
void requiresClient (SmaccClientType *&storage)
 
template<typename SmaccComponentType >
void requiresComponent (SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
virtual void onEntry ()
 
virtual void onExit ()
 

Protected Member Functions

void postSuccessEvent ()
 
void postFailureEvent ()
 
virtual 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 ()
 
ISmaccStategetCurrentState ()
 
virtual rclcpp::Node::SharedPtr getNode () const
 
virtual rclcpp::Logger getLogger () const
 

Private Member Functions

void waitFutureIfNotFinished (std::optional< std::future< int > > &threadfut, bool requestFinish)
 

Private Attributes

std::optional< std::future< int > > onEntryThread_
 
std::optional< std::future< int > > onExitThread_
 
std::function< void()> postFinishEventFn_
 
std::function< void()> postSuccessEventFn_
 
std::function< void()> postFailureEventFn_
 
SmaccSignal< void()> onFinished_
 
SmaccSignal< void()> onSuccess_
 
SmaccSignal< void()> onFailure_
 
bool isShutdownRequested_ = false
 

Detailed Description

Definition at line 55 of file smacc_asynchronous_client_behavior.hpp.

Constructor & Destructor Documentation

◆ ~SmaccAsyncClientBehavior()

smacc2::SmaccAsyncClientBehavior::~SmaccAsyncClientBehavior ( )
virtual

Definition at line 143 of file smacc_client_async_behavior.cpp.

143{}

Member Function Documentation

◆ dispose()

void smacc2::SmaccAsyncClientBehavior::dispose ( )
overrideprotectedvirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 116 of file smacc_client_async_behavior.cpp.

117{
118 RCLCPP_DEBUG_STREAM(
119 getLogger(), "[" << getName()
120 << "] Destroying client behavior- Waiting finishing of asynchronous onExit "
121 "thread");
122 try
123 {
124 if (this->onExitThread_)
125 {
126 this->onExitThread_->get();
127 }
128 }
129 catch (...)
130 {
131 RCLCPP_DEBUG(
132 getLogger(),
133 "[SmaccAsyncClientBehavior] trying to Join onExit function, but it was already "
134 "finished.");
135 }
136
137 RCLCPP_DEBUG_STREAM(
138 getLogger(), "[" << getName()
139 << "] Destroying client behavior- onExit thread finished. Proccedding "
140 "destruction.");
141}
virtual rclcpp::Logger getLogger() const
std::optional< std::future< int > > onExitThread_

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), and onExitThread_.

Here is the call graph for this function:

◆ executeOnEntry()

void smacc2::SmaccAsyncClientBehavior::executeOnEntry ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 27 of file smacc_client_async_behavior.cpp.

28{
29 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onEntry thread started");
30 this->onEntryThread_ = std::async(
31 std::launch::async,
32 [=]
33 {
34 this->onEntry();
35 this->postFinishEventFn_();
36 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onEntry thread finished");
37 return 0;
38 });
39}
std::optional< std::future< int > > onEntryThread_

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::onEntry(), onEntryThread_, and postFinishEventFn_.

Here is the call graph for this function:

◆ executeOnExit()

void smacc2::SmaccAsyncClientBehavior::executeOnExit ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 99 of file smacc_client_async_behavior.cpp.

100{
101 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] onExit - join async onEntry thread");
102 this->waitOnEntryThread(true);
103
104 RCLCPP_INFO_STREAM(
105 getLogger(), "[" << getName() << "] onExit - Creating asynchronous onExit thread");
106 this->onExitThread_ = std::async(
107 std::launch::async,
108 [=]
109 {
110 this->onExit();
111 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onExit done.");
112 return 0;
113 });
114}

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::onExit(), onExitThread_, and waitOnEntryThread().

Here is the call graph for this function:

◆ isShutdownRequested()

bool smacc2::SmaccAsyncClientBehavior::isShutdownRequested ( )
protected

◆ onFailure() [1/2]

template<typename TCallback , typename T >
smacc2::SmaccSignalConnection smacc2::SmaccAsyncClientBehavior::onFailure ( TCallback callback,
T * object )

◆ onFailure() [2/2]

template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection smacc2::SmaccAsyncClientBehavior::onFailure ( TCallbackMethod callback,
T * object )

Definition at line 77 of file smacc_asynchronous_client_behavior_impl.hpp.

79{
80 return this->getStateMachine()->createSignalConnection(onFailure_, callback, object);
81}
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)

References smacc2::ISmaccStateMachine::createSignalConnection(), smacc2::ISmaccClientBehavior::getStateMachine(), and onFailure_.

Here is the call graph for this function:

◆ onFinished() [1/2]

template<typename TCallback , typename T >
smacc2::SmaccSignalConnection smacc2::SmaccAsyncClientBehavior::onFinished ( TCallback callback,
T * object )

◆ onFinished() [2/2]

template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection smacc2::SmaccAsyncClientBehavior::onFinished ( TCallbackMethod callback,
T * object )

◆ onStateOrthogonalAllocation()

template<typename TOrthogonal , typename TSourceObject >
void smacc2::SmaccAsyncClientBehavior::onStateOrthogonalAllocation ( )

Definition at line 29 of file smacc_asynchronous_client_behavior_impl.hpp.

30{
32 {
33 RCLCPP_WARN(
34 getLogger(),
35 "SmaccAsyncClientBehavior already has event posting functions assigned. Skipping "
36 "re-assignment. This could be a problem if you are using the same behavior in multiple "
37 "states. This may be related with the deprecation of onStateOrthogonalAllocation in favor of "
38 "onStateOrthogonalAllocation.");
39
40 return;
41 }
42
43 postFinishEventFn_ = [this]
44 {
45 this->onFinished_();
47 };
48
49 postSuccessEventFn_ = [this]
50 {
51 this->onSuccess_();
53 };
54
55 postFailureEventFn_ = [this]
56 {
57 this->onFailure_();
59 };
60}

References smacc2::ISmaccClientBehavior::getLogger(), onFailure_, onFinished_, onSuccess_, smacc2::ISmaccClientBehavior::postEvent(), postFailureEventFn_, postFinishEventFn_, and postSuccessEventFn_.

Referenced by cl_lifecycle_node::CbActivate::onStateOrthogonalAllocation(), cl_lifecycle_node::CbCleanup::onStateOrthogonalAllocation(), cl_lifecycle_node::CbConfigure::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDeactivate::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDeactivateOnExit::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDestroy::onStateOrthogonalAllocation(), cl_lifecycle_node::CbShutdown::onStateOrthogonalAllocation(), cl_modbus_tcp_relay::CbAllRelaysOff::onStateOrthogonalAllocation(), cl_modbus_tcp_relay::CbAllRelaysOn::onStateOrthogonalAllocation(), cl_modbus_tcp_relay::CbRelayOff::onStateOrthogonalAllocation(), cl_modbus_tcp_relay::CbRelayOn::onStateOrthogonalAllocation(), cl_modbus_tcp_relay::CbRelayStatus::onStateOrthogonalAllocation(), cl_moveit2z::CbMoveEndEffectorTrajectory::onStateOrthogonalAllocation(), cl_nav2z::CbAbortNavigation::onStateOrthogonalAllocation(), cl_nav2z::CbNav2ZClientBehaviorBase::onStateOrthogonalAllocation(), cl_nav2z::CbNavigateNextWaypointUntilReached::onStateOrthogonalAllocation(), cl_nav2z::CbWaitPose::onStateOrthogonalAllocation(), cl_nav2z::CbWaitTransform::onStateOrthogonalAllocation(), smacc2::client_behaviors::CbRosLaunch2::onStateOrthogonalAllocation(), smacc2::client_behaviors::CbRosLaunch::onStateOrthogonalAllocation(), smacc2::client_behaviors::CbRosStop2::onStateOrthogonalAllocation(), smacc2::client_behaviors::CbWaitActionServer2< ActionT >::onStateOrthogonalAllocation(), smacc2::client_behaviors::CbWaitActionServer::onStateOrthogonalAllocation(), smacc2::client_behaviors::onStateOrthogonalAllocation(), and smacc2::client_behaviors::CbSequence::then().

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

◆ onSuccess() [1/2]

template<typename TCallback , typename T >
smacc2::SmaccSignalConnection smacc2::SmaccAsyncClientBehavior::onSuccess ( TCallback callback,
T * object )

Referenced by cl_nav2z::CbNavigateNextWaypointFree::onEntry().

Here is the caller graph for this function:

◆ onSuccess() [2/2]

template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection smacc2::SmaccAsyncClientBehavior::onSuccess ( TCallbackMethod callback,
T * object )

Definition at line 63 of file smacc_asynchronous_client_behavior_impl.hpp.

65{
66 return this->getStateMachine()->createSignalConnection(onSuccess_, callback, object);
67}

References smacc2::ISmaccStateMachine::createSignalConnection(), smacc2::ISmaccClientBehavior::getStateMachine(), and onSuccess_.

Here is the call graph for this function:

◆ postFailureEvent()

void smacc2::SmaccAsyncClientBehavior::postFailureEvent ( )
protected

Definition at line 147 of file smacc_client_async_behavior.cpp.

References postFailureEventFn_.

Referenced by cl_moveit2z::CbMoveEndEffectorTrajectory::executeJointSpaceTrajectory(), cl_moveit2z::CbMoveJoints::moveJoints(), cl_moveit2z::CbMoveEndEffector::moveToAbsolutePose(), cl_gcalcli::CbDetectCalendarEvent::onEntry(), cl_gcalcli::CbEventDetect::onEntry(), cl_gcalcli::CbQuickAdd::onEntry(), cl_gcalcli::CbWaitConnection::onEntry(), cl_modbus_tcp_relay::CbAllRelaysOff::onEntry(), cl_modbus_tcp_relay::CbAllRelaysOn::onEntry(), cl_modbus_tcp_relay::CbRelayOff::onEntry(), cl_modbus_tcp_relay::CbRelayOn::onEntry(), cl_moveit2z::CbAttachObject::onEntry(), cl_moveit2z::CbDetachObject::onEntry(), cl_moveit2z::CbMoveEndEffectorTrajectory::onEntry(), cl_nav2z::CbWaitPose::onEntry(), cl_nav2z::CbWaitTransform::onEntry(), cl_px4_mr::CbArmPX4::onEntry(), cl_px4_mr::CbConnectMicroRosAgent::onEntry(), smacc2::client_behaviors::CbSequence::onEntry(), smacc2::client_behaviors::CbServiceCall< ServiceType >::onEntry(), smacc2::client_behaviors::CbWaitActionServer2< ActionT >::onEntry(), smacc2::client_behaviors::CbWaitActionServer::onEntry(), smacc2::client_behaviors::CbWaitNode::onEntry(), smacc2::client_behaviors::CbWaitTopic::onEntry(), cl_lifecycle_node::CbActivate::onStateOrthogonalAllocation(), cl_lifecycle_node::CbCleanup::onStateOrthogonalAllocation(), cl_lifecycle_node::CbConfigure::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDeactivate::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDeactivateOnExit::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDestroy::onStateOrthogonalAllocation(), cl_lifecycle_node::CbShutdown::onStateOrthogonalAllocation(), smacc2::client_behaviors::CbSequence::onSubNodeAbort(), cl_modbus_tcp_relay::CbRelayStatus::readAllChannels(), and cl_modbus_tcp_relay::CbRelayStatus::readSingleChannel().

Here is the caller graph for this function:

◆ postSuccessEvent()

void smacc2::SmaccAsyncClientBehavior::postSuccessEvent ( )
protected

Definition at line 145 of file smacc_client_async_behavior.cpp.

References postSuccessEventFn_.

Referenced by cl_moveit2z::CbMoveEndEffectorTrajectory::executeJointSpaceTrajectory(), cl_moveit2z::CbMoveJoints::moveJoints(), cl_moveit2z::CbMoveEndEffector::moveToAbsolutePose(), cl_px4_mr::CbDisarmPX4::onDisarmedCallback(), cl_gcalcli::CbQuickAdd::onEntry(), cl_gcalcli::CbWaitConnection::onEntry(), cl_modbus_tcp_relay::CbAllRelaysOff::onEntry(), cl_modbus_tcp_relay::CbAllRelaysOn::onEntry(), cl_modbus_tcp_relay::CbRelayOff::onEntry(), cl_modbus_tcp_relay::CbRelayOn::onEntry(), cl_moveit2z::CbAttachObject::onEntry(), cl_moveit2z::CbDetachObject::onEntry(), cl_nav2z::CbAbortNavigation::onEntry(), cl_nav2z::CbActiveStop::onEntry(), cl_nav2z::CbLoadWaypointsFile::onEntry(), cl_nav2z::CbPositionControlFreeSpace::onEntry(), cl_nav2z::CbPureSpinning::onEntry(), cl_nav2z::CbSpiralMotion::onEntry(), cl_nav2z::CbWaitPose::onEntry(), cl_nav2z::CbWaitTransform::onEntry(), cl_px4_mr::CbArmPX4::onEntry(), cl_px4_mr::CbConnectMicroRosAgent::onEntry(), cl_px4_mr::CbFollowWaypoints::onEntry(), smacc2::client_behaviors::CbSequence::onEntry(), smacc2::client_behaviors::CbServiceCall< ServiceType >::onEntry(), smacc2::client_behaviors::CbSleepFor::onEntry(), smacc2::client_behaviors::CbWaitActionServer2< ActionT >::onEntry(), smacc2::client_behaviors::CbWaitActionServer::onEntry(), smacc2::client_behaviors::CbWaitNode::onEntry(), smacc2::client_behaviors::CbWaitTopic::onEntry(), cl_gcalcli::CbDetectCalendarEvent::onEventStarted(), cl_gcalcli::CbEventDetect::onEventStarted(), cl_px4_mr::CbChangeAltitude::onGoalReachedCallback(), cl_px4_mr::CbFollowWaypoints::onGoalReachedCallback(), cl_px4_mr::CbGoToLocation::onGoalReachedCallback(), cl_px4_mr::CbReturnToHome::onGoalReachedCallback(), cl_px4_mr::CbTakeOff::onGoalReachedCallback(), cl_px4_mr::CbLand::onLandedCallback(), cl_nav2z::CbWaitNav2Nodes::onMessageReceived(), smacc2::client_behaviors::CbWaitTopicMessage< TMessage >::onMessageReceived(), cl_nav2z::CbNavigateNextWaypointUntilReached::onNavigationActionSuccess(), cl_lifecycle_node::CbActivate::onStateOrthogonalAllocation(), cl_lifecycle_node::CbCleanup::onStateOrthogonalAllocation(), cl_lifecycle_node::CbConfigure::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDeactivate::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDeactivateOnExit::onStateOrthogonalAllocation(), cl_lifecycle_node::CbDestroy::onStateOrthogonalAllocation(), cl_lifecycle_node::CbShutdown::onStateOrthogonalAllocation(), cl_modbus_tcp_relay::CbRelayStatus::readAllChannels(), cl_modbus_tcp_relay::CbRelayStatus::readSingleChannel(), cl_px4_mr::CbFigureEight::update(), cl_px4_mr::CbHoldPosition::update(), cl_px4_mr::CbOrbitLocation::update(), cl_px4_mr::CbSpiralPattern::update(), and cl_px4_mr::CbYawRotate::update().

Here is the caller graph for this function:

◆ requestForceFinish()

void smacc2::SmaccAsyncClientBehavior::requestForceFinish ( )

Definition at line 149 of file smacc_client_async_behavior.cpp.

150{
151 RCLCPP_FATAL_STREAM_THROTTLE(
152 getLogger(), *(getNode()->get_clock()), 1000,
153 "[" << getName() << "] " << ((uint64_t)this) << " requestForceFinish");
155}
virtual rclcpp::Node::SharedPtr getNode() const

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::getNode(), and isShutdownRequested_.

Referenced by smacc2::client_behaviors::CbSequence::onSubNodeAbort(), and waitFutureIfNotFinished().

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

◆ waitFutureIfNotFinished()

void smacc2::SmaccAsyncClientBehavior::waitFutureIfNotFinished ( std::optional< std::future< int > > & threadfut,
bool requestFinish )
private

Definition at line 41 of file smacc_client_async_behavior.cpp.

43{
44 try
45 {
46 rclcpp::Rate r(100);
47 while (rclcpp::ok())
48 {
49 if (threadfut && threadfut->valid())
50 {
51 auto status = threadfut->wait_for(std::chrono::milliseconds(20));
52 if (status == std::future_status::ready)
53 {
54 // done
55 threadfut->get();
56 break;
57 }
58 else
59 {
60 // in progress
61 RCLCPP_INFO_STREAM(
62 getLogger(),
63 "[" << getName()
64 << "] fut valid but waiting for asynchronous onEntry thread to finish: state: "
65 << (int)status);
66 }
67 }
68 else
69 {
70 RCLCPP_INFO_STREAM(
71 getLogger(),
72 "[" << getName()
73 << "] waiting future onEntryThread. It was not even created. Skipping wait.");
74 break;
75 }
76 rclcpp::sleep_for(100ms);
77 RCLCPP_WARN_THROTTLE(
78 getLogger(), *(getNode()->get_clock()), 1000,
79 "[%s] waiting for finishing client behavior, before leaving the state. Is the client "
80 "behavior stuck? requesting force finish",
81 this->getName().c_str());
82
83 if (requestFinish) requestForceFinish();
84 }
85 }
86 catch (const std::exception & e)
87 {
88 RCLCPP_DEBUG(
89 getLogger(),
90 "[SmaccAsyncClientBehavior] trying to join function, but it was already finished.");
91 }
92}

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::getNode(), and requestForceFinish().

Referenced by waitOnEntryThread().

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

◆ waitOnEntryThread()

void smacc2::SmaccAsyncClientBehavior::waitOnEntryThread ( bool requestFinish)

Definition at line 94 of file smacc_client_async_behavior.cpp.

95{
96 waitFutureIfNotFinished(this->onEntryThread_, requestFinish);
97}
void waitFutureIfNotFinished(std::optional< std::future< int > > &threadfut, bool requestFinish)

References onEntryThread_, and waitFutureIfNotFinished().

Referenced by executeOnExit().

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

Member Data Documentation

◆ isShutdownRequested_

bool smacc2::SmaccAsyncClientBehavior::isShutdownRequested_ = false
private

◆ onEntryThread_

std::optional<std::future<int> > smacc2::SmaccAsyncClientBehavior::onEntryThread_
private

Definition at line 97 of file smacc_asynchronous_client_behavior.hpp.

Referenced by executeOnEntry(), and waitOnEntryThread().

◆ onExitThread_

std::optional<std::future<int> > smacc2::SmaccAsyncClientBehavior::onExitThread_
private

Definition at line 98 of file smacc_asynchronous_client_behavior.hpp.

Referenced by dispose(), and executeOnExit().

◆ onFailure_

SmaccSignal<void()> smacc2::SmaccAsyncClientBehavior::onFailure_
private

◆ onFinished_

SmaccSignal<void()> smacc2::SmaccAsyncClientBehavior::onFinished_
private

◆ onSuccess_

SmaccSignal<void()> smacc2::SmaccAsyncClientBehavior::onSuccess_
private

◆ postFailureEventFn_

std::function<void()> smacc2::SmaccAsyncClientBehavior::postFailureEventFn_
private

◆ postFinishEventFn_

std::function<void()> smacc2::SmaccAsyncClientBehavior::postFinishEventFn_
private

◆ postSuccessEventFn_

std::function<void()> smacc2::SmaccAsyncClientBehavior::postSuccessEventFn_
private

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