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  return this->async_get_result();
63 }
64 
65 template<typename ActionT>
68 {
69  std::lock_guard<std::mutex> guard(handle_mutex_);
70  if (!is_result_aware_) {
72  }
73  return result_future_;
74 }
75 
76 template<typename ActionT>
77 void
78 ClientGoalHandle<ActionT>::set_result(const WrappedResult & wrapped_result)
79 {
80  std::lock_guard<std::mutex> guard(handle_mutex_);
81  status_ = static_cast<int8_t>(wrapped_result.code);
82  result_promise_.set_value(wrapped_result);
83  if (result_callback_) {
84  result_callback_(wrapped_result);
85  }
86 }
87 
88 template<typename ActionT>
89 void
90 ClientGoalHandle<ActionT>::set_feedback_callback(FeedbackCallback callback)
91 {
92  std::lock_guard<std::mutex> guard(handle_mutex_);
93  feedback_callback_ = callback;
94 }
95 
96 template<typename ActionT>
97 void
98 ClientGoalHandle<ActionT>::set_result_callback(ResultCallback callback)
99 {
100  std::lock_guard<std::mutex> guard(handle_mutex_);
101  result_callback_ = callback;
102 }
103 
104 template<typename ActionT>
105 int8_t
107 {
108  std::lock_guard<std::mutex> guard(handle_mutex_);
109  return status_;
110 }
111 
112 template<typename ActionT>
113 void
115 {
116  std::lock_guard<std::mutex> guard(handle_mutex_);
117  status_ = status;
118 }
119 
120 template<typename ActionT>
121 bool
123 {
124  std::lock_guard<std::mutex> guard(handle_mutex_);
125  return feedback_callback_ != nullptr;
126 }
127 
128 template<typename ActionT>
129 bool
131 {
132  std::lock_guard<std::mutex> guard(handle_mutex_);
133  return is_result_aware_;
134 }
135 
136 template<typename ActionT>
137 bool
139 {
140  std::lock_guard<std::mutex> guard(handle_mutex_);
141  bool previous = is_result_aware_;
142  is_result_aware_ = awareness;
143  return previous;
144 }
145 
146 template<typename ActionT>
147 void
148 ClientGoalHandle<ActionT>::invalidate()
149 {
150  std::lock_guard<std::mutex> guard(handle_mutex_);
151  status_ = GoalStatus::STATUS_UNKNOWN;
152  result_promise_.set_exception(std::make_exception_ptr(exceptions::UnawareGoalHandleError()));
153 }
154 
155 template<typename ActionT>
156 void
157 ClientGoalHandle<ActionT>::call_feedback_callback(
158  typename ClientGoalHandle<ActionT>::SharedPtr shared_this,
159  typename std::shared_ptr<const Feedback> feedback_message)
160 {
161  if (shared_this.get() != this) {
162  RCLCPP_ERROR(rclcpp::get_logger("rclcpp_action"), "Sent feedback to wrong goal handle.");
163  return;
164  }
165  std::lock_guard<std::mutex> guard(handle_mutex_);
166  if (nullptr == feedback_callback_) {
167  // Normal, some feedback messages may arrive after the goal result.
168  RCLCPP_DEBUG(rclcpp::get_logger("rclcpp_action"), "Received feedback but goal ignores it.");
169  return;
170  }
171  feedback_callback_(shared_this, feedback_message);
172 }
173 
174 } // namespace rclcpp_action
175 
176 #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:58
rclcpp::get_logger
Logger get_logger(const std::string &name)
rclcpp::Time
std::lock_guard
rclcpp_action
Definition: client.hpp:44
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:122
rclcpp_action::ClientGoalHandle::async_result
std::shared_future< WrappedResult > async_result()
Get a future to the goal result.
Definition: client_goal_handle_impl.hpp:60
rclcpp_action::exceptions::UnawareGoalHandleError
Definition: exceptions.hpp:33
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:106
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:130
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