Skip to content

Commit cdccfe1

Browse files
Publish is_active status from LifecycleManager (#5627)
* Publish `is_active` status from LifecycleManager Signed-off-by: Sushant Chavan <[email protected]> * Update nav2_lifecycle_manager/src/lifecycle_manager.cpp Signed-off-by: Steve Macenski <[email protected]> --------- Signed-off-by: Sushant Chavan <[email protected]> Signed-off-by: Steve Macenski <[email protected]> Co-authored-by: Steve Macenski <[email protected]>
1 parent b35a4b5 commit cdccfe1

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

nav2_lifecycle_manager/include/nav2_lifecycle_manager/lifecycle_manager.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
#include "nav2_util/lifecycle_service_client.hpp"
2727
#include "nav2_ros_common/node_thread.hpp"
28+
#include "nav2_ros_common/publisher.hpp"
2829
#include "nav2_ros_common/service_server.hpp"
2930
#include "rclcpp/rclcpp.hpp"
31+
#include "std_msgs/msg/bool.hpp"
3032
#include "std_srvs/srv/empty.hpp"
3133
#include "nav2_msgs/srv/manage_lifecycle_nodes.hpp"
3234
#include "std_srvs/srv/trigger.hpp"
@@ -78,6 +80,10 @@ class LifecycleManager : public rclcpp::Node
7880
// The services provided by this node
7981
nav2::ServiceServer<ManageLifecycleNodes>::SharedPtr manager_srv_;
8082
nav2::ServiceServer<std_srvs::srv::Trigger>::SharedPtr is_active_srv_;
83+
84+
// Latched publisher for is_active status
85+
nav2::Publisher<std_msgs::msg::Bool>::SharedPtr is_active_pub_;
86+
8187
/**
8288
* @brief Lifecycle node manager callback function
8389
* @param request_header Header of the service request
@@ -156,6 +162,12 @@ class LifecycleManager : public rclcpp::Node
156162
*/
157163
void createLifecycleServiceServers();
158164

165+
// Support function for creating publishers
166+
/**
167+
* @brief Support function for creating publishers
168+
*/
169+
void createLifecyclePublishers();
170+
159171
// Support functions for shutdown
160172
/**
161173
* @brief Support function for shutdown
@@ -165,6 +177,10 @@ class LifecycleManager : public rclcpp::Node
165177
* @brief Destroy all the lifecycle service clients.
166178
*/
167179
void destroyLifecycleServiceClients();
180+
/**
181+
* @brief Destroy all the lifecycle publishers.
182+
*/
183+
void destroyLifecyclePublishers();
168184

169185
// Support function for creating bond timer
170186
/**
@@ -230,11 +246,21 @@ class LifecycleManager : public rclcpp::Node
230246
*/
231247
void registerRclPreshutdownCallback();
232248

249+
/**
250+
* @brief Set the state of managed nodes
251+
*/
252+
void setState(const NodeState & state);
253+
233254
/**
234255
* @brief function to check if managed nodes are active
235256
*/
236257
bool isActive();
237258

259+
/**
260+
* @brief Publish the is_active state
261+
*/
262+
void publishIsActiveState();
263+
238264
// Timer thread to look at bond connections
239265
rclcpp::TimerBase::SharedPtr init_timer_;
240266
rclcpp::TimerBase::SharedPtr bond_timer_;

nav2_lifecycle_manager/src/lifecycle_manager.cpp

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ LifecycleManager::LifecycleManager(const rclcpp::NodeOptions & options)
8686
0s,
8787
[this]() -> void {
8888
init_timer_->cancel();
89+
createLifecyclePublishers();
8990
createLifecycleServiceClients();
9091
createLifecycleServiceServers();
9192
if (autostart_) {
@@ -142,12 +143,29 @@ LifecycleManager::managerCallback(
142143
}
143144
}
144145

146+
void
147+
LifecycleManager::setState(const NodeState & state)
148+
{
149+
managed_nodes_state_ = state;
150+
publishIsActiveState();
151+
}
152+
145153
inline bool
146154
LifecycleManager::isActive()
147155
{
148156
return managed_nodes_state_ == NodeState::ACTIVE;
149157
}
150158

159+
void
160+
LifecycleManager::publishIsActiveState()
161+
{
162+
if (is_active_pub_ && is_active_pub_->is_activated()) {
163+
auto message = std::make_unique<std_msgs::msg::Bool>();
164+
message->data = isActive();
165+
is_active_pub_->publish(std::move(message));
166+
}
167+
}
168+
151169
void
152170
LifecycleManager::isActiveCallback(
153171
const std::shared_ptr<rmw_request_id_t>/*request_header*/,
@@ -217,6 +235,21 @@ LifecycleManager::createLifecycleServiceServers()
217235
callback_group_);
218236
}
219237

238+
void
239+
LifecycleManager::createLifecyclePublishers()
240+
{
241+
message("Creating and initializing lifecycle publishers");
242+
243+
is_active_pub_ = nav2::interfaces::create_publisher<std_msgs::msg::Bool>(
244+
shared_from_this(),
245+
get_name() + std::string("/is_active"),
246+
nav2::qos::LatchedSubscriptionQoS(),
247+
callback_group_);
248+
is_active_pub_->on_activate();
249+
// Publish the initial state once at startup
250+
publishIsActiveState();
251+
}
252+
220253
void
221254
LifecycleManager::destroyLifecycleServiceClients()
222255
{
@@ -226,6 +259,16 @@ LifecycleManager::destroyLifecycleServiceClients()
226259
}
227260
}
228261

