AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
check_nav_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_NAV_STATE "nav_state"
23
24namespace auto_apms_px4
25{
26
38class CheckNavState : 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 CheckNavState(
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>(INPUT_KEY_NAV_STATE, "Expected PX4 navigation state (mode id) the vehicle should be in."),
58 });
59 }
60
61 BT::NodeStatus onTick(const std::shared_ptr<px4_msgs::msg::VehicleStatus> & last_msg_ptr) final
62 {
63 if (last_msg_ptr) last_msg_ = last_msg_ptr;
64
65 // No vehicle status received yet - the mode cannot be confirmed.
66 if (!last_msg_) return BT::NodeStatus::FAILURE;
67
68 const BT::Expected<int> expected_nav_state = getInput<int>(INPUT_KEY_NAV_STATE);
69 if (!expected_nav_state) {
70 RCLCPP_ERROR(
71 logger_, "%s - Missing required input '%s': %s", context_.getFullyQualifiedTreeNodeName(this).c_str(),
72 INPUT_KEY_NAV_STATE, expected_nav_state.error().c_str());
73 return BT::NodeStatus::FAILURE;
74 }
75
76 return last_msg_->nav_state == static_cast<uint8_t>(expected_nav_state.value()) ? BT::NodeStatus::SUCCESS
77 : BT::NodeStatus::FAILURE;
78 }
79};
80
81} // namespace auto_apms_px4
82
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 active PX4 navigation 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.