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  _set_aborted();
84 
87  void
89 
92  void
94 
97  void
98  _set_canceled();
99 
102  void
103  _set_executing();
104 
108  bool
109  try_canceling() noexcept;
110 
111  // End API for communication between ServerGoalHandleBase and ServerGoalHandle<>
112  // -----------------------------------------------------------------------------
113 
114 private:
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 
147  void
149  {
150  feedback_msg->action_goal_id.uuid = uuid_;
151  publish_feedback_(feedback_msg);
152  }
153 
154  // TODO(sloretz) which exception is raised?
156 
164  void
165  set_aborted(typename ActionT::Result::SharedPtr result_msg)
166  {
167  _set_aborted();
168  result_msg->action_status = action_msgs::msg::GoalStatus::STATUS_ABORTED;
169  on_terminal_state_(uuid_, result_msg);
170  }
171 
173 
181  void
182  set_succeeded(typename ActionT::Result::SharedPtr result_msg)
183  {
184  _set_succeeded();
185  result_msg->action_status = action_msgs::msg::GoalStatus::STATUS_SUCCEEDED;
186  on_terminal_state_(uuid_, result_msg);
187  }
188 
190 
198  void
199  set_canceled(typename ActionT::Result::SharedPtr result_msg)
200  {
201  _set_canceled();
202  result_msg->action_status = action_msgs::msg::GoalStatus::STATUS_CANCELED;
203  on_terminal_state_(uuid_, result_msg);
204  }
205 
207 
211  void
213  {
214  _set_executing();
215  on_executing_(uuid_);
216  }
217 
220  get_goal() const
221  {
222  return goal_;
223  }
224 
226  const GoalID &
227  get_goal_id() const
228  {
229  return uuid_;
230  }
231 
233  {
234  // Cancel goal if handle was allowed to destruct without reaching a terminal state
235  if (try_canceling()) {
236  auto null_result = std::make_shared<typename ActionT::Result>();
237  null_result->action_status = action_msgs::msg::GoalStatus::STATUS_CANCELED;
238  on_terminal_state_(uuid_, null_result);
239  }
240  }
241 
242 protected:
246  GoalID uuid,
248  std::function<void(const GoalID &, std::shared_ptr<void>)> on_terminal_state,
249  std::function<void(const GoalID &)> on_executing,
251  )
252  : ServerGoalHandleBase(rcl_handle), goal_(goal), uuid_(uuid),
253  on_terminal_state_(on_terminal_state), on_executing_(on_executing),
254  publish_feedback_(publish_feedback)
255  {
256  }
257 
260 
262  const GoalID uuid_;
263 
265 
269 };
270 } // namespace rclcpp_action
271 
272 #endif // RCLCPP_ACTION__SERVER_GOAL_HANDLE_HPP_
RCLCPP_ACTION_PUBLIC ServerGoalHandleBase(std::shared_ptr< rcl_action_goal_handle_t > rcl_handle)
Definition: server_goal_handle.hpp:73
RCLCPP_ACTION_PUBLIC bool is_canceling() const
void set_succeeded(typename ActionT::Result::SharedPtr result_msg)
Indicate that a goal has been reached.
Definition: server_goal_handle.hpp:182
const std::shared_ptr< const typename ActionT::Goal > get_goal() const
Get the original request message describing the goal.
Definition: server_goal_handle.hpp:220
RCLCPP_ACTION_PUBLIC bool try_canceling() noexcept
Definition: server_goal_handle.hpp:42
const GoalID uuid_
A unique id for the goal request.
Definition: server_goal_handle.hpp:262
RCLCPP_ACTION_PUBLIC void _set_executing()
Action Server.
Definition: server.hpp:268
Class to interact with goals on a server.
Definition: server_goal_handle.hpp:135
void set_canceled(typename ActionT::Result::SharedPtr result_msg)
Indicate that a goal has been canceled.
Definition: server_goal_handle.hpp:199
void set_executing()
Indicate that the server is starting to execute a goal.
Definition: server_goal_handle.hpp:212
virtual RCLCPP_ACTION_PUBLIC ~ServerGoalHandleBase()
const std::shared_ptr< const typename ActionT::Goal > goal_
The original request message describing the goal.
Definition: server_goal_handle.hpp:259
virtual ~ServerGoalHandle()
Definition: server_goal_handle.hpp:232
#define RCLCPP_ACTION_PUBLIC
Definition: visibility_control.hpp:50
RCLCPP_ACTION_PUBLIC void _set_canceled()
RCLCPP_ACTION_PUBLIC void _set_succeeded()
Definition: client.hpp:44
std::function< void(const GoalID &)> on_executing_
Definition: server_goal_handle.hpp:267
void set_aborted(typename ActionT::Result::SharedPtr result_msg)
Indicate that a goal could not be reached and has been aborted.
Definition: server_goal_handle.hpp:165
ServerGoalHandle(std::shared_ptr< rcl_action_goal_handle_t > rcl_handle, GoalID uuid, std::shared_ptr< const typename ActionT::Goal > goal, std::function< void(const GoalID &, std::shared_ptr< void >)> on_terminal_state, std::function< void(const GoalID &)> on_executing, std::function< void(std::shared_ptr< typename ActionT::Feedback >)> publish_feedback)
Definition: server_goal_handle.hpp:244
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:148
RCLCPP_ACTION_PUBLIC void _set_aborted()
const GoalID & get_goal_id() const
Get the unique identifier of the goal.
Definition: server_goal_handle.hpp:227
RCLCPP_ACTION_PUBLIC bool is_executing() const
RCLCPP_ACTION_PUBLIC void _set_canceling()
RCLCPP_ACTION_PUBLIC bool is_active() const