SMACC2
Loading...
Searching...
No Matches
smacc_state_machine_base.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20#pragma once
21#include <smacc2/common.hpp>
22
25
26namespace smacc2
27{
29template <typename DerivedStateMachine, typename InitialStateType>
31: public ISmaccStateMachine,
33 DerivedStateMachine, InitialStateType, SmaccFifoScheduler, SmaccAllocator>
34{
35public:
37 my_context ctx, SignalDetector * signalDetector,
38 const rclcpp::NodeOptions & node_options = rclcpp::NodeOptions())
40 smacc2::utils::cleanShortTypeName<DerivedStateMachine>(), signalDetector, node_options),
41 sc::asynchronous_state_machine<
42 DerivedStateMachine, InitialStateType, SmaccFifoScheduler, SmaccAllocator>(ctx)
43 {
44 }
45
47
48 void reset() override
49 {
51 this->terminate();
53 }
54
55 void stop() override
56 {
58 this->terminate();
59 }
60
61 void eStop() override
62 {
64 this->terminate();
65 }
66
67 // Called by Boost.Statechart for an event no active state reacted to. An unhandled state
68 // initialization failure is posted again, addressed to the next container state.
69 void unconsumed_event(const sc::event_base & ev)
70 {
71 const auto * failure = dynamic_cast<const default_events::EvStInitializationFailureInfo *>(&ev);
72 if (failure == nullptr) return;
73 if (failure->escalate)
74 {
75 failure->escalate();
76 }
77 else
78 {
79 this->onUnhandledStInitializationFailure(failure->stateName, failure->what);
80 }
81 }
82
83 void initiate_impl() override
84 {
85 globalNh_ = this->getNode();
86
87 // this is before because this creates orthogonals
88 this->onInitialize();
89
90 RCLCPP_INFO(getLogger(), "[SmaccStateMachine] Introspecting state machine via typeWalker");
92
93 RCLCPP_INFO(getLogger(), "[SmaccStateMachine] initiate_impl");
94 auto shortname = smacc2::utils::cleanShortTypeName(typeid(DerivedStateMachine));
95
96 this->initializeROS(shortname);
97
98 RCLCPP_INFO(getLogger(), "[SmaccStateMachine] Initializing ROS communication mechanisms");
99 this->onInitialized();
100
101 // publish startup state machine transition info
102 auto transitionInfo = std::make_shared<SmaccTransitionInfo>();
103 transitionInfo->destinyState = this->stateMachineInfo_->template getState<InitialStateType>();
104 this->publishTransition(*transitionInfo);
105
106 RCLCPP_INFO(getLogger(), "[SmaccStateMachine] Initializing state machine");
107 sc::state_machine<DerivedStateMachine, InitialStateType, SmaccAllocator>::initiate();
108 }
109};
110} // namespace smacc2
void publishTransition(const SmaccTransitionInfo &transitionInfo)
rclcpp::Node::SharedPtr getNode()
virtual void onUnhandledStInitializationFailure(const std::string &stateName, const std::string &what)
virtual void onInitialize()
this function should be implemented by the user to create the orthogonals
void initializeROS(const std::string &shortname)
std::shared_ptr< SmaccStateMachineInfo > stateMachineInfo_
boost::statechart::processor_container< boost::statechart::fifo_scheduler<>, boost::function0< void >, std::allocator< boost::statechart::none > >::processor_context my_context
Definition common.hpp:56
rclcpp::Node::SharedPtr globalNh_
std::allocator< boost::statechart::none > SmaccAllocator
std::string cleanShortTypeName()
Definition common.hpp:69
void run(ExecutionModel executionModel=ExecutionModel::SINGLE_THREAD_SPINNER)
boost::statechart::fifo_scheduler< SmaccFifoWorker, SmaccAllocator > SmaccFifoScheduler
SmaccStateMachineBase(my_context ctx, SignalDetector *signalDetector, const rclcpp::NodeOptions &node_options=rclcpp::NodeOptions())
void unconsumed_event(const sc::event_base &ev)