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
17
18#include <chrono>
19
20namespace cl_px4_mr
21{
22
24: timeoutSec_(timeoutSec), rate_(5)
25{
26}
27
29{
31 {
32 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: launching micro_ros_agent...");
34 }
35
36 std::string targetNodeName = microRosAgent_->getNodeName();
37 RCLCPP_INFO(
38 getLogger(), "CbConnectMicroRosAgent: waiting for node '%s' (timeout %.1fs)",
39 targetNodeName.c_str(), timeoutSec_);
40
41 auto startTime = std::chrono::steady_clock::now();
42 bool found = false;
43
44 while (!this->isShutdownRequested() && !found)
45 {
46 // Check timeout
47 auto elapsed = std::chrono::steady_clock::now() - startTime;
48 double elapsedSec = std::chrono::duration<double>(elapsed).count();
49 if (elapsedSec > timeoutSec_)
50 {
51 RCLCPP_ERROR(
52 getLogger(), "CbConnectMicroRosAgent: timeout (%.1fs) waiting for '%s'", timeoutSec_,
53 targetNodeName.c_str());
54 this->postPx4Failure();
55 return;
56 }
57
58 // Poll for node
59 std::stringstream ss;
60 auto nodeNames = getNode()->get_node_names();
61
62 for (const auto & n : nodeNames)
63 {
64 ss << " - " << n << std::endl;
65
66 if (n == targetNodeName)
67 {
68 found = true;
69 }
70 }
71
72 RCLCPP_INFO_STREAM(
73 getLogger(), "[" << getName() << "] listing nodes (" << nodeNames.size() << ")" << std::endl
74 << ss.str());
75
76 rate_.sleep();
77 }
78
79 if (!found)
80 {
81 RCLCPP_WARN(getLogger(), "CbConnectMicroRosAgent: shutdown requested before node found");
82 this->postPx4Failure();
83 return;
84 }
85
86 RCLCPP_INFO(
87 getLogger(), "CbConnectMicroRosAgent: node '%s' detected - starting failsafe health check",
88 targetNodeName.c_str());
89
90 // Phase 2: Wait for PX4 failsafe health flags to clear
91 healthOk_.store(false);
92 attitudeInvalid_.store(true);
93 localAltitudeInvalid_.store(true);
94 localPositionInvalid_.store(true);
95
96 failsafeSub_ = getNode()->create_subscription<px4_msgs::msg::FailsafeFlags>(
97 "/fmu/out/failsafe_flags", rclcpp::SensorDataQoS(),
98 [this](const px4_msgs::msg::FailsafeFlags::SharedPtr msg)
99 {
100 attitudeInvalid_.store(msg->attitude_invalid);
101 localAltitudeInvalid_.store(msg->local_altitude_invalid);
102 localPositionInvalid_.store(msg->local_position_invalid);
103 healthOk_.store(
104 !msg->attitude_invalid && !msg->local_altitude_invalid && !msg->local_position_invalid);
105 });
106
107 rclcpp::Rate healthRate(2.0);
108 while (!this->isShutdownRequested() && !healthOk_.load())
109 {
110 auto elapsed = std::chrono::steady_clock::now() - startTime;
111 double elapsedSec = std::chrono::duration<double>(elapsed).count();
112 if (elapsedSec > timeoutSec_)
113 {
114 RCLCPP_ERROR(
115 getLogger(),
116 "CbConnectMicroRosAgent: timeout (%.1fs) waiting for health check. "
117 "attitude_invalid=%d, local_altitude_invalid=%d, local_position_invalid=%d",
119 localPositionInvalid_.load());
120 this->postPx4Failure();
121 return;
122 }
123
124 RCLCPP_INFO(
125 getLogger(),
126 "CbConnectMicroRosAgent: health check: attitude_invalid=%d, "
127 "local_altitude_invalid=%d, local_position_invalid=%d",
129
130 healthRate.sleep();
131 }
132
133 if (healthOk_.load())
134 {
135 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: health check passed - posting success");
136 this->postPx4Success();
137 }
138 else
139 {
140 RCLCPP_WARN(
141 getLogger(), "CbConnectMicroRosAgent: shutdown requested before health check passed");
142 this->postPx4Failure();
143 }
144}
145
147
148} // 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...