moveit2
The MoveIt Motion Planning Framework for ROS 2.
trajectory_display.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2015, University of Colorado, Boulder
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 the Univ of CO, Boulder 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: Dave Coleman
36  Desc: Wraps a trajectory_visualization playback class for Rviz into a stand alone display
37 */
38 
40 #include <rviz_common/properties/string_property.hpp>
41 #include <moveit/utils/logger.hpp>
42 
43 #include <memory>
44 
45 namespace moveit_rviz_plugin
46 {
47 
48 TrajectoryDisplay::TrajectoryDisplay() : Display(), logger_(moveit::getLogger("moveit.ros.trajectory_display"))
49 
50 {
51  // The robot description property is only needed when using the trajectory playback standalone (not within motion
52  // planning plugin)
53  robot_description_property_ = new rviz_common::properties::StringProperty(
54  "Robot Description", "robot_description", "The name of the ROS parameter where the URDF for the robot is loaded",
55  this, SLOT(changedRobotDescription()), this);
56 
57  trajectory_visual_ = std::make_shared<TrajectoryVisualization>(this, this);
58 }
59 
61 
63 {
64  Display::onInitialize();
65 
66  auto ros_node_abstraction = context_->getRosNodeAbstraction().lock();
67  if (!ros_node_abstraction)
68  {
69  RCLCPP_INFO(logger_, "Unable to lock weak_ptr from DisplayContext in TrajectoryDisplay constructor");
70  return;
71  }
72  node_ = ros_node_abstraction->get_raw_node();
73  trajectory_visual_->onInitialize(node_, scene_node_, context_);
74 }
75 
77 {
78  try
79  {
80  rdf_loader_ = std::make_shared<rdf_loader::RDFLoader>(node_, robot_description_property_->getStdString());
81 
82  if (!rdf_loader_->getURDF())
83  {
84  setStatus(rviz_common::properties::StatusProperty::Error, "Robot Model",
85  "Failed to load from parameter " + robot_description_property_->getString());
86  return;
87  }
88  setStatus(rviz_common::properties::StatusProperty::Ok, "Robot Model", "Successfully loaded");
89 
90  const srdf::ModelSharedPtr& srdf =
91  rdf_loader_->getSRDF() ? rdf_loader_->getSRDF() : std::make_shared<srdf::Model>();
92  robot_model_ = std::make_shared<moveit::core::RobotModel>(rdf_loader_->getURDF(), srdf);
93 
94  // Send to child class
95  trajectory_visual_->onRobotModelLoaded(robot_model_);
96  }
97  catch (std::exception& e)
98  {
99  setStatus(rviz_common::properties::StatusProperty::Error, "RobotModel", QString("Loading failed: %1").arg(e.what()));
100  }
101 }
102 
104 {
105  Display::reset();
106  loadRobotModel();
107  trajectory_visual_->reset();
108 }
109 
110 void TrajectoryDisplay::load(const rviz_common::Config& config)
111 {
112  // This property needs to be loaded in onEnable() below, which is triggered
113  // in the beginning of Display::load() before the other property would be available
114  robot_description_property_->load(config.mapGetChild("Robot Description"));
115  Display::load(config);
116 }
117 
119 {
120  Display::onEnable();
121  if (!rdf_loader_)
122  loadRobotModel();
123  trajectory_visual_->onEnable();
124 }
125 
127 {
128  Display::onDisable();
129  trajectory_visual_->onDisable();
130 }
131 
132 void TrajectoryDisplay::update(float wall_dt, float ros_dt)
133 {
134  Display::update(wall_dt, ros_dt);
135  trajectory_visual_->update(wall_dt, ros_dt);
136 }
137 
138 void TrajectoryDisplay::changedRobotDescription()
139 {
140  if (isEnabled())
141  {
142  reset();
143  }
144  else
145  {
146  loadRobotModel();
147  }
148 }
149 
150 } // namespace moveit_rviz_plugin
void load(const rviz_common::Config &config) override
moveit::core::RobotModelConstPtr robot_model_
TrajectoryVisualizationPtr trajectory_visual_
void update(float wall_dt, float ros_dt) override
rviz_common::properties::StringProperty * robot_description_property_
void update(moveit::core::RobotState *self, bool force, std::string &category)
Definition: robot_state.cpp:47
Main namespace for MoveIt.
Definition: exceptions.hpp:43