moveit2
The MoveIt Motion Planning Framework for ROS 2.
rviz_panel.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2021, PickNik Robotics
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 PickNik Robotics 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: David V. Lu!! */
39 #include <QApplication>
40 #include <rviz_rendering/render_window.hpp>
41 #include <rviz_common/tool_manager.hpp>
42 
43 namespace moveit_setup
44 {
45 RVizPanel::RVizPanel(QWidget* parent,
46  const rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr& node_abstraction,
47  const DataWarehousePtr& config_data)
48  : QWidget(parent)
49  , parent_(parent)
50  , node_abstraction_(node_abstraction)
51  , node_(node_abstraction_.lock()->get_raw_node())
52  , config_data_(config_data)
53 {
54  logger_ = std::make_shared<rclcpp::Logger>(node_->get_logger().get_child("RVizPanel"));
55 
56  connect(this, SIGNAL(highlightLinkSignal(const std::string&, const QColor&)), this,
57  SLOT(highlightLinkEvent(const std::string&, const QColor&)));
58  connect(this, SIGNAL(highlightGroupSignal(const std::string&)), this, SLOT(highlightGroupEvent(const std::string&)));
59  connect(this, SIGNAL(unhighlightAllSignal()), this, SLOT(unhighlightAllEvent()));
60 }
61 
63 {
64  // Create rviz frame
65  rviz_render_panel_ = new rviz_common::RenderPanel();
66  rviz_render_panel_->setMinimumWidth(200);
67  rviz_render_panel_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
68 
69  QApplication::processEvents();
70  rviz_render_panel_->getRenderWindow()->initialize();
71 
73  new rviz_common::VisualizationManager(rviz_render_panel_, node_abstraction_, this, node_->get_clock());
74  rviz_render_panel_->initialize(rviz_manager_);
75  rviz_manager_->initialize();
76  rviz_manager_->startUpdate();
77 
78  auto tm = rviz_manager_->getToolManager();
79  tm->addTool("rviz_default_plugins/MoveCamera");
80 
81  // Create the MoveIt Rviz Plugin and attach to display
83  robot_state_display_->setName("Robot State");
84 
85  rviz_manager_->addDisplay(robot_state_display_, true);
86 
87  // Set the fixed and target frame
89 
90  // Set the topic on which the moveit_msgs::msg::PlanningScene messages are received
91  robot_state_display_->subProp("Robot State Topic")->setValue(QString::fromStdString(MOVEIT_ROBOT_STATE));
92 
93  // Set robot description
94  robot_state_display_->subProp("Robot Description")->setValue(QString::fromStdString(ROBOT_DESCRIPTION));
96  // Zoom into robot
97  rviz_common::ViewController* view = rviz_manager_->getViewManager()->getCurrent();
98  view->subProp("Distance")->setValue(4.0f);
99 
100  // Add Rviz to Planning Groups Widget
101  QVBoxLayout* rviz_layout = new QVBoxLayout();
102  rviz_layout->addWidget(rviz_render_panel_);
103  setLayout(rviz_layout);
104 
105  // visual / collision buttons
106  auto btn_layout = new QHBoxLayout();
107  rviz_layout->addLayout(btn_layout);
108 
109  QCheckBox* btn;
110  btn_layout->addWidget(btn = new QCheckBox("visual"), 0);
111  btn->setChecked(true);
112  connect(btn, &QCheckBox::toggled,
113  [this](bool checked) { robot_state_display_->subProp("Visual Enabled")->setValue(checked); });
114 
115  btn_layout->addWidget(btn = new QCheckBox("collision"), 1);
116  btn->setChecked(false);
117  connect(btn, &QCheckBox::toggled,
118  [this](bool checked) { robot_state_display_->subProp("Collision Enabled")->setValue(checked); });
119 }
121 {
122  if (rviz_manager_ != nullptr)
123  rviz_manager_->removeAllDisplays();
124  if (rviz_render_panel_ != nullptr)
125  delete rviz_render_panel_;
126  if (rviz_manager_ != nullptr)
127  delete rviz_manager_;
128 }
129 
130 moveit::core::RobotModelPtr RVizPanel::getRobotModel() const
131 {
132  auto urdf = config_data_->get<moveit_setup::URDFConfig>("urdf");
133 
134  if (!urdf->isConfigured())
135  {
136  return nullptr;
137  }
138 
139  auto srdf = config_data_->get<moveit_setup::SRDFConfig>("srdf");
140 
141  return srdf->getRobotModel();
142 }
143 
145 {
146  auto rm = getRobotModel();
147  if (rm && rviz_manager_ && robot_state_display_)
148  {
149  std::string frame = rm->getModelFrame();
150  rviz_manager_->setFixedFrame(QString::fromStdString(frame));
153  }
154 }
155 
156 // ******************************************************************************************
157 // Highlight a robot link
158 // ******************************************************************************************
159 void RVizPanel::highlightLinkEvent(const std::string& link_name, const QColor& color)
160 {
161  auto rm = getRobotModel();
162  if (!rm)
163  return;
164  const moveit::core::LinkModel* lm = rm->getLinkModel(link_name);
165  if (!lm->getShapes().empty()) // skip links with no geometry
166  robot_state_display_->setLinkColor(link_name, color);
167 }
168 
169 // ******************************************************************************************
170 // Highlight a robot group
171 // ******************************************************************************************
172 void RVizPanel::highlightGroupEvent(const std::string& group_name)
173 {
174  auto rm = getRobotModel();
175  if (!rm)
176  return;
177  // Highlight the selected planning group by looping through the links
178  if (!rm->hasJointModelGroup(group_name))
179  return;
180 
181  const moveit::core::JointModelGroup* joint_model_group = rm->getJointModelGroup(group_name);
182  if (joint_model_group)
183  {
184  const std::vector<const moveit::core::LinkModel*>& link_models = joint_model_group->getLinkModels();
185  // Iterate through the links
186  for (std::vector<const moveit::core::LinkModel*>::const_iterator link_it = link_models.begin();
187  link_it < link_models.end(); ++link_it)
188  highlightLink((*link_it)->getName(), QColor(255, 0, 0));
189  }
190 }
191 
192 // ******************************************************************************************
193 // Unhighlight all robot links
194 // ******************************************************************************************
196 {
197  auto rm = getRobotModel();
198  if (!rm)
199  return;
200  // Get the names of the all links robot
201  const std::vector<std::string>& links = rm->getLinkModelNamesWithCollisionGeometry();
202 
203  // Quit if no links found
204  if (links.empty())
205  {
206  return;
207  }
208 
209  // check if rviz is ready
211  {
212  return;
213  }
214 
215  // Iterate through the links
216  for (std::vector<std::string>::const_iterator link_it = links.begin(); link_it < links.end(); ++link_it)
217  {
218  if ((*link_it).empty())
219  continue;
220 
222  }
223 }
224 
225 } // namespace moveit_setup
const std::vector< const LinkModel * > & getLinkModels() const
Get the links that are part of this joint group.
A link from the robot. Contains the constant transform applied to the link and its geometry.
Definition: link_model.h:72
const std::vector< shapes::ShapeConstPtr > & getShapes() const
Get shape associated to the collision geometry for this link.
Definition: link_model.h:176
void unsetLinkColor(const std::string &link_name)
void setLinkColor(const std::string &link_name, const QColor &color)
void highlightLinkEvent(const std::string &link_name, const QColor &color)
Definition: rviz_panel.cpp:159
rviz_common::VisualizationManager * rviz_manager_
Definition: rviz_panel.hpp:133
DataWarehousePtr config_data_
Definition: rviz_panel.hpp:139
rclcpp::Node::SharedPtr node_
Definition: rviz_panel.hpp:136
std::shared_ptr< rclcpp::Logger > logger_
Definition: rviz_panel.hpp:137
void highlightLinkSignal(const std::string &link_name, const QColor &color)
rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr node_abstraction_
Definition: rviz_panel.hpp:135
void highlightGroupEvent(const std::string &group_name)
Definition: rviz_panel.cpp:172
void highlightGroupSignal(const std::string &group_name)
moveit::core::RobotModelPtr getRobotModel() const
Definition: rviz_panel.cpp:130
void highlightLink(const std::string &link_name, const QColor &color)
Definition: rviz_panel.hpp:96
moveit_rviz_plugin::RobotStateDisplay * robot_state_display_
Definition: rviz_panel.hpp:134
RVizPanel(QWidget *parent, const rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr &node_abstraction, const DataWarehousePtr &config_data)
Definition: rviz_panel.cpp:45
rviz_common::RenderPanel * rviz_render_panel_
Definition: rviz_panel.hpp:132
moveit::core::RobotModelPtr getRobotModel() const
Definition: srdf_config.hpp:82