AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
check_arming_state.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// 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
15#include <cstdint>
16#include <memory>
17
18#include "auto_apms_behavior_tree_core/node.hpp"
19#include "px4_msgs/msg/vehicle_status.hpp"
20#include "px4_ros2/utils/message_version.hpp"
21
22#define INPUT_KEY_ARMING_STATE "arming_state"
23
24namespace auto_apms_px4
25{
26
38class CheckArmingState : public auto_apms_behavior_tree::core::RosSubscriberNode<px4_msgs::msg::VehicleStatus>
39{
40 std::shared_ptr<px4_msgs::msg::VehicleStatus> last_msg_;
41
42public:
43 CheckArmingState(
44 const std::string & instance_name, const BT::NodeConfig & config,
46 : RosSubscriberNode{instance_name, config, context, rclcpp::SensorDataQoS{}}
47 {
48 // Resolve the topic's message version suffix at runtime from the message definition, rather than baking it into
49 // the node manifest at configure time. Since the manifest does not fix a topic, the base class cannot resolve one
50 // at construction and defers creation to this constructor.
51 createSubscriber("fmu/out/vehicle_status" + px4_ros2::getMessageNameVersion<px4_msgs::msg::VehicleStatus>());
52 }
53
54 static BT::PortsList providedPorts()
55 {
56 return providedBasicPorts({
57 BT::InputPort<int>(
58 INPUT_KEY_ARMING_STATE, "Expected PX4 arming state the vehicle should be in (1 = disarmed, 2 = armed)."),
59 });
60 }
61
62 BT::NodeStatus onTick(const std::shared_ptr<px4_msgs::msg::VehicleStatus> & last_msg_ptr) final
63 {
64 if (last_msg_ptr) last_msg_ = last_msg_ptr;
65
66 // No vehicle status received yet - the arming state cannot be confirmed.
67 if (!last_msg_) return BT::NodeStatus::FAILURE;
68
69 const BT::Expected<int> expected_arming_state = getInput<int>(INPUT_KEY_ARMING_STATE);
70 if (!expected_arming_state) {
71 RCLCPP_ERROR(
72 logger_, "%s - Missing required input '%s': %s", context_.getFullyQualifiedTreeNodeName(this).c_str(),
73 INPUT_KEY_ARMING_STATE, expected_arming_state.error().c_str());
74 return BT::NodeStatus::FAILURE;
75 }
76
77 return last_msg_->arming_state == static_cast<uint8_t>(expected_arming_state.value()) ? BT::NodeStatus::SUCCESS
78 : BT::NodeStatus::FAILURE;
79 }
80};
81
82} // namespace auto_apms_px4
83
Additional parameters specific to ROS 2 determined at runtime by TreeBuilder.
Generic behavior tree node wrapper for a ROS 2 subscriber.
RosSubscriberNode(const std::string &instance_name, const Config &config, Context context, const rclcpp::QoS &qos={10})
Condition node that succeeds once the vehicle's arming state matches the expected one.
#define AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE(type)
Macro for registering a behavior tree node plugin.
Definition node.hpp:50
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.