SMACC2
Loading...
Searching...
No Matches
cb_arm_px4.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#include <thread>
22
23namespace cl_px4_mr
24{
25
27
29{
30 // Enable offboard keepalive and set offboard mode so PX4's offboard signal
31 // requirement is satisfied before arming. Without this, canArm() fails because
32 // offboard_control_signal_lost is true when nav_state == OFFBOARD.
34 {
35 RCLCPP_INFO(getLogger(), "CbArmPX4: enabling offboard keepalive");
37 }
38
39 RCLCPP_INFO(getLogger(), "CbArmPX4: sending setOffboardMode command");
41
42 // Wait for PX4 to register offboard signal (needs at least one
43 // offboard_control_mode message processed by PX4's health checks)
44 RCLCPP_INFO(getLogger(), "CbArmPX4: waiting 2s for offboard signal registration...");
45 std::this_thread::sleep_for(std::chrono::seconds(2));
46
47 for (int attempt = 0; attempt < MAX_RETRIES; attempt++)
48 {
49 if (attempt < 2)
50 {
51 RCLCPP_INFO(
52 getLogger(), "CbArmPX4: sending arm command (attempt %d/%d)", attempt + 1, MAX_RETRIES);
54 }
55 else
56 {
57 RCLCPP_WARN(getLogger(), "CbArmPX4: force-arming (attempt %d/%d)", attempt + 1, MAX_RETRIES);
59 }
60
61 // Wait for armed confirmation or timeout
62 for (int i = 0; i < RETRY_INTERVAL_SEC * 10; i++)
63 {
64 if (armed_) break;
65 std::this_thread::sleep_for(std::chrono::milliseconds(100));
66 }
67
68 if (armed_)
69 {
70 RCLCPP_INFO(getLogger(), "CbArmPX4: vehicle ARMED - posting success");
71 this->postPx4Success();
72 return;
73 }
74
75 RCLCPP_WARN(
76 getLogger(), "CbArmPX4: attempt %d/%d timed out, retrying...", attempt + 1, MAX_RETRIES);
77 }
78
79 RCLCPP_ERROR(getLogger(), "CbArmPX4: all %d attempts failed - posting failure", MAX_RETRIES);
80 this->postPx4Failure();
81}
82
84
86
88{
89 if (vehicleStatus_ != nullptr)
90 {
93 }
94 else
95 {
96 RCLCPP_WARN(getLogger(), "CbArmPX4: completion component missing, no completion signal wired");
97 }
98}
99
100} // namespace cl_px4_mr
std::atomic< bool > armed_
void onExit() override
void onEntry() override
static constexpr int MAX_RETRIES
void wireCompletionSignals() override
static constexpr int RETRY_INTERVAL_SEC
smacc2::SmaccSignal< void()> onArmed_
virtual rclcpp::Logger getLogger() const
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)