AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
offset_global_position.cpp
1// Copyright 2026 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 <Eigen/Core>
16
17#include "auto_apms_behavior_tree_core/node.hpp"
18#include "px4_ros2/utils/geodesic.hpp"
19
20#define INPUT_KEY_LAT "lat"
21#define INPUT_KEY_LON "lon"
22#define INPUT_KEY_ALT "alt"
23#define INPUT_KEY_N "north"
24#define INPUT_KEY_E "east"
25#define INPUT_KEY_D "down"
26#define OUTPUT_KEY_LAT "target_lat"
27#define OUTPUT_KEY_LON "target_lon"
28#define OUTPUT_KEY_ALT "target_alt"
29#define OUTPUT_KEY_VEC "target_vector"
30
31namespace auto_apms_px4
32{
33
43class OffsetGlobalPosition : public BT::SyncActionNode
44{
45public:
46 using SyncActionNode::SyncActionNode;
47
48 static BT::PortsList providedPorts()
49 {
50 return {
51 BT::InputPort<double>(INPUT_KEY_LAT, "{lat}", "Latitude [°] of the position to offset"),
52 BT::InputPort<double>(INPUT_KEY_LON, "{lon}", "Longitude [°] of the position to offset"),
53 BT::InputPort<double>(INPUT_KEY_ALT, "{alt}", "Altitude [m] (AMSL) of the position to offset"),
54 BT::InputPort<double>(INPUT_KEY_N, 0.0, "Offset towards north [m]"),
55 BT::InputPort<double>(INPUT_KEY_E, 0.0, "Offset towards east [m]"),
56 BT::InputPort<double>(INPUT_KEY_D, 0.0, "Offset downwards [m] (negative to go up)"),
57 BT::OutputPort<double>(OUTPUT_KEY_LAT, "{target_lat}", "Resulting latitude [°]"),
58 BT::OutputPort<double>(OUTPUT_KEY_LON, "{target_lon}", "Resulting longitude [°]"),
59 BT::OutputPort<double>(OUTPUT_KEY_ALT, "{target_alt}", "Resulting altitude [m] (AMSL)"),
60 BT::OutputPort<Eigen::MatrixXd>(
61 OUTPUT_KEY_VEC, "{target_vec}",
62 "Resulting global position vector (latitude [°], longitude [°], altitude AMSL [m])")};
63 }
64
65 BT::NodeStatus tick() override final
66 {
67 auto get_input = [this](const char * key) {
68 const BT::Expected<double> expected = getInput<double>(key);
69 if (!expected) {
70 throw BT::RuntimeError(name() + " (" + registrationName() + ") - " + expected.error());
71 }
72 return expected.value();
73 };
74 const Eigen::Vector3d global_position(get_input(INPUT_KEY_LAT), get_input(INPUT_KEY_LON), get_input(INPUT_KEY_ALT));
75 const Eigen::Vector3f vector_ned =
76 Eigen::Vector3d(get_input(INPUT_KEY_N), get_input(INPUT_KEY_E), get_input(INPUT_KEY_D)).cast<float>();
77
78 const Eigen::Vector3d target = px4_ros2::addVectorToGlobalPosition(global_position, vector_ned);
79 Eigen::MatrixXd mat(1, 3);
80 mat << target.x(), target.y(), target.z();
81 setOutput(OUTPUT_KEY_LAT, target.x());
82 setOutput(OUTPUT_KEY_LON, target.y());
83 setOutput(OUTPUT_KEY_ALT, target.z());
84 setOutput(OUTPUT_KEY_VEC, mat);
85 return BT::NodeStatus::SUCCESS;
86 }
87};
88
89} // namespace auto_apms_px4
90
Behavior tree node that offsets a global WGS84 position by a local NED vector given in meters.
#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.