Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
waypoint_follower.hpp
1 // Copyright (c) 2019 Samsung Research America
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 NAV2_WAYPOINT_FOLLOWER__WAYPOINT_FOLLOWER_HPP_
16 #define NAV2_WAYPOINT_FOLLOWER__WAYPOINT_FOLLOWER_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include <mutex>
22 
23 #include "rclcpp_action/rclcpp_action.hpp"
24 #include "pluginlib/class_loader.hpp"
25 #include "pluginlib/class_list_macros.hpp"
26 #include "geographic_msgs/msg/geo_pose.hpp"
27 #include "nav2_util/lifecycle_node.hpp"
28 #include "nav2_msgs/action/navigate_to_pose.hpp"
29 #include "nav2_msgs/action/follow_waypoints.hpp"
30 #include "nav2_msgs/msg/missed_waypoint.hpp"
31 #include "nav_msgs/msg/path.hpp"
32 #include "nav2_util/simple_action_server.hpp"
33 #include "nav2_util/node_utils.hpp"
34 #include "nav2_util/string_utils.hpp"
35 #include "nav2_msgs/action/follow_gps_waypoints.hpp"
36 #include "nav2_util/service_client.hpp"
37 #include "nav2_core/waypoint_task_executor.hpp"
38 
39 #include "robot_localization/srv/from_ll.hpp"
40 #include "tf2_ros/buffer.h"
41 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
42 #include "tf2_ros/transform_listener.h"
43 
44 namespace nav2_waypoint_follower
45 {
46 
47 enum class ActionStatus
48 {
49  UNKNOWN = 0,
50  PROCESSING = 1,
51  FAILED = 2,
52  SUCCEEDED = 3
53 };
54 
55 struct GoalStatus
56 {
57  ActionStatus status;
58  int error_code;
59 };
60 
67 {
68 public:
69  using ActionT = nav2_msgs::action::FollowWaypoints;
70  using ClientT = nav2_msgs::action::NavigateToPose;
72  using ActionClient = rclcpp_action::Client<ClientT>;
73 
74  // Shorten the types for GPS waypoint following
75  using ActionTGPS = nav2_msgs::action::FollowGPSWaypoints;
77 
82  explicit WaypointFollower(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
87 
88 protected:
96  nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
102  nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
108  nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
114  nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
120  nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
121 
135  template<typename T, typename V, typename Z>
136  void followWaypointsHandler(const T & action_server, const V & feedback, const Z & result);
137 
142 
151 
156  void resultCallback(const rclcpp_action::ClientGoalHandle<ClientT>::WrappedResult & result);
157 
162  void goalResponseCallback(const rclcpp_action::ClientGoalHandle<ClientT>::SharedPtr & goal);
163 
171  std::vector<geometry_msgs::msg::PoseStamped> convertGPSPosesToMapPoses(
172  const std::vector<geographic_msgs::msg::GeoPose> & gps_poses);
173 
174 
183  template<typename T>
184  std::vector<geometry_msgs::msg::PoseStamped> getLatestGoalPoses(const T & action_server);
185 
186  // Common vars used for both GPS and cartesian point following
187  std::vector<int> failed_ids_;
188  std::string global_frame_id_{"map"};
189 
194  rcl_interfaces::msg::SetParametersResult
195  dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters);
196 
197  // Dynamic parameters handler
198  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_;
199 
200  // Our action server
201  std::unique_ptr<ActionServer> xyz_action_server_;
202  ActionClient::SharedPtr nav_to_pose_client_;
203  rclcpp::CallbackGroup::SharedPtr callback_group_;
204  rclcpp::executors::SingleThreadedExecutor callback_group_executor_;
205  std::shared_future<rclcpp_action::ClientGoalHandle<ClientT>::SharedPtr> future_goal_handle_;
206 
207  // Our action server for GPS waypoint following
208  std::unique_ptr<ActionServerGPS> gps_action_server_;
209  std::unique_ptr<nav2_util::ServiceClient<robot_localization::srv::FromLL,
210  std::shared_ptr<nav2_util::LifecycleNode>>> from_ll_to_map_client_;
211 
212  bool stop_on_failure_;
213  int loop_rate_;
214  GoalStatus current_goal_status_;
215 
216  // Task Execution At Waypoint Plugin
217  pluginlib::ClassLoader<nav2_core::WaypointTaskExecutor>
218  waypoint_task_executor_loader_;
219  pluginlib::UniquePtr<nav2_core::WaypointTaskExecutor>
220  waypoint_task_executor_;
221  std::string waypoint_task_executor_id_;
222  std::string waypoint_task_executor_type_;
223 };
224 
225 } // namespace nav2_waypoint_follower
226 
227 #endif // NAV2_WAYPOINT_FOLLOWER__WAYPOINT_FOLLOWER_HPP_
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
A simple wrapper on ROS2 services for invoke() and block-style calling.
An action server wrapper to make applications simpler using Actions.
An action server that uses behavior tree for navigating a robot to its goal position.
void resultCallback(const rclcpp_action::ClientGoalHandle< ClientT >::WrappedResult &result)
Action client result callback.
~WaypointFollower()
A destructor for nav2_waypoint_follower::WaypointFollower class.
WaypointFollower(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
A constructor for nav2_waypoint_follower::WaypointFollower class.
void goalResponseCallback(const rclcpp_action::ClientGoalHandle< ClientT >::SharedPtr &goal)
Action client goal response callback.
nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Configures member variables.
nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Resets member variables.
nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activates action server.
nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in shutdown state.
nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivates action server.
void followGPSWaypointsCallback()
send robot through each of GPS point , which are converted to map frame first then using a client to ...
std::vector< geometry_msgs::msg::PoseStamped > convertGPSPosesToMapPoses(const std::vector< geographic_msgs::msg::GeoPose > &gps_poses)
given some gps_poses, converts them to map frame using robot_localization's service fromLL....
void followWaypointsHandler(const T &action_server, const V &feedback, const Z &result)
Templated function to perform internal logic behind waypoint following, Both GPS and non GPS waypoint...
void followWaypointsCallback()
Action server callbacks.
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector< rclcpp::Parameter > parameters)
Callback executed when a parameter change is detected.
std::vector< geometry_msgs::msg::PoseStamped > getLatestGoalPoses(const T &action_server)
get the latest poses on the action server goal. If they are GPS poses, convert them to the global car...