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