moveit2
The MoveIt Motion Planning Framework for ROS 2.
robot_state_visualization.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 */
36 
40 #include <rviz_common/properties/parse_color.hpp>
41 #include <rviz_default_plugins/robot/robot_link.hpp>
42 #include <QApplication>
43 #include <moveit/utils/logger.hpp>
44 
45 namespace moveit_rviz_plugin
46 {
47 namespace
48 {
49 rclcpp::Logger getLogger()
50 {
51  return moveit::getLogger("moveit.ros.rviz_plugin_render_tools.robot_state_visualization");
52 }
53 } // namespace
54 
55 RobotStateVisualization::RobotStateVisualization(Ogre::SceneNode* root_node, rviz_common::DisplayContext* context,
56  const std::string& name,
57  rviz_common::properties::Property* parent_property)
58  : robot_(root_node, context, name, parent_property)
59  , octree_voxel_render_mode_(OCTOMAP_OCCUPIED_VOXELS)
60  , octree_voxel_color_mode_(OCTOMAP_Z_AXIS_COLOR)
61  , visible_(true)
62  , visual_visible_(true)
63  , collision_visible_(false)
64 {
65  default_attached_object_color_.r = 0.0f;
66  default_attached_object_color_.g = 0.7f;
67  default_attached_object_color_.b = 0.0f;
68  default_attached_object_color_.a = 1.0f;
69  render_shapes_ = std::make_shared<RenderShapes>(context);
70 }
71 
72 void RobotStateVisualization::load(const urdf::ModelInterface& descr, bool visual, bool collision)
73 {
74  // clear previously loaded model
75  clear();
76 
77  robot_.load(descr, visual, collision);
78  robot_.setVisualVisible(visual_visible_);
79  robot_.setCollisionVisible(collision_visible_);
80  robot_.setVisible(visible_);
81 }
82 
84 {
85  render_shapes_->clear();
86  robot_.clear();
87 }
88 
89 void RobotStateVisualization::setDefaultAttachedObjectColor(const std_msgs::msg::ColorRGBA& default_attached_object_color)
90 {
91  default_attached_object_color_ = default_attached_object_color;
92 }
93 
94 void RobotStateVisualization::updateAttachedObjectColors(const std_msgs::msg::ColorRGBA& attached_object_color)
95 {
96  render_shapes_->updateShapeColors(attached_object_color.r, attached_object_color.g, attached_object_color.b,
97  robot_.getAlpha());
98 }
99 
100 void RobotStateVisualization::update(const moveit::core::RobotStateConstPtr& robot_state)
101 {
102  updateHelper(robot_state, default_attached_object_color_, nullptr);
103 }
104 
105 void RobotStateVisualization::update(const moveit::core::RobotStateConstPtr& robot_state,
106  const std_msgs::msg::ColorRGBA& default_attached_object_color)
107 {
108  updateHelper(robot_state, default_attached_object_color, nullptr);
109 }
110 
111 void RobotStateVisualization::update(const moveit::core::RobotStateConstPtr& robot_state,
112  const std_msgs::msg::ColorRGBA& default_attached_object_color,
113  const std::map<std::string, std_msgs::msg::ColorRGBA>& color_map)
114 {
115  updateHelper(robot_state, default_attached_object_color, &color_map);
116 }
117 
118 void RobotStateVisualization::updateHelper(const moveit::core::RobotStateConstPtr& robot_state,
119  const std_msgs::msg::ColorRGBA& default_attached_object_color,
120  const std::map<std::string, std_msgs::msg::ColorRGBA>* color_map)
121 {
122  robot_.update(PlanningLinkUpdater(robot_state));
123  render_shapes_->clear();
124 
125  std::vector<const moveit::core::AttachedBody*> attached_bodies;
126  robot_state->getAttachedBodies(attached_bodies);
127  for (const moveit::core::AttachedBody* attached_body : attached_bodies)
128  {
129  std_msgs::msg::ColorRGBA color = default_attached_object_color;
130  double alpha = robot_.getAlpha();
131  if (color_map)
132  {
133  std::map<std::string, std_msgs::msg::ColorRGBA>::const_iterator it = color_map->find(attached_body->getName());
134  if (it != color_map->end())
135  { // render attached bodies with a color that is a bit different
136  color.r = std::max(1.0f, it->second.r * 1.05f);
137  color.g = std::max(1.0f, it->second.g * 1.05f);
138  color.b = std::max(1.0f, it->second.b * 1.05f);
139  alpha = color.a = it->second.a;
140  }
141  }
142  rviz_default_plugins::robot::RobotLink* link = robot_.getLink(attached_body->getAttachedLinkName());
143  if (!link)
144  {
145  RCLCPP_ERROR_STREAM(getLogger(), "Link " << attached_body->getAttachedLinkName() << " not found in rviz::Robot");
146  continue;
147  }
148  Ogre::ColourValue rcolor(color.r, color.g, color.b, color.a);
149  const EigenSTL::vector_Isometry3d& ab_t = attached_body->getShapePosesInLinkFrame();
150  const std::vector<shapes::ShapeConstPtr>& ab_shapes = attached_body->getShapes();
151  for (std::size_t j = 0; j < ab_shapes.size(); ++j)
152  {
153  render_shapes_->renderShape(link->getVisualNode(), ab_shapes[j].get(), ab_t[j], octree_voxel_render_mode_,
154  octree_voxel_color_mode_, rcolor, alpha);
155  render_shapes_->renderShape(link->getCollisionNode(), ab_shapes[j].get(), ab_t[j], octree_voxel_render_mode_,
156  octree_voxel_color_mode_, rcolor, alpha);
157  }
158  }
159  robot_.setVisualVisible(visual_visible_);
160  robot_.setCollisionVisible(collision_visible_);
161  robot_.setVisible(visible_);
162 }
163 
164 void RobotStateVisualization::updateKinematicState(const moveit::core::RobotStateConstPtr& robot_state)
165 {
166  robot_.update(PlanningLinkUpdater(robot_state));
167 }
168 
170 {
171  visible_ = visible;
172  robot_.setVisible(visible);
173 }
174 
176 {
177  visual_visible_ = visible;
178  robot_.setVisualVisible(visible);
179 }
180 
182 {
183  collision_visible_ = visible;
184  robot_.setCollisionVisible(visible);
185 }
186 
188 {
189  robot_.setAlpha(alpha);
190 }
191 } // namespace moveit_rviz_plugin
Object defining bodies that can be attached to robot links.
Definition: attached_body.h:58
Update the links of an rviz::Robot using a moveit::core::RobotState.
void update(const moveit::core::RobotStateConstPtr &robot_state)
void setCollisionVisible(bool visible)
Set whether the collision meshes/primitives of the robot should be visible.
void updateKinematicState(const moveit::core::RobotStateConstPtr &robot_state)
void setVisualVisible(bool visible)
Set whether the visual meshes of the robot should be visible.
void setVisible(bool visible)
Set the robot as a whole to be visible or not.
RobotStateVisualization(Ogre::SceneNode *root_node, rviz_common::DisplayContext *context, const std::string &name, rviz_common::properties::Property *parent_property)
void load(const urdf::ModelInterface &descr, bool visual=true, bool collision=true)
void setDefaultAttachedObjectColor(const std_msgs::msg::ColorRGBA &default_attached_object_color)
void updateAttachedObjectColors(const std_msgs::msg::ColorRGBA &attached_object_color)
update color of all attached object shapes
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
Definition: logger.cpp:79
name
Definition: setup.py:7