SMACC2
Loading...
Searching...
No Matches
cb_connect_micro_ros_agent.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
19
20#include <chrono>
21
22namespace cl_px4_mr
23{
24
26: timeoutSec_(timeoutSec), rate_(5)
27{
28}
29
31{
33 {
34 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: launching micro_ros_agent...");
36 }
37
38 std::string targetNodeName = microRosAgent_->getNodeName();
39 RCLCPP_INFO(
40 getLogger(), "CbConnectMicroRosAgent: waiting for node '%s' (timeout %.1fs)",
41 targetNodeName.c_str(), timeoutSec_);
42
43 auto startTime = std::chrono::steady_clock::now();
44 bool found = false;
45
46 while (!this->isShutdownRequested() && !found)
47 {
48 // Check timeout
49 auto elapsed = std::chrono::steady_clock::now() - startTime;
50 double elapsedSec = std::chrono::duration<double>(elapsed).count();
51 if (elapsedSec > timeoutSec_)
52 {
53 RCLCPP_ERROR(
54 getLogger(), "CbConnectMicroRosAgent: timeout (%.1fs) waiting for '%s'", timeoutSec_,
55 targetNodeName.c_str());
56 this->postPx4Failure();
57 return;
58 }
59
60 // Poll for node
61 std::stringstream ss;
62 auto nodeNames = getNode()->get_node_names();
63
64 for (const auto & n : nodeNames)
65 {
66 ss << " - " << n << "\n";
67
68 if (n == targetNodeName)
69 {
70 found = true;
71 }
72 }
73
74 RCLCPP_INFO_STREAM(
75 getLogger(), "[" << getName() << "] listing nodes (" << nodeNames.size() << ")\n"
76 << ss.str());
77
78 rate_.sleep();
79 }
80
81 if (!found)
82 {
83 RCLCPP_WARN(getLogger(), "CbConnectMicroRosAgent: shutdown requested before node found");
84 this->postPx4Failure();
85 return;
86 }
87
88 RCLCPP_INFO(
89 getLogger(), "CbConnectMicroRosAgent: node '%s' detected - starting failsafe health check",
90 targetNodeName.c_str());
91
92 // Phase 2: Wait for PX4 failsafe health flags to clear, read from the state-machine-lifetime
93 // CpFailsafeFlags component, so no subscription callback can run into this behavior at dispose.
94 if (failsafeFlags_ == nullptr)
95 {
96 RCLCPP_ERROR(getLogger(), "CbConnectMicroRosAgent: no CpFailsafeFlags component on the client");
97 this->postPx4Failure();
98 return;
99 }
100
101 // the failsafe flags say an estimate exists; the local position and velocity
102 // also have to be valid. NOT heading_good_for_control: with a magnetometer
103 // heading PX4 only sets it after the in-flight yaw alignment (moving with GPS
104 // course), so on the ground it is false by design; it is logged for diagnosis.
105 auto controlReady = [this]()
106 {
107 return localPosition_ == nullptr ||
109 };
110 auto flag = [this](bool (CpVehicleLocalPosition::*f)() const)
111 { return localPosition_ ? static_cast<int>((localPosition_->*f)()) : -1; };
112 rclcpp::Rate healthRate(2.0);
113 while (!this->isShutdownRequested() && !(failsafeFlags_->isHealthOk() && controlReady()))
114 {
115 auto elapsed = std::chrono::steady_clock::now() - startTime;
116 double elapsedSec = std::chrono::duration<double>(elapsed).count();
117 if (elapsedSec > timeoutSec_)
118 {
119 RCLCPP_ERROR(
120 getLogger(),
121 "CbConnectMicroRosAgent: timeout (%.1fs) waiting for health check. "
122 "attitude_invalid=%d, local_altitude_invalid=%d, local_position_invalid=%d, "
123 "heading_good_for_control=%d, velocity_valid=%d",
128 this->postPx4Failure();
129 return;
130 }
131 RCLCPP_INFO(
132 getLogger(),
133 "CbConnectMicroRosAgent: health check: attitude_invalid=%d, "
134 "local_altitude_invalid=%d, local_position_invalid=%d, heading_good_for_control=%d, "
135 "velocity_valid=%d",
140 healthRate.sleep();
141 }
142 if (failsafeFlags_->isHealthOk() && controlReady())
143 {
144 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: health check passed - posting success");
145 this->postPx4Success();
146 }
147 else
148 {
149 RCLCPP_WARN(
150 getLogger(), "CbConnectMicroRosAgent: shutdown requested before health check passed");
151 this->postPx4Failure();
152 }
153}
154
155} // namespace cl_px4_mr
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...