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
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
84 {
85  std::lock_guard<std::mutex> guard(handle_mutex_);
86  feedback_callback_ = callback;
87 }
88 
89 template<typename ActionT>
90 void
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
142 {
143  std::lock_guard<std::mutex> guard(handle_mutex_);
144  status_ = GoalStatus::STATUS_UNKNOWN;
145  result_promise_.set_exception(std::make_exception_ptr(exceptions::UnawareGoalHandleError()));
146 }
147 
148 template<typename ActionT>
149 void
151  typename ClientGoalHandle<ActionT>::SharedPtr shared_this,
152  typename std::shared_ptr<const Feedback> feedback_message)
153 {
154  if (shared_this.get() != this) {
155  RCLCPP_ERROR(rclcpp::get_logger("rclcpp_action"), "Sent feedback to wrong goal handle.");
156  return;
157  }
158  std::lock_guard<std::mutex> guard(handle_mutex_);
159  if (nullptr == feedback_callback_) {
160  // Normal, some feedback messages may arrive after the goal result.
161  RCLCPP_DEBUG(rclcpp::get_logger("rclcpp_action"), "Received feedback but goal ignores it.");
162  return;
163  }
164  feedback_callback_(shared_this, feedback_message);
165 }
166 
167 } // namespace rclcpp_action
168 
169 #endif // RCLCPP_ACTION__CLIENT_GOAL_HANDLE_IMPL_HPP_
Definition: client_goal_handle.hpp:64
T make_exception_ptr(T... args)
int8_t get_status()
Get the goal status code.
Definition: client_goal_handle_impl.hpp:99
Logger get_logger(const std::string &name)
virtual ~ClientGoalHandle()
Definition: client_goal_handle_impl.hpp:40
bool is_result_aware()
Check if an action client has requested the result for the goal.
Definition: client_goal_handle_impl.hpp:123
const GoalUUID & get_goal_id() const
Get the unique ID for the goal.
Definition: client_goal_handle_impl.hpp:46
#define RCLCPP_DEBUG(logger,...)
#define RCLCPP_ERROR(logger,...)
rclcpp::Time get_goal_stamp() const
Get the time when the goal was accepted.
Definition: client_goal_handle_impl.hpp:53
ResultCode code
A status to indicate if the goal was canceled, aborted, or suceeded.
Definition: client_goal_handle.hpp:69
action_msgs::msg::GoalInfo GoalInfo
Definition: types.hpp:34
Definition: client.hpp:44
Class for interacting with goals sent from action clients.
Definition: client_goal_handle.hpp:58
std::shared_future< WrappedResult > async_result()
Get a future to the goal result.
Definition: client_goal_handle_impl.hpp:60
bool is_feedback_aware()
Check if an action client has subscribed to feedback for the goal.
Definition: client_goal_handle_impl.hpp:115