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 // gets the client behavior in a given orthogonal
81 // the index is used to distinguish between multiple client behaviors of the same type
82 template <typename TOrthogonal, typename TClientBehavior>
83 inline TClientBehavior * getClientBehavior(int index = 0)
84 {
85 auto orthogonal = this->template getOrthogonal<TOrthogonal>();
86
87 return orthogonal->template getClientBehavior<TClientBehavior>(index);
88 }
89
90 const std::map<std::string, std::shared_ptr<smacc2::ISmaccOrthogonal>> & getOrthogonals() const;
91
92 template <typename SmaccComponentType>
93 void requiresComponent(SmaccComponentType *& storage, bool throwsExceptionIfNotExist = false);
94
95 template <typename EventType>
96 void postEvent(EventType * ev, EventLifeTime evlifetime = EventLifeTime::ABSOLUTE);
97
98 template <typename EventType>
100
101 // gets data from the state machine blackboard
102 template <typename T>
103 bool getGlobalSMData(std::string name, T & ret);
104
105 // sets data in the state machine blackboard
106 template <typename T>
107 void setGlobalSMData(std::string name, T value);
108
109 template <typename StateField, typename BehaviorType>
110 void mapBehavior();
111
112 std::string getStateMachineName();
113
115
116 inline std::shared_ptr<SmaccStateInfo> getCurrentStateInfo() { return currentStateInfo_; }
117
118 void publishTransition(const SmaccTransitionInfo & transitionInfo);
119
121 virtual void onInitialize();
122
124 const std::shared_ptr<rmw_request_id_t> request_header,
125 const std::shared_ptr<smacc2_msgs::srv::SmaccGetTransitionHistory::Request> req,
126 std::shared_ptr<smacc2_msgs::srv::SmaccGetTransitionHistory::Response> res);
127
128 template <typename TSmaccSignal, typename TMemberFunctionPrototype, typename TSmaccObjectType>
129 boost::signals2::connection createSignalConnection(
130 TSmaccSignal & signal, TMemberFunctionPrototype callback, TSmaccObjectType * object);
131
132 void disconnectSmaccSignalObject(void * object);
133
134 template <typename StateType>
135 void notifyOnStateEntryStart(StateType * state);
136
137 template <typename StateType>
138 void notifyOnStateEntryEnd(StateType * state);
139
140 template <typename StateType>
141 void notifyOnRuntimeConfigured(StateType * state);
142
143 template <typename StateType>
144 void notifyOnStateExitting(StateType * state);
145
146 template <typename StateType>
147 void notifyOnStateExited(StateType * state);
148
149 template <typename StateType>
150 void notifyOnRuntimeConfigurationFinished(StateType * state);
151
152 inline uint64_t getCurrentStateCounter() const;
153
154 inline ISmaccState * getCurrentState() const;
155
157
158 template <typename InitialStateType>
160
161 rclcpp::Node::SharedPtr getNode();
162
163 inline rclcpp::Logger getLogger() { return nh_->get_logger(); }
164
165 inline std::recursive_mutex & getMutex() { return this->m_mutex_; }
166
167protected:
169
170 void initializeROS(std::string smshortname);
171
172 void onInitialized();
173
174 template <typename TOrthogonal>
175 void createOrthogonal();
176
177 // The node handle for this state
178 rclcpp::Node::SharedPtr nh_;
179
180 rclcpp::TimerBase::SharedPtr timer_;
181 rclcpp::Publisher<smacc2_msgs::msg::SmaccStateMachine>::SharedPtr stateMachinePub_;
182 rclcpp::Publisher<smacc2_msgs::msg::SmaccStatus>::SharedPtr stateMachineStatusPub_;
183 rclcpp::Publisher<smacc2_msgs::msg::SmaccTransitionLogEntry>::SharedPtr transitionLogPub_;
184 rclcpp::Service<smacc2_msgs::srv::SmaccGetTransitionHistory>::SharedPtr transitionHistoryService_;
185
186 // if it is null, you may be located in a transition. There is a small gap of time where internally
187 // this currentState_ is null. This may change in the future.
188 std::vector<ISmaccState *> currentState_;
189
190 std::shared_ptr<SmaccStateInfo> currentStateInfo_;
191
192 smacc2_msgs::msg::SmaccStatus status_msg_;
193
194 // orthogonals
195 std::map<std::string, std::shared_ptr<smacc2::ISmaccOrthogonal>> orthogonals_;
196
197 std::vector<boost::signals2::scoped_connection> longLivedSignalConnections_;
198
199protected:
200 std::shared_ptr<SmaccStateMachineInfo> stateMachineInfo_;
201
202private:
203 std::recursive_mutex m_mutex_;
204 std::recursive_mutex eventQueueMutex_;
205
207
208 std::map<void *, std::shared_ptr<CallbackCounterSemaphore>> stateCallbackConnections;
209
210 // shared variables
211 std::map<std::string, std::pair<std::function<std::string()>, boost::any>> globalData_;
212
213 // contains the whole history of transitions of the state machine
214 std::vector<smacc2_msgs::msg::SmaccTransitionLogEntry> transitionLogHistory_;
215
217
218 // Event to notify to the signaldetection thread that a request has been created...
220
222
223 void lockStateMachine(std::string msg);
224
225 void unlockStateMachine(std::string msg);
226
227 template <typename EventType>
228 void propagateEventToStateReactors(ISmaccState * st, EventType * ev);
229
230 void updateStatusMessage();
231
232 friend class ISmaccState;
233 friend class SignalDetector;
234};
235} // namespace smacc2
236
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_
TClientBehavior * getClientBehavior(int index=0)
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_
void requiresComponent(SmaccComponentType *&storage, bool throwsExceptionIfNotExist=false)
std::vector< boost::signals2::scoped_connection > longLivedSignalConnections_
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 notifyOnStateEntryStart(StateType *state)
SMRunMode
Definition: common.hpp:68