15#include <Eigen/Geometry>
18#include "auto_apms_behavior_tree_core/node.hpp"
19#include "px4_msgs/msg/vehicle_global_position.hpp"
20#include "px4_msgs/msg/vehicle_local_position.hpp"
22#define OUTPUT_KEY_LAT "lat"
23#define OUTPUT_KEY_LON "lon"
24#define OUTPUT_KEY_ALT "alt"
25#define OUTPUT_KEY_N "north"
26#define OUTPUT_KEY_E "east"
27#define OUTPUT_KEY_D "down"
28#define OUTPUT_KEY_VEC "vector"
30using GlobalPositionMsg = px4_msgs::msg::VehicleGlobalPosition;
31using LocalPositionMsg = px4_msgs::msg::VehicleLocalPosition;
37class GetPosition :
public auto_apms_behavior_tree::core::RosSubscriberNode<T>
39 bool first_message_received_ =
false;
44 const std::string & instance_name,
const BT::NodeConfig & config,
45 const auto_apms_behavior_tree::core::RosNodeContext & context)
46 : auto_apms_behavior_tree::core::
RosSubscriberNode<T>{instance_name, config, context, rclcpp::SensorDataQoS{}}
50 static BT::PortsList providedPorts()
52 if constexpr (std::is_same_v<T, GlobalPositionMsg>) {
54 BT::OutputPort<Eigen::MatrixXd>(
55 OUTPUT_KEY_VEC,
"{pos_vec}",
56 "Current global position vector (latitude [°], longitude [°], altitude AMSL [m])"),
57 BT::OutputPort<double>(OUTPUT_KEY_LAT,
"{lat}",
"Current latitude in degree [°]"),
58 BT::OutputPort<double>(OUTPUT_KEY_LON,
"{lon}",
"Current longitude in degree [°]"),
59 BT::OutputPort<double>(OUTPUT_KEY_ALT,
"{alt}",
"Current altitude in meter (AMSL)")};
62 BT::OutputPort<Eigen::MatrixXd>(
63 OUTPUT_KEY_VEC,
"{pos_vec}",
"Current local position vector (north [m], east [m], down [m])"),
64 BT::OutputPort<double>(OUTPUT_KEY_N,
"{north}",
"Current north [m] relative to origin"),
65 BT::OutputPort<double>(OUTPUT_KEY_E,
"{east}",
"Current east [m] relative to origin"),
66 BT::OutputPort<double>(OUTPUT_KEY_D,
"{down}",
"Current down [m] relative to origin")};
70 BT::NodeStatus onTick(
const std::shared_ptr<T> & last_msg_ptr)
final
74 first_message_received_ =
true;
75 last_msg_ = *last_msg_ptr;
78 if (!first_message_received_)
return BT::NodeStatus::FAILURE;
80 if constexpr (std::is_same_v<T, GlobalPositionMsg>) {
81 Eigen::MatrixXd mat(1, 3);
82 mat(0, 0) = last_msg_.lat;
83 mat(0, 1) = last_msg_.lon;
84 mat(0, 2) = last_msg_.alt;
85 this->setOutput(OUTPUT_KEY_LAT, mat(0, 0));
86 this->setOutput(OUTPUT_KEY_LON, mat(0, 1));
87 this->setOutput(OUTPUT_KEY_ALT, mat(0, 2));
88 this->setOutput(OUTPUT_KEY_VEC, mat);
90 Eigen::MatrixXd mat(1, 3);
91 mat(0, 0) = last_msg_.x;
92 mat(0, 1) = last_msg_.y;
93 mat(0, 2) = last_msg_.z;
94 this->setOutput(OUTPUT_KEY_N, mat(0, 0));
95 this->setOutput(OUTPUT_KEY_E, mat(0, 1));
96 this->setOutput(OUTPUT_KEY_D, mat(0, 2));
97 this->setOutput(OUTPUT_KEY_VEC, mat);
100 return BT::NodeStatus::SUCCESS;
RosSubscriberNode(const std::string &instance_name, const Config &config, Context context, const rclcpp::QoS &qos={10})
#define AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(type)
Macro for registering a behavior tree node plugin.
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.