262+
void
263+
LifecycleManager::destroyLifecyclePublishers()
264+
{
265+
message("Destroying lifecycle publishers");
266+
if (is_active_pub_) {
267+
is_active_pub_->on_deactivate();
268+
is_active_pub_.reset();
269+
}
270+
}
271+
229272
bool
230273
LifecycleManager::createBondConnection(const std::string & node_name)
231274
{
@@ -319,7 +362,7 @@ void
319362
LifecycleManager::shutdownAllNodes()
320363
{
321364
message("Deactivate, cleanup, and shutdown nodes");
322-
managed_nodes_state_ = NodeState::FINALIZED;
365+
setState(NodeState::FINALIZED);
323366
changeStateForAllNodes(Transition::TRANSITION_DEACTIVATE);
324367
changeStateForAllNodes(Transition::TRANSITION_CLEANUP);
325368
changeStateForAllNodes(Transition::TRANSITION_UNCONFIGURED_SHUTDOWN);
@@ -333,11 +376,11 @@ LifecycleManager::startup()
333376
!changeStateForAllNodes(Transition::TRANSITION_ACTIVATE))
334377
{
335378
RCLCPP_ERROR(get_logger(), "Failed to bring up all requested nodes. Aborting bringup.");
336-
managed_nodes_state_ = NodeState::UNKNOWN;
379+
setState(NodeState::UNKNOWN);
337380
return false;
338381
}
339382
message("Managed nodes are active");
340-
managed_nodes_state_ = NodeState::ACTIVE;
383+
setState(NodeState::ACTIVE);
341384
createBondTimer();
342385
return true;
343386
}
@@ -348,11 +391,11 @@ LifecycleManager::configure()
348391
message("Configuring managed nodes...");
349392
if (!changeStateForAllNodes(Transition::TRANSITION_CONFIGURE)) {
350393
RCLCPP_ERROR(get_logger(), "Failed to configure all requested nodes. Aborting bringup.");
351-
managed_nodes_state_ = NodeState::UNKNOWN;
394+
setState(NodeState::UNKNOWN);
352395
return false;
353396
}
354397
message("Managed nodes are now configured");
355-
managed_nodes_state_ = NodeState::INACTIVE;
398+
setState(NodeState::INACTIVE);
356399
return true;
357400
}
358401

@@ -362,11 +405,11 @@ LifecycleManager::cleanup()
362405
message("Cleaning up managed nodes...");
363406
if (!changeStateForAllNodes(Transition::TRANSITION_CLEANUP)) {
364407
RCLCPP_ERROR(get_logger(), "Failed to cleanup all requested nodes. Aborting cleanup.");
365-
managed_nodes_state_ = NodeState::UNKNOWN;
408+
setState(NodeState::UNKNOWN);
366409
return false;
367410
}
368411
message("Managed nodes have been cleaned up");
369-
managed_nodes_state_ = NodeState::UNCONFIGURED;
412+
setState(NodeState::UNCONFIGURED);
370413
return true;
371414
}
372415

@@ -378,6 +421,7 @@ LifecycleManager::shutdown()
378421
message("Shutting down managed nodes...");
379422
shutdownAllNodes();
380423
destroyLifecycleServiceClients();
424+
destroyLifecyclePublishers();
381425
message("Managed nodes have been shut down");
382426
return true;
383427
}
@@ -394,13 +438,13 @@ LifecycleManager::reset(bool hard_reset)
394438
{
395439
if (!hard_reset) {
396440
RCLCPP_ERROR(get_logger(), "Failed to reset nodes: aborting reset");
397-
managed_nodes_state_ = NodeState::UNKNOWN;
441+
setState(NodeState::UNKNOWN);
398442
return false;
399443
}
400444
}
401445

402446
message("Managed nodes have been reset");
403-
managed_nodes_state_ = NodeState::UNCONFIGURED;
447+
setState(NodeState::UNCONFIGURED);
404448
return true;
405449
}
406450

@@ -412,12 +456,12 @@ LifecycleManager::pause()
412456
message("Pausing managed nodes...");
413457
if (!changeStateForAllNodes(Transition::TRANSITION_DEACTIVATE)) {
414458
RCLCPP_ERROR(get_logger(), "Failed to pause nodes: aborting pause");
415-
managed_nodes_state_ = NodeState::UNKNOWN;
459+
setState(NodeState::UNKNOWN);
416460
return false;
417461
}
418462

419463
message("Managed nodes have been paused");
420-
managed_nodes_state_ = NodeState::INACTIVE;
464+
setState(NodeState::INACTIVE);
421465
return true;
422466
}
423467

@@ -427,12 +471,12 @@ LifecycleManager::resume()
427471
message("Resuming managed nodes...");
428472
if (!changeStateForAllNodes(Transition::TRANSITION_ACTIVATE)) {
429473
RCLCPP_ERROR(get_logger(), "Failed to resume nodes: aborting resume");
430-
managed_nodes_state_ = NodeState::UNKNOWN;
474+
setState(NodeState::UNKNOWN);
431475
return false;
432476
}
433477

434478
message("Managed nodes are active");
435-
managed_nodes_state_ = NodeState::ACTIVE;
479+
setState(NodeState::ACTIVE);
436480
createBondTimer();
437481
return true;
438482
}

0 commit comments

Comments
 (0)