rclcpp_action  master
C++ ROS Action Client Library
client_goal_handle_impl.hpp
Go to the documentation of this file.
1 // Copyright 2018 Open Source Robotics Foundation, Inc.
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 #ifndef RCLCPP_ACTION__CLIENT_GOAL_HANDLE_IMPL_HPP_
16 #define RCLCPP_ACTION__CLIENT_GOAL_HANDLE_IMPL_HPP_
17 
18 #include <rcl_action/types.h>
19 
20 #include <memory>
21 
22 #include "rclcpp/logging.hpp"
25 
26 namespace rclcpp_action
27 {
28 
29 template<typename ActionT>
30 ClientGoalHandle<ActionT>::ClientGoalHandle(
31  const GoalInfo & info, FeedbackCallback feedback_callback, ResultCallback result_callback)
32 : info_(info),
33  result_future_(result_promise_.get_future()),
34  feedback_callback_(feedback_callback),
35  result_callback_(result_callback)
36 {
37 }
38 
39 template<typename ActionT>
41 {
42 }
43 
44 template<typename ActionT>
45 const GoalUUID &
47 {
48  return info_.goal_id.uuid;
49 }
50 
51 template<typename ActionT>
54 {
55  return info_.stamp;
56 }
57 
58 template<typename ActionT>
61 {
62  std::lock_guard<std::mutex> guard(handle_mutex_);
63  if (!is_result_aware_) {
65  }
66  return result_future_;
67 }
68 
69 template<typename ActionT>
70 void
71 ClientGoalHandle<ActionT>::set_result(const WrappedResult & wrapped_result)
72 {
73  std::lock_guard<std::mutex> guard(handle_mutex_);
74  status_ = static_cast<int8_t>(wrapped_result.code);
75  result_promise_.set_value(wrapped_result);
76  if (result_callback_) {
77  result_callback_(wrapped_result);
78  }
79 }
80 
81 template<typename ActionT>
82 void
83 ClientGoalHandle<ActionT>::set_feedback_callback(FeedbackCallback callback)
84 {
85  std::lock_guard<std::mutex> guard(handle_mutex_);
86  feedback_callback_ = callback;
87 }
88 
89 template<typename ActionT>
90 void
91 ClientGoalHandle<ActionT>::set_result_callback(ResultCallback callback)
92 {
93  std::lock_guard<std::mutex> guard(handle_mutex_);
94  result_callback_ = callback;
95 }
96 
97 template<typename ActionT>
98 int8_t
100 {
101  std::lock_guard<std::mutex> guard(handle_mutex_);
102  return status_;
103 }
104 
105 template<typename ActionT>
106 void
108 {
109  std::lock_guard<std::mutex> guard(handle_mutex_);
110  status_ = status;
111 }
112 
113 template<typename ActionT>
114 bool
116 {
117  std::lock_guard<std::mutex> guard(handle_mutex_);
118  return feedback_callback_ != nullptr;
119 }
120 
121 template<typename ActionT>
122 bool
124 {
125  std::lock_guard<std::mutex> guard(handle_mutex_);
126  return is_result_aware_;
127 }
128 
129 template<typename ActionT>
130 bool
132 {
133  std::lock_guard<std::mutex> guard(handle_mutex_);
134  bool previous = is_result_aware_;
135  is_result_aware_ = awareness;
136  return previous;
137 }
138 
139 template<typename ActionT>
140 void
141 ClientGoalHandle<ActionT>::invalidate(const exceptions::UnawareGoalHandleError & ex)
142 {
143  std::lock_guard<std::mutex> guard(handle_mutex_);
144  // Guard against multiple calls
145  if (is_invalidated()) {
146  return;
147  }
148  is_result_aware_ = false;
149  invalidate_exception_ = std::make_exception_ptr(ex);
150  status_ = GoalStatus::STATUS_UNKNOWN;
151  result_promise_.set_exception(invalidate_exception_);
152 }
153 
154 template<typename ActionT>
155 bool
156 ClientGoalHandle<ActionT>::is_invalidated() const
157 {
158  return invalidate_exception_ != nullptr;
159 }
160 
161 template<typename ActionT>
162 void
163 ClientGoalHandle<ActionT>::call_feedback_callback(
164  typename ClientGoalHandle<ActionT>::SharedPtr shared_this,
165  typename std::shared_ptr<const Feedback> feedback_message)
166 {
167  if (shared_this.get() != this) {
168  RCLCPP_ERROR(rclcpp::get_logger("rclcpp_action"), "Sent feedback to wrong goal handle.");
169  return;
170  }
171  std::lock_guard<std::mutex> guard(handle_mutex_);
172  if (nullptr == feedback_callback_) {
173  // Normal, some feedback messages may arrive after the goal result.
174  RCLCPP_DEBUG(rclcpp::get_logger("rclcpp_action"), "Received feedback but goal ignores it.");
175  return;
176  }
177  feedback_callback_(shared_this, feedback_message);
178 }
179 
180 } // namespace rclcpp_action
181 
182 #endif // RCLCPP_ACTION__CLIENT_GOAL_HANDLE_IMPL_HPP_
std::shared_future
exceptions.hpp
rclcpp_action::ClientGoalHandle::get_goal_id
const GoalUUID & get_goal_id() const
Get the unique ID for the goal.
Definition: client_goal_handle_impl.hpp:46
std::make_exception_ptr
T make_exception_ptr(T... args)
std::shared_ptr
rclcpp_action::ClientGoalHandle
Class for interacting with goals sent from action clients.
Definition: client_goal_handle.hpp:59
rclcpp::get_logger
Logger get_logger(const std::string &name)
rclcpp::Time
std::lock_guard
rclcpp_action
Definition: client.hpp:46
rclcpp_action::ClientGoalHandle::is_feedback_aware
bool is_feedback_aware()
Check if an action client has subscribed to feedback for the goal.
Definition: client_goal_handle_impl.hpp:115
rclcpp_action::exceptions::UnawareGoalHandleError
Definition: exceptions.hpp:34
std::array
RCLCPP_DEBUG
#define RCLCPP_DEBUG(logger,...)
rclcpp_action::GoalInfo
action_msgs::msg::GoalInfo GoalInfo
Definition: types.hpp:34
rclcpp_action::ClientGoalHandle::~ClientGoalHandle
virtual ~ClientGoalHandle()
Definition: client_goal_handle_impl.hpp:40
rclcpp_action::ClientGoalHandle::get_status
int8_t get_status()
Get the goal status code.
Definition: client_goal_handle_impl.hpp:99
rclcpp_action::ClientGoalHandle::is_result_aware
bool is_result_aware()
Check if an action client has requested the result for the goal.
Definition: client_goal_handle_impl.hpp:123
client_goal_handle.hpp
RCLCPP_ERROR
#define RCLCPP_ERROR(logger,...)
logging.hpp
rclcpp_action::ClientGoalHandle::get_goal_stamp
rclcpp::Time get_goal_stamp() const
Get the time when the goal was accepted.
Definition: client_goal_handle_impl.hpp:53