SMACC2
Loading...
Searching...
No Matches
smacc_state_machine.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
22#include <boost/any.hpp>
23#include <map>
24#include <mutex>
25
26#include <smacc2/common.hpp>
31
32#include <smacc2_msgs/msg/smacc_state_machine.hpp>
33#include <smacc2_msgs/msg/smacc_status.hpp>
34#include <smacc2_msgs/msg/smacc_transition_log_entry.hpp>
35#include <smacc2_msgs/srv/smacc_get_transition_history.hpp>
36
40//#include <smacc2/smacc_event_generator.hpp>
41
42namespace smacc2
43{
44using namespace smacc2::introspection;
45
46enum class EventLifeTime
47{
49 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*/
50};
51
53{
59};
60
61// This class describes the concept of Smacc State Machine in an abastract way.
62// The SmaccStateMachineBase inherits from this state machine and from
63// statechart::StateMachine<> (via multiple inheritance)
65{
66public:
67 ISmaccStateMachine(std::string stateMachineName, SignalDetector * signalDetector);
68
69 virtual ~ISmaccStateMachine();
70
71 virtual void reset();
72
73 virtual void stop();
74
75 virtual void eStop();
76
77 template <typename TOrthogonal>
78 TOrthogonal * getOrthogonal();
79
80 const std::map<std::string, std::shared_ptr<smacc2::ISmaccOrthogonal>> & getOrthogonals() const;
81
82 template <typename SmaccComponentType>
83 void requiresComponent(SmaccComponentType *& storage, bool throwsException = false);
84
85 template <typename EventType>
86 void postEvent(EventType * ev, EventLifeTime evlifetime = EventLifeTime::ABSOLUTE);
87
88 template <typename EventType>
90
91 // gets data from the state machine blackboard
92 template <typename T>
93 bool getGlobalSMData(std::string name, T & ret);
94
95 // sets data in the state machine blackboard
96 template <typename T>
97 void setGlobalSMData(std::string name, T value);
98
99 template <typename StateField, typename BehaviorType>
100 void mapBehavior();
101
102 std::string getStateMachineName();
103
105
106 inline std::shared_ptr<SmaccStateInfo> getCurrentStateInfo() { return currentStateInfo_; }
107
108 void publishTransition(const SmaccTransitionInfo & transitionInfo);
109
111 virtual void onInitialize();
112
114 const std::shared_ptr<rmw_request_id_t> request_header,
115 const std::shared_ptr<smacc2_msgs::srv::SmaccGetTransitionHistory::Request> req,
116 std::shared_ptr<smacc2_msgs::srv::SmaccGetTransitionHistory::Response> res);
117
118 template <typename TSmaccSignal, typename TMemberFunctionPrototype, typename TSmaccObjectType>
119 boost::signals2::connection createSignalConnection(
120 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType * object);
121
122 void disconnectSmaccSignalObject(void * object);
123
124 template <typename StateType>
125 void notifyOnStateEntryStart(StateType * state);
126
127 template <typename StateType>
128 void notifyOnStateEntryEnd(StateType * state);
129
130 template <typename StateType>
131 void notifyOnRuntimeConfigured(StateType * state);
132
133 template <typename StateType>
134 void notifyOnStateExitting(StateType * state);
135
136 template <typename StateType>
137 void notifyOnStateExited(StateType * state);
138
139 template <typename StateType>
140 void notifyOnRuntimeConfigurationFinished(StateType * state);
141
142 inline uint64_t getCurrentStateCounter() const;
143
144 inline ISmaccState * getCurrentState() const;
145
147
148 template <typename InitialStateType>
150
151 rclcpp::Node::SharedPtr getNode();
152
153 inline rclcpp::Logger getLogger() { return nh_->get_logger(); }
154
155 inline std::recursive_mutex & getMutex() { return this->m_mutex_; }
156
157protected:
159
160 void initializeROS(std::string smshortname);
161
162 void onInitialized();
163
164 template <typename TOrthogonal>
165 void createOrthogonal();
166
167 // The node handle for this state
168 rclcpp::Node::SharedPtr nh_;
169
170 rclcpp::TimerBase::SharedPtr timer_;
171 rclcpp::Publisher<smacc2_msgs::msg::SmaccStateMachine>::SharedPtr stateMachinePub_;
172 rclcpp::Publisher<smacc2_msgs::msg::SmaccStatus>::SharedPtr stateMachineStatusPub_;
173 rclcpp::Publisher<smacc2_msgs::msg::SmaccTransitionLogEntry>::SharedPtr transitionLogPub_;
174 rclcpp::Service<smacc2_msgs::srv::SmaccGetTransitionHistory>::SharedPtr transitionHistoryService_;
175
176 // if it is null, you may be located in a transition. There is a small gap of time where internally
177 // this currentState_ is null. This may change in the future.
178 std::vector<ISmaccState *> currentState_;
179
180 std::shared_ptr<SmaccStateInfo> currentStateInfo_;
181
182 smacc2_msgs::msg::SmaccStatus status_msg_;
183
184 // orthogonals
185 std::map<std::string, std::shared_ptr<smacc2::ISmaccOrthogonal>> orthogonals_;
186
187protected:
188 std::shared_ptr<SmaccStateMachineInfo> stateMachineInfo_;
189
190private:
191 std::recursive_mutex m_mutex_;
192 std::recursive_mutex eventQueueMutex_;
193
195
196 std::map<void *, std::shared_ptr<CallbackCounterSemaphore>> stateCallbackConnections;
197
198 // shared variables
199 std::map<std::string, std::pair<std::function<std::string()>, boost::any>> globalData_;
200
201 // contains the whole history of transitions of the state machine
202 std::vector<smacc2_msgs::msg::SmaccTransitionLogEntry> transitionLogHistory_;
203
205
206 // Event to notify to the signaldetection thread that a request has been created...
208
210
211 void lockStateMachine(std::string msg);
212
213 void unlockStateMachine(std::string msg);
214
215 template <typename EventType>
216 void propagateEventToStateReactors(ISmaccState * st, EventType * ev);
217
218 void updateStatusMessage();
219
220 friend class ISmaccState;
221 friend class SignalDetector;
222};
223} // namespace smacc2
224
std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > orthogonals_
std::vector< ISmaccState * > currentState_
std::recursive_mutex eventQueueMutex_
bool getGlobalSMData(std::string name, T &ret)
void publishTransition(const SmaccTransitionInfo &transitionInfo)
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
StateMachineInternalAction stateMachineCurrentAction
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_
void notifyOnStateExitting(StateType *state)
rclcpp::Service< smacc2_msgs::srv::SmaccGetTransitionHistory >::SharedPtr transitionHistoryService_
void setGlobalSMData(std::string name, T value)
virtual void onInitialize()
this function should be implemented by the user to create the orthogonals
std::vector< smacc2_msgs::msg::SmaccTransitionLogEntry > transitionLogHistory_
void notifyOnRuntimeConfigurationFinished(StateType *state)
const std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > & getOrthogonals() const
rclcpp::Publisher< smacc2_msgs::msg::SmaccStatus >::SharedPtr stateMachineStatusPub_
void notifyOnStateExited(StateType *state)
void lockStateMachine(std::string msg)
void notifyOnStateEntryEnd(StateType *state)
void propagateEventToStateReactors(ISmaccState *st, EventType *ev)
void disconnectSmaccSignalObject(void *object)
const SmaccStateMachineInfo & getStateMachineInfo()
rclcpp::Node::SharedPtr nh_
std::shared_ptr< SmaccStateInfo > getCurrentStateInfo()
void unlockStateMachine(std::string msg)
rclcpp::TimerBase::SharedPtr timer_
rclcpp::Publisher< smacc2_msgs::msg::SmaccTransitionLogEntry >::SharedPtr transitionLogPub_
std::map< void *, std::shared_ptr< CallbackCounterSemaphore > > stateCallbackConnections
void notifyOnRuntimeConfigured(StateType *state)
void initializeROS(std::string smshortname)
smacc2_msgs::msg::SmaccStatus status_msg_
std::shared_ptr< SmaccStateMachineInfo > stateMachineInfo_
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
std::recursive_mutex & getMutex()
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 requiresComponent(SmaccComponentType *&storage, bool throwsException=false)
void notifyOnStateEntryStart(StateType *state)
SMRunMode
Definition: common.hpp:68