28#include <smacc2_msgs/msg/smacc_status.hpp>
29#include <smacc2_msgs/msg/smacc_transition_log_entry.hpp>
33using namespace std::chrono_literals;
36 std::string stateMachineName,
SignalDetector * signalDetector, rclcpp::NodeOptions nodeOptions)
37: nh_(nullptr), stateSeqCounter_(0)
45 nh_ = rclcpp::Node::make_shared(stateMachineName, nodeOptions);
47 nh_->get_logger(),
"Creating state machine base: " <<
nh_->get_fully_qualified_name());
53 if (
nh_->get_parameter(
"run_mode", runMode))
55 if (runMode ==
"debug")
59 else if (runMode ==
"release")
65 RCLCPP_ERROR(
nh_->get_logger(),
"Incorrect run_mode value: %s", runMode.c_str());
76 RCLCPP_INFO(
nh_->get_logger(),
"Finishing State Machine");
81 RCLCPP_INFO(
nh_->get_logger(),
"[SmaccSignal] Object signal disconnecting %ld", (
long)object_ptr);
85 callbackSemaphore->finalize();
90 RCLCPP_INFO(
nh_->get_logger(),
"[SmaccSignal] No signals found %ld", (
long)object_ptr);
102const std::map<std::string, std::shared_ptr<smacc2::ISmaccOrthogonal>> &
110 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
121 std::list<const SmaccStateInfo *> ancestorList;
124 for (
auto & ancestor : ancestorList)
126 status_msg_.current_states.push_back(ancestor->toShortName());
134 status_msg_.global_variable_names.push_back(entry.first);
136 entry.second.first());
147 smacc2_msgs::msg::SmaccTransitionLogEntry transitionLogEntry;
148 transitionLogEntry.timestamp = this->
nh_->now();
159 auto ros_clock = rclcpp::Clock::make_shared();
166 RCLCPP_WARN_STREAM(
nh_->get_logger(),
"State machine base creation: " << shortname);
169 shortname +
"/smacc/state_machine_description", rclcpp::QoS(1));
171 shortname +
"/smacc/status", rclcpp::QoS(1));
173 shortname +
"/smacc/transition_log", rclcpp::QoS(1));
176 shortname +
"/smacc/event_log", rclcpp::QoS(100));
180 shortname +
"/smacc/transition_log_history",
183 std::placeholders::_2, std::placeholders::_3));
187 const std::shared_ptr<rmw_request_id_t> ,
188 const std::shared_ptr<smacc2_msgs::srv::SmaccGetTransitionHistory::Request> ,
189 std::shared_ptr<smacc2_msgs::srv::SmaccGetTransitionHistory::Response> res)
192 nh_->get_logger(),
"Requesting transition log history, current size: %ld",
193 this->transitionLogHistory_.size());
199 std::lock_guard<std::recursive_mutex> lock(
m_mutex_);
201 smacc2_msgs::msg::SmaccStateMachine state_machine_msg;
205 state_machine_msg.states.begin(), state_machine_msg.states.end(),
206 [](
auto & a,
auto & b) { return a.index < b.index; });
215 RCLCPP_DEBUG(
nh_->get_logger(),
"-- locking SM: %s", msg.c_str());
221 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_
rclcpp::Service< smacc2_msgs::srv::SmaccGetTransitionHistory >::SharedPtr transitionHistoryService_
virtual void onInitialize()
this function should be implemented by the user to create the orthogonals
std::vector< smacc2_msgs::msg::SmaccTransitionLogEntry > transitionLogHistory_
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, rclcpp::NodeOptions nodeOptions=rclcpp::NodeOptions())
void lockStateMachine(std::string msg)
void disconnectSmaccSignalObject(void *object)
rclcpp::Node::SharedPtr nh_
void unlockStateMachine(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 initializeROS(std::string smshortname)
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)
std::string demangleSymbol()
void transitionInfoToMsg(const SmaccTransitionInfo &transition, smacc2_msgs::msg::SmaccTransition &transitionMsg)