38template <
class ActionT>
39class ModeBase :
public px4_ros2::ModeBase
43 using Goal =
typename ActionContextType::Goal;
44 using Result =
typename ActionContextType::Result;
45 using Feedback =
typename ActionContextType::Feedback;
48 rclcpp::Node & node,
const Settings & settings,
const std::string & topic_namespace_prefix,
49 std::shared_ptr<ActionContextType> action_context_ptr)
50 : px4_ros2::ModeBase{node, settings, topic_namespace_prefix}, action_context_ptr_{action_context_ptr}
55 virtual void OnActivateWithGoal(std::shared_ptr<const Goal> goal_ptr);
56 virtual void UpdateSetpointWithGoal(
57 float dt_s, std::shared_ptr<const Goal> goal_ptr, std::shared_ptr<Feedback> feedback_ptr,
58 std::shared_ptr<Result> result_ptr) = 0;
59 virtual void onDeactivate()
override {}
61 void onActivate()
override;
62 void updateSetpoint(
float dt_s)
override;
64 const std::shared_ptr<ActionContextType> action_context_ptr_;
39class ModeBase :
public px4_ros2::ModeBase {
…};
67template <
class ActionT>
68void ModeBase<ActionT>::OnActivateWithGoal(std::shared_ptr<const Goal> goal_ptr)
73template <
class ActionT>
74void ModeBase<ActionT>::onActivate()
76 OnActivateWithGoal(action_context_ptr_->getGoalHandlePtr()->get_goal());
79template <
class ActionT>
80void ModeBase<ActionT>::updateSetpoint(
float dt_s)
82 UpdateSetpointWithGoal(
83 dt_s, action_context_ptr_->getGoalHandlePtr()->get_goal(), action_context_ptr_->getFeedbackPtr(),
84 action_context_ptr_->getResultPtr());
87template <
class ActionT>
88class PositionAwareMode :
public ModeBase<ActionT>
91 using typename ModeBase<ActionT>::ActionContextType;
94 rclcpp::Node & node,
const px4_ros2::ModeBase::Settings & settings,
const std::string & topic_namespace_prefix,
95 std::shared_ptr<ActionContextType> action_context_ptr)
96 : ModeBase<ActionT>{node, settings, topic_namespace_prefix, action_context_ptr}
98 vehicle_global_position_ptr_ = std::make_shared<px4_ros2::OdometryGlobalPosition>(*
this);
99 vehicle_local_position_ptr_ = std::make_shared<px4_ros2::OdometryLocalPosition>(*
this);
100 vehicle_attitude_ptr_ = std::make_shared<px4_ros2::OdometryAttitude>(*
this);
103 bool IsGlobalPositionReached(
104 const Eigen::Vector3d & target_position_f_glob,
double reached_thresh_pos_m = 0.5,
105 double reached_thresh_vel_m_s = 0.3)
const;
107 bool IsLocalPositionReached(
108 const Eigen::Vector3d & target_position_f_ned,
double reached_thresh_pos_m = 0.5,
109 double reached_thresh_vel_m_s = 0.3)
const;
111 bool IsGlobalAltitudeReached(
112 float target_altitude_amsl_m,
double reached_thresh_pos_m = 0.5,
double reached_thresh_vel_m_s = 0.3)
const;
114 bool IsLocalAltitudeReached(
115 float target_altitude_hagl_m,
double reached_thresh_pos_m = 0.5,
double reached_thresh_vel_m_s = 0.3)
const;
117 bool IsHeadingReached(
float target_heading_rad,
double reached_thresh_heading_rad = 0.12)
const;
120 std::shared_ptr<px4_ros2::OdometryGlobalPosition> vehicle_global_position_ptr_;
121 std::shared_ptr<px4_ros2::OdometryLocalPosition> vehicle_local_position_ptr_;
122 std::shared_ptr<px4_ros2::OdometryAttitude> vehicle_attitude_ptr_;
125template <
class ActionT>
126bool PositionAwareMode<ActionT>::IsGlobalPositionReached(
127 const Eigen::Vector3d & target_position_f_glob,
double reached_thresh_pos_m,
double reached_thresh_vel_m_s)
const
129 const float position_error_m =
130 px4_ros2::distanceToGlobalPosition(vehicle_global_position_ptr_->position(), target_position_f_glob);
131 return (position_error_m < reached_thresh_pos_m) &&
132 (vehicle_local_position_ptr_->velocityNed().norm() <= reached_thresh_vel_m_s);
135template <
class ActionT>
136bool PositionAwareMode<ActionT>::IsLocalPositionReached(
137 const Eigen::Vector3d & target_position_f_ned,
double reached_thresh_pos_m,
double reached_thresh_vel_m_s)
const
139 const px4_msgs::msg::VehicleLocalPosition & pos = vehicle_local_position_ptr_->last();
140 const Eigen::Vector3d vec(pos.x, pos.y, pos.z);
141 const double position_error_m = (target_position_f_ned - vec).norm();
142 return (position_error_m < reached_thresh_pos_m) &&
143 (vehicle_local_position_ptr_->velocityNed().norm() <= reached_thresh_vel_m_s);
146template <
class ActionT>
147bool PositionAwareMode<ActionT>::IsGlobalAltitudeReached(
148 float target_altitude_amsl_m,
double reached_thresh_pos_m,
double reached_thresh_vel_m_s)
const
150 Eigen::Vector3d target_position_f_glob = vehicle_global_position_ptr_->position();
151 target_position_f_glob.z() = target_altitude_amsl_m;
152 return IsGlobalPositionReached(target_position_f_glob, reached_thresh_pos_m, reached_thresh_vel_m_s);
155template <
class ActionT>
156bool PositionAwareMode<ActionT>::IsLocalAltitudeReached(
157 float target_altitude_hagl_m,
double reached_thresh_pos_m,
double reached_thresh_vel_m_s)
const
159 const px4_msgs::msg::VehicleLocalPosition & pos = vehicle_local_position_ptr_->last();
160 const Eigen::Vector3d target_position_f_ned(pos.x, pos.y, target_altitude_hagl_m);
161 return IsLocalPositionReached(target_position_f_ned, reached_thresh_pos_m, reached_thresh_vel_m_s);
164template <
class ActionT>
165bool PositionAwareMode<ActionT>::IsHeadingReached(
float target_heading_rad,
double reached_thresh_heading_rad)
const
167 const float heading_error_wrapped =
168 px4_ros2::wrapPi(target_heading_rad - vehicle_attitude_ptr_->yaw());
169 return fabsf(heading_error_wrapped) <= fabsf(reached_thresh_heading_rad);
Helper class that stores contextual information related to a ROS 2 action.