SMACC2
Loading...
Searching...
No Matches
cb_wait_nav2_nodes.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
22#include <cl_nav2z/common.hpp>
23
24namespace cl_nav2z
25{
26CbWaitNav2Nodes::CbWaitNav2Nodes(std::vector<Nav2Nodes> waitNodes) : waitNodes_(waitNodes) {}
27
29{
30 std::stringstream ss;
31 for (auto v : waitNodes_)
32 {
33 receivedAliveMsg_[v] = false;
34 ss << "[CbWaitNav2Nodes] - " << toString(v) << ": waiting" << '\n';
35 }
36 RCLCPP_INFO(getLogger(), "%s", ss.str().c_str());
37
39 if (bondListener_ == nullptr)
40 {
41 RCLCPP_ERROR(getLogger(), "[CbWaitNav2Nodes] no CpBondListener component on the nav2 client");
42 return;
43 }
44 // Connected on the state machine thread through the state-lifetime signal map: dispose finalizes
45 // the slot and waits for a callback in flight, so no heartbeat runs into a disposed behavior.
47}
48
49void CbWaitNav2Nodes::onMessageReceived(const bond::msg::Status & msg)
50{
51 auto value = fromString(msg.id);
52 bool updated = false;
53 // RCLCPP_INFO(getLogger(), "[CbWaitNav2Nodes] received '%s'", msg.id.c_str());
54
55 if (receivedAliveMsg_.count(value) && !receivedAliveMsg_[value])
56 {
57 RCLCPP_INFO(getLogger(), "[CbWaitNav2Nodes] '%s' alive received", msg.id.c_str());
58 receivedAliveMsg_[value] = true;
59 updated = true;
60 }
61
62 if (updated)
63 {
64 bool success = true;
65 std::stringstream ss;
66 for (const auto & pair : receivedAliveMsg_)
67 {
68 if (!pair.second)
69 {
70 success = false;
71 }
72 ss << "- " << toString(pair.first) << ": " << (pair.second ? "ALIVE" : "WAITING") << '\n';
73 }
74
75 RCLCPP_INFO_STREAM(
76 getLogger(), "[CbWaitNav2Nodes] waiting nodes status:" << '\n'
77 << ss.str().c_str());
78
79 if (success)
80 {
81 RCLCPP_INFO(getLogger(), "[CbWaitNav2Nodes] success event");
82 this->postSuccessEvent();
83 }
84 else
85 {
86 RCLCPP_INFO(getLogger(), "[CbWaitNav2Nodes] still missing nodes");
87 }
88 }
89}
90
92{
93 if (bondListener_ == nullptr)
94 {
95 this->postFailureEvent();
96 return;
97 }
98 RCLCPP_INFO(
99 getLogger(), "[CbWaitNav2Nodes] waiting for heartbeats on %s",
100 bondListener_->getTopicName().c_str());
101}
102
103std::string toString(Nav2Nodes value)
104{
105 switch (value)
106 {
108 return "planner_server";
110 return "controller_server";
112 return "behavior_server";
114 return "bt_navigator";
116 return "map_server";
117 default:
118 return "";
119 }
120}
121
122Nav2Nodes fromString(const std::string & id)
123{
124 if (id == "planner_server")
126 else if (id == "controller_server")
128 else if (id == "behavior_server")
130 else if (id == "bt_navigator")
132 else if (id == "map_server")
134 else
135 return Nav2Nodes::None;
136}
137
138} // namespace cl_nav2z
std::map< Nav2Nodes, bool > receivedAliveMsg_
std::vector< Nav2Nodes > waitNodes_
void onMessageReceived(const bond::msg::Status &msg)
CbWaitNav2Nodes(std::vector< Nav2Nodes > waitNodes={ Nav2Nodes::PlannerServer, Nav2Nodes::ControllerServer, Nav2Nodes::BtNavigator})
const std::string & getTopicName() const
smacc2::SmaccSignalConnection onBondMessage(void(T::*callback)(const bond::msg::Status &), T *object)
virtual rclcpp::Logger getLogger() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
Nav2Nodes fromString(const std::string &str)
std::string toString(Nav2Nodes value)