AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
mode.hpp
Go to the documentation of this file.
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// https://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
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"
24
25namespace auto_apms_px4
26{
27
31template <class ActionT>
32class ModeBase : public px4_ros2::ModeBase
33{
34protected:
36 using Goal = typename ActionContextType::Goal;
39
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 }
43 {
44 }
45
46private:
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
51 {
52 }
53
54 void onActivate() override;
55 void updateSetpoint(float dt_s) override;
56
57 const std::shared_ptr<ActionContextType> action_context_ptr_;
58};
59
60template <class ActionT>
61void ModeBase<ActionT>::OnActivateWithGoal(std::shared_ptr<const Goal> goal_ptr)
62{
63 (void)goal_ptr;
64}
65
66template <class ActionT>
67void ModeBase<ActionT>::onActivate()
68{
69 OnActivateWithGoal(action_context_ptr_->getGoalHandlePtr()->get_goal());
70}
71
72template <class ActionT>
73void ModeBase<ActionT>::updateSetpoint(float dt_s)
74{
75 UpdateSetpointWithGoal(dt_s, action_context_ptr_->getGoalHandlePtr()->get_goal(),
76 action_context_ptr_->getFeedbackPtr(), action_context_ptr_->getResultPtr());
77}
78
79template <class ActionT>
80class PositionAwareMode : public ModeBase<ActionT>
81{
82protected:
84
85 PositionAwareMode(rclcpp::Node& node, const px4_ros2::ModeBase::Settings& settings,
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 }
88 {
89 vehicle_global_position_ptr_ = std::make_shared<px4_ros2::OdometryGlobalPosition>(*this);
90 vehicle_local_position_ptr_ = std::make_shared<px4_ros2::OdometryLocalPosition>(*this);
91 vehicle_attitude_ptr_ = std::make_shared<px4_ros2::OdometryAttitude>(*this);
92 }
93
94 bool IsPositionReached(const Eigen::Vector3d& target_position_f_glob) const;
95 bool IsAltitudeReached(float target_altitude_amsl_m) const;
96 bool IsHeadingReached(float target_heading_rad) const;
97
98protected:
99 std::shared_ptr<px4_ros2::OdometryGlobalPosition> vehicle_global_position_ptr_;
100 std::shared_ptr<px4_ros2::OdometryLocalPosition> vehicle_local_position_ptr_;
101 std::shared_ptr<px4_ros2::OdometryAttitude> vehicle_attitude_ptr_;
102};
103
104template <class ActionT>
105bool PositionAwareMode<ActionT>::IsPositionReached(const Eigen::Vector3d& target_position_f_glob) const
106{
107 static constexpr float kPositionErrorThresholdMeter = 0.5f; // [m]
108 static constexpr float kVelocityErrorThresholdMeterPerSecond = 0.3f; // [m/s]
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);
113}
114
115template <class ActionT>
116bool PositionAwareMode<ActionT>::IsAltitudeReached(float target_altitude_amsl_m) const
117{
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);
121}
122
123template <class ActionT>
124bool PositionAwareMode<ActionT>::IsHeadingReached(float target_heading_rad) const
125{
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;
130}
131
132} // namespace auto_apms_px4
typename ActionT::Goal Goal
typename ActionT::Result Result
typename ActionT::Feedback Feedback
Class to model the mode referring to an external task.
Definition mode.hpp:33
typename ActionContextType::Feedback Feedback
Definition mode.hpp:38
ModeBase(rclcpp::Node &node, const Settings &settings, const std::string &topic_namespace_prefix, std::shared_ptr< ActionContextType > action_context_ptr)
Definition mode.hpp:40
typename ActionContextType::Goal Goal
Definition mode.hpp:36
typename ActionContextType::Result Result
Definition mode.hpp:37
std::shared_ptr< px4_ros2::OdometryGlobalPosition > vehicle_global_position_ptr_
Definition mode.hpp:99
std::shared_ptr< px4_ros2::OdometryAttitude > vehicle_attitude_ptr_
Definition mode.hpp:101
std::shared_ptr< px4_ros2::OdometryLocalPosition > vehicle_local_position_ptr_
Definition mode.hpp:100
bool IsHeadingReached(float target_heading_rad) const
Definition mode.hpp:124
PositionAwareMode(rclcpp::Node &node, const px4_ros2::ModeBase::Settings &settings, const std::string &topic_namespace_prefix, std::shared_ptr< ActionContextType > action_context_ptr)
Definition mode.hpp:85
bool IsAltitudeReached(float target_altitude_amsl_m) const
Definition mode.hpp:116
bool IsPositionReached(const Eigen::Vector3d &target_position_f_glob) const
Definition mode.hpp:105