Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
nav2_panel.hpp
1 // Copyright (c) 2019 Intel Corporation
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_RVIZ_PLUGINS__NAV2_PANEL_HPP_
16 #define NAV2_RVIZ_PLUGINS__NAV2_PANEL_HPP_
17 
18 #include <QtWidgets>
19 #include <QBasicTimer>
20 #undef NO_ERROR
21 
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "nav2_lifecycle_manager/lifecycle_manager_client.hpp"
27 #include "nav2_msgs/action/navigate_to_pose.hpp"
28 #include "nav2_msgs/action/navigate_through_poses.hpp"
29 #include "nav2_msgs/action/follow_waypoints.hpp"
30 #include "nav2_rviz_plugins/ros_action_qevent.hpp"
31 #include "rclcpp/rclcpp.hpp"
32 #include "rclcpp_action/rclcpp_action.hpp"
33 #include "rviz_common/panel.hpp"
34 #include "rviz_common/ros_integration/ros_node_abstraction_iface.hpp"
35 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
36 #include "visualization_msgs/msg/marker_array.hpp"
37 #include "nav2_util/geometry_utils.hpp"
38 #include "tf2_ros/transform_listener.hpp"
39 #include "tf2_ros/create_timer_ros.hpp"
40 #include "tf2_ros/buffer.hpp"
41 
42 class QPushButton;
43 
44 namespace nav2_rviz_plugins
45 {
46 
47 class InitialThread;
48 
50 class Nav2Panel : public rviz_common::Panel
51 {
52  Q_OBJECT
53 
54 public:
55  explicit Nav2Panel(QWidget * parent = 0);
56  virtual ~Nav2Panel();
57 
58  void onInitialize() override;
59 
61  void load(const rviz_common::Config & config) override;
62  void save(rviz_common::Config config) const override;
63 
64 private Q_SLOTS:
65  void startThread();
66  void onStartup();
67  void onShutdown();
68  void onCancel();
69  void onPause();
70  void onResume();
71  void onResumedWp();
72  void onAccumulatedWp();
73  void onAccumulatedNTP();
74  void onAccumulating();
75  void onNewGoal(double x, double y, double theta, QString frame);
76  void handleGoalSaver();
77  void handleGoalLoader();
78  void loophandler();
79  void initialStateHandler();
80 
81 private:
82  void loadLogFiles();
83  void onCancelButtonPressed();
84  void timerEvent(QTimerEvent * event) override;
85  bool isLoopValueValid(std::string & loop);
86 
87  int unique_id {0};
88  int goal_index_ = 0;
89  int loop_count_ = 0;
90  bool store_initial_pose_ = false;
91  bool initial_pose_stored_ = false;
92  bool loop_counter_stop_ = true;
93  std::string loop_no_ = "0";
94  std::string base_frame_;
95 
96  // The Node pointer that we need to keep alive for the duration of this plugin.
97  std::shared_ptr<rviz_common::ros_integration::RosNodeAbstractionIface> node_ptr_;
98 
99  // Call to send NavigateToPose action request for goal poses
100  geometry_msgs::msg::PoseStamped convert_to_msg(
101  std::vector<double> pose,
102  std::vector<double> orientation);
103  void startWaypointFollowing(std::vector<geometry_msgs::msg::PoseStamped> poses);
104  void startNavigation(geometry_msgs::msg::PoseStamped);
105  void startNavThroughPoses(nav_msgs::msg::Goals poses);
106  using NavigationGoalHandle =
107  rclcpp_action::ClientGoalHandle<nav2_msgs::action::NavigateToPose>;
108  using WaypointFollowerGoalHandle =
109  rclcpp_action::ClientGoalHandle<nav2_msgs::action::FollowWaypoints>;
110  using NavThroughPosesGoalHandle =
111  rclcpp_action::ClientGoalHandle<nav2_msgs::action::NavigateThroughPoses>;
112 
113  // The (non-spinning) client node used to invoke the action client
114  rclcpp::Node::SharedPtr client_node_;
115 
116  // Timeout value when waiting for action servers to respond
117  std::chrono::milliseconds server_timeout_;
118 
119  // A timer used to check on the completion status of the action
120  QBasicTimer timer_;
121 
122  // The NavigateToPose action client
123  nav2::ActionClient<nav2_msgs::action::NavigateToPose>::SharedPtr navigation_action_client_;
124  nav2::ActionClient<nav2_msgs::action::FollowWaypoints>::SharedPtr
125  waypoint_follower_action_client_;
126  nav2::ActionClient<nav2_msgs::action::NavigateThroughPoses>::SharedPtr
127  nav_through_poses_action_client_;
128 
129  // Navigation action feedback subscribers
130  nav2::Subscription<nav2_msgs::action::NavigateToPose::Impl::FeedbackMessage>::SharedPtr
131  navigation_feedback_sub_;
132  nav2::Subscription<nav2_msgs::action::NavigateThroughPoses::Impl::FeedbackMessage>::SharedPtr
133  nav_through_poses_feedback_sub_;
134  nav2::Subscription<nav2_msgs::action::NavigateToPose::Impl::GoalStatusMessage>::SharedPtr
135  navigation_goal_status_sub_;
136  nav2::Subscription<nav2_msgs::action::NavigateThroughPoses::Impl::GoalStatusMessage>::SharedPtr
137  nav_through_poses_goal_status_sub_;
138 
139  // Tf's for initial pose
140  std::shared_ptr<tf2_ros::Buffer> tf2_buffer_;
141  std::shared_ptr<tf2_ros::TransformListener> transform_listener_;
142 
143  // Goal-related state
144  nav2_msgs::action::NavigateToPose::Goal navigation_goal_;
145  nav2_msgs::action::FollowWaypoints::Goal waypoint_follower_goal_;
146  nav2_msgs::action::NavigateThroughPoses::Goal nav_through_poses_goal_;
147  NavigationGoalHandle::SharedPtr navigation_goal_handle_;
148  WaypointFollowerGoalHandle::SharedPtr waypoint_follower_goal_handle_;
149  NavThroughPosesGoalHandle::SharedPtr nav_through_poses_goal_handle_;
150 
151  // The client used to control the nav2 stack
152  std::shared_ptr<nav2_lifecycle_manager::LifecycleManagerClient> client_nav_;
153  std::shared_ptr<nav2_lifecycle_manager::LifecycleManagerClient> client_loc_;
154 
155  QCheckBox * store_initial_pose_checkbox_{nullptr};
156 
157  QPushButton * start_reset_button_{nullptr};
158  QPushButton * pause_resume_button_{nullptr};
159  QPushButton * navigation_mode_button_{nullptr};
160  QPushButton * save_waypoints_button_{nullptr};
161  QPushButton * load_waypoints_button_{nullptr};
162  QPushButton * pause_waypoint_button_{nullptr};
163 
164  QLabel * navigation_status_indicator_{nullptr};
165  QLabel * localization_status_indicator_{nullptr};
166  QLabel * navigation_goal_status_indicator_{nullptr};
167  QLabel * navigation_feedback_indicator_{nullptr};
168  QLabel * waypoint_status_indicator_{nullptr};
169  QLabel * number_of_loops_{nullptr};
170 
171  QLineEdit * nr_of_loops_{nullptr};
172 
173  QStateMachine state_machine_;
174  InitialThread * initial_thread_;
175 
176  QState * pre_initial_{nullptr};
177  QState * initial_{nullptr};
178  QState * idle_{nullptr};
179  QState * reset_{nullptr};
180  QState * paused_{nullptr};
181  QState * resumed_{nullptr};
182  QState * paused_wp_{nullptr};
183  QState * resumed_wp_{nullptr};
184 
185  QLabel * imgDisplayLabel_{nullptr};
186 
187  // The following states are added to allow for the state of the button to only expose reset
188  // while the NavigateToPoses action is not active. While running, the user will be allowed to
189  // cancel the action. The ROSActionTransition allows for the state of the action to be detected
190  // and the button state to change automatically.
191  QState * running_{nullptr};
192  QState * canceled_{nullptr};
193  // The following states are added to allow to collect several poses to perform a waypoint-mode
194  // navigation or navigate through poses mode.
195  QState * accumulating_{nullptr};
196  QState * accumulated_wp_{nullptr};
197  QState * accumulated_nav_through_poses_{nullptr};
198 
199  nav_msgs::msg::Goals acummulated_poses_;
200  nav_msgs::msg::Goals store_poses_;
201 
202  // Publish the visual markers with the waypoints
203  void updateWpNavigationMarkers();
204 
205  // Create unique id numbers for markers
206  int getUniqueId();
207 
208  void resetUniqueId();
209 
210  // create label string from feedback msg
211  static inline QString getNavToPoseFeedbackLabel(
212  nav2_msgs::action::NavigateToPose::Feedback msg =
213  nav2_msgs::action::NavigateToPose::Feedback());
214  static inline QString getNavThroughPosesFeedbackLabel(
215  nav2_msgs::action::NavigateThroughPoses::Feedback =
216  nav2_msgs::action::NavigateThroughPoses::Feedback());
217  template<typename T>
218  static inline std::string toLabel(T & msg);
219 
220  // round off double to the specified precision and convert to string
221  static inline std::string toString(double val, int precision = 0);
222 
223  // Waypoint navigation visual markers publisher
224  rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr wp_navigation_markers_pub_;
225 };
226 
227 class InitialThread : public QThread
228 {
229  Q_OBJECT
230 
231 public:
232  using SystemStatus = nav2_lifecycle_manager::SystemStatus;
233 
234  explicit InitialThread(
235  std::shared_ptr<nav2_lifecycle_manager::LifecycleManagerClient> & client_nav,
236  std::shared_ptr<nav2_lifecycle_manager::LifecycleManagerClient> & client_loc)
237  : client_nav_(client_nav), client_loc_(client_loc)
238  {}
239 
240  void run() override
241  {
242  SystemStatus status_nav = SystemStatus::TIMEOUT;
243  SystemStatus status_loc = SystemStatus::TIMEOUT;
244 
245  while (status_nav == SystemStatus::TIMEOUT) {
246  if (status_nav == SystemStatus::TIMEOUT) {
247  status_nav = client_nav_->is_active(std::chrono::seconds(1));
248  }
249  }
250 
251  // try to communicate twice, might not actually be up if in SLAM mode
252  bool tried_loc_bringup_once = false;
253  while (status_loc == SystemStatus::TIMEOUT) {
254  status_loc = client_loc_->is_active(std::chrono::seconds(1));
255  if (tried_loc_bringup_once) {
256  break;
257  }
258  tried_loc_bringup_once = true;
259  }
260 
261  if (status_nav == SystemStatus::ACTIVE) {
262  emit navigationActive();
263  } else {
264  emit navigationInactive();
265  }
266 
267  if (status_loc == SystemStatus::ACTIVE) {
268  emit localizationActive();
269  } else {
270  emit localizationInactive();
271  }
272  }
273 
274 signals:
275  void navigationActive();
276  void navigationInactive();
277  void localizationActive();
278  void localizationInactive();
279 
280 private:
281  std::shared_ptr<nav2_lifecycle_manager::LifecycleManagerClient> client_nav_;
282  std::shared_ptr<nav2_lifecycle_manager::LifecycleManagerClient> client_loc_;
283 };
284 
285 } // namespace nav2_rviz_plugins
286 
287 #endif // NAV2_RVIZ_PLUGINS__NAV2_PANEL_HPP_
Panel to interface to the nav2 stack.
Definition: nav2_panel.hpp:51
void load(const rviz_common::Config &config) override
Load and save configuration data.