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"
21#include "px4_ros2/utils/message_version.hpp"
23#define OUTPUT_KEY_LAT "lat"
24#define OUTPUT_KEY_LON "lon"
25#define OUTPUT_KEY_ALT "alt"
26#define OUTPUT_KEY_N "north"
27#define OUTPUT_KEY_E "east"
28#define OUTPUT_KEY_D "down"
29#define OUTPUT_KEY_VEC "vector"
31using GlobalPositionMsg = px4_msgs::msg::VehicleGlobalPosition;
32using LocalPositionMsg = px4_msgs::msg::VehicleLocalPosition;
38class GetPosition :
public auto_apms_behavior_tree::core::RosSubscriberNode<T>
40 bool first_message_received_ =
false;
45 const std::string & instance_name,
const BT::NodeConfig & config,
46 const auto_apms_behavior_tree::core::RosNodeContext & context)
47 : auto_apms_behavior_tree::core::
RosSubscriberNode<T>{instance_name, config, context, rclcpp::SensorDataQoS{}}
52 const std::string base_topic =
53 std::is_same_v<T, GlobalPositionMsg> ?
"fmu/out/vehicle_global_position" :
"fmu/out/vehicle_local_position";
57 static BT::PortsList providedPorts()
59 if constexpr (std::is_same_v<T, GlobalPositionMsg>) {
61 BT::OutputPort<Eigen::MatrixXd>(
62 OUTPUT_KEY_VEC,
"{pos_vec}",
63 "Current global position vector (latitude [°], longitude [°], altitude AMSL [m])"),
64 BT::OutputPort<double>(OUTPUT_KEY_LAT,
"{lat}",
"Current latitude in degree [°]"),
65 BT::OutputPort<double>(OUTPUT_KEY_LON,
"{lon}",
"Current longitude in degree [°]"),
66 BT::OutputPort<double>(OUTPUT_KEY_ALT,
"{alt}",
"Current altitude in meter (AMSL)")};
69 BT::OutputPort<Eigen::MatrixXd>(
70 OUTPUT_KEY_VEC,
"{pos_vec}",
"Current local position vector (north [m], east [m], down [m])"),
71 BT::OutputPort<double>(OUTPUT_KEY_N,
"{north}",
"Current north [m] relative to origin"),
72 BT::OutputPort<double>(OUTPUT_KEY_E,
"{east}",
"Current east [m] relative to origin"),
73 BT::OutputPort<double>(OUTPUT_KEY_D,
"{down}",
"Current down [m] relative to origin")};
77 BT::NodeStatus onTick(
const std::shared_ptr<T> & last_msg_ptr)
final
81 first_message_received_ =
true;
82 last_msg_ = *last_msg_ptr;
85 if (!first_message_received_)
return BT::NodeStatus::FAILURE;
87 if constexpr (std::is_same_v<T, GlobalPositionMsg>) {
88 Eigen::MatrixXd mat(1, 3);
89 mat(0, 0) = last_msg_.lat;
90 mat(0, 1) = last_msg_.lon;
91 mat(0, 2) = last_msg_.alt;
92 this->setOutput(OUTPUT_KEY_LAT, mat(0, 0));
93 this->setOutput(OUTPUT_KEY_LON, mat(0, 1));
94 this->setOutput(OUTPUT_KEY_ALT, mat(0, 2));
95 this->setOutput(OUTPUT_KEY_VEC, mat);
97 Eigen::MatrixXd mat(1, 3);
98 mat(0, 0) = last_msg_.x;
99 mat(0, 1) = last_msg_.y;
100 mat(0, 2) = last_msg_.z;
101 this->setOutput(OUTPUT_KEY_N, mat(0, 0));
102 this->setOutput(OUTPUT_KEY_E, mat(0, 1));
103 this->setOutput(OUTPUT_KEY_D, mat(0, 2));
104 this->setOutput(OUTPUT_KEY_VEC, mat);
107 return BT::NodeStatus::SUCCESS;
RosSubscriberNode(const std::string &instance_name, const Config &config, Context context, const rclcpp::QoS &qos={10})
bool createSubscriber(const std::string &topic_name)
#define AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE(type)
Macro for registering a behavior tree node plugin.
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.