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