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