AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
arm_disarm.cpp
1// Copyright 2024 Robin Müller
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// https://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
15#include "auto_apms_px4_interfaces/action/arm_disarm.hpp"
16
17#include "auto_apms_px4/vehicle_command_client.hpp"
18#include "auto_apms_util/action_wrapper.hpp"
19#include "px4_msgs/msg/vehicle_status.hpp"
20#include "px4_ros2/utils/message_version.hpp"
21
22namespace auto_apms_px4
23{
24
25class ArmDisarmSkill : public auto_apms_util::ActionWrapper<auto_apms_px4_interfaces::action::ArmDisarm>
26{
27 enum State
28 {
29 WAIT_FOR_READY_TO_ARM,
30 SEND_COMMAND,
31 WAIT_FOR_ARMING_STATE_REACHED
32 };
33
34 rclcpp::Subscription<px4_msgs::msg::VehicleStatus>::SharedPtr vehicle_status_sub_ptr_;
35 State state_;
36 bool ready_to_arm_{false};
37 bool is_armed_{false};
38 std::function<bool()> is_arming_state_reached_check__;
39 std::function<bool()> send_command_callback_;
40 const VehicleCommandClient vehicle_command_client_;
41
42public:
43 explicit ArmDisarmSkill(const rclcpp::NodeOptions & options)
44 : ActionWrapper{_AUTO_APMS_PX4__ARM_DISARM_ACTION_NAME, options}, vehicle_command_client_{*this->node_ptr_}
45 {
46 vehicle_status_sub_ptr_ = this->node_ptr_->create_subscription<px4_msgs::msg::VehicleStatus>(
47 "/fmu/out/vehicle_status" + px4_ros2::getMessageNameVersion<px4_msgs::msg::VehicleStatus>(),
48 rclcpp::QoS(1).best_effort(), [this](px4_msgs::msg::VehicleStatus::UniquePtr msg) {
49 is_armed_ = msg->arming_state == px4_msgs::msg::VehicleStatus::ARMING_STATE_ARMED;
50 ready_to_arm_ = msg->pre_flight_checks_pass;
51 });
52 }
53
54private:
55 bool onGoalRequest(std::shared_ptr<const Goal> goal_ptr) override final
56 {
57 // Reject goal if goal wants to arm, but UAV is not ready to arm and it is not requested to wait for ready to
58 // arm
59 if (goal_ptr->arming_state == Goal::ARMING_STATE_ARM && !goal_ptr->wait_until_ready_to_arm && !ready_to_arm_) {
60 RCLCPP_ERROR(
61 this->node_ptr_->get_logger(), "UAV can currently not be armed and wait_until_ready_to_arm is false.");
62 return false;
63 }
64
65 if (goal_ptr->arming_state == Goal::ARMING_STATE_ARM) {
66 // If goal is to arm
67 state_ = ready_to_arm_ ? SEND_COMMAND : WAIT_FOR_READY_TO_ARM;
68 is_arming_state_reached_check__ = [this]() { return is_armed_; };
69 send_command_callback_ = [this]() { return vehicle_command_client_.arm(); };
70 } else {
71 // If goal is to disarm
72 state_ = SEND_COMMAND;
73 is_arming_state_reached_check__ = [this]() { return !is_armed_; };
74 send_command_callback_ = [this]() { return vehicle_command_client_.disarm(); };
75 }
76
77 // Succeed directly if arming state is already reached
78 if (
79 (goal_ptr->arming_state == Goal::ARMING_STATE_ARM && is_armed_) ||
80 (goal_ptr->arming_state == Goal::ARMING_STATE_DISARM && !is_armed_)) {
81 state_ = WAIT_FOR_ARMING_STATE_REACHED;
82 }
83
84 return true;
85 }
86
87 Status executeGoal(
88 std::shared_ptr<const Goal> goal_ptr, std::shared_ptr<Feedback> feedback_ptr,
89 std::shared_ptr<Result> result_ptr) override final
90 {
91 (void)goal_ptr;
92 (void)feedback_ptr;
93
94 switch (state_) {
95 case WAIT_FOR_READY_TO_ARM:
96 if (ready_to_arm_) {
97 state_ = SEND_COMMAND;
98 }
99 break;
100 case SEND_COMMAND:
101 if (send_command_callback_()) {
102 state_ = WAIT_FOR_ARMING_STATE_REACHED;
103 } else {
104 RCLCPP_ERROR(this->node_ptr_->get_logger(), "Couldn't send arm/disarm command. Aborting...");
105 result_ptr->state_changed = false;
106 return Status::FAILURE;
107 }
108 break;
109 case WAIT_FOR_ARMING_STATE_REACHED:
110 if (is_arming_state_reached_check__()) {
111 result_ptr->state_changed = true;
112 return Status::SUCCESS;
113 }
114 break;
115 }
116
117 return Status::RUNNING;
118 }
119};
120
121} // namespace auto_apms_px4
122
123#include "rclcpp_components/register_node_macro.hpp"
124RCLCPP_COMPONENTS_REGISTER_NODE(auto_apms_px4::ArmDisarmSkill)
ActionWrapper(const std::string &action_name, rclcpp::Node::SharedPtr node_ptr, std::shared_ptr< ActionContextType > action_context_ptr)
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.
Definition mode.hpp:28