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{
33
36
37 // Enable offboard keepalive and set offboard mode so PX4's offboard signal
38 // requirement is satisfied before arming. Without this, canArm() fails because
39 // offboard_control_signal_lost is true when nav_state == OFFBOARD.
41 {
42 RCLCPP_INFO(getLogger(), "CbArmPX4: enabling offboard keepalive");
44 }
45
46 RCLCPP_INFO(getLogger(), "CbArmPX4: sending setOffboardMode command");
48
49 // Wait for PX4 to register offboard signal (needs at least one
50 // offboard_control_mode message processed by PX4's health checks)
51 RCLCPP_INFO(getLogger(), "CbArmPX4: waiting 2s for offboard signal registration...");
52 std::this_thread::sleep_for(std::chrono::seconds(2));
53
54 for (int attempt = 0; attempt < MAX_RETRIES; attempt++)
55 {
56 if (attempt < 2)
57 {
58 RCLCPP_INFO(
59 getLogger(), "CbArmPX4: sending arm command (attempt %d/%d)", attempt + 1, MAX_RETRIES);
61 }
62 else
63 {
64 RCLCPP_WARN(getLogger(), "CbArmPX4: force-arming (attempt %d/%d)", attempt + 1, MAX_RETRIES);
66 }
67
68 // Wait for armed confirmation or timeout
69 for (int i = 0; i < RETRY_INTERVAL_SEC * 10; i++)
70 {
71 if (armed_) break;
72 std::this_thread::sleep_for(std::chrono::milliseconds(100));
73 }
74
75 if (armed_)
76 {
77 RCLCPP_INFO(getLogger(), "CbArmPX4: vehicle ARMED - posting success");
78 this->postSuccessEvent();
79 return;
80 }
81
82 RCLCPP_WARN(
83 getLogger(), "CbArmPX4: attempt %d/%d timed out, retrying...", attempt + 1, MAX_RETRIES);
84 }
85
86 RCLCPP_ERROR(getLogger(), "CbArmPX4: all %d attempts failed - posting failure", MAX_RETRIES);
87 this->postFailureEvent();
88}
89
91
93
94} // namespace cl_px4_mr
std::atomic< bool > armed_
void onExit() override
CpVehicleStatus * vehicleStatus_
void onEntry() override
static constexpr int MAX_RETRIES
CpVehicleCommand * vehicleCommand_
static constexpr int RETRY_INTERVAL_SEC
CpOffboardKeepAlive * offboardKeepAlive_
smacc2::SmaccSignal< void()> onArmed_
virtual rclcpp::Logger getLogger() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)