18#include "px4_ros2/components/mode.hpp"
19#include "px4_ros2/odometry/attitude.hpp"
20#include "px4_ros2/odometry/global_position.hpp"
21#include "px4_ros2/odometry/local_position.hpp"
22#include "px4_ros2/utils/geodesic.hpp"
23#include "px4_ros2/utils/geometry.hpp"
31template <
class ActionT>
40 ModeBase(rclcpp::Node& node,
const Settings& settings,
const std::string& topic_namespace_prefix,
41 std::shared_ptr<ActionContextType> action_context_ptr)
42 : px4_ros2::
ModeBase{ node, settings, topic_namespace_prefix }, action_context_ptr_{ action_context_ptr }
47 virtual void OnActivateWithGoal(std::shared_ptr<const Goal> goal_ptr);
48 virtual void UpdateSetpointWithGoal(
float dt_s, std::shared_ptr<const Goal> goal_ptr,
49 std::shared_ptr<Feedback> feedback_ptr, std::shared_ptr<Result> result_ptr) = 0;
50 virtual void onDeactivate()
override
54 void onActivate()
override;
55 void updateSetpoint(
float dt_s)
override;
57 const std::shared_ptr<ActionContextType> action_context_ptr_;
60template <
class ActionT>
61void ModeBase<ActionT>::OnActivateWithGoal(std::shared_ptr<const Goal> goal_ptr)
66template <
class ActionT>
67void ModeBase<ActionT>::onActivate()
69 OnActivateWithGoal(action_context_ptr_->getGoalHandlePtr()->get_goal());
72template <
class ActionT>
73void ModeBase<ActionT>::updateSetpoint(
float dt_s)
75 UpdateSetpointWithGoal(dt_s, action_context_ptr_->getGoalHandlePtr()->get_goal(),
76 action_context_ptr_->getFeedbackPtr(), action_context_ptr_->getResultPtr());
79template <
class ActionT>
86 const std::string& topic_namespace_prefix, std::shared_ptr<ActionContextType> action_context_ptr)
87 :
ModeBase<ActionT>{ node, settings, topic_namespace_prefix, action_context_ptr }
104template <
class ActionT>
107 static constexpr float kPositionErrorThresholdMeter = 0.5f;
108 static constexpr float kVelocityErrorThresholdMeterPerSecond = 0.3f;
109 const float position_error_m =
110 px4_ros2::distanceToGlobalPosition(vehicle_global_position_ptr_->position(), target_position_f_glob);
111 return (position_error_m < kPositionErrorThresholdMeter) &&
112 (vehicle_local_position_ptr_->velocityNed().norm() < kVelocityErrorThresholdMeterPerSecond);
115template <
class ActionT>
118 auto target_position_f_glob = vehicle_global_position_ptr_->position();
119 target_position_f_glob.z() = target_altitude_amsl_m;
120 return IsPositionReached(target_position_f_glob);
123template <
class ActionT>
126 using namespace px4_ros2::literals;
127 static constexpr float kHeadingErrorThresholdRad = 7.0_deg;
128 const float heading_error_wrapped = px4_ros2::wrapPi(target_heading_rad - vehicle_attitude_ptr_->yaw());
129 return fabsf(heading_error_wrapped) < kHeadingErrorThresholdRad;
typename ActionT::Goal Goal
typename ActionT::Result Result
typename ActionT::Feedback Feedback
Class to model the mode referring to an external task.
typename ActionContextType::Feedback Feedback
ModeBase(rclcpp::Node &node, const Settings &settings, const std::string &topic_namespace_prefix, std::shared_ptr< ActionContextType > action_context_ptr)
typename ActionContextType::Goal Goal
typename ActionContextType::Result Result
std::shared_ptr< px4_ros2::OdometryGlobalPosition > vehicle_global_position_ptr_
std::shared_ptr< px4_ros2::OdometryAttitude > vehicle_attitude_ptr_
std::shared_ptr< px4_ros2::OdometryLocalPosition > vehicle_local_position_ptr_
bool IsHeadingReached(float target_heading_rad) const
PositionAwareMode(rclcpp::Node &node, const px4_ros2::ModeBase::Settings &settings, const std::string &topic_namespace_prefix, std::shared_ptr< ActionContextType > action_context_ptr)
bool IsAltitudeReached(float target_altitude_amsl_m) const
bool IsPositionReached(const Eigen::Vector3d &target_position_f_glob) const