42 #include <rviz_default_plugins/robot/robot.hpp>
43 #include <rviz_default_plugins/robot/robot_link.hpp>
45 #include <rviz_common/properties/property.hpp>
46 #include <rviz_common/properties/string_property.hpp>
47 #include <rviz_common/properties/bool_property.hpp>
48 #include <rviz_common/properties/float_property.hpp>
49 #include <rviz_common/properties/ros_topic_property.hpp>
50 #include <rviz_common/properties/color_property.hpp>
51 #include <rviz_common/display_context.hpp>
52 #include <rviz_common/frame_manager_iface.hpp>
54 #include <OgreSceneManager.h>
55 #include <OgreSceneNode.h>
67 "Robot Description",
"robot_description",
"The name of the ROS parameter where the URDF for the robot is loaded",
68 this, SLOT(changedRobotDescription()),
this);
71 "Robot State Topic",
"display_robot_state",
72 rosidl_generator_traits::data_type<moveit_msgs::msg::DisplayRobotState>(),
73 "The topic on which the moveit_msgs::msg::DisplayRobotState messages are received",
this,
74 SLOT(changedRobotStateTopic()),
this);
78 new rviz_common::properties::StringProperty(
"Robot Root Link",
"",
79 "Shows the name of the root link for the robot model",
this,
80 SLOT(changedRootLinkName()),
this);
84 "Robot Alpha", 1.0f,
"Specifies the alpha for the robot links",
this, SLOT(changedRobotSceneAlpha()),
this);
89 new rviz_common::properties::ColorProperty(
"Attached Body Color", QColor(150, 50, 150),
90 "The color for the attached bodies",
this,
91 SLOT(changedAttachedBodyColor()),
this);
94 "Specifies whether link highlighting is enabled",
95 this, SLOT(changedEnableLinkHighlight()),
this);
97 new rviz_common::properties::BoolProperty(
"Visual Enabled",
true,
98 "Whether to display the visual representation of the robot.",
this,
99 SLOT(changedEnableVisualVisible()),
this);
101 new rviz_common::properties::BoolProperty(
"Collision Enabled",
false,
102 "Whether to display the collision representation of the robot.",
this,
103 SLOT(changedEnableCollisionVisible()),
this);
106 "Show All Links",
true,
"Toggle all links visibility on or off.",
this, SLOT(changedAllLinks()),
this);
116 Display::onInitialize();
117 auto ros_node_abstraction = context_->getRosNodeAbstraction().lock();
118 if (!ros_node_abstraction)
120 RVIZ_COMMON_LOG_WARNING(
"Unable to lock weak_ptr from DisplayContext in RobotStateDisplay constructor");
124 node_ = ros_node_abstraction->get_raw_node();
125 robot_ = std::make_shared<RobotStateVisualization>(scene_node_, context_,
"Robot State",
this);
126 changedEnableVisualVisible();
127 changedEnableCollisionVisible();
128 robot_->setVisible(
false);
140 void RobotStateDisplay::changedAllLinks()
142 Property* links_prop = subProp(
"Links");
145 for (
int i = 0; i < links_prop->numChildren(); ++i)
147 Property* link_prop = links_prop->childAt(i);
148 link_prop->setValue(value);
154 rviz_default_plugins::robot::RobotLink* link =
robot_->getRobot().getLink(link_name);
157 link->setColor(color.r, color.g, color.b);
164 rviz_default_plugins::robot::RobotLink* link =
robot_->getRobot().getLink(link_name);
172 void RobotStateDisplay::changedEnableLinkHighlight()
176 for (std::pair<const std::string, std_msgs::msg::ColorRGBA>& highlight :
highlights_)
183 for (std::pair<const std::string, std_msgs::msg::ColorRGBA>& highlight :
highlights_)
190 void RobotStateDisplay::changedEnableVisualVisible()
195 void RobotStateDisplay::changedEnableCollisionVisible()
201 const moveit_msgs::msg::DisplayRobotState::_highlight_links_type& highlight_links)
203 if (highlight_links.empty() &&
highlights_.empty())
206 std::map<std::string, std_msgs::msg::ColorRGBA> highlights;
207 for (
const moveit_msgs::msg::ObjectColor& highlight_link : highlight_links)
209 highlights[highlight_link.id] = highlight_link.color;
214 std::map<std::string, std_msgs::msg::ColorRGBA>::iterator ho =
highlights_.begin();
215 std::map<std::string, std_msgs::msg::ColorRGBA>::iterator hn = highlights.begin();
216 while (ho !=
highlights_.end() || hn != highlights.end())
223 else if (hn == highlights.end())
228 else if (hn->first < ho->first)
233 else if (hn->first > ho->first)
238 else if (hn->second != ho->second)
255 void RobotStateDisplay::changedAttachedBodyColor()
260 std_msgs::msg::ColorRGBA color_msg;
261 color_msg.r = color.redF();
262 color_msg.g = color.greenF();
263 color_msg.b = color.blueF();
265 robot_->setDefaultAttachedObjectColor(color_msg);
270 void RobotStateDisplay::changedRobotDescription()
276 void RobotStateDisplay::changedRootLinkName()
280 void RobotStateDisplay::changedRobotSceneAlpha()
286 std_msgs::msg::ColorRGBA color_msg;
287 color_msg.r = color.redF();
288 color_msg.g = color.greenF();
289 color_msg.b = color.blueF();
291 robot_->setDefaultAttachedObjectColor(color_msg);
296 void RobotStateDisplay::changedRobotStateTopic()
298 using namespace std::placeholders;
303 robot_->setVisible(
false);
304 setStatus(rviz_common::properties::StatusProperty::Warn,
"RobotState",
"No msg received");
308 [
this](
const moveit_msgs::msg::DisplayRobotState::ConstSharedPtr& state) {
return newRobotStateCallback(state); });
328 setStatus(rviz_common::properties::StatusProperty::Error,
"RobotState", e.what());
329 robot_->setVisible(
false);
333 if (
robot_->isVisible() != !state_msg->hide)
335 robot_->setVisible(!state_msg->hide);
338 setStatus(rviz_common::properties::StatusProperty::Ok,
"RobotState",
"");
342 setStatus(rviz_common::properties::StatusProperty::Warn,
"RobotState",
"Hidden");
361 robot_->setVisible(visible);
367 rviz_default_plugins::robot::RobotLink* link = robot->getLink(link_name);
371 link->setColor(color.redF(), color.greenF(), color.blueF());
376 rviz_default_plugins::robot::RobotLink* link = robot->getLink(link_name);
390 setStatus(rviz_common::properties::StatusProperty::Error,
"RobotModel",
"`Robot Description` field can't be empty");
405 const srdf::ModelSharedPtr& srdf =
415 setStatus(rviz_common::properties::StatusProperty::Ok,
"RobotModel",
"Loaded successfully");
417 changedEnableVisualVisible();
418 changedEnableCollisionVisible();
420 catch (std::exception& e)
422 setStatus(rviz_common::properties::StatusProperty::Error,
"RobotModel",
423 QString(
"Loading failed: %1").arg(e.what()));
427 setStatus(rviz_common::properties::StatusProperty::Error,
"RobotModel",
"Loading failed");
437 Display::load(config);
445 changedRobotStateTopic();
456 robot_->setVisible(
false);
457 Display::onDisable();
480 Ogre::Vector3 position;
481 Ogre::Quaternion orientation;
483 context_->getFrameManager()->getTransform(
getRobotModel()->getModelFrame(), rclcpp::Time(0, 0, RCL_ROS_TIME),
484 position, orientation);
486 scene_node_->setPosition(position);
487 scene_node_->setOrientation(orientation);
492 Display::fixedFrameChanged();
This may be thrown if unrecoverable errors occur.
rviz_common::properties::RosTopicProperty * robot_state_topic_property_
RobotStateVisualizationPtr robot_
void load(const rviz_common::Config &config) override
moveit::core::RobotModelConstPtr robot_model_
void setHighlight(const std::string &link_name, const std_msgs::msg::ColorRGBA &color)
~RobotStateDisplay() override
void update(float wall_dt, float ros_dt) override
void setRobotHighlights(const moveit_msgs::msg::DisplayRobotState::_highlight_links_type &highlight_links)
rviz_common::properties::FloatProperty * robot_alpha_property_
rviz_common::properties::ColorProperty * attached_body_color_property_
rviz_common::properties::StringProperty * root_link_name_property_
rclcpp::Subscription< moveit_msgs::msg::DisplayRobotState >::SharedPtr robot_state_subscriber_
void unsetHighlight(const std::string &link_name)
rclcpp::Node::SharedPtr node_
rviz_common::properties::BoolProperty * enable_visual_visible_
void fixedFrameChanged() override
void calculateOffsetPosition()
Set the scene node's position, given the target frame and the planning frame.
void setVisible(bool visible)
void onInitialize() override
void unsetLinkColor(const std::string &link_name)
void newRobotStateCallback(const moveit_msgs::msg::DisplayRobotState::ConstSharedPtr &state)
rviz_common::properties::BoolProperty * enable_link_highlight_
rviz_common::properties::BoolProperty * enable_collision_visible_
rviz_common::properties::BoolProperty * show_all_links_
rviz_common::properties::StringProperty * robot_description_property_
rdf_loader::RDFLoaderPtr rdf_loader_
void setLinkColor(const std::string &link_name, const QColor &color)
std::map< std::string, std_msgs::msg::ColorRGBA > highlights_
moveit::core::RobotStatePtr robot_state_
const moveit::core::RobotModelConstPtr & getRobotModel() const
void onDisable() override
bool isEmpty(const moveit_msgs::msg::PlanningScene &msg)
Check if a message includes any information about a planning scene, or whether it is empty.
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::msg::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
Convert a robot state msg (with accompanying extra transforms) to a MoveIt robot state.
void update(moveit::core::RobotState *self, bool force, std::string &category)