path_plugin.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
31 
32 // C++ standard libraries
33 #include <cstdio>
34 #include <vector>
35 
36 // QT libraries
37 #include <QDialog>
38 #include <QGLWidget>
39 
40 // ROS libraries
41 #include <ros/master.h>
42 
44 
45 // Declare plugin
48 
49 namespace mapviz_plugins
50 {
51  PathPlugin::PathPlugin() : config_widget_(new QWidget())
52  {
53  ui_.setupUi(config_widget_);
54  ui_.path_color->setColor(Qt::green);
55 
56  // Set background white
57  QPalette p(config_widget_->palette());
58  p.setColor(QPalette::Background, Qt::white);
59  config_widget_->setPalette(p);
60 
61  // Set status text red
62  QPalette p3(ui_.status->palette());
63  p3.setColor(QPalette::Text, Qt::red);
64  ui_.status->setPalette(p3);
65 
66  connect(ui_.selecttopic, SIGNAL(clicked()), this, SLOT(SelectTopic()));
67  connect(ui_.topic, SIGNAL(editingFinished()), this, SLOT(TopicEdited()));
68  connect(ui_.path_color, SIGNAL(colorEdited(const QColor&)), this,
69  SLOT(SetColor(const QColor&)));
70  }
71 
73  {
74  }
75 
77  {
80 
81  if (!topic.name.empty())
82  {
83  ui_.topic->setText(QString::fromStdString(topic.name));
84  TopicEdited();
85  }
86  }
87 
89  {
90  std::string topic = ui_.topic->text().trimmed().toStdString();
91  if (topic != topic_)
92  {
93  initialized_ = false;
94  points_.clear();
95  has_message_ = false;
96  PrintWarning("No messages received.");
97 
99 
100  topic_ = topic;
101  if (!topic.empty())
102  {
104 
105  ROS_INFO("Subscribing to %s", topic_.c_str());
106  }
107  }
108  }
109 
110  void PathPlugin::pathCallback(const nav_msgs::PathConstPtr& path)
111  {
112  if (!has_message_)
113  {
114  initialized_ = true;
115  has_message_ = true;
116  }
117 
118  points_.clear();
119 
120  for (unsigned int i = 0; i < path->poses.size(); i++)
121  {
122  StampedPoint stamped_point;
123  stamped_point.stamp = path->header.stamp;
124  stamped_point.source_frame = path->header.frame_id;
125  stamped_point.point = tf::Point(path->poses[i].pose.position.x,
126  path->poses[i].pose.position.y, 0);
127 
128  points_.push_back(stamped_point);
129  }
130  }
131 
132  void PathPlugin::PrintError(const std::string& message)
133  {
134  PrintErrorHelper(ui_.status, message);
135  }
136 
137  void PathPlugin::PrintInfo(const std::string& message)
138  {
139  PrintInfoHelper(ui_.status, message);
140  }
141 
142  void PathPlugin::PrintWarning(const std::string& message)
143  {
144  PrintWarningHelper(ui_.status, message);
145  }
146 
147  QWidget* PathPlugin::GetConfigWidget(QWidget* parent)
148  {
149  config_widget_->setParent(parent);
150 
151  return config_widget_;
152  }
153 
154  bool PathPlugin::Initialize(QGLWidget* canvas)
155  {
156  canvas_ = canvas;
157  DrawIcon();
158  return true;
159  }
160 
161  void PathPlugin::Draw(double x, double y, double scale)
162  {
163  bool lines;
164  bool points;
165  QColor old_color = ui_.path_color->color();
166  draw_style_ = LINES;
167  lines = DrawPoints(scale);
168  color_ = color_.dark(200);
170  points = DrawPoints(scale);
171  color_ = old_color;
172  if (lines && points)
173  {
174  PrintInfo("OK");
175  }
176  }
177 
178  void PathPlugin::LoadConfig(const YAML::Node& node, const std::string& path)
179  {
180  if (swri_yaml_util::FindValue(node, "topic"))
181  {
182  std::string topic;
183  node["topic"] >> topic;
184  ui_.topic->setText(topic.c_str());
185  TopicEdited();
186  }
187 
188  if (swri_yaml_util::FindValue(node, "color"))
189  {
190  std::string color;
191  node["color"] >> color;
192  SetColor(QColor(color.c_str()));
193  ui_.path_color->setColor(color_);
194  }
195  }
196 
197  void PathPlugin::SaveConfig(YAML::Emitter& emitter, const std::string& path)
198  {
199  std::string topic = ui_.topic->text().toStdString();
200  emitter << YAML::Key << "topic" << YAML::Value << topic;
201 
202  std::string color = ui_.path_color->color().name().toStdString();
203  emitter << YAML::Key << "color" << YAML::Value << color;
204  }
205 }
void PrintInfo(const std::string &message)
static ros::master::TopicInfo selectTopic(const std::string &datatype, QWidget *parent=0)
ros::NodeHandle node_
ros::Subscriber path_sub_
Definition: path_plugin.h:92
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool Initialize(QGLWidget *canvas)
static void PrintWarningHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
static void PrintErrorHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
tf::Vector3 Point
static void PrintInfoHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
void PrintWarning(const std::string &message)
bool FindValue(const YAML::Node &node, const std::string &name)
#define ROS_INFO(...)
lines
QWidget * GetConfigWidget(QWidget *parent)
void SaveConfig(YAML::Emitter &emitter, const std::string &path)
virtual void SetColor(const QColor &color)
void LoadConfig(const YAML::Node &node, const std::string &path)
void pathCallback(const nav_msgs::PathConstPtr &path)
void PrintError(const std::string &message)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
void Draw(double x, double y, double scale)


mapviz_plugins
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 19:25:16