AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
action_context.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// 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_util
20{
21
27template <typename ActionT>
29{
30public:
31 using Type = ActionT;
32 using Goal = typename ActionT::Goal;
33 using Feedback = typename ActionT::Feedback;
34 using Result = typename ActionT::Result;
35 using GoalHandle = rclcpp_action::ServerGoalHandle<ActionT>;
36
41 ActionContext(rclcpp::Logger logger);
42
47 void setUp(std::shared_ptr<GoalHandle> goal_handle_ptr);
48
55
61 void succeed();
62
68 void cancel();
69
75 void abort();
76
83 void invalidate();
84
89 bool isValid();
90
95 std::shared_ptr<GoalHandle> getGoalHandlePtr();
96
101 std::shared_ptr<Feedback> getFeedbackPtr();
102
107 std::shared_ptr<Result> getResultPtr();
108
109private:
110 const rclcpp::Logger logger_;
111 std::shared_ptr<GoalHandle> goal_handle_ptr_;
112 const std::shared_ptr<Feedback> feedback_ptr_{std::make_shared<Feedback>()};
113 const std::shared_ptr<Result> result_ptr_{std::make_shared<Result>()};
114};
115
119
120template <class ActionT>
121ActionContext<ActionT>::ActionContext(rclcpp::Logger logger) : logger_{logger.get_child("action_context")}
122{
123}
124
125template <class ActionT>
126void ActionContext<ActionT>::setUp(std::shared_ptr<GoalHandle> goal_handle_ptr)
127{
128 goal_handle_ptr_ = goal_handle_ptr;
129 *feedback_ptr_ = Feedback{};
130 *result_ptr_ = Result{};
131}
132
133template <class ActionT>
135{
136 if (!goal_handle_ptr_) {
137 RCLCPP_FATAL(logger_, "Tried to publish feedback on a goal, but no goal handle was set up.");
138 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
139 }
140 if (goal_handle_ptr_->is_executing()) {
141 goal_handle_ptr_->publish_feedback(feedback_ptr_);
142 } else {
143 RCLCPP_WARN(
144 logger_, "The node tried to publish feedback on goal %s which is not executing. Ignoring ...",
145 rclcpp_action::to_string(goal_handle_ptr_->get_goal_id()).c_str());
146 }
147}
148
149template <class ActionT>
151{
152 if (!goal_handle_ptr_) {
153 RCLCPP_FATAL(logger_, "Tried to succeed the goal, but no goal handle was set up.");
154 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
155 }
156 RCLCPP_DEBUG(logger_, "Goal %s succeeded.", rclcpp_action::to_string(goal_handle_ptr_->get_goal_id()).c_str());
157 goal_handle_ptr_->succeed(result_ptr_);
158}
159
160template <class ActionT>
162{
163 if (!goal_handle_ptr_) {
164 RCLCPP_FATAL(logger_, "Tried to cancel the goal, but no goal handle was set up.");
165 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
166 }
167 RCLCPP_DEBUG(logger_, "Goal %s was canceled.", rclcpp_action::to_string(goal_handle_ptr_->get_goal_id()).c_str());
168 goal_handle_ptr_->canceled(result_ptr_);
169}
170
171template <class ActionT>
173{
174 if (!goal_handle_ptr_) {
175 RCLCPP_FATAL(logger_, "Tried to abort the goal, but no goal handle was set up.");
176 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
177 }
178 RCLCPP_DEBUG(logger_, "Goal %s was aborted.", rclcpp_action::to_string(goal_handle_ptr_->get_goal_id()).c_str());
179 goal_handle_ptr_->abort(result_ptr_);
180}
181
182template <typename ActionT>
184{
185 goal_handle_ptr_.reset();
186}
187
188template <typename ActionT>
190{
191 return !!goal_handle_ptr_;
192}
193
194template <class ActionT>
195std::shared_ptr<typename ActionContext<ActionT>::GoalHandle> ActionContext<ActionT>::getGoalHandlePtr()
196{
197 if (!goal_handle_ptr_) {
198 RCLCPP_FATAL(
199 logger_, "Tried to access the goal handle calling ActionContext::goal_handle() but no goal handle was set up.");
200 throw std::runtime_error("goal_handle_ptr_ is nullptr.");
201 }
202 return goal_handle_ptr_;
203}
204
205template <typename ActionT>
206inline std::shared_ptr<typename ActionContext<ActionT>::Feedback> ActionContext<ActionT>::getFeedbackPtr()
207{
208 return feedback_ptr_;
209}
210
211template <typename ActionT>
212inline std::shared_ptr<typename ActionContext<ActionT>::Result> ActionContext<ActionT>::getResultPtr()
213{
214 return result_ptr_;
215}
216
217} // namespace auto_apms_util
void cancel()
Terminate the current goal and mark it as canceled.
std::shared_ptr< Feedback > getFeedbackPtr()
Access the internal action feedback buffer.
std::shared_ptr< GoalHandle > getGoalHandlePtr()
Get the goal handle managed by this ActionContext instance.
void publishFeedback()
Publish the feedback written to the internal buffer.
std::shared_ptr< Result > getResultPtr()
Access the internal action result buffer.
void setUp(std::shared_ptr< GoalHandle > goal_handle_ptr)
Initialize the action context using the current goal handle.
void invalidate()
Invalidate the goal handle managed by this ActionContext instance.
void succeed()
Terminate the current goal and mark it as succeeded.
ActionContext(rclcpp::Logger logger)
Constructor.
void abort()
Terminate the current goal and mark it as aborted.
bool isValid()
Check if this ActionContext is valid (e.g. is managing a valid action goal handle).
Fundamental helper classes and utility functions.