15#include "px4_ros2/control/setpoint_types/goto.hpp"
19#include "auto_apms_interfaces/action/go_to.hpp"
20#include "auto_apms_px4/mode_executor.hpp"
21#include "px4_ros2/utils/geodesic.hpp"
22#include "tf2_eigen/tf2_eigen.hpp"
27using GoToActionType = auto_apms_interfaces::action::GoTo;
29class GoToMode :
public PositionAwareMode<GoToActionType>
31 std::shared_ptr<px4_ros2::GotoGlobalSetpointType> goto_setpoint_ptr_;
32 Eigen::Vector3d position_target_f_glob_;
33 float heading_target_rad_;
37 rclcpp::Node & node,
const px4_ros2::ModeBase::Settings & settings,
const std::string & topic_namespace_prefix,
38 std::shared_ptr<ActionContextType> action_context_ptr)
39 : PositionAwareMode{node, settings, topic_namespace_prefix, action_context_ptr}
41 goto_setpoint_ptr_ = std::make_shared<px4_ros2::GotoGlobalSetpointType>(*
this);
45 void OnActivateWithGoal(std::shared_ptr<const Goal> goal_ptr)
final
47 position_target_f_glob_ = Eigen::Vector3d(goal_ptr->lat, goal_ptr->lon, goal_ptr->alt);
50 if (goal_ptr->head_towards_destination) {
53 px4_ros2::headingToGlobalPosition(vehicle_global_position_ptr_->position(), position_target_f_glob_);
55 heading_target_rad_ = goal_ptr->heading_clockwise_from_north_rad;
58 auto double_to_string = [](
double num,
int decimals) {
59 std::stringstream stream;
60 stream << std::fixed << std::setprecision(decimals) << num;
65 node().get_logger(),
"GoTo target: Position: [%s°, %s°, %sm] - Heading: %f",
66 double_to_string(position_target_f_glob_.x(), 10).c_str(),
67 double_to_string(position_target_f_glob_.y(), 10).c_str(),
68 double_to_string(position_target_f_glob_.z(), 2).c_str(), heading_target_rad_);
70 void UpdateSetpointWithGoal(
71 float dt_s, std::shared_ptr<const Goal> goal_ptr, std::shared_ptr<Feedback> feedback_ptr,
72 std::shared_ptr<Result> result_ptr)
final
78 goto_setpoint_ptr_->update(position_target_f_glob_, heading_target_rad_);
81 Eigen::Vector3d distance_f_ned =
82 px4_ros2::vectorToGlobalPosition(vehicle_global_position_ptr_->position(), position_target_f_glob_)
84 tf2::convert(distance_f_ned, feedback_ptr->remaining_distance_f_ned);
87 feedback_ptr->remaining_time_s =
88 distance_f_ned.norm() /
89 (vehicle_local_position_ptr_->velocityNed().cast<double>().dot(distance_f_ned.normalized()));
91 if (IsPositionReached(position_target_f_glob_)) {
92 completed(px4_ros2::Result::Success);
100 explicit GoToSkill(
const rclcpp::NodeOptions & options)
101 : ModeExecutorFactory{_AUTO_APMS_PX4__GOTO_ACTION_NAME, options}
108#include "rclcpp_components/register_node_macro.hpp"
109RCLCPP_COMPONENTS_REGISTER_NODE(auto_apms_px4::GoToSkill)
Helper template class that creates a ModeExecutor for a custom PX4 mode implemented by inheriting fro...
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.