29#include <smacc2_msgs/msg/smacc_status.hpp>
30#include <smacc2_msgs/msg/smacc_transition_log_entry.hpp>
36using namespace std::chrono_literals;
40 const rclcpp::NodeOptions & nodeOptions)
41: nh_(rclcpp::Node::make_shared(stateMachineName, nodeOptions))
44 nh_->get_logger(),
"Creating state machine base: " <<
nh_->get_fully_qualified_name());
50 if (
nh_->get_parameter(
"run_mode", runMode))
52 if (runMode ==
"debug")
56 else if (runMode ==
"release")
62 RCLCPP_ERROR(
nh_->get_logger(),
"Incorrect run_mode value: %s", runMode.c_str());
73 RCLCPP_INFO(
nh_->get_logger(),
"Finishing State Machine");
86 RCLCPP_DEBUG(
nh_->get_logger(),
"[SmaccSignal] Object signal disconnecting %p", object_ptr);
90 callbackSemaphore->finalize();
95 RCLCPP_DEBUG(
nh_->get_logger(),
"[SmaccSignal] No signals found %p", object_ptr);
113 for (
auto & behaviorsOfOneState : orthogonalEntry.second->getClientBehaviors())
115 for (
auto & clBehavior : behaviorsOfOneState)
118 if (asyncBehavior !=
nullptr)
120 asyncBehavior->waitOnEntryThread(
true);
127const std::map<std::string, std::shared_ptr<smacc2::ISmaccOrthogonal>> &
135 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
146 std::list<const SmaccStateInfo *> ancestorList;
149 for (
auto & ancestor : ancestorList)
151 status_msg_.current_states.push_back(ancestor->toShortName());
159 status_msg_.global_variable_names.push_back(entry.first);
161 entry.second.first());
172 smacc2_msgs::msg::SmaccTransitionLogEntry transitionLogEntry;
173 transitionLogEntry.timestamp = this->
nh_->now();
191 const std::string & stateName,
const std::string & what)
195 "[SmaccStateMachine] no state reacts to the initialization failure of %s (%s); the state "
197 "stays in that state with nothing running",
198 stateName.c_str(), what.c_str());
203 auto ros_clock = rclcpp::Clock::make_shared();
210 RCLCPP_WARN_STREAM(
nh_->get_logger(),
"State machine base creation: " << shortname);
213 shortname +
"/smacc/state_machine_description", rclcpp::QoS(1));
215 shortname +
"/smacc/status", rclcpp::QoS(1));
217 shortname +
"/smacc/transition_log", rclcpp::QoS(1));
219 int64_t historySize =
nh_->declare_parameter<int64_t>(
"transition_log_history_size", 1000);
222 RCLCPP_WARN(
nh_->get_logger(),
"transition_log_history_size %ld clamped to 0", historySize);
228 shortname +
"/smacc/event_log", rclcpp::QoS(100));
232 shortname +
"/smacc/transition_log_history",
235 std::placeholders::_2, std::placeholders::_3));
243 const std::shared_ptr<rmw_request_id_t> ,
244 const std::shared_ptr<smacc2_msgs::srv::SmaccGetTransitionHistory::Request> ,
245 std::shared_ptr<smacc2_msgs::srv::SmaccGetTransitionHistory::Response> res)
249 nh_->get_logger(),
"Requesting transition log history, current size: %ld",
250 this->transitionLogHistory_.size());
256 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
258 smacc2_msgs::msg::SmaccStateMachine state_machine_msg;
262 state_machine_msg.states.begin(), state_machine_msg.states.end(),
263 [](
const auto & a,
const auto & b) { return a.index < b.index; });
272 RCLCPP_DEBUG(
nh_->get_logger(),
"-- locking SM: %s", msg.c_str());
278 RCLCPP_DEBUG(
nh_->get_logger(),
"-- unlocking SM: %s", msg.c_str());
std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > orthogonals_
smacc2::SMRunMode runMode_
virtual ~ISmaccStateMachine()
std::string getStateMachineName()
void publishTransition(const SmaccTransitionInfo &transitionInfo)
rclcpp::Publisher< smacc2_msgs::msg::SmaccStateMachine >::SharedPtr stateMachinePub_
std::shared_ptr< SmaccStateInfo > currentStateInfo_
rclcpp::Node::SharedPtr getNode()
std::map< std::string, std::pair< std::function< std::string()>, boost::any > > globalData_
std::deque< smacc2_msgs::msg::SmaccTransitionLogEntry > transitionLogHistory_
virtual void onUnhandledStInitializationFailure(const std::string &stateName, const std::string &what)
rclcpp::Service< smacc2_msgs::srv::SmaccGetTransitionHistory >::SharedPtr transitionHistoryService_
virtual void onInitialize()
this function should be implemented by the user to create the orthogonals
void joinAsyncClientBehaviorThreads()
void disconnectSmaccSignalObject(void *object_ptr)
const std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > & getOrthogonals() const
std::recursive_mutex m_mutex_
rclcpp::Publisher< smacc2_msgs::msg::SmaccStatus >::SharedPtr stateMachineStatusPub_
ISmaccStateMachine(std::string stateMachineName, SignalDetector *signalDetector, const rclcpp::NodeOptions &nodeOptions=rclcpp::NodeOptions())
rclcpp::Node::SharedPtr nh_
size_t transitionLogHistorySize_
static thread_local int reactorWalkDepth_
void initializeROS(const std::string &shortname)
void unlockStateMachine(const std::string &msg)
rclcpp::TimerBase::SharedPtr timer_
rclcpp::Publisher< smacc2_msgs::msg::SmaccTransitionLogEntry >::SharedPtr transitionLogPub_
void checkStateMachineConsistence()
SignalDetector * signalDetector_
std::map< void *, std::shared_ptr< CallbackCounterSemaphore > > stateCallbackConnections
void lockStateMachine(const std::string &msg)
rclcpp::Logger getLogger()
std::mutex transitionLogMutex_
smacc2_msgs::msg::SmaccStatus status_msg_
void state_machine_visualization()
std::shared_ptr< SmaccStateMachineInfo > stateMachineInfo_
rclcpp::Publisher< smacc2_msgs::msg::SmaccEvent >::SharedPtr eventsLogPub_
void updateStatusMessage()
void getTransitionLogHistory(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< smacc2_msgs::srv::SmaccGetTransitionHistory::Request > req, std::shared_ptr< smacc2_msgs::srv::SmaccGetTransitionHistory::Response > res)
void initialize(ISmaccStateMachine *stateMachine)
void notifyRosInitialized()
rclcpp::Node::SharedPtr globalNh_
std::string demangleSymbol()
void transitionInfoToMsg(const SmaccTransitionInfo &transition, smacc2_msgs::msg::SmaccTransition &transitionMsg)