SMACC2
Loading...
Searching...
No Matches
cl_lifecycle_node.cpp
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#include <chrono>
23#include <future>
24#include <string>
25
26namespace cl_lifecycle_node
27{
29: nodeName_(node_name), eventMonitor_(nullptr)
30{
31}
32
34
36{
37 // Create service clients
38 client_get_state_ = this->getNode()->create_client<lifecycle_msgs::srv::GetState>(
40 client_change_state_ = this->getNode()->create_client<lifecycle_msgs::srv::ChangeState>(
42}
43
44namespace
45{
46// Waits for the service, sends the request and waits for the response, checking `cancelled` and
47// rclcpp::ok() every 100 ms; both waits are unbounded but abandonable. Returns nullptr if abandoned.
48template <typename ServiceT>
49typename ServiceT::Response::SharedPtr callService(
50 const rclcpp::Logger & logger, typename rclcpp::Client<ServiceT>::SharedPtr client,
51 typename ServiceT::Request::SharedPtr request, const ClLifecycleNode::CancelPredicate & cancelled,
52 const std::string & what)
53{
54 const std::string serviceName = client->get_service_name();
55 auto abandoned = [&cancelled]() { return (cancelled && cancelled()) || !rclcpp::ok(); };
56
57 int laps = 0;
58 while (!client->wait_for_service(std::chrono::milliseconds(100)))
59 {
60 if (abandoned())
61 {
62 RCLCPP_INFO(
63 logger, "[ClLifecycleNode] %s abandoned while waiting for service %s", what.c_str(),
64 serviceName.c_str());
65 return nullptr;
66 }
67 if (++laps % 50 == 0)
68 {
69 RCLCPP_WARN(
70 logger, "[ClLifecycleNode] still waiting for service %s (%d s)", serviceName.c_str(),
71 laps / 10);
72 }
73 }
74
75 auto future = client->async_send_request(request);
76 laps = 0;
77 while (future.wait_for(std::chrono::milliseconds(100)) != std::future_status::ready)
78 {
79 if (abandoned())
80 {
81 client->remove_pending_request(future);
82 RCLCPP_INFO(
83 logger, "[ClLifecycleNode] %s abandoned while waiting for the response of %s", what.c_str(),
84 serviceName.c_str());
85 return nullptr;
86 }
87 if (++laps % 50 == 0)
88 {
89 RCLCPP_WARN(
90 logger, "[ClLifecycleNode] no response from %s after %d s", serviceName.c_str(), laps / 10);
91 }
92 }
93 return future.get();
94}
95} // namespace
96
97bool ClLifecycleNode::changeState(uint8_t transition, const CancelPredicate & cancelled)
98{
99 auto logger = this->getNode()->get_logger();
100 auto request = std::make_shared<lifecycle_msgs::srv::ChangeState::Request>();
101 request->transition.id = transition;
102 auto response = callService<lifecycle_msgs::srv::ChangeState>(
103 logger, client_change_state_, request, cancelled, "transition " + std::to_string(transition));
104 if (!response)
105 {
106 return false;
107 }
108
109 const std::string serviceName = client_change_state_->get_service_name();
110 if (response->success)
111 {
112 RCLCPP_INFO(
113 logger, "[ClLifecycleNode] transition %d accepted by %s", transition, serviceName.c_str());
114 }
115 else
116 {
117 RCLCPP_ERROR(
118 logger, "[ClLifecycleNode] transition %d rejected by %s; no transition event will follow",
119 transition, serviceName.c_str());
120 }
121 return response->success;
122}
123
124bool ClLifecycleNode::getState(uint8_t & state, const CancelPredicate & cancelled)
125{
126 auto request = std::make_shared<lifecycle_msgs::srv::GetState::Request>();
127 auto response = callService<lifecycle_msgs::srv::GetState>(
128 this->getNode()->get_logger(), client_get_state_, request, cancelled, "get_state");
129 if (!response)
130 {
131 return false;
132 }
133 state = response->current_state.id;
134 return true;
135}
136
138{
139 return changeState(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE, cancelled);
140}
141
143{
144 return changeState(lifecycle_msgs::msg::Transition::TRANSITION_DEACTIVATE, cancelled);
145}
146
148{
149 return changeState(lifecycle_msgs::msg::Transition::TRANSITION_CONFIGURE, cancelled);
150}
151
153{
154 return changeState(lifecycle_msgs::msg::Transition::TRANSITION_CLEANUP, cancelled);
155}
156
158{
159 // Each primary state has its own shutdown transition id, so ask the node which state it is in.
160 using lifecycle_msgs::msg::State;
161 using lifecycle_msgs::msg::Transition;
162 uint8_t state;
163 if (!getState(state, cancelled))
164 {
165 return false;
166 }
167 switch (state)
168 {
169 case State::PRIMARY_STATE_UNCONFIGURED:
170 return changeState(Transition::TRANSITION_UNCONFIGURED_SHUTDOWN, cancelled);
171 case State::PRIMARY_STATE_INACTIVE:
172 return changeState(Transition::TRANSITION_INACTIVE_SHUTDOWN, cancelled);
173 case State::PRIMARY_STATE_ACTIVE:
174 return changeState(Transition::TRANSITION_ACTIVE_SHUTDOWN, cancelled);
175 default:
176 RCLCPP_ERROR(
177 this->getNode()->get_logger(),
178 "[ClLifecycleNode] %s is in lifecycle state %d, which has no shutdown transition",
179 nodeName_.c_str(), state);
180 return false;
181 }
182}
183
184} // 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())
bool deactivate(const CancelPredicate &cancelled=CancelPredicate())
bool cleanup(const CancelPredicate &cancelled=CancelPredicate())
rclcpp::Client< lifecycle_msgs::srv::GetState >::SharedPtr client_get_state_
bool getState(uint8_t &state, const CancelPredicate &cancelled=CancelPredicate())
bool configure(const CancelPredicate &cancelled=CancelPredicate())
rclcpp::Node::SharedPtr getNode()
Definition client.cpp:60
ServiceT::Response::SharedPtr callService(const rclcpp::Logger &logger, typename rclcpp::Client< ServiceT >::SharedPtr client, typename ServiceT::Request::SharedPtr request, const ClLifecycleNode::CancelPredicate &cancelled, const std::string &what)