.. _program_listing_file__tmp_ws_src_smacc2_smacc2_include_smacc2_smacc_state_machine.hpp: Program Listing for File smacc_state_machine.hpp ================================================ |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/smacc2/smacc2/include/smacc2/smacc_state_machine.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2021 RobosoftAI Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /***************************************************************************************************************** * * Authors: Pablo Inigo Blasco, Brett Aldrich * ******************************************************************************************************************/ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include namespace smacc2 { using namespace smacc2::introspection; enum class EventLifeTime { ABSOLUTE, CURRENT_STATE /*events are discarded if we are leaving the state it were created. I is used for client behaviors whose lifetime is associated to state*/ }; enum class StateMachineInternalAction { STATE_CONFIGURING, STATE_ENTERING, STATE_RUNNING, STATE_EXITING, TRANSITIONING }; // This class describes the concept of Smacc State Machine in an abastract way. // The SmaccStateMachineBase inherits from this state machine and from // statechart::StateMachine<> (via multiple inheritance) class ISmaccStateMachine { public: ISmaccStateMachine( std::string stateMachineName, SignalDetector * signalDetector, rclcpp::NodeOptions nodeOptions = rclcpp::NodeOptions()); virtual ~ISmaccStateMachine(); virtual void reset(); virtual void stop(); virtual void eStop(); template TOrthogonal * getOrthogonal(); // gets the client behavior in a given orthogonal // the index is used to distinguish between multiple client behaviors of the same type template inline TClientBehavior * getClientBehavior(int index = 0) { auto orthogonal = this->template getOrthogonal(); return orthogonal->template getClientBehavior(index); } const std::map> & getOrthogonals() const; template void requiresComponent(SmaccComponentType *& storage, bool throwsExceptionIfNotExist = false); template void postEvent(EventType * ev, EventLifeTime evlifetime = EventLifeTime::ABSOLUTE); template void postEvent(EventLifeTime evlifetime = EventLifeTime::ABSOLUTE); // gets data from the state machine blackboard template bool getGlobalSMData(std::string name, T & ret); // sets data in the state machine blackboard template void setGlobalSMData(std::string name, T value); template void mapBehavior(); std::string getStateMachineName(); void state_machine_visualization(); inline std::shared_ptr getCurrentStateInfo() { return currentStateInfo_; } void publishTransition(const SmaccTransitionInfo & transitionInfo); virtual void onInitialize(); void getTransitionLogHistory( const std::shared_ptr request_header, const std::shared_ptr req, std::shared_ptr res); template boost::signals2::connection createSignalConnection( TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType * object); void disconnectSmaccSignalObject(void * object); template void notifyOnStateEntryStart(StateType * state); template void notifyOnStateEntryEnd(StateType * state); template void notifyOnRuntimeConfigured(StateType * state); template void notifyOnStateExitting(StateType * state); template void notifyOnStateExited(StateType * state); template void notifyOnRuntimeConfigurationFinished(StateType * state); inline int64_t getCurrentStateCounter() const; inline ISmaccState * getCurrentState() const; inline const SmaccStateMachineInfo & getStateMachineInfo(); template void buildStateMachineInfo(); rclcpp::Node::SharedPtr getNode(); inline rclcpp::Logger getLogger() { return nh_->get_logger(); } inline std::recursive_mutex & getMutex() { return this->m_mutex_; } protected: void checkStateMachineConsistence(); void initializeROS(std::string smshortname); void onInitialized(); template void createOrthogonal(); // The node handle for this state rclcpp::Node::SharedPtr nh_; rclcpp::TimerBase::SharedPtr timer_; rclcpp::Publisher::SharedPtr stateMachinePub_; rclcpp::Publisher::SharedPtr stateMachineStatusPub_; rclcpp::Publisher::SharedPtr transitionLogPub_; rclcpp::Service::SharedPtr transitionHistoryService_; // if it is null, you may be located in a transition. There is a small gap of time where internally // this currentState_ is null. This may change in the future. std::vector currentState_; std::shared_ptr currentStateInfo_; smacc2_msgs::msg::SmaccStatus status_msg_; // orthogonals std::map> orthogonals_; std::vector longLivedSignalConnections_; protected: std::shared_ptr stateMachineInfo_; private: std::recursive_mutex m_mutex_; std::recursive_mutex eventQueueMutex_; StateMachineInternalAction stateMachineCurrentAction; std::map> stateCallbackConnections; // shared variables std::map, boost::any>> globalData_; // contains the whole history of transitions of the state machine std::vector transitionLogHistory_; smacc2::SMRunMode runMode_; // Event to notify to the signaldetection thread that a request has been created... SignalDetector * signalDetector_; int64_t stateSeqCounter_; void lockStateMachine(std::string msg); void unlockStateMachine(std::string msg); template void propagateEventToStateReactors(ISmaccState * st, EventType * ev); void updateStatusMessage(); friend class ISmaccState; friend class SignalDetector; }; } // namespace smacc2 #include #include #include #include