SMACC2
Loading...
Searching...
No Matches
lifecyclenode_client.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 <lifecycle_msgs/msg/transition_event.hpp>
23#include <lifecycle_msgs/srv/change_state.hpp>
24#include <lifecycle_msgs/srv/get_state.hpp>
25#include <smacc2/smacc.hpp>
27
28namespace cl_lifecyclenode
29{
30template <typename TSourceObject, typename TOrthogonal>
32: sc::event<EvLifecycleNodeChangeState<TSourceObject, TOrthogonal>>
33{
34 uint8_t transition;
35};
36
37template <typename TSourceObject, typename TOrthogonal>
38struct EvTransitionCreate : public sc::event<EvTransitionCreate<TSourceObject, TOrthogonal>>
39{
40};
41
42template <typename TSourceObject, typename TOrthogonal>
43struct EvTransitionConfigure : public sc::event<EvTransitionConfigure<TSourceObject, TOrthogonal>>
44{
45};
46
47template <typename TSourceObject, typename TOrthogonal>
48struct EvTransitionCleanup : sc::event<EvTransitionCleanup<TSourceObject, TOrthogonal>>
49{
50};
51
52template <typename TSourceObject, typename TOrthogonal>
53struct EvTransitionActivate : sc::event<EvTransitionActivate<TSourceObject, TOrthogonal>>
54{
55};
56
57template <typename TSourceObject, typename TOrthogonal>
58struct EvTransitionDeactivate : sc::event<EvTransitionDeactivate<TSourceObject, TOrthogonal>>
59{
60};
61
62template <typename TSourceObject, typename TOrthogonal>
64: sc::event<EvTransitionUnconfiguredShutdown<TSourceObject, TOrthogonal>>
65{
66};
67
68template <typename TSourceObject, typename TOrthogonal>
70: sc::event<EvTransitionInactiveShutdown<TSourceObject, TOrthogonal>>
71{
72};
73
74template <typename TSourceObject, typename TOrthogonal>
76: sc::event<EvTransitionActiveShutdown<TSourceObject, TOrthogonal>>
77{
78};
79
80template <typename TSourceObject, typename TOrthogonal>
81struct EvTransitionDestroy : sc::event<EvTransitionDestroy<TSourceObject, TOrthogonal>>
82{
83};
84
85template <typename TSourceObject, typename TOrthogonal>
87: sc::event<EvTransitionOnConfigureSuccess<TSourceObject, TOrthogonal>>
88{
89};
90
91template <typename TSourceObject, typename TOrthogonal>
93: sc::event<EvTransitionOnConfigureFailure<TSourceObject, TOrthogonal>>
94{
95};
96
97template <typename TSourceObject, typename TOrthogonal>
99: sc::event<EvTransitionOnConfigureError<TSourceObject, TOrthogonal>>
100{
101};
102
103template <typename TSourceObject, typename TOrthogonal>
105: sc::event<EvTransitionOnActivateSuccess<TSourceObject, TOrthogonal>>
106{
107};
108
109template <typename TSourceObject, typename TOrthogonal>
111: sc::event<EvTransitionOnActivateFailure<TSourceObject, TOrthogonal>>
112{
113};
114
115template <typename TSourceObject, typename TOrthogonal>
117: sc::event<EvTransitionOnActivateError<TSourceObject, TOrthogonal>>
118{
119};
120
121template <typename TSourceObject, typename TOrthogonal>
123: sc::event<EvTransitionOnDeactivateSuccess<TSourceObject, TOrthogonal>>
124{
125};
126
127template <typename TSourceObject, typename TOrthogonal>
129: sc::event<EvTransitionOnDeactivateFailure<TSourceObject, TOrthogonal>>
130{
131};
132
133template <typename TSourceObject, typename TOrthogonal>
135: sc::event<EvTransitionOnDeactivateError<TSourceObject, TOrthogonal>>
136{
137};
138
139template <typename TSourceObject, typename TOrthogonal>
141: sc::event<EvTransitionOnCleanupSuccess<TSourceObject, TOrthogonal>>
142{
143};
144
145template <typename TSourceObject, typename TOrthogonal>
147: sc::event<EvTransitionOnCleanupFailure<TSourceObject, TOrthogonal>>
148{
149};
150
151template <typename TSourceObject, typename TOrthogonal>
153: sc::event<EvTransitionOnCleanupError<TSourceObject, TOrthogonal>>
154{
155};
156
157template <typename TSourceObject, typename TOrthogonal>
159: sc::event<EvTransitionOnShutdownSuccess<TSourceObject, TOrthogonal>>
160{
161};
162
163template <typename TSourceObject, typename TOrthogonal>
165: sc::event<EvTransitionOnShutdownFailure<TSourceObject, TOrthogonal>>
166{
167};
168
169template <typename TSourceObject, typename TOrthogonal>
171: sc::event<EvTransitionOnShutdownError<TSourceObject, TOrthogonal>>
172{
173};
174
175template <typename TSourceObject, typename TOrthogonal>
177: sc::event<EvTransitionOnErrorSuccess<TSourceObject, TOrthogonal>>
178{
179};
180
181template <typename TSourceObject, typename TOrthogonal>
183: sc::event<EvTransitionOnErrorFailure<TSourceObject, TOrthogonal>>
184{
185};
186
187template <typename TSourceObject, typename TOrthogonal>
188struct EvTransitionOnErrorError : sc::event<EvTransitionOnErrorError<TSourceObject, TOrthogonal>>
189{
190};
191
193{
194public:
195 ClLifecycleNode(std::string nodeName);
196
197 virtual ~ClLifecycleNode();
198
199 void onInitialize() override;
200
201 // @brief execute this method to trigger a transition to some state
202 void changeState(uint8_t state);
203
204 // @brief execute this method to trigger the configure transition
205 void configure();
206
207 // @brief execute this method to trigger the activate transition
208 void activate();
209
210 // @brief execute this method to trigger the deactivate transition
211 void deactivate();
212
213 // @brief execute this method to trigger the cleanup transition
214 void cleanup();
215
216 // @brief execute this method to trigger the shutdown transition
217 void shutdown();
218
219 // @brief execute this method to trigger the destroy transition
220 void destroy();
221
222 // @brief execute this method to trigger the create transition
223 virtual void onTransitionEvent(const lifecycle_msgs::msg::TransitionEvent::SharedPtr msg);
224
225 boost::signals2::signal<void(const lifecycle_msgs::msg::TransitionEvent::SharedPtr)>
227
228 boost::signals2::signal<void()> onTransitionCreate_;
229 boost::signals2::signal<void()> onTransitionConfigure_;
230 boost::signals2::signal<void()> onTransitionCleanup_;
231 boost::signals2::signal<void()> onTransitionActivate_;
232 boost::signals2::signal<void()> onTransitionDeactivate_;
233
234 boost::signals2::signal<void()> onTransitionUnconfiguredShutdown_;
235 boost::signals2::signal<void()> onTransitionInactiveShutdown_;
236 boost::signals2::signal<void()> onTransitionActiveShutdown_;
237
238 boost::signals2::signal<void()> onTransitionDestroy_;
239
240 boost::signals2::signal<void()> onTransitionOnConfigureSuccess_;
241 boost::signals2::signal<void()> onTransitionOnConfigureFailure_;
242 boost::signals2::signal<void()> onTransitionOnConfigureError_;
243
244 boost::signals2::signal<void()> onTransitionOnActivateSuccess_;
245 boost::signals2::signal<void()> onTransitionOnActivateFailure_;
246 boost::signals2::signal<void()> onTransitionOnActivateError_;
247
248 boost::signals2::signal<void()> onTransitionOnDeactivateSuccess_;
249 boost::signals2::signal<void()> onTransitionOnDeactivateFailure_;
250 boost::signals2::signal<void()> onTransitionOnDeactivateError_;
251
252 boost::signals2::signal<void()> onTransitionOnCleanupSuccess_;
253 boost::signals2::signal<void()> onTransitionOnCleanupFailure_;
254 boost::signals2::signal<void()> onTransitionOnCleanupError_;
255
256 boost::signals2::signal<void()> onTransitionOnShutdownSuccess_;
257 boost::signals2::signal<void()> onTransitionOnShutdownFailure_;
258 boost::signals2::signal<void()> onTransitionOnShutdownError_;
259
260 boost::signals2::signal<void()> onTransitionOnActiveShutdownSuccess_;
261 boost::signals2::signal<void()> onTransitionOnActiveShutdownFailure_;
262 boost::signals2::signal<void()> onTransitionOnActiveShutdownError_;
263
264 boost::signals2::signal<void()> onTransitionOnErrorSuccess_;
265 boost::signals2::signal<void()> onTransitionOnErrorFailure_;
266 boost::signals2::signal<void()> onTransitionOnErrorError_;
267
268 template <typename TOrthogonal, typename TSourceObject>
270 {
271 this->postOnTransitionCreate_ = [=]()
272 {
273 RCLCPP_INFO(this->getLogger(), "postOnTransitionCreate_");
274 this->onTransitionCreate_();
277 this->postEvent(ev);
278 };
279
280 this->postOnTransitionConfigure_ = [=]()
281 {
283 this->postEvent<EvTransitionConfigure<TSourceObject, TOrthogonal>>();
284 };
285
286 this->postOnTransitionActivate_ = [=]()
287 {
288 this->onTransitionActivate_();
289 this->postEvent<EvTransitionActivate<TSourceObject, TOrthogonal>>();
290 };
291
292 this->postOnTransitionDeactivate_ = [=]()
293 {
295 this->postEvent<EvTransitionDeactivate<TSourceObject, TOrthogonal>>();
296 };
297
299 {
301 this->postEvent<EvTransitionUnconfiguredShutdown<TSourceObject, TOrthogonal>>();
302 };
303
305 {
307 this->postEvent<EvTransitionInactiveShutdown<TSourceObject, TOrthogonal>>();
308 };
309
311 {
313 this->postEvent<EvTransitionActiveShutdown<TSourceObject, TOrthogonal>>();
314 };
315
316 this->postOnTransitionDestroy_ = [=]()
317 {
318 this->onTransitionDestroy_();
319 this->postEvent<EvTransitionDestroy<TSourceObject, TOrthogonal>>();
320 };
321
323 {
325 this->postEvent<EvTransitionOnConfigureSuccess<TSourceObject, TOrthogonal>>();
326 };
327
329 {
331 this->postEvent<EvTransitionOnConfigureFailure<TSourceObject, TOrthogonal>>();
332 };
333
335 {
337 this->postEvent<EvTransitionOnConfigureError<TSourceObject, TOrthogonal>>();
338 };
339
341 {
343 this->postEvent<EvTransitionOnActivateSuccess<TSourceObject, TOrthogonal>>();
344 };
345
347 {
349 this->postEvent<EvTransitionOnActivateFailure<TSourceObject, TOrthogonal>>();
350 };
351
353 {
355 this->postEvent<EvTransitionOnActivateError<TSourceObject, TOrthogonal>>();
356 };
357
359 {
361 this->postEvent<EvTransitionOnDeactivateSuccess<TSourceObject, TOrthogonal>>();
362 };
363
365 {
367 this->postEvent<EvTransitionOnDeactivateFailure<TSourceObject, TOrthogonal>>();
368 };
369
371 {
373 this->postEvent<EvTransitionOnDeactivateError<TSourceObject, TOrthogonal>>();
374 };
375
376 this->postOnTransitionCleanup_ = [=]()
377 {
378 this->onTransitionCleanup_();
379 this->postEvent<EvTransitionCleanup<TSourceObject, TOrthogonal>>();
380 };
381
383 {
385 this->postEvent<EvTransitionOnCleanupSuccess<TSourceObject, TOrthogonal>>();
386 };
387
389 {
391 this->postEvent<EvTransitionOnCleanupFailure<TSourceObject, TOrthogonal>>();
392 };
393
395 {
397 this->postEvent<EvTransitionOnCleanupError<TSourceObject, TOrthogonal>>();
398 };
399
401 {
403 this->postEvent<EvTransitionOnShutdownSuccess<TSourceObject, TOrthogonal>>();
404 };
405
407 {
409 this->postEvent<EvTransitionOnShutdownFailure<TSourceObject, TOrthogonal>>();
410 };
411
413 {
415 this->postEvent<EvTransitionOnShutdownError<TSourceObject, TOrthogonal>>();
416 };
417
419 {
421 this->postEvent<EvTransitionOnErrorSuccess<TSourceObject, TOrthogonal>>();
422 };
423
425 {
427 this->postEvent<EvTransitionOnErrorFailure<TSourceObject, TOrthogonal>>();
428 };
429
431 {
433 this->postEvent<EvTransitionOnErrorError<TSourceObject, TOrthogonal>>();
434 };
435 }
436
437private:
438 rclcpp::Client<lifecycle_msgs::srv::GetState>::SharedPtr client_get_state_;
439 rclcpp::Client<lifecycle_msgs::srv::ChangeState>::SharedPtr client_change_state_;
440
441 rclcpp::Subscription<lifecycle_msgs::msg::TransitionEvent>::SharedPtr
443
444 std::string nodeName_;
445 const std::string node_get_state_topic = "/get_state";
446 const std::string node_change_state_topic = "/change_state";
447 const std::string node_transition_event_topic = "/transition_event";
448
449 lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_;
450
451 std::function<void()> postOnTransitionCreate_;
452 std::function<void()> postOnTransitionConfigure_;
453 std::function<void()> postOnTransitionCleanup_;
454 std::function<void()> postOnTransitionActivate_;
455 std::function<void()> postOnTransitionDeactivate_;
458 std::function<void()> postOnTransitionActiveShutdown_;
459 std::function<void()> postOnTransitionDestroy_;
460
464
467 std::function<void()> postOnTransitionOnActivateError_;
468
472
475 std::function<void()> postOnTransitionOnCleanupError_;
476
479 std::function<void()> postOnTransitionOnShutdownError_;
480
481 std::function<void()> postOnTransitionOnErrorSuccess_;
482 std::function<void()> postOnTransitionOnErrorFailure_;
483 std::function<void()> postOnTransitionOnErrorError_;
484};
485
486} // namespace cl_lifecyclenode
std::function< void()> postOnTransitionActivate_
virtual void onTransitionEvent(const lifecycle_msgs::msg::TransitionEvent::SharedPtr msg)
boost::signals2::signal< void()> onTransitionOnShutdownSuccess_
std::function< void()> postOnTransitionOnConfigureFailure_
boost::signals2::signal< void()> onTransitionOnErrorSuccess_
std::function< void()> postOnTransitionOnDeactivateFailure_
boost::signals2::signal< void()> onTransitionInactiveShutdown_
boost::signals2::signal< void()> onTransitionOnActiveShutdownSuccess_
boost::signals2::signal< void()> onTransitionDeactivate_
boost::signals2::signal< void()> onTransitionUnconfiguredShutdown_
boost::signals2::signal< void()> onTransitionCreate_
boost::signals2::signal< void()> onTransitionOnDeactivateFailure_
boost::signals2::signal< void()> onTransitionOnShutdownError_
boost::signals2::signal< void()> onTransitionOnActivateSuccess_
boost::signals2::signal< void()> onTransitionOnDeactivateSuccess_
std::function< void()> postOnTransitionOnCleanupFailure_
boost::signals2::signal< void()> onTransitionActivate_
boost::signals2::signal< void()> onTransitionOnActivateError_
std::function< void()> postOnTransitionConfigure_
boost::signals2::signal< void()> onTransitionOnConfigureFailure_
boost::signals2::signal< void()> onTransitionOnErrorFailure_
std::function< void()> postOnTransitionUnconfiguredShutdown_
boost::signals2::signal< void()> onTransitionOnActiveShutdownError_
rclcpp::Client< lifecycle_msgs::srv::GetState >::SharedPtr client_get_state_
boost::signals2::signal< void()> onTransitionOnConfigureSuccess_
std::function< void()> postOnTransitionOnConfigureSuccess_
boost::signals2::signal< void()> onTransitionActiveShutdown_
rclcpp::Subscription< lifecycle_msgs::msg::TransitionEvent >::SharedPtr subscription_transition_event_
boost::signals2::signal< void()> onTransitionOnErrorError_
std::function< void()> postOnTransitionOnErrorError_
lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_
rclcpp::Client< lifecycle_msgs::srv::ChangeState >::SharedPtr client_change_state_
boost::signals2::signal< void()> onTransitionDestroy_
boost::signals2::signal< void(const lifecycle_msgs::msg::TransitionEvent::SharedPtr)> onTransitionEventSignal
std::function< void()> postOnTransitionOnErrorSuccess_
std::function< void()> postOnTransitionOnCleanupSuccess_
boost::signals2::signal< void()> onTransitionCleanup_
std::function< void()> postOnTransitionOnCleanupError_
std::function< void()> postOnTransitionInactiveShutdown_
boost::signals2::signal< void()> onTransitionOnActiveShutdownFailure_
std::function< void()> postOnTransitionCleanup_
std::function< void()> postOnTransitionOnShutdownError_
std::function< void()> postOnTransitionOnErrorFailure_
std::function< void()> postOnTransitionOnDeactivateSuccess_
std::function< void()> postOnTransitionDeactivate_
std::function< void()> postOnTransitionOnShutdownFailure_
std::function< void()> postOnTransitionOnConfigureError_
boost::signals2::signal< void()> onTransitionOnConfigureError_
boost::signals2::signal< void()> onTransitionOnActivateFailure_
std::function< void()> postOnTransitionOnActivateFailure_
std::function< void()> postOnTransitionOnShutdownSuccess_
boost::signals2::signal< void()> onTransitionOnCleanupSuccess_
boost::signals2::signal< void()> onTransitionOnDeactivateError_
std::function< void()> postOnTransitionDestroy_
boost::signals2::signal< void()> onTransitionOnShutdownFailure_
boost::signals2::signal< void()> onTransitionOnCleanupFailure_
std::function< void()> postOnTransitionCreate_
std::function< void()> postOnTransitionActiveShutdown_
boost::signals2::signal< void()> onTransitionConfigure_
std::function< void()> postOnTransitionOnActivateSuccess_
boost::signals2::signal< void()> onTransitionOnCleanupError_
std::function< void()> postOnTransitionOnDeactivateError_
std::function< void()> postOnTransitionOnActivateError_
rclcpp::Logger getLogger()