SMACC2
Loading...
Searching...
No Matches
cl_lifecycle_node.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft 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
23#include <lifecycle_msgs/msg/transition_event.hpp>
24#include <lifecycle_msgs/srv/change_state.hpp>
25#include <lifecycle_msgs/srv/get_state.hpp>
27#include <smacc2/smacc.hpp>
29
30#include <functional>
31
33{
34template <typename TSourceObject, typename TOrthogonal>
36: sc::event<EvLifecycleNodeChangeState<TSourceObject, TOrthogonal>>
37{
38 uint8_t transition;
39};
40
41template <typename TSourceObject, typename TOrthogonal>
42struct EvTransitionCreate : public sc::event<EvTransitionCreate<TSourceObject, TOrthogonal>>
43{
44};
45
46template <typename TSourceObject, typename TOrthogonal>
47struct EvTransitionConfigure : public sc::event<EvTransitionConfigure<TSourceObject, TOrthogonal>>
48{
49};
50
51template <typename TSourceObject, typename TOrthogonal>
52struct EvTransitionCleanup : sc::event<EvTransitionCleanup<TSourceObject, TOrthogonal>>
53{
54};
55
56template <typename TSourceObject, typename TOrthogonal>
57struct EvTransitionActivate : sc::event<EvTransitionActivate<TSourceObject, TOrthogonal>>
58{
59};
60
61template <typename TSourceObject, typename TOrthogonal>
62struct EvTransitionDeactivate : sc::event<EvTransitionDeactivate<TSourceObject, TOrthogonal>>
63{
64};
65
66template <typename TSourceObject, typename TOrthogonal>
67struct EvTransitionUnconfiguredShutdown
68: sc::event<EvTransitionUnconfiguredShutdown<TSourceObject, TOrthogonal>>
69{
70};
71
72template <typename TSourceObject, typename TOrthogonal>
73struct EvTransitionInactiveShutdown
74: sc::event<EvTransitionInactiveShutdown<TSourceObject, TOrthogonal>>
75{
76};
77
78template <typename TSourceObject, typename TOrthogonal>
79struct EvTransitionActiveShutdown
80: sc::event<EvTransitionActiveShutdown<TSourceObject, TOrthogonal>>
81{
82};
83
84template <typename TSourceObject, typename TOrthogonal>
85struct EvTransitionDestroy : sc::event<EvTransitionDestroy<TSourceObject, TOrthogonal>>
86{
87};
88
89template <typename TSourceObject, typename TOrthogonal>
90struct EvTransitionOnConfigureSuccess
91: sc::event<EvTransitionOnConfigureSuccess<TSourceObject, TOrthogonal>>
92{
93};
94
95template <typename TSourceObject, typename TOrthogonal>
96struct EvTransitionOnConfigureFailure
97: sc::event<EvTransitionOnConfigureFailure<TSourceObject, TOrthogonal>>
98{
99};
100
101template <typename TSourceObject, typename TOrthogonal>
102struct EvTransitionOnConfigureError
103: sc::event<EvTransitionOnConfigureError<TSourceObject, TOrthogonal>>
104{
105};
106
107template <typename TSourceObject, typename TOrthogonal>
108struct EvTransitionOnActivateSuccess
109: sc::event<EvTransitionOnActivateSuccess<TSourceObject, TOrthogonal>>
110{
111};
112
113template <typename TSourceObject, typename TOrthogonal>
114struct EvTransitionOnActivateFailure
115: sc::event<EvTransitionOnActivateFailure<TSourceObject, TOrthogonal>>
116{
117};
118
119template <typename TSourceObject, typename TOrthogonal>
120struct EvTransitionOnActivateError
121: sc::event<EvTransitionOnActivateError<TSourceObject, TOrthogonal>>
122{
123};
124
125template <typename TSourceObject, typename TOrthogonal>
126struct EvTransitionOnDeactivateSuccess
127: sc::event<EvTransitionOnDeactivateSuccess<TSourceObject, TOrthogonal>>
128{
129};
130
131template <typename TSourceObject, typename TOrthogonal>
132struct EvTransitionOnDeactivateFailure
133: sc::event<EvTransitionOnDeactivateFailure<TSourceObject, TOrthogonal>>
134{
135};
136
137template <typename TSourceObject, typename TOrthogonal>
138struct EvTransitionOnDeactivateError
139: sc::event<EvTransitionOnDeactivateError<TSourceObject, TOrthogonal>>
140{
141};
142
143template <typename TSourceObject, typename TOrthogonal>
144struct EvTransitionOnCleanupSuccess
145: sc::event<EvTransitionOnCleanupSuccess<TSourceObject, TOrthogonal>>
146{
147};
148
149template <typename TSourceObject, typename TOrthogonal>
150struct EvTransitionOnCleanupFailure
151: sc::event<EvTransitionOnCleanupFailure<TSourceObject, TOrthogonal>>
152{
153};
154
155template <typename TSourceObject, typename TOrthogonal>
156struct EvTransitionOnCleanupError
157: sc::event<EvTransitionOnCleanupError<TSourceObject, TOrthogonal>>
158{
159};
160
161template <typename TSourceObject, typename TOrthogonal>
162struct EvTransitionOnShutdownSuccess
163: sc::event<EvTransitionOnShutdownSuccess<TSourceObject, TOrthogonal>>
164{
165};
166
167template <typename TSourceObject, typename TOrthogonal>
168struct EvTransitionOnShutdownFailure
169: sc::event<EvTransitionOnShutdownFailure<TSourceObject, TOrthogonal>>
170{
171};
172
173template <typename TSourceObject, typename TOrthogonal>
174struct EvTransitionOnShutdownError
175: sc::event<EvTransitionOnShutdownError<TSourceObject, TOrthogonal>>
176{
177};
178
179template <typename TSourceObject, typename TOrthogonal>
180struct EvTransitionOnErrorSuccess
181: sc::event<EvTransitionOnErrorSuccess<TSourceObject, TOrthogonal>>
182{
183};
184
185template <typename TSourceObject, typename TOrthogonal>
186struct EvTransitionOnErrorFailure
187: sc::event<EvTransitionOnErrorFailure<TSourceObject, TOrthogonal>>
188{
189};
190
191template <typename TSourceObject, typename TOrthogonal>
192struct EvTransitionOnErrorError : sc::event<EvTransitionOnErrorError<TSourceObject, TOrthogonal>>
193{
194};
195
197{
198public:
199 ClLifecycleNode(std::string nodeName);
200
201 virtual ~ClLifecycleNode();
202
203 void onInitialize() override;
204
205 // Predicate polled between the 100 ms waits inside changeState(); returning true abandons the
206 // request. Behaviors pass their isShutdownRequested(), so a state exit or a process shutdown
207 // ends the wait instead of leaving the behavior thread blocked.
208 using CancelPredicate = std::function<bool()>;
209
210 // @brief trigger a lifecycle transition on the node. Waits until the change_state service has
211 // been discovered, sends the request and waits for the response, checking `cancelled` and
212 // rclcpp::ok() between waits. Returns true if the server accepted the transition. Returns false
213 // if the wait was abandoned (predicate true or ROS shut down) or the server rejected the
214 // transition; a rejection is logged at ERROR, and no transition event will follow it.
215 bool changeState(uint8_t transition, const CancelPredicate & cancelled = CancelPredicate());
216
217 // @brief execute this method to trigger the configure transition
218 bool configure(const CancelPredicate & cancelled = CancelPredicate());
219
220 // @brief execute this method to trigger the activate transition
221 bool activate(const CancelPredicate & cancelled = CancelPredicate());
222
223 // @brief execute this method to trigger the deactivate transition
224 bool deactivate(const CancelPredicate & cancelled = CancelPredicate());
225
226 // @brief execute this method to trigger the cleanup transition
227 bool cleanup(const CancelPredicate & cancelled = CancelPredicate());
228
229 // @brief trigger the shutdown transition that matches the node's current primary state
230 // (unconfigured, inactive or active), read through get_state; false if the state has none.
231 bool shutdown(const CancelPredicate & cancelled = CancelPredicate());
232
233 // @brief read the node's current lifecycle state id (lifecycle_msgs::msg::State constants).
234 // Same waits and cancellation as changeState(); returns false if abandoned.
235 bool getState(uint8_t & state, const CancelPredicate & cancelled = CancelPredicate());
236
240 template <typename TOrthogonal, typename TSourceObject>
242 {
243 // Create and register component with orthogonal
244 if (!eventMonitor_)
245 {
248 nodeName_);
249 }
250
251 // Configure component for this orthogonal
253 }
254
255private:
256 // Phase 2: Service clients (will be migrated to CpServiceClient components in future enhancement)
257 rclcpp::Client<lifecycle_msgs::srv::GetState>::SharedPtr client_get_state_;
258 rclcpp::Client<lifecycle_msgs::srv::ChangeState>::SharedPtr client_change_state_;
259
260 std::string nodeName_;
261
262 // Phase 3: Event monitor component
264 const std::string node_get_state_topic = "/get_state";
265 const std::string node_change_state_topic = "/change_state";
266
267 lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_;
268};
269
270} // namespace cl_lifecycle_node
rclcpp::Client< lifecycle_msgs::srv::ChangeState >::SharedPtr client_change_state_
bool changeState(uint8_t transition, const CancelPredicate &cancelled=CancelPredicate())
bool activate(const CancelPredicate &cancelled=CancelPredicate())
bool shutdown(const CancelPredicate &cancelled=CancelPredicate())
void onStateOrthogonalAllocation()
Configure component for event posting during orthogonal allocation.
bool deactivate(const CancelPredicate &cancelled=CancelPredicate())
bool cleanup(const CancelPredicate &cancelled=CancelPredicate())
rclcpp::Client< lifecycle_msgs::srv::GetState >::SharedPtr client_get_state_
lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_
bool getState(uint8_t &state, const CancelPredicate &cancelled=CancelPredicate())
CpLifecycleEventMonitor * eventMonitor_
bool configure(const CancelPredicate &cancelled=CancelPredicate())
Component that monitors ROS2 lifecycle node transition events.
SmaccComponentType * createComponent(const TArgs &... targs)