SMACC2
Loading...
Searching...
No Matches
lifecyclenode_client.cpp
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 ******************************************************************************************************************/
21#include <string>
22
23namespace cl_lifecyclenode
24{
25ClLifecycleNode::ClLifecycleNode(std::string node_name) : nodeName_(node_name) {}
26
28
30{
31 client_get_state_ = this->getNode()->create_client<lifecycle_msgs::srv::GetState>(
33 client_change_state_ = this->getNode()->create_client<lifecycle_msgs::srv::ChangeState>(
35
37 this->getNode() /* */->create_subscription<lifecycle_msgs::msg::TransitionEvent>(
39 std::bind(&ClLifecycleNode::onTransitionEvent, this, std::placeholders::_1));
40}
41
43{
44 auto request = std::make_shared<lifecycle_msgs::srv::ChangeState::Request>();
45 request->transition.id = state;
46 auto future = client_change_state_->async_send_request(request);
47 future.wait();
48}
49
51{
52 changeState(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE);
53}
54
56{
57 changeState(lifecycle_msgs::msg::Transition::TRANSITION_DEACTIVATE);
58}
59
61{
62 changeState(lifecycle_msgs::msg::Transition::TRANSITION_CONFIGURE);
63}
64
66{
67 changeState(lifecycle_msgs::msg::Transition::TRANSITION_CLEANUP);
68}
69
71{
72 changeState(lifecycle_msgs::msg::Transition::TRANSITION_UNCONFIGURED_SHUTDOWN);
73}
74
75void ClLifecycleNode::onTransitionEvent(const lifecycle_msgs::msg::TransitionEvent::SharedPtr msg)
76{
77 RCLCPP_INFO(
78 this->getNode()->get_logger(), "[ClLifecycleNode] Transition event received: %d -> %d",
79 msg->start_state.id, msg->goal_state.id);
80
82 // switch (msg->transition.id)
83 // {
84 // case lifecycle_msgs::msg::Transition::TRANSITION_CREATE:
85 // if (msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_UNKNOWN &&
86 // msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED)
87 // {
88 // RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_CREATE");
89 // this->postOnTransitionCreate_();
90 // }
91 // // case lifecycle_msgs::msg::Transition::TRANSITION_CONFIGURE:
92 // else
93 if (
94 msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED &&
95 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_CONFIGURING)
96 {
97 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_CONFIGURE");
99 }
100 // case lifecycle_msgs::msg::Transition::TRANSITION_CLEANUP:
101 else if (
102 msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE &&
103 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_CLEANINGUP)
104 {
105 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_CLEANUP");
107 }
108 // case lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE:
109 else if (
110 msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE &&
111 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ACTIVATING)
112 {
113 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ACTIVATE");
115 }
116 // case lifecycle_msgs::msg::Transition::TRANSITION_DEACTIVATE:
117 else if (
118 msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE &&
119 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_DEACTIVATING)
120 {
121 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_DEACTIVATE");
123 }
124 // case lifecycle_msgs::msg::Transition::TRANSITION_UNCONFIGURED_SHUTDOWN:
125 else if (
126 msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED &&
127 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_SHUTTINGDOWN)
128 {
129 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_UNCONFIGURED_SHUTDOWN");
131 }
132 // case lifecycle_msgs::msg::Transition::TRANSITION_INACTIVE_SHUTDOWN:
133 else if (
134 msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE &&
135 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_SHUTTINGDOWN)
136 {
137 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_INACTIVE_SHUTDOWN");
139 }
140 // case lifecycle_msgs::msg::Transition::TRANSITION_ACTIVE_SHUTDOWN:
141 else if (
142 msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE &&
143 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_SHUTTINGDOWN)
144 {
145 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ACTIVE_SHUTDOWN");
147 }
148 // case lifecycle_msgs::msg::Transition::TRANSITION_DESTROY:
149 else if (msg->start_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED)
150 // &&msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_DESTROYING
151
152 {
153 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_DESTROY");
155 }
156
157 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_CONFIGURE_SUCCESS:
158 else if (
159 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_CONFIGURING &&
160 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE)
161 {
162 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_CONFIGURE_SUCCESS");
164 }
165 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_CONFIGURE_FAILURE:
166 else if (
167 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_CONFIGURING &&
168 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED)
169 {
170 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_CONFIGURE_FAILURE");
172 }
173 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_CONFIGURE_ERROR:
174 if (
175 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_CONFIGURING &&
176 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING)
177 {
178 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_CONFIGURE_ERROR");
180 }
181
182 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_CLEANUP_SUCCESS:
183 else if (
184 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_CLEANINGUP &&
185 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED)
186 {
187 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_CLEANUP_SUCCESS");
189 }
190 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_CLEANUP_FAILURE:
191 // else if (msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_CLEANINGUP
192 // &&msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING)
193 // {
194 // RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_CLEANUP_FAILURE");
195 // this->postOnTransitionOnCleanupFailure_();
196 // }
197 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_CLEANUP_ERROR:
198 else if (
199 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_CLEANINGUP &&
200 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING)
201 {
202 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_CLEANUP_ERROR");
204 }
205
206 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_ACTIVATE_SUCCESS:
207 else if (
208 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ACTIVATING &&
209 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
210 {
211 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_ACTIVATE_SUCCESS");
213 }
214 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_ACTIVATE_FAILURE:
215 else if (
216 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ACTIVATING &&
217 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE)
218 {
219 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_ACTIVATE_FAILURE");
221 }
222 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_ACTIVATE_ERROR:
223 else if (
224 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ACTIVATING &&
225 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING)
226 {
227 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_ACTIVATE_ERROR");
229 }
230
231 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_DEACTIVATE_SUCCESS:
232 else if (
233 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_DEACTIVATING &&
234 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE)
235 {
236 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_DEACTIVATE_SUCCESS");
238 }
239 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_DEACTIVATE_FAILURE:
240 // else if (msg->start_state.id == &&msg->goal_state.id ==)
241 // {
242 // RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_DEACTIVATE_FAILURE");
243 // this->postOnTransitionOnDeactivateFailure_();
244 // }
245 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_DEACTIVATE_ERROR:
246 else if (
247 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_DEACTIVATING &&
248 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING)
249 {
250 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_DEACTIVATE_ERROR");
252 }
253
254 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_SHUTDOWN_SUCCESS:
255 else if (
256 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_SHUTTINGDOWN &&
257 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED)
258 {
259 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_SHUTDOWN_SUCCESS");
261 }
262 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_SHUTDOWN_FAILURE:
263 // else if (msg->start_state.id == &&msg->goal_state.id ==)
264 // {
265 // RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_SHUTDOWN_FAILURE");
266 // this->postOnTransitionOnShutdownFailure_();
267 // }
268 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_SHUTDOWN_ERROR:
269 else if (
270 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_SHUTTINGDOWN &&
271 msg->goal_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING)
272 {
273 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_SHUTDOWN_ERROR");
275 }
276
277 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_ERROR_SUCCESS:
278 else if (
279 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING &&
280 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED)
281 {
282 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_ERROR_SUCCESS");
284 }
285 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_ERROR_FAILURE:
286 else if (
287 msg->start_state.id == lifecycle_msgs::msg::State::TRANSITION_STATE_ERRORPROCESSING &&
288 msg->goal_state.id == lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED)
289 {
290 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_ERROR_FAILURE");
292 }
293 // case lifecycle_msgs::msg::Transition::TRANSITION_ON_ERROR_ERROR:
294 // else if (msg->start_state.id == &&msg->goal_state.id ==)
295 // {
296 // RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_ON_ERROR_ERROR");
297 // this->postOnTransitionOnErrorError_();
298 // }
299
300 // default:
301 else
302 {
303 RCLCPP_INFO(this->getNode()->get_logger(), "TRANSITION_UNKNOWN");
304 }
305 // }
306
307} // namespace cl_lifecyclenode
308} // namespace cl_lifecyclenode
std::function< void()> postOnTransitionActivate_
virtual void onTransitionEvent(const lifecycle_msgs::msg::TransitionEvent::SharedPtr msg)
std::function< void()> postOnTransitionOnConfigureFailure_
std::function< void()> postOnTransitionConfigure_
std::function< void()> postOnTransitionUnconfiguredShutdown_
rclcpp::Client< lifecycle_msgs::srv::GetState >::SharedPtr client_get_state_
std::function< void()> postOnTransitionOnConfigureSuccess_
rclcpp::Subscription< lifecycle_msgs::msg::TransitionEvent >::SharedPtr subscription_transition_event_
lifecycle_msgs::msg::TransitionEvent::SharedPtr lastTransitionEvent_
rclcpp::Client< lifecycle_msgs::srv::ChangeState >::SharedPtr client_change_state_
std::function< void()> postOnTransitionOnErrorSuccess_
std::function< void()> postOnTransitionOnCleanupSuccess_
std::function< void()> postOnTransitionOnCleanupError_
std::function< void()> postOnTransitionInactiveShutdown_
std::function< void()> postOnTransitionCleanup_
std::function< void()> postOnTransitionOnShutdownError_
std::function< void()> postOnTransitionOnErrorFailure_
std::function< void()> postOnTransitionOnDeactivateSuccess_
std::function< void()> postOnTransitionDeactivate_
std::function< void()> postOnTransitionOnConfigureError_
std::function< void()> postOnTransitionOnActivateFailure_
std::function< void()> postOnTransitionOnShutdownSuccess_
std::function< void()> postOnTransitionDestroy_
std::function< void()> postOnTransitionActiveShutdown_
std::function< void()> postOnTransitionOnActivateSuccess_
std::function< void()> postOnTransitionOnDeactivateError_
std::function< void()> postOnTransitionOnActivateError_
rclcpp::Node::SharedPtr getNode()
Definition: client.cpp:60