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
18
19#include <chrono>
20
21namespace cl_px4_mr
22{
23
25: timeoutSec_(timeoutSec), rate_(5)
26{
27}
28
30{
32 {
33 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: launching micro_ros_agent...");
35 }
36
37 std::string targetNodeName = microRosAgent_->getNodeName();
38 RCLCPP_INFO(
39 getLogger(), "CbConnectMicroRosAgent: waiting for node '%s' (timeout %.1fs)",
40 targetNodeName.c_str(), timeoutSec_);
41
42 auto startTime = std::chrono::steady_clock::now();
43 bool found = false;
44
45 while (!this->isShutdownRequested() && !found)
46 {
47 // Check timeout
48 auto elapsed = std::chrono::steady_clock::now() - startTime;
49 double elapsedSec = std::chrono::duration<double>(elapsed).count();
50 if (elapsedSec > timeoutSec_)
51 {
52 RCLCPP_ERROR(
53 getLogger(), "CbConnectMicroRosAgent: timeout (%.1fs) waiting for '%s'", timeoutSec_,
54 targetNodeName.c_str());
55 this->postPx4Failure();
56 return;
57 }
58
59 // Poll for node
60 std::stringstream ss;
61 auto nodeNames = getNode()->get_node_names();
62
63 for (const auto & n : nodeNames)
64 {
65 ss << " - " << n << std::endl;
66
67 if (n == targetNodeName)
68 {
69 found = true;
70 }
71 }
72
73 RCLCPP_INFO_STREAM(
74 getLogger(), "[" << getName() << "] listing nodes (" << nodeNames.size() << ")" << std::endl
75 << ss.str());
76
77 rate_.sleep();
78 }
79
80 if (!found)
81 {
82 RCLCPP_WARN(getLogger(), "CbConnectMicroRosAgent: shutdown requested before node found");
83 this->postPx4Failure();
84 return;
85 }
86
87 RCLCPP_INFO(
88 getLogger(), "CbConnectMicroRosAgent: node '%s' detected - starting failsafe health check",
89 targetNodeName.c_str());
90
91 // Phase 2: Wait for PX4 failsafe health flags to clear
92 healthOk_.store(false);
93 attitudeInvalid_.store(true);
94 localAltitudeInvalid_.store(true);
95 localPositionInvalid_.store(true);
96
97 failsafeSub_ = getNode()->create_subscription<px4_msgs::msg::FailsafeFlags>(
98 "/fmu/out/failsafe_flags", rclcpp::SensorDataQoS(),
99 [this](const px4_msgs::msg::FailsafeFlags::SharedPtr msg)
100 {
101 attitudeInvalid_.store(msg->attitude_invalid);
102 localAltitudeInvalid_.store(msg->local_altitude_invalid);
103 localPositionInvalid_.store(msg->local_position_invalid);
104 healthOk_.store(
105 !msg->attitude_invalid && !msg->local_altitude_invalid && !msg->local_position_invalid);
106 });
107
108 // the failsafe flags say an estimate exists; the local position and velocity
109 // also have to be valid. NOT heading_good_for_control: with a magnetometer
110 // heading PX4 only sets it after the in-flight yaw alignment (moving with GPS
111 // course), so on the ground it is false by design; it is logged for diagnosis.
112 auto controlReady = [this]()
113 {
114 return localPosition_ == nullptr ||
116 };
117 auto flag = [this](bool (CpVehicleLocalPosition::*f)() const)
118 { return localPosition_ ? static_cast<int>((localPosition_->*f)()) : -1; };
119 rclcpp::Rate healthRate(2.0);
120 while (!this->isShutdownRequested() && !(healthOk_.load() && controlReady()))
121 {
122 auto elapsed = std::chrono::steady_clock::now() - startTime;
123 double elapsedSec = std::chrono::duration<double>(elapsed).count();
124 if (elapsedSec > timeoutSec_)
125 {
126 RCLCPP_ERROR(
127 getLogger(),
128 "CbConnectMicroRosAgent: timeout (%.1fs) waiting for health check. "
129 "attitude_invalid=%d, local_altitude_invalid=%d, local_position_invalid=%d, "
130 "heading_good_for_control=%d, velocity_valid=%d",
134 this->postPx4Failure();
135 return;
136 }
137 RCLCPP_INFO(
138 getLogger(),
139 "CbConnectMicroRosAgent: health check: attitude_invalid=%d, "
140 "local_altitude_invalid=%d, local_position_invalid=%d, heading_good_for_control=%d, "
141 "velocity_valid=%d",
145 healthRate.sleep();
146 }
147 if (healthOk_.load() && controlReady())
148 {
149 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: health check passed - posting success");
150 this->postPx4Success();
151 }
152 else
153 {
154 RCLCPP_WARN(
155 getLogger(), "CbConnectMicroRosAgent: shutdown requested before health check passed");
156 this->postPx4Failure();
157 }
158}
159
161
162} // namespace cl_px4_mr
rclcpp::Subscription< px4_msgs::msg::FailsafeFlags >::SharedPtr failsafeSub_
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...