SMACC2
Loading...
Searching...
No Matches
cp_lifecycle_event_monitor.hpp
Go to the documentation of this file.
1// Copyright 2024 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#pragma once
16
17#include <smacc2/component.hpp>
19
20#include <lifecycle_msgs/msg/transition_event.hpp>
21#include <rclcpp/rclcpp.hpp>
22
23#include <functional>
24#include <mutex>
25#include <optional>
26#include <string>
27
28// Forward declarations
29namespace cl_lifecycle_node
30{
31class CpLifecycleEventMonitor;
32
33// Event declarations - component is now the source
34template <typename TSourceObject, typename TOrthogonal>
36template <typename TSourceObject, typename TOrthogonal>
38template <typename TSourceObject, typename TOrthogonal>
40template <typename TSourceObject, typename TOrthogonal>
42template <typename TSourceObject, typename TOrthogonal>
44template <typename TSourceObject, typename TOrthogonal>
46template <typename TSourceObject, typename TOrthogonal>
48template <typename TSourceObject, typename TOrthogonal>
50template <typename TSourceObject, typename TOrthogonal>
52template <typename TSourceObject, typename TOrthogonal>
54template <typename TSourceObject, typename TOrthogonal>
56template <typename TSourceObject, typename TOrthogonal>
58template <typename TSourceObject, typename TOrthogonal>
60template <typename TSourceObject, typename TOrthogonal>
62template <typename TSourceObject, typename TOrthogonal>
64template <typename TSourceObject, typename TOrthogonal>
66template <typename TSourceObject, typename TOrthogonal>
68template <typename TSourceObject, typename TOrthogonal>
70template <typename TSourceObject, typename TOrthogonal>
72template <typename TSourceObject, typename TOrthogonal>
74template <typename TSourceObject, typename TOrthogonal>
76template <typename TSourceObject, typename TOrthogonal>
78template <typename TSourceObject, typename TOrthogonal>
80template <typename TSourceObject, typename TOrthogonal>
82template <typename TSourceObject, typename TOrthogonal>
84} // namespace cl_lifecycle_node
85
86namespace cl_lifecycle_node
87{
99{
100public:
105 CpLifecycleEventMonitor(std::string nodeName);
106
107 virtual ~CpLifecycleEventMonitor() = default;
108
115 {
116 owner_ = owner;
117 stateMachine_ = stateMachine;
118 }
119
120 // SMACC2 Signals for lifecycle transition events
121 // Transition initiation signals
131
132 // Configure transition result signals
136
137 // Activate transition result signals
141
142 // Deactivate transition result signals
146
147 // Cleanup transition result signals
151
152 // Shutdown transition result signals
156
157 // Active shutdown specific signals
161
162 // Error processing result signals
166
171 std::optional<lifecycle_msgs::msg::TransitionEvent> getLastTransitionEvent() const;
172
176 void onInitialize() override;
177
182 template <typename TOrthogonal, typename TClient>
184 {
185 // Set up event posting lambdas with correct template parameters
186 // Component is the event source (TSourceObject = CpLifecycleEventMonitor)
187
188 postEventConfigure_ = [this]()
190
191 postEventActivate_ = [this]()
193
194 postEventDeactivate_ = [this]()
196
197 postEventCleanup_ = [this]()
199
202
205
206 postEventActiveShutdown_ = [this]()
208
209 postEventDestroy_ = [this]()
211
214
217
220
223
226
229
232
235
238
241
244
245 postEventOnCleanupError_ = [this]()
247
250
253
256
257 postEventOnErrorSuccess_ = [this]()
259
260 postEventOnErrorFailure_ = [this]()
262 }
263
264private:
265 std::string nodeName_;
266 rclcpp::Subscription<lifecycle_msgs::msg::TransitionEvent>::SharedPtr subscription_;
267 lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_;
268 mutable std::mutex eventMutex_;
269
274 void onTransitionEvent(const lifecycle_msgs::msg::TransitionEvent::SharedPtr msg);
275
276 // Event posting lambdas - configured via onStateOrthogonalAllocation<TOrthogonal, TClient>()
277 std::function<void()> postEventConfigure_;
278 std::function<void()> postEventActivate_;
279 std::function<void()> postEventDeactivate_;
280 std::function<void()> postEventCleanup_;
281 std::function<void()> postEventUnconfiguredShutdown_;
282 std::function<void()> postEventInactiveShutdown_;
283 std::function<void()> postEventActiveShutdown_;
284 std::function<void()> postEventDestroy_;
285
286 std::function<void()> postEventOnConfigureSuccess_;
287 std::function<void()> postEventOnConfigureFailure_;
288 std::function<void()> postEventOnConfigureError_;
289
290 std::function<void()> postEventOnActivateSuccess_;
291 std::function<void()> postEventOnActivateFailure_;
292 std::function<void()> postEventOnActivateError_;
293
294 std::function<void()> postEventOnDeactivateSuccess_;
295 std::function<void()> postEventOnDeactivateFailure_;
296 std::function<void()> postEventOnDeactivateError_;
297
298 std::function<void()> postEventOnCleanupSuccess_;
299 std::function<void()> postEventOnCleanupFailure_;
300 std::function<void()> postEventOnCleanupError_;
301
302 std::function<void()> postEventOnShutdownSuccess_;
303 std::function<void()> postEventOnShutdownFailure_;
304 std::function<void()> postEventOnShutdownError_;
305
306 std::function<void()> postEventOnErrorSuccess_;
307 std::function<void()> postEventOnErrorFailure_;
308};
309
310} // namespace cl_lifecycle_node
Component that monitors ROS2 lifecycle node transition events.
smacc2::SmaccSignal< void()> onTransitionOnActiveShutdownSuccess_
rclcpp::Subscription< lifecycle_msgs::msg::TransitionEvent >::SharedPtr subscription_
CpLifecycleEventMonitor(std::string nodeName)
Constructor with node name.
void onStateOrthogonalAllocation()
Configure event posting with orthogonal template parameter This is called from the client's onStateOr...
void onTransitionEvent(const lifecycle_msgs::msg::TransitionEvent::SharedPtr msg)
Callback for transition event subscription.
lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_
smacc2::SmaccSignal< void()> onTransitionOnActiveShutdownFailure_
std::optional< lifecycle_msgs::msg::TransitionEvent > getLastTransitionEvent() const
Get the last received transition event.
void setOwner(smacc2::ISmaccClient *owner, smacc2::ISmaccStateMachine *stateMachine)
Set the owner client and state machine for this component.
void onInitialize() override
Component initialization - creates subscription.
ISmaccClient * owner_
Definition component.hpp:88
ISmaccStateMachine * stateMachine_
Definition component.hpp:86