moveit2
The MoveIt Motion Planning Framework for ROS 2.
motion_planning_param_widget.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: Robert Haschke */
36 
39 #include <rviz_common/properties/int_property.hpp>
40 #include <rviz_common/properties/float_property.hpp>
41 #include <rviz_common/properties/string_property.hpp>
42 
44 
45 namespace moveit_rviz_plugin
46 {
47 static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit_ros_visualization.motion_planning_param_widget");
48 
50  : rviz_common::properties::PropertyTreeWidget(parent)
51 {
52  property_tree_model_ = nullptr;
53 }
54 
56 {
57  delete property_tree_model_;
58 }
59 
60 void MotionPlanningParamWidget::setMoveGroup(const mpi::MoveGroupInterfacePtr& mg)
61 {
62  move_group_ = mg;
63  if (mg)
64  setPlannerId(planner_id_);
65 }
66 
67 void MotionPlanningParamWidget::setGroupName(const std::string& group_name)
68 {
69  group_name_ = group_name;
70  this->setModel(nullptr);
71  if (property_tree_model_)
72  delete property_tree_model_;
73  property_tree_model_ = nullptr;
74 }
75 
76 bool try_lexical_convert(const QString& value, long& lvalue)
77 {
78  bool ok;
79  lvalue = value.toLong(&ok);
80  return ok;
81 }
82 
83 bool try_lexical_convert(const QString& value, double& dvalue)
84 {
85  bool ok;
86  dvalue = value.toDouble(&ok);
87  return ok;
88 }
89 
90 rviz_common::properties::Property* MotionPlanningParamWidget::createPropertyTree()
91 {
92  if (planner_id_.empty())
93  return nullptr;
94  const std::map<std::string, std::string>& params = move_group_->getPlannerParams(planner_id_, group_name_);
95 
96  auto root = new rviz_common::properties::Property(QString::fromStdString(planner_id_ + " parameters"));
97  for (const std::pair<const std::string, std::string>& param : params)
98  {
99  const QString key = QString::fromStdString(param.first);
100  const QString value = QString::fromStdString(param.second);
101  long value_long;
102  double value_double;
103 
104  if (try_lexical_convert(value, value_long))
105  {
106  new rviz_common::properties::IntProperty(key, value_long, QString(), root, SLOT(changedValue()), this);
107  }
108  else if (try_lexical_convert(value, value_double))
109  {
110  new rviz_common::properties::FloatProperty(key, value_double, QString(), root, SLOT(changedValue()), this);
111  }
112  else
113  new rviz_common::properties::StringProperty(key, value, QString(), root, SLOT(changedValue()), this);
114  }
115  return root;
116 }
117 
118 void MotionPlanningParamWidget::changedValue()
119 {
120  if (!move_group_)
121  return;
122  rviz_common::properties::Property* source = qobject_cast<rviz_common::properties::Property*>(QObject::sender());
123  std::map<std::string, std::string> params;
124  params[source->getName().toStdString()] = source->getValue().toString().toStdString();
125  move_group_->setPlannerParams(planner_id_, group_name_, params);
126 }
127 
128 void MotionPlanningParamWidget::setPlannerId(const std::string& planner_id)
129 {
130  planner_id_ = planner_id;
131  if (!move_group_)
132  return;
133 
134  rviz_common::properties::PropertyTreeModel* old_model = property_tree_model_;
135  rviz_common::properties::Property* root = createPropertyTree();
136  property_tree_model_ = root ? new rviz_common::properties::PropertyTreeModel(root) : nullptr;
137  this->setModel(property_tree_model_);
138  if (old_model)
139  delete old_model;
140 }
141 } // namespace moveit_rviz_plugin
void setMoveGroup(const moveit::planning_interface::MoveGroupInterfacePtr &mg)
MotionPlanningParamWidget(const MotionPlanningParamWidget &)=delete
Simple interface to MoveIt components.
Definition: moveit_cpp.h:203
bool try_lexical_convert(const QString &value, long &lvalue)
const rclcpp::Logger LOGGER
Definition: async_test.h:31