AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
mode.hpp
1// Copyright 2024 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#pragma once
16
17#include <Eigen/Geometry>
18
19#include "auto_apms_util/action_context.hpp"
20#include "px4_ros2/components/mode.hpp"
21#include "px4_ros2/odometry/attitude.hpp"
22#include "px4_ros2/odometry/global_position.hpp"
23#include "px4_ros2/odometry/local_position.hpp"
24#include "px4_ros2/utils/geodesic.hpp"
25#include "px4_ros2/utils/geometry.hpp"
26
28{
29
40template <class ActionT>
41class ModeBase : public px4_ros2::ModeBase
42{
43protected:
44 using ActionContextType = auto_apms_util::ActionContext<ActionT>;
45 using Goal = typename ActionContextType::Goal;
46 using Result = typename ActionContextType::Result;
47 using Feedback = typename ActionContextType::Feedback;
48
49 ModeBase(rclcpp::Node & node, Settings settings, std::shared_ptr<ActionContextType> action_context_ptr)
50 : px4_ros2::ModeBase{node, settings}, action_context_ptr_{action_context_ptr}
51 {
52 }
53
54private:
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 {}
60
61 void onActivate() override;
62 void updateSetpoint(float dt_s) override;
63
64 const std::shared_ptr<ActionContextType> action_context_ptr_;
65};
66
67template <class ActionT>
68void ModeBase<ActionT>::OnActivateWithGoal(std::shared_ptr<const Goal> goal_ptr)
69{
70 (void)goal_ptr;
71}
72
73template <class ActionT>
74void ModeBase<ActionT>::onActivate()
75{
76 OnActivateWithGoal(action_context_ptr_->getGoalHandlePtr()->get_goal());
77}
78
79template <class ActionT>
80void ModeBase<ActionT>::updateSetpoint(float dt_s)
81{
82 UpdateSetpointWithGoal(
83 dt_s, action_context_ptr_->getGoalHandlePtr()->get_goal(), action_context_ptr_->getFeedbackPtr(),
84 action_context_ptr_->getResultPtr());
85}
86
87template <class ActionT>
88class PositionAwareMode : public ModeBase<ActionT>
89{
90protected:
91 using typename ModeBase<ActionT>::ActionContextType;
92
93 PositionAwareMode(
94 rclcpp::Node & node, px4_ros2::ModeBase::Settings settings, std::shared_ptr<ActionContextType> action_context_ptr)
95 : ModeBase<ActionT>{node, settings, action_context_ptr}
96 {
97 vehicle_global_position_ptr_ = std::make_shared<px4_ros2::OdometryGlobalPosition>(*this);
98 vehicle_local_position_ptr_ = std::make_shared<px4_ros2::OdometryLocalPosition>(*this);
99 vehicle_attitude_ptr_ = std::make_shared<px4_ros2::OdometryAttitude>(*this);
100 }
101
102 bool IsGlobalPositionReached(
103 const Eigen::Vector3d & target_position_f_glob, double reached_thresh_pos_m = 0.5,
104 double reached_thresh_vel_m_s = 0.3) const;
105
106 bool IsLocalPositionReached(
107 const Eigen::Vector3d & target_position_f_ned, double reached_thresh_pos_m = 0.5,
108 double reached_thresh_vel_m_s = 0.3) const;
109
110 bool IsGlobalAltitudeReached(
111 float target_altitude_amsl_m, double reached_thresh_pos_m = 0.5, double reached_thresh_vel_m_s = 0.3) const;
112
113 bool IsLocalAltitudeReached(
114 float target_altitude_hagl_m, double reached_thresh_pos_m = 0.5, double reached_thresh_vel_m_s = 0.3) const;
115
116 bool IsHeadingReached(float target_heading_rad, double reached_thresh_heading_rad = 0.12) const;
117
118protected:
119 std::shared_ptr<px4_ros2::OdometryGlobalPosition> vehicle_global_position_ptr_;
120 std::shared_ptr<px4_ros2::OdometryLocalPosition> vehicle_local_position_ptr_;
121 std::shared_ptr<px4_ros2::OdometryAttitude> vehicle_attitude_ptr_;
122};
123
124template <class ActionT>
125bool PositionAwareMode<ActionT>::IsGlobalPositionReached(
126 const Eigen::Vector3d & target_position_f_glob, double reached_thresh_pos_m, double reached_thresh_vel_m_s) const
127{
128 const float position_error_m =
129 px4_ros2::distanceToGlobalPosition(vehicle_global_position_ptr_->position(), target_position_f_glob);
130 return (position_error_m < reached_thresh_pos_m) &&
131 (vehicle_local_position_ptr_->velocityNed().norm() <= reached_thresh_vel_m_s);
132}
133
134template <class ActionT>
135bool PositionAwareMode<ActionT>::IsLocalPositionReached(
136 const Eigen::Vector3d & target_position_f_ned, double reached_thresh_pos_m, double reached_thresh_vel_m_s) const
137{
138 const px4_msgs::msg::VehicleLocalPosition & pos = vehicle_local_position_ptr_->last();
139 const Eigen::Vector3d vec(pos.x, pos.y, pos.z);
140 const double position_error_m = (target_position_f_ned - vec).norm();
141 return (position_error_m < reached_thresh_pos_m) &&
142 (vehicle_local_position_ptr_->velocityNed().norm() <= reached_thresh_vel_m_s);
143}
144
145template <class ActionT>
146bool PositionAwareMode<ActionT>::IsGlobalAltitudeReached(
147 float target_altitude_amsl_m, double reached_thresh_pos_m, double reached_thresh_vel_m_s) const
148{
149 Eigen::Vector3d target_position_f_glob = vehicle_global_position_ptr_->position();
150 target_position_f_glob.z() = target_altitude_amsl_m;
151 return IsGlobalPositionReached(target_position_f_glob, reached_thresh_pos_m, reached_thresh_vel_m_s);
152}
153
154template <class ActionT>
155bool PositionAwareMode<ActionT>::IsLocalAltitudeReached(
156 float target_altitude_hagl_m, double reached_thresh_pos_m, double reached_thresh_vel_m_s) const
157{
158 const px4_msgs::msg::VehicleLocalPosition & pos = vehicle_local_position_ptr_->last();
159 const Eigen::Vector3d target_position_f_ned(pos.x, pos.y, target_altitude_hagl_m);
160 return IsLocalPositionReached(target_position_f_ned, reached_thresh_pos_m, reached_thresh_vel_m_s);
161}
162
163template <class ActionT>
164bool PositionAwareMode<ActionT>::IsHeadingReached(float target_heading_rad, double reached_thresh_heading_rad) const
165{
166 const float heading_error_wrapped = px4_ros2::wrapPi(target_heading_rad - vehicle_attitude_ptr_->yaw());
167 return fabsf(heading_error_wrapped) <= fabsf(reached_thresh_heading_rad);
168}
169
170} // namespace auto_apms_px4
Generic template class for a custom PX4 mode.
Definition mode.hpp:42
Helper class that stores contextual information related to a ROS 2 action.
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.
Definition mode.hpp:28