rclcpp_action  master
C++ ROS Action Client Library
server_goal_handle.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__SERVER_GOAL_HANDLE_HPP_
16 #define RCLCPP_ACTION__SERVER_GOAL_HANDLE_HPP_
17 
18 #include <rcl_action/types.h>
19 #include <rcl_action/goal_handle.h>
20 
21 #include <action_msgs/msg/goal_status.hpp>
22 
23 #include <functional>
24 #include <memory>
25 #include <mutex>
26 
28 #include "rclcpp_action/types.hpp"
29 
30 namespace rclcpp_action
31 {
32 
35 
43 {
44 public:
48  bool
49  is_canceling() const;
50 
54  bool
55  is_active() const;
56 
60  bool
61  is_executing() const;
62 
64  virtual
66 
67 protected:
68  // -------------------------------------------------------------------------
69  // API for communication between ServerGoalHandleBase and ServerGoalHandle<>
70 
75  )
76  : rcl_handle_(rcl_handle)
77  {
78  }
79 
82  void
83  _abort();
84 
87  void
88  _succeed();
89 
92  void
93  _cancel_goal();
94 
97  void
98  _canceled();
99 
102  void
103  _execute();
104 
108  bool
109  try_canceling() noexcept;
110 
111  // End API for communication between ServerGoalHandleBase and ServerGoalHandle<>
112  // -----------------------------------------------------------------------------
113 
114 private:
115  std::shared_ptr<rcl_action_goal_handle_t> rcl_handle_;
116  mutable std::mutex rcl_handle_mutex_;
117 };
118 
119 // Forward declare server
120 template<typename ActionT>
121 class Server;
122 
124 
134 template<typename ActionT>
136 {
137 public:
139 
148  void
150  {
151  auto feedback_message = std::make_shared<typename ActionT::Impl::FeedbackMessage>();
152  feedback_message->goal_id.uuid = uuid_;
153  feedback_message->feedback = *feedback_msg;
154  publish_feedback_(feedback_message);
155  }
156 
158 
167  void
168  abort(typename ActionT::Result::SharedPtr result_msg)
169  {
170  _abort();
171  auto response = std::make_shared<typename ActionT::Impl::GetResultService::Response>();
172  response->status = action_msgs::msg::GoalStatus::STATUS_ABORTED;
173  response->result = *result_msg;
174  on_terminal_state_(uuid_, response);
175  }
176 
178 
187  void
188  succeed(typename ActionT::Result::SharedPtr result_msg)
189  {
190  _succeed();
191  auto response = std::make_shared<typename ActionT::Impl::GetResultService::Response>();
192  response->status = action_msgs::msg::GoalStatus::STATUS_SUCCEEDED;
193  response->result = *result_msg;
194  on_terminal_state_(uuid_, response);
195  }
196 
198 
207  void
208  canceled(typename ActionT::Result::SharedPtr result_msg)
209  {
210  _canceled();
211  auto response = std::make_shared<typename ActionT::Impl::GetResultService::Response>();
212  response->status = action_msgs::msg::GoalStatus::STATUS_CANCELED;
213  response->result = *result_msg;
214  on_terminal_state_(uuid_, response);
215  }
216 
218 
223  void
225  {
226  _execute();
227  on_executing_(uuid_);
228  }
229 
232  get_goal() const
233  {
234  return goal_;
235  }
236 
238  const GoalUUID &
239  get_goal_id() const
240  {
241  return uuid_;
242  }
243 
245  {
246  // Cancel goal if handle was allowed to destruct without reaching a terminal state
247  if (try_canceling()) {
248  auto null_result = std::make_shared<typename ActionT::Impl::GetResultService::Response>();
249  null_result->status = action_msgs::msg::GoalStatus::STATUS_CANCELED;
250  on_terminal_state_(uuid_, null_result);
251  }
252  }
253 
254 protected:
258  GoalUUID uuid,
260  std::function<void(const GoalUUID &, std::shared_ptr<void>)> on_terminal_state,
261  std::function<void(const GoalUUID &)> on_executing,
263  )
264  : ServerGoalHandleBase(rcl_handle), goal_(goal), uuid_(uuid),
265  on_terminal_state_(on_terminal_state), on_executing_(on_executing),
266  publish_feedback_(publish_feedback)
267  {
268  }
269 
272 
275 
277 
281 };
282 } // namespace rclcpp_action
283 
284 #endif // RCLCPP_ACTION__SERVER_GOAL_HANDLE_HPP_
rclcpp_action::ServerGoalHandle::on_executing_
std::function< void(const GoalUUID &)> on_executing_
Definition: server_goal_handle.hpp:279
rclcpp_action::ServerGoalHandle::goal_
const std::shared_ptr< const typename ActionT::Goal > goal_
The user provided message describing the goal.
Definition: server_goal_handle.hpp:271
std::shared_ptr< rcl_action_goal_handle_t >
rclcpp_action::ServerGoalHandleBase::try_canceling
RCLCPP_ACTION_PUBLIC bool try_canceling() noexcept
rclcpp_action::ServerGoalHandleBase::_canceled
RCLCPP_ACTION_PUBLIC void _canceled()
rclcpp_action::ServerGoalHandle::abort
void abort(typename ActionT::Result::SharedPtr result_msg)
Indicate that a goal could not be reached and has been aborted.
Definition: server_goal_handle.hpp:168
types.hpp
rclcpp_action::ServerGoalHandle::ServerGoalHandle
ServerGoalHandle(std::shared_ptr< rcl_action_goal_handle_t > rcl_handle, GoalUUID uuid, std::shared_ptr< const typename ActionT::Goal > goal, std::function< void(const GoalUUID &, std::shared_ptr< void >)> on_terminal_state, std::function< void(const GoalUUID &)> on_executing, std::function< void(std::shared_ptr< typename ActionT::Impl::FeedbackMessage >)> publish_feedback)
Definition: server_goal_handle.hpp:256
std::function
rclcpp_action::ServerGoalHandle::publish_feedback
void publish_feedback(std::shared_ptr< typename ActionT::Feedback > feedback_msg)
Send an update about the progress of a goal.
Definition: server_goal_handle.hpp:149
rclcpp_action::ServerGoalHandleBase::ServerGoalHandleBase
RCLCPP_ACTION_PUBLIC ServerGoalHandleBase(std::shared_ptr< rcl_action_goal_handle_t > rcl_handle)
Definition: server_goal_handle.hpp:73
rclcpp_action::ServerGoalHandle::get_goal_id
const GoalUUID & get_goal_id() const
Get the unique identifier of the goal.
Definition: server_goal_handle.hpp:239
rclcpp_action
Definition: client.hpp:44
rclcpp_action::ServerGoalHandleBase::~ServerGoalHandleBase
virtual RCLCPP_ACTION_PUBLIC ~ServerGoalHandleBase()
rclcpp_action::ServerGoalHandleBase::is_executing
RCLCPP_ACTION_PUBLIC bool is_executing() const
rclcpp_action::ServerGoalHandleBase::is_canceling
RCLCPP_ACTION_PUBLIC bool is_canceling() const
std::array
rclcpp_action::ServerGoalHandle::on_terminal_state_
std::function< void(const GoalUUID &, std::shared_ptr< void >)> on_terminal_state_
Definition: server_goal_handle.hpp:278
rclcpp_action::ServerGoalHandleBase::_cancel_goal
RCLCPP_ACTION_PUBLIC void _cancel_goal()
rclcpp_action::ServerGoalHandle::canceled
void canceled(typename ActionT::Result::SharedPtr result_msg)
Indicate that a goal has been canceled.
Definition: server_goal_handle.hpp:208
rclcpp_action::ServerGoalHandle::execute
void execute()
Indicate that the server is starting to execute a goal.
Definition: server_goal_handle.hpp:224
rclcpp_action::ServerGoalHandle::publish_feedback_
std::function< void(std::shared_ptr< typename ActionT::Impl::FeedbackMessage >)> publish_feedback_
Definition: server_goal_handle.hpp:280
rclcpp_action::Server
Action Server.
Definition: server.hpp:268
rclcpp_action::ServerGoalHandle::get_goal
const std::shared_ptr< const typename ActionT::Goal > get_goal() const
Get the user provided message describing the goal.
Definition: server_goal_handle.hpp:232
rclcpp_action::ServerGoalHandleBase::_abort
RCLCPP_ACTION_PUBLIC void _abort()
rclcpp_action::ServerGoalHandle::succeed
void succeed(typename ActionT::Result::SharedPtr result_msg)
Indicate that a goal has succeeded.
Definition: server_goal_handle.hpp:188
visibility_control.hpp
std
rclcpp_action::ServerGoalHandleBase
Definition: server_goal_handle.hpp:42
RCLCPP_ACTION_PUBLIC
#define RCLCPP_ACTION_PUBLIC
Definition: visibility_control.hpp:50
rclcpp_action::ServerGoalHandleBase::is_active
RCLCPP_ACTION_PUBLIC bool is_active() const
rclcpp_action::ServerGoalHandle::uuid_
const GoalUUID uuid_
A unique id for the goal request.
Definition: server_goal_handle.hpp:274
rclcpp_action::ServerGoalHandleBase::_execute
RCLCPP_ACTION_PUBLIC void _execute()
rcl_action_goal_handle_t
rclcpp_action::ServerGoalHandleBase::_succeed
RCLCPP_ACTION_PUBLIC void _succeed()
rclcpp_action::ServerGoalHandle
Class to interact with goals on a server.
Definition: server_goal_handle.hpp:135
rclcpp_action::ServerGoalHandle::~ServerGoalHandle
virtual ~ServerGoalHandle()
Definition: server_goal_handle.hpp:244