SMACC2
Loading...
Searching...
No Matches
smacc_state_machine_base.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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 : ISmaccStateMachine(smacc2::utils::cleanShortTypeName<DerivedStateMachine>(), signalDetector),
38 sc::asynchronous_state_machine<
39 DerivedStateMachine, InitialStateType, SmaccFifoScheduler, SmaccAllocator>(ctx)
40 {
41 }
42
44 {
45 //updateCurrentState<InitialStateType>(false);
46 }
47
48 void reset() override
49 {
51 this->terminate();
52 smacc2::run<DerivedStateMachine>();
53 }
54
55 void stop() override
56 {
58 this->terminate();
59 }
60
61 void eStop() override
62 {
64 this->terminate();
65 }
66
67 void initiate_impl() override
68 {
69 globalNh_ = this->getNode();
70
71 // this is before because this creates orthogonals
72 this->onInitialize();
73
74 RCLCPP_INFO(getLogger(), "[SmaccStateMachine] Introspecting state machine via typeWalker");
75 this->buildStateMachineInfo<InitialStateType>();
76
77 RCLCPP_INFO(getLogger(), "[SmaccStateMachine] initiate_impl");
78 auto shortname = smacc2::utils::cleanShortTypeName(typeid(DerivedStateMachine));
79
80 this->initializeROS(shortname);
81
82 RCLCPP_INFO(getLogger(), "[SmaccStateMachine] Initializing ROS communication mechanisms");
83 this->onInitialized();
84
85 // publish startup state machine transition info
86 auto transitionInfo = std::make_shared<SmaccTransitionInfo>();
87 transitionInfo->destinyState = this->stateMachineInfo_->getState<InitialStateType>();
88 this->publishTransition(*transitionInfo);
89
90 RCLCPP_INFO(getLogger(), "[SmaccStateMachine] Initializing state machine");
91 sc::state_machine<DerivedStateMachine, InitialStateType, SmaccAllocator>::initiate();
92 }
93};
94} // namespace smacc2
void publishTransition(const SmaccTransitionInfo &transitionInfo)
rclcpp::Node::SharedPtr getNode()
virtual void onInitialize()
this function should be implemented by the user to create the orthogonals
void initializeROS(std::string smshortname)
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:48
rclcpp::Node::SharedPtr globalNh_
Definition: reflection.cpp:28
std::allocator< boost::statechart::none > SmaccAllocator
std::string cleanShortTypeName()
Definition: common.hpp:61
boost::statechart::fifo_scheduler< SmaccFifoWorker, SmaccAllocator > SmaccFifoScheduler
SmaccStateMachineBase(my_context ctx, SignalDetector *signalDetector)