37 #include <boost/algorithm/string.hpp>
38 #include <boost/algorithm/string/replace.hpp>
44 #include <rviz_default_plugins/robot/robot.hpp>
47 #include <rviz_common/display_context.hpp>
48 #include <rviz_common/properties/bool_property.hpp>
49 #include <rviz_common/properties/color_property.hpp>
50 #include <rviz_common/properties/editable_enum_property.hpp>
51 #include <rviz_common/properties/float_property.hpp>
52 #include <rviz_common/properties/int_property.hpp>
53 #include <rviz_common/properties/property.hpp>
54 #include <rviz_common/properties/ros_topic_property.hpp>
55 #include <rviz_common/properties/string_property.hpp>
56 #include <rviz_default_plugins/robot/robot_link.hpp>
57 #include <rviz_common/window_manager_interface.hpp>
61 using namespace std::placeholders;
65 static const rclcpp::Logger
LOGGER = rclcpp::get_logger(
"moveit_rviz_plugin_render_tools.trajectory_visualization");
67 TrajectoryVisualization::TrajectoryVisualization(rviz_common::properties::Property* widget,
68 rviz_common::Display* display)
69 : animating_path_(false)
70 , drop_displaying_trajectory_(false)
74 , trajectory_slider_panel_(nullptr)
75 , trajectory_slider_dock_panel_(nullptr)
78 "Trajectory Topic",
"/display_planned_path",
79 rosidl_generator_traits::data_type<moveit_msgs::msg::DisplayTrajectory>(),
80 "The topic on which the moveit_msgs::msg::DisplayTrajectory messages are received", widget,
81 SLOT(changedTrajectoryTopic()),
this);
84 new rviz_common::properties::BoolProperty(
"Show Robot Visual",
true,
85 "Indicates whether the geometry of the robot as defined for "
86 "visualisation purposes should be displayed",
87 widget, SLOT(changedDisplayPathVisualEnabled()),
this);
90 new rviz_common::properties::BoolProperty(
"Show Robot Collision",
false,
91 "Indicates whether the geometry of the robot as defined "
92 "for collision detection purposes should be displayed",
93 widget, SLOT(changedDisplayPathCollisionEnabled()),
this);
96 "Robot Alpha", 0.5f,
"Specifies the alpha for the robot links", widget, SLOT(changedRobotPathAlpha()),
this);
101 new rviz_common::properties::EditableEnumProperty(
"State Display Time",
"3x",
102 "Replay speed of trajectory. Either as factor of its time"
103 "parameterization or as constant time (s) per waypoint.",
104 widget, SLOT(changedStateDisplayTime()),
this);
113 "Use Sim Time",
false,
"Indicates whether simulation time or wall-time is used for state display timing.", widget,
117 "Indicates whether the last received path "
118 "is to be animated in a loop",
119 widget, SLOT(changedLoopDisplay()),
this);
121 trail_display_property_ =
new rviz_common::properties::BoolProperty(
"Show Trail",
false,
"Show a path trail", widget,
122 SLOT(changedShowTrail()),
this);
125 "Specifies the step size of the samples "
126 "shown in the trajectory trail.",
127 widget, SLOT(changedTrailStepSize()),
this);
131 "Interrupt Display",
false,
132 "Immediately show newly planned trajectory, interrupting the currently displayed one.", widget);
135 "Robot Color", QColor(150, 50, 150),
"The color of the animated robot", widget, SLOT(changedRobotColor()),
this);
138 "Color Enabled",
false,
"Specifies whether robot coloring is enabled", widget, SLOT(enabledRobotColor()),
this);
153 rviz_common::DisplayContext* context)
160 auto ros_node_abstraction =
context_->getRosNodeAbstraction().lock();
161 if (!ros_node_abstraction)
163 RVIZ_COMMON_LOG_WARNING(
"Unable to lock weak_ptr from DisplayContext in TrajectoryVisualization constructor");
174 rviz_common::WindowManagerInterface* window_context =
context_->getWindowManager();
182 SLOT(trajectorySliderPanelVisibilityChange(
bool)));
204 RCLCPP_ERROR_STREAM(LOGGER,
"No robot model found");
218 changedTrajectoryTopic();
238 void TrajectoryVisualization::changedLoopDisplay()
245 void TrajectoryVisualization::changedShowTrail()
259 trajectory_trail_.resize(
static_cast<int>(std::ceil((t->getWayPointCount() + stepsize - 1) / (
float)stepsize)));
262 int waypoint_i = std::min(i * stepsize, t->getWayPointCount() - 1);
264 std::make_unique<RobotStateVisualization>(
scene_node_,
context_,
"Trail Robot " + std::to_string(i),
nullptr);
277 void TrajectoryVisualization::changedTrailStepSize()
283 void TrajectoryVisualization::changedRobotPathAlpha()
290 void TrajectoryVisualization::changedTrajectoryTopic()
297 [
this](
const moveit_msgs::msg::DisplayTrajectory::ConstSharedPtr& msg) {
303 void TrajectoryVisualization::changedDisplayPathVisualEnabled()
314 void TrajectoryVisualization::changedStateDisplayTime()
318 void TrajectoryVisualization::changedDisplayPathCollisionEnabled()
331 changedRobotPathAlpha();
343 changedTrajectoryTopic();
350 r->setVisible(
false);
368 constexpr
char default_time_string[] =
"3x";
369 constexpr
float default_time_value = -3.0f;
376 if (tm.back() ==
'x')
380 else if (tm.back() ==
's')
387 return default_time_value;
390 tm.resize(tm.size() - 1);
391 boost::trim_right(tm);
396 value = std::stof(tm);
398 catch (
const std::invalid_argument& ex)
401 return default_time_value;
407 return default_time_value;
491 const float rt_factor = -tm;
544 RCLCPP_ERROR_STREAM(LOGGER,
"No robot state or robot model loaded");
548 if (!msg->model_id.empty() && msg->model_id !=
robot_model_->getName())
549 RCLCPP_WARN(LOGGER,
"Received a trajectory to display for model '%s' but model '%s' was expected",
550 msg->model_id.c_str(),
robot_model_->getName().c_str());
554 auto t = std::make_shared<robot_trajectory::RobotTrajectory>(
robot_model_,
"");
557 for (std::size_t i = 0; i < msg->trajectory.size(); ++i)
561 t->setRobotTrajectoryMsg(*
robot_state_, msg->trajectory_start, msg->trajectory[i]);
570 display_->setStatus(rviz_common::properties::StatusProperty::Ok,
"Trajectory",
"");
574 display_->setStatus(rviz_common::properties::StatusProperty::Error,
"Trajectory", e.what());
587 void TrajectoryVisualization::changedRobotColor()
593 void TrajectoryVisualization::enabledRobotColor()
603 for (
auto& link :
robot->getLinks())
604 link.second->unsetColor();
625 for (
auto& link :
robot->getLinks())
626 robot->getLink(link.first)->setColor(color.redF(), color.greenF(), color.blueF());
629 void TrajectoryVisualization::trajectorySliderPanelVisibilityChange(
bool enable)
This may be thrown if unrecoverable errors occur.
void onInitialize() override
void setSliderPosition(int position)
int getSliderPosition() const
void pauseButton(bool check)
void update(int way_point_count)
rviz_common::properties::BoolProperty * enable_robot_color_property_
void clearTrajectoryTrail()
void incomingDisplayTrajectory(const moveit_msgs::msg::DisplayTrajectory::ConstSharedPtr &msg)
ROS callback for an incoming path message.
Ogre::SceneNode * scene_node_
rviz_common::DisplayContext * context_
rviz_common::properties::BoolProperty * display_path_visual_enabled_property_
void setName(const QString &name)
rviz_common::properties::BoolProperty * interrupt_display_property_
robot_trajectory::RobotTrajectoryPtr trajectory_message_to_display_
robot_trajectory::RobotTrajectoryPtr displaying_trajectory_message_
std::mutex update_trajectory_message_
rviz_common::PanelDockWidget * trajectory_slider_dock_panel_
void onInitialize(const rclcpp::Node::SharedPtr &node, Ogre::SceneNode *scene_node, rviz_common::DisplayContext *context)
rviz_common::properties::BoolProperty * trail_display_property_
TrajectoryPanel * trajectory_slider_panel_
float current_state_time_
rviz_common::properties::BoolProperty * display_path_collision_enabled_property_
moveit::core::RobotStatePtr robot_state_
rviz_common::properties::RosTopicProperty * trajectory_topic_property_
moveit::core::RobotModelConstPtr robot_model_
rviz_common::properties::BoolProperty * use_sim_time_property_
std_msgs::msg::ColorRGBA default_attached_object_color_
rviz_common::properties::Property * widget_
rviz_common::properties::BoolProperty * loop_display_property_
rviz_common::properties::ColorProperty * robot_color_property_
void setRobotColor(rviz_default_plugins::robot::Robot *robot, const QColor &color)
float getStateDisplayTime()
get time to show each single robot state
rclcpp::Node::SharedPtr node_
rviz_common::properties::EditableEnumProperty * state_display_time_property_
void setDefaultAttachedObjectColor(const QColor &color)
rviz_common::properties::IntProperty * trail_step_size_property_
void interruptCurrentDisplay()
rviz_common::Display * display_
std::vector< RobotStateVisualizationUniquePtr > trajectory_trail_
void unsetRobotColor(rviz_default_plugins::robot::Robot *robot)
~TrajectoryVisualization() override
void onRobotModelLoaded(const moveit::core::RobotModelConstPtr &robot_model)
virtual void update(float wall_dt, float sim_dt)
RobotStateVisualizationPtr display_path_robot_
rviz_common::properties::FloatProperty * robot_path_alpha_property_
bool drop_displaying_trajectory_
rclcpp::Subscription< moveit_msgs::msg::DisplayTrajectory >::SharedPtr trajectory_topic_sub_
Maintain a sequence of waypoints and the time durations between these waypoints.
RobotTrajectory & setRobotTrajectoryMsg(const moveit::core::RobotState &reference_state, const trajectory_msgs::msg::JointTrajectory &trajectory)
Copy the content of the trajectory message into this class. The trajectory message itself is not requ...
RobotTrajectory & append(const RobotTrajectory &source, double dt, size_t start_index=0, size_t end_index=std::numeric_limits< std::size_t >::max())
Add a specified part of a trajectory to the end of the current trajectory. The default (when start_in...
const rclcpp::Logger LOGGER