moveit2
The MoveIt Motion Planning Framework for ROS 2.
motion_planning_frame_context.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan */
41 
42 #include <rviz_common/display_context.hpp>
43 #include <rviz_common/window_manager_interface.hpp>
44 
45 #include <QMessageBox>
46 #include <QInputDialog>
47 
48 #include "ui_motion_planning_rviz_plugin_frame.h"
49 
50 namespace moveit_rviz_plugin
51 {
52 static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit_ros_visualization.motion_planning_frame_context");
53 
54 void MotionPlanningFrame::databaseConnectButtonClicked()
55 {
56  planning_display_->addBackgroundJob([this] { computeDatabaseConnectButtonClicked(); }, "connect to database");
57 }
58 
59 void MotionPlanningFrame::planningPipelineIndexChanged(int index)
60 {
61  // Refresh planner interface description for selected pipeline
62  if (index >= 0 && static_cast<size_t>(index) < planner_descriptions_.size())
63  {
64  // Set the selected pipeline id
65  if (move_group_)
66  move_group_->setPlanningPipelineId(planner_descriptions_[index].pipeline_id);
67 
68  populatePlannerDescription(planner_descriptions_[index]);
69  }
70 }
71 
72 void MotionPlanningFrame::planningAlgorithmIndexChanged(int index)
73 {
74  std::string planner_id = ui_->planning_algorithm_combo_box->itemText(index).toStdString();
75  if (index <= 0)
76  planner_id = "";
77 
78  ui_->planner_param_treeview->setPlannerId(planner_id);
79 
80  if (move_group_)
81  move_group_->setPlannerId(planner_id);
82 }
83 
84 void MotionPlanningFrame::resetDbButtonClicked()
85 {
86  if (QMessageBox::warning(this, "Data about to be deleted",
87  "The following dialog will allow you to drop a MoveIt "
88  "Warehouse database. Are you sure you want to continue?",
89  QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
90  return;
91 
92  QStringList dbs;
93  dbs.append("Planning Scenes");
94  dbs.append("Constraints");
95  dbs.append("Robot States");
96 
97  bool ok = false;
98  QString response =
99  QInputDialog::getItem(this, tr("Select Database"), tr("Choose the database to reset:"), dbs, 2, false, &ok);
100  if (!ok)
101  return;
102 
103  if (QMessageBox::critical(
104  this, "Data about to be deleted",
105  QString("All data in database '").append(response).append("'. Are you sure you want to continue?"),
106  QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
107  return;
108 
109  planning_display_->addBackgroundJob([this, db = response.toStdString()] { computeResetDbButtonClicked(db); },
110  "reset database");
111 }
112 
113 void MotionPlanningFrame::computeDatabaseConnectButtonClicked()
114 {
115  RCLCPP_INFO(LOGGER, "Connect to database: {host: %s, port: %d}", ui_->database_host->text().toStdString().c_str(),
116  ui_->database_port->value());
118  {
119  planning_scene_storage_.reset();
120  robot_state_storage_.reset();
121  constraints_storage_.reset();
122  planning_display_->addMainLoopJob([this] { computeDatabaseConnectButtonClickedHelper(1); });
123  }
124  else
125  {
126  planning_display_->addMainLoopJob([this] { computeDatabaseConnectButtonClickedHelper(2); });
127  try
128  {
129  warehouse_ros::DatabaseConnection::Ptr conn = moveit_warehouse::loadDatabase(node_);
130  conn->setParams(ui_->database_host->text().toStdString(), ui_->database_port->value(), 5.0);
131  if (conn->connect())
132  {
133  planning_scene_storage_ = std::make_shared<moveit_warehouse::PlanningSceneStorage>(conn);
134  robot_state_storage_ = std::make_shared<moveit_warehouse::RobotStateStorage>(conn);
135  constraints_storage_ = std::make_shared<moveit_warehouse::ConstraintsStorage>(conn);
136  }
137  else
138  {
139  planning_display_->addMainLoopJob([this] { computeDatabaseConnectButtonClickedHelper(3); });
140  return;
141  }
142  }
143  catch (std::exception& ex)
144  {
145  planning_display_->addMainLoopJob([this] { computeDatabaseConnectButtonClickedHelper(3); });
146  RCLCPP_ERROR(LOGGER, "%s", ex.what());
147  return;
148  }
149  planning_display_->addMainLoopJob([this] { computeDatabaseConnectButtonClickedHelper(4); });
150  }
151 }
152 
153 void MotionPlanningFrame::computeDatabaseConnectButtonClickedHelper(int mode)
154 {
155  if (mode == 1)
156  {
157  ui_->planning_scene_tree->setUpdatesEnabled(false);
158  ui_->planning_scene_tree->clear();
159  ui_->planning_scene_tree->setUpdatesEnabled(true);
160 
161  ui_->database_connect_button->setUpdatesEnabled(false);
162  ui_->database_connect_button->setText(QString::fromStdString("Connect"));
163  ui_->database_connect_button->setStyleSheet("QPushButton { color : green }");
164  ui_->database_connect_button->setUpdatesEnabled(true);
165  ui_->reset_db_button->hide();
166 
167  ui_->load_scene_button->setEnabled(false);
168  ui_->load_query_button->setEnabled(false);
169  ui_->save_query_button->setEnabled(false);
170  ui_->save_scene_button->setEnabled(false);
171  ui_->delete_query_button->setEnabled(false);
172  ui_->delete_scene_button->setEnabled(false);
173  populateConstraintsList(std::vector<std::string>());
174  }
175  else if (mode == 2)
176  {
177  ui_->database_connect_button->setUpdatesEnabled(false);
178  ui_->database_connect_button->setText(QString::fromStdString("Connecting ..."));
179  ui_->database_connect_button->setUpdatesEnabled(true);
180  populateConstraintsList(std::vector<std::string>());
181  }
182  else if (mode == 3)
183  {
184  ui_->database_connect_button->setUpdatesEnabled(false);
185  ui_->database_connect_button->setText(QString::fromStdString("Connect"));
186  ui_->database_connect_button->setStyleSheet("QPushButton { color : green }");
187  ui_->database_connect_button->setUpdatesEnabled(true);
188  ui_->reset_db_button->hide();
189  }
190  else if (mode == 4)
191  {
192  ui_->database_connect_button->setUpdatesEnabled(false);
193  ui_->database_connect_button->setText(QString::fromStdString("Disconnect"));
194  ui_->database_connect_button->setStyleSheet("QPushButton { color : darkBlue }");
195  ui_->database_connect_button->setUpdatesEnabled(true);
196  ui_->save_scene_button->setEnabled(true);
197  ui_->reset_db_button->show();
198  populatePlanningSceneTreeView();
199  loadStoredStates(".*"); // automatically populate the 'Stored States' tab with all states
200  if (move_group_)
201  {
202  move_group_->setConstraintsDatabase(ui_->database_host->text().toStdString(), ui_->database_port->value());
203  planning_display_->addBackgroundJob([this]() { populateConstraintsList(); }, "populateConstraintsList");
204  }
205  }
206 }
207 
208 void MotionPlanningFrame::computeResetDbButtonClicked(const std::string& db)
209 {
210  if (db == "Constraints" && constraints_storage_)
211  constraints_storage_->reset();
212  else if (db == "Robot States" && robot_state_storage_)
213  robot_state_storage_->reset();
214  else if (db == "Planning Scenes")
215  planning_scene_storage_->reset();
216 }
217 } // namespace moveit_rviz_plugin
moveit_warehouse::PlanningSceneStoragePtr planning_scene_storage_
std::vector< moveit_msgs::msg::PlannerInterfaceDescription > planner_descriptions_
moveit_warehouse::ConstraintsStoragePtr constraints_storage_
moveit::planning_interface::MoveGroupInterfacePtr move_group_
moveit_warehouse::RobotStateStoragePtr robot_state_storage_
void addMainLoopJob(const std::function< void()> &job)
queue the execution of this function for the next time the main update() loop gets called
void addBackgroundJob(const std::function< void()> &job, const std::string &name)
warehouse_ros::DatabaseConnection::Ptr loadDatabase(const rclcpp::Node::SharedPtr &node)
Load a database connection.
std::string append(const std::string &left, const std::string &right)
const rclcpp::Logger LOGGER
Definition: async_test.h:31