15#include "auto_apms_interfaces/action/arm_disarm.hpp"
20#include "px4_msgs/msg/vehicle_status.hpp"
29 WAIT_FOR_READY_TO_ARM,
31 WAIT_FOR_ARMING_STATE_REACHED
34 rclcpp::Subscription<px4_msgs::msg::VehicleStatus>::SharedPtr vehicle_status_sub_ptr_;
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_;
43 explicit ArmDisarmTask(
const rclcpp::NodeOptions& options)
46 vehicle_status_sub_ptr_ = this->
node_ptr_->create_subscription<px4_msgs::msg::VehicleStatus>(
47 "/fmu/out/vehicle_status", rclcpp::QoS(1).best_effort(), [
this](px4_msgs::msg::VehicleStatus::UniquePtr msg) {
48 is_armed_ = msg->arming_state == px4_msgs::msg::VehicleStatus::ARMING_STATE_ARMED;
49 ready_to_arm_ = msg->pre_flight_checks_pass;
54 bool onGoalRequest(std::shared_ptr<const Goal> goal_ptr)
override final
58 if (goal_ptr->arming_state == Goal::ARMING_STATE_ARM && !goal_ptr->wait_until_ready_to_arm && !ready_to_arm_)
60 RCLCPP_ERROR(this->
node_ptr_->get_logger(),
61 "UAV can currently not be armed and wait_until_ready_to_arm is false.");
65 if (goal_ptr->arming_state == Goal::ARMING_STATE_ARM)
68 state_ = ready_to_arm_ ? SEND_COMMAND : WAIT_FOR_READY_TO_ARM;
69 is_arming_state_reached_check__ = [
this]() {
return is_armed_; };
70 send_command_callback_ = [
this]() {
return vehicle_command_client_.Arm(); };
75 state_ = SEND_COMMAND;
76 is_arming_state_reached_check__ = [
this]() {
return !is_armed_; };
77 send_command_callback_ = [
this]() {
return vehicle_command_client_.Disarm(); };
81 if ((goal_ptr->arming_state == Goal::ARMING_STATE_ARM && is_armed_) ||
82 (goal_ptr->arming_state == Goal::ARMING_STATE_DISARM && !is_armed_))
84 state_ = WAIT_FOR_ARMING_STATE_REACHED;
90 Status executeGoal(std::shared_ptr<const Goal> goal_ptr, std::shared_ptr<Feedback> feedback_ptr,
91 std::shared_ptr<Result> result_ptr)
override final
98 case WAIT_FOR_READY_TO_ARM:
101 state_ = SEND_COMMAND;
105 if (send_command_callback_())
107 state_ = WAIT_FOR_ARMING_STATE_REACHED;
111 RCLCPP_ERROR(this->
node_ptr_->get_logger(),
"Couldn't send arm/disarm command. Aborting...");
112 result_ptr->state_changed =
false;
113 return Status::FAILURE;
116 case WAIT_FOR_ARMING_STATE_REACHED:
117 if (is_arming_state_reached_check__())
119 result_ptr->state_changed =
true;
120 return Status::SUCCESS;
125 return Status::RUNNING;
131#include "rclcpp_components/register_node_macro.hpp"
132RCLCPP_COMPONENTS_REGISTER_NODE(auto_apms_px4::ArmDisarmTask)
Generic base class for robot actions.
rclcpp::Node::SharedPtr node_ptr_
Task(const std::string &name, rclcpp::Node::SharedPtr node_ptr, std::shared_ptr< ActionContextType > action_context_ptr, std::chrono::milliseconds execution_interval=DEFAULT_VALUE_EXECUTION_INTERVAL, std::chrono::milliseconds feedback_interval=DEFAULT_VALUE_FEEDBACK_INTERVAL)
const char ARM_DISARM_TASK_NAME[]