44#include <boost/function_types/function_arity.hpp>
45#include <boost/function_types/function_type.hpp>
46#include <boost/function_types/parameter_types.hpp>
48#include <smacc2_msgs/msg/smacc_status.hpp>
54template <
typename TOrthogonal>
57 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
59 std::string orthogonalkey = demangledTypeName<TOrthogonal>();
68 "Orthogonal %s resource is being required from some state, client or component. Found "
71 orthogonalkey.c_str());
72 ret =
dynamic_cast<TOrthogonal *
>(it->second.get());
78 ss <<
"Orthogonal not found " << orthogonalkey.c_str() << std::endl;
79 ss <<
"The existing orthogonals are the following: " << std::endl;
82 ss <<
" - " << orthogonal.first << std::endl;
85 RCLCPP_WARN_STREAM(
getLogger(), ss.str());
92template <
typename TOrthogonal>
96 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
97 std::string orthogonalkey = demangledTypeName<TOrthogonal>();
101 auto ret = std::make_shared<TOrthogonal>();
102 orthogonals_[orthogonalkey] = dynamic_pointer_cast<smacc2::ISmaccOrthogonal>(ret);
104 ret->setStateMachine(
this);
106 RCLCPP_INFO(
getLogger(),
"%s Orthogonal is created", orthogonalkey.c_str());
111 getLogger(),
"There were already one existing orthogonal of type "
112 << orthogonalkey.c_str() <<
". Skipping creation orthogonal request. ");
113 std::stringstream ss;
114 ss <<
"The existing orthogonals are the following: " << std::endl;
117 ss <<
" - " << orthogonal.first << std::endl;
119 RCLCPP_WARN_STREAM(
getLogger(), ss.str());
125template <
typename SmaccComponentType>
131 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
135 for (
auto & client : ortho.second->clients_)
137 storage = client->getComponent<SmaccComponentType>();
138 if (storage !=
nullptr)
146 getLogger(),
"component %s is required but it was not found in any orthogonal",
173template <
typename EventType>
178#define eventtypename demangleSymbol<EventType>().c_str()
189 "[ISmaccStateMachine] CURRENT STATE SCOPED EVENT DISCARDED, state is exiting/transitioning "
204 if (currentstate !=
nullptr)
212template <
typename EventType>
215 auto evname = smacc2::introspection::demangleSymbol<EventType>();
216 RCLCPP_INFO_STREAM(
getLogger(),
"Event " << evname);
217 auto * ev =
new EventType();
224 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
226 bool success =
false;
241 ret = boost::any_cast<T>(v.second);
245 catch (boost::bad_any_cast & ex)
247 RCLCPP_ERROR(
getLogger(),
"bad any cast: %s", ex.what());
260 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
265 std::stringstream ss;
276template <
typename StateField,
typename BehaviorType>
279 std::string stateFieldName =
demangleSymbol(
typeid(StateField).name());
280 std::string behaviorType =
demangleSymbol(
typeid(BehaviorType).name());
282 getLogger(),
"Mapping state field '%s' to stateReactor '%s'", stateFieldName.c_str(),
283 behaviorType.c_str());
289 BehaviorType * behavior;
302 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
304 TSmaccSignal & signal, TMemberFunctionPrototype
callback, TSmaccObjectType *
object);
310 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
312 TSmaccSignal & signal, TMemberFunctionPrototype
callback, TSmaccObjectType *
object)
314 return signal.connect([=]() {
return (object->*
callback)(); });
321 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
323 TSmaccSignal & signal, TMemberFunctionPrototype
callback, TSmaccObjectType *
object)
325 return signal.connect([=](
auto a1) {
return (object->*
callback)(a1); });
332 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
334 TSmaccSignal & signal, TMemberFunctionPrototype
callback, TSmaccObjectType *
object)
336 return signal.connect([=](
auto a1,
auto a2) {
return (object->*
callback)(a1, a2); });
343 template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
345 TSmaccSignal & signal, TMemberFunctionPrototype
callback, TSmaccObjectType *
object)
347 return signal.connect(
348 [=](
auto a1,
auto a2,
auto a3) {
return (object->*
callback)(a1, a2, a3); });
354template <
typename TSmaccSignal,
typename TMemberFunctionPrototype,
typename TSmaccObjectType>
356 TSmaccSignal & signal, TMemberFunctionPrototype
callback, TSmaccObjectType *
object)
359 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
360 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
361 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value ||
362 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
363 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value,
364 "Only are accepted smacc types as subscribers for smacc signals");
368 boost::signals2::connection connection = binder.
bindaux(signal,
callback,
object);
372 std::is_base_of<ISmaccComponent, TSmaccObjectType>::value ||
373 std::is_base_of<ISmaccClient, TSmaccObjectType>::value ||
374 std::is_base_of<ISmaccOrthogonal, TSmaccObjectType>::value ||
375 std::is_base_of<ISmaccStateMachine, TSmaccObjectType>::value)
379 std::is_base_of<ISmaccState, TSmaccObjectType>::value ||
380 std::is_base_of<StateReactor, TSmaccObjectType>::value ||
381 std::is_base_of<ISmaccClientBehavior, TSmaccObjectType>::value)
385 "[StateMachine] life-time constrained smacc signal subscription created. Subscriber is %s",
386 demangledTypeName<TSmaccObjectType>().c_str());
393 "[StateMachine] connecting signal to an unknown object with life-time unknown "
394 "behavior. It might provoke "
395 "an exception if the object is destroyed during the execution.");
401template <
typename StateType>
404 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
408 "[State Machne] Initializating a new state '%s' and updating current state. Getting state "
409 "meta-information. number of orthogonals: %ld",
417template <
typename StateType>
421 getLogger(),
"[%s] State OnEntry code finished",
426 RCLCPP_DEBUG(
getLogger(),
"ortho onentry: %s", pair.second->getName().c_str());
427 auto & orthogonal = pair.second;
430 orthogonal->onEntry();
432 catch (
const std::exception & e)
436 "[Orthogonal %s] Exception on Entry - continuing with next orthogonal. Exception info: %s",
437 pair.second->getName().c_str(), e.what());
444 RCLCPP_INFO_STREAM(
getLogger(),
"state reactor onEntry: " << srname);
449 catch (
const std::exception & e)
453 "[State Reactor %s] Exception on Entry - continuing with next state reactor. Exception "
455 srname.c_str(), e.what());
462 RCLCPP_INFO_STREAM(
getLogger(),
"event generator onEntry: " << egname);
467 catch (
const std::exception & e)
471 "[Event generator %s] Exception on Entry - continuing with next state reactor. Exception "
473 egname.c_str(), e.what());
481template <
typename StateType>
487 auto & orthogonal = pair.second;
488 orthogonal->runtimeConfigure();
496template <
typename StateType>
502template <
typename StateType>
508 RCLCPP_WARN_STREAM(
getLogger(),
"exiting state: " << fullname);
511 RCLCPP_INFO_STREAM(
getLogger(),
"Notification State Exit: leaving state " << state);
514 auto & orthogonal = pair.second;
517 orthogonal->onExit();
519 catch (
const std::exception & e)
523 "[Orthogonal %s] Exception onExit - continuing with next orthogonal. Exception info: %s",
524 pair.second->getName().c_str(), e.what());
528 for (
auto & sr : state->getStateReactors())
531 RCLCPP_INFO_STREAM(
getLogger(),
"state reactor OnExit: " << srname);
536 catch (
const std::exception & e)
540 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
542 srname.c_str(), e.what());
546 for (
auto & eg : state->getEventGenerators())
549 RCLCPP_INFO_STREAM(
getLogger(),
"event generator OnExit: " << egname);
554 catch (
const std::exception & e)
558 "[State Reactor %s] Exception on OnExit - continuing with next state reactor. Exception "
560 egname.c_str(), e.what());
567template <
typename StateType>
573 RCLCPP_WARN_STREAM(
getLogger(),
"state exit: " << fullname);
579template <
typename EventType>
583 getLogger(),
"PROPAGATING EVENT [%s] TO SRs [%s]: ", demangleSymbol<EventType>().c_str(),
597template <
typename InitialStateType>
std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > orthogonals_
std::recursive_mutex eventQueueMutex_
bool getGlobalSMData(std::string name, T &ret)
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
StateMachineInternalAction stateMachineCurrentAction
std::shared_ptr< SmaccStateInfo > currentStateInfo_
rclcpp::Node::SharedPtr getNode()
std::map< std::string, std::pair< std::function< std::string()>, boost::any > > globalData_
unsigned long stateSeqCounter_
void notifyOnStateExitting(StateType *state)
void setGlobalSMData(std::string name, T value)
TOrthogonal * getOrthogonal()
void buildStateMachineInfo()
void notifyOnRuntimeConfigurationFinished(StateType *state)
std::recursive_mutex m_mutex_
void notifyOnStateExited(StateType *state)
void notifyOnStateEntryEnd(StateType *state)
void propagateEventToStateReactors(ISmaccState *st, EventType *ev)
ISmaccState * currentState_
const SmaccStateMachineInfo & getStateMachineInfo()
void requiresComponent(SmaccComponentType *&storage)
unsigned long getCurrentStateCounter() const
ISmaccState * getCurrentState() const
void checkStateMachineConsistence()
SignalDetector * signalDetector_
rclcpp::Logger getLogger()
void notifyOnRuntimeConfigured(StateType *state)
std::list< boost::signals2::connection > stateCallbackConnections
std::shared_ptr< SmaccStateMachineInfo > stateMachineInfo_
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
void updateStatusMessage()
void notifyOnStateEntryStart(StateType *state)
ISmaccState * getParentState()
virtual std::string getClassName()
std::vector< std::shared_ptr< StateReactor > > & getStateReactors()
std::vector< std::shared_ptr< SmaccEventGenerator > > & getEventGenerators()
void postEvent(EventType *ev)
std::string demangleSymbol(const std::string &name)
void callback(const image_tools::ROSCvMatContainer &img)
void TRACEPOINT(spinOnce)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection bindaux(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)