AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
action_context.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
17#include "rclcpp_action/rclcpp_action.hpp"
18
19namespace auto_apms_core
20{
21
22template <typename ActionT>
24{
25public:
26 using Type = ActionT;
27 using Goal = typename ActionT::Goal;
28 using Feedback = typename ActionT::Feedback;
29 using Result = typename ActionT::Result;
30 using GoalHandle = rclcpp_action::ServerGoalHandle<ActionT>;
31
32 ActionContext(const rclcpp::Logger& logger);
33
34 void setUp(std::shared_ptr<GoalHandle> goal_handle_ptr);
35
37
38 void succeed();
39
40 void cancel();
41
42 void abort();
43
44 void invalidate();
45
46 bool isValid();
47
48 std::shared_ptr<GoalHandle> getGoalHandlePtr();
49
50 std::shared_ptr<Feedback> getFeedbackPtr();
51
52 std::shared_ptr<Result> getResultPtr();
53
54private:
55 const rclcpp::Logger logger_;
56 std::shared_ptr<GoalHandle> goal_handle_ptr_;
57 const std::shared_ptr<Feedback> feedback_ptr_{ std::make_shared<Feedback>() };
58 const std::shared_ptr<Result> result_ptr_{ std::make_shared<Result>() };
59};
60
65template <class ActionT>
66ActionContext<ActionT>::ActionContext(const rclcpp::Logger& logger) : logger_{ logger }
67{
68}
69
70template <class ActionT>
71void ActionContext<ActionT>::setUp(std::shared_ptr<GoalHandle> goal_handle_ptr)
72{
73 goal_handle_ptr_ = goal_handle_ptr;
74 *feedback_ptr_ = Feedback{};
75 *result_ptr_ = Result{};
76}
77
78template <class ActionT>
80{
81 if (!goal_handle_ptr_)
82 {
83 RCLCPP_FATAL(logger_, "Tried to publish feedback on a goal, but no goal handle was set up.");
84 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
85 }
86 if (goal_handle_ptr_->is_executing())
87 {
88 goal_handle_ptr_->publish_feedback(feedback_ptr_);
89 }
90 else
91 {
92 RCLCPP_WARN(logger_, "The node tried to publish feedback on goal %s which is not executing. Ignoring ...",
93 rclcpp_action::to_string(goal_handle_ptr_->get_goal_id()).c_str());
94 }
95}
96
97template <class ActionT>
99{
100 if (!goal_handle_ptr_)
101 {
102 RCLCPP_FATAL(logger_, "Tried to succeed the goal, but no goal handle was set up.");
103 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
104 }
105 RCLCPP_DEBUG(logger_, "Goal %s succeeded.", rclcpp_action::to_string(goal_handle_ptr_->get_goal_id()).c_str());
106 goal_handle_ptr_->succeed(result_ptr_);
107}
108
109template <class ActionT>
111{
112 if (!goal_handle_ptr_)
113 {
114 RCLCPP_FATAL(logger_, "Tried to cancel the goal, but no goal handle was set up.");
115 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
116 }
117 RCLCPP_DEBUG(logger_, "Goal %s was canceled.", rclcpp_action::to_string(goal_handle_ptr_->get_goal_id()).c_str());
118 goal_handle_ptr_->canceled(result_ptr_);
119}
120
121template <class ActionT>
123{
124 if (!goal_handle_ptr_)
125 {
126 RCLCPP_FATAL(logger_, "Tried to abort the goal, but no goal handle was set up.");
127 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
128 }
129 RCLCPP_DEBUG(logger_, "Goal %s was aborted.", rclcpp_action::to_string(goal_handle_ptr_->get_goal_id()).c_str());
130 goal_handle_ptr_->abort(result_ptr_);
131}
132
133template <typename ActionT>
135{
136 goal_handle_ptr_.reset();
137}
138
139template <typename ActionT>
141{
142 return !!goal_handle_ptr_;
143}
144
145template <class ActionT>
146std::shared_ptr<typename ActionContext<ActionT>::GoalHandle> ActionContext<ActionT>::getGoalHandlePtr()
147{
148 if (!goal_handle_ptr_)
149 {
150 RCLCPP_FATAL(logger_,
151 "Tried to access the goal handle calling ActionContext::goal_handle() but no goal handle was set up.");
152 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
153 }
154 return goal_handle_ptr_;
155}
156
157template <typename ActionT>
158inline std::shared_ptr<typename ActionContext<ActionT>::Feedback> ActionContext<ActionT>::getFeedbackPtr()
159{
160 return feedback_ptr_;
161}
162
163template <typename ActionT>
164inline std::shared_ptr<typename ActionContext<ActionT>::Result> ActionContext<ActionT>::getResultPtr()
165{
166 return result_ptr_;
167}
168
169} // namespace auto_apms_core
std::shared_ptr< Feedback > getFeedbackPtr()
std::shared_ptr< GoalHandle > getGoalHandlePtr()
std::shared_ptr< Result > getResultPtr()
typename ActionT::Goal Goal
void setUp(std::shared_ptr< GoalHandle > goal_handle_ptr)
rclcpp_action::ServerGoalHandle< ActionT > GoalHandle
typename ActionT::Result Result
typename ActionT::Feedback Feedback
ActionContext(const rclcpp::Logger &logger)