gps_plugin.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (C) 2013 All Right Reserved, Southwest Research Institute® (SwRI®)
4 //
5 // Contract No. 10-58058A
6 // Contractor Southwest Research Institute® (SwRI®)
7 // Address 6220 Culebra Road, San Antonio, Texas 78228-0510
8 // Contact Steve Dellenback <sdellenback@swri.org> (210) 522-3914
9 //
10 // This code was developed as part of an internal research project fully funded
11 // by Southwest Research Institute®.
12 //
13 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
14 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
15 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
16 // PARTICULAR PURPOSE.
17 //
18 // *****************************************************************************
19 
21 
22 // C++ standard libraries
23 #include <cstdio>
24 #include <vector>
25 
26 // QT libraries
27 #include <QDialog>
28 #include <QGLWidget>
29 #include <QPalette>
30 
31 #include <opencv2/core/core.hpp>
32 
33 // ROS libraries
34 #include <ros/master.h>
35 
39 
40 // Declare plugin
43 
44 namespace mapviz_plugins
45 {
46  GpsPlugin::GpsPlugin() : config_widget_(new QWidget())
47  {
48  ui_.setupUi(config_widget_);
49 
50  ui_.color->setColor(Qt::green);
51 
52  // Set background white
53  QPalette p(config_widget_->palette());
54  p.setColor(QPalette::Background, Qt::white);
55  config_widget_->setPalette(p);
56 
57  // Set status text red
58  QPalette p3(ui_.status->palette());
59  p3.setColor(QPalette::Text, Qt::red);
60  ui_.status->setPalette(p3);
61 
62  QObject::connect(ui_.selecttopic, SIGNAL(clicked()), this,
63  SLOT(SelectTopic()));
64  QObject::connect(ui_.topic, SIGNAL(editingFinished()), this,
65  SLOT(TopicEdited()));
66  QObject::connect(ui_.positiontolerance, SIGNAL(valueChanged(double)), this,
67  SLOT(PositionToleranceChanged(double)));
68  QObject::connect(ui_.buffersize, SIGNAL(valueChanged(int)), this,
69  SLOT(BufferSizeChanged(int)));
70  QObject::connect(ui_.drawstyle, SIGNAL(activated(QString)), this,
71  SLOT(SetDrawStyle(QString)));
72  QObject::connect(ui_.static_arrow_sizes, SIGNAL(clicked(bool)),
73  this, SLOT(SetStaticArrowSizes(bool)));
74  QObject::connect(ui_.arrow_size, SIGNAL(valueChanged(int)),
75  this, SLOT(SetArrowSize(int)));
76  connect(ui_.color, SIGNAL(colorEdited(const QColor&)), this,
77  SLOT(SetColor(const QColor&)));
78  }
79 
81  {
82  }
83 
85  {
87  mapviz::SelectTopicDialog::selectTopic("gps_common/GPSFix");
88 
89  if (!topic.name.empty())
90  {
91  ui_.topic->setText(QString::fromStdString(topic.name));
92  TopicEdited();
93  }
94  }
95 
97  {
98  std::string topic = ui_.topic->text().trimmed().toStdString();
99  if (topic != topic_)
100  {
101  initialized_ = false;
102  points_.clear();
103  has_message_ = false;
104  PrintWarning("No messages received.");
105 
106  gps_sub_.shutdown();
107 
108  topic_ = topic;
109  if (!topic.empty())
110  {
112 
113  ROS_INFO("Subscribing to %s", topic_.c_str());
114  }
115  }
116  }
117 
118  void GpsPlugin::GPSFixCallback(const gps_common::GPSFixConstPtr& gps)
119  {
121  {
122  return;
123  }
124  if (!has_message_)
125  {
126  initialized_ = true;
127  has_message_ = true;
128  }
129 
130  StampedPoint stamped_point;
131  stamped_point.stamp = gps->header.stamp;
132  stamped_point.source_frame = local_xy_util_.Frame();
133  double x;
134  double y;
135  local_xy_util_.ToLocalXy(gps->latitude, gps->longitude, x, y);
136 
137  stamped_point.point = tf::Point(x, y, gps->altitude);
138  lap_checked_ = ui_.show_laps->isChecked();
139  // The GPS "track" is in degrees, but createQuaternionFromYaw expects
140  // radians.
141  // Furthermore, the track rotates in the opposite direction and is also
142  // offset by 90 degrees, so all of that has to be compensated for.
143  stamped_point.orientation =
144  tf::createQuaternionFromYaw((-gps->track * (M_PI / 180.0)) + M_PI_2);
145 
146  if (points_.empty() ||
147  (stamped_point.point.distance(points_.back().point)) >=
149  {
150  points_.push_back(stamped_point);
151  }
152 
153  if (buffer_size_ > 0)
154  {
155  while (static_cast<int>(points_.size()) > buffer_size_)
156  {
157  points_.pop_front();
158  }
159  }
160 
161  cur_point_ = stamped_point;
162  }
163 
164  void GpsPlugin::PrintError(const std::string& message)
165  {
166  PrintErrorHelper(ui_.status, message);
167  }
168 
169  void GpsPlugin::PrintInfo(const std::string& message)
170  {
171  PrintInfoHelper(ui_.status, message);
172  }
173 
174  void GpsPlugin::PrintWarning(const std::string& message)
175  {
176  PrintWarningHelper(ui_.status, message);
177  }
178 
179  QWidget* GpsPlugin::GetConfigWidget(QWidget* parent)
180  {
181  config_widget_->setParent(parent);
182 
183  return config_widget_;
184  }
185 
186  bool GpsPlugin::Initialize(QGLWidget* canvas)
187  {
188  canvas_ = canvas;
189  SetColor(ui_.color->color());
190 
191  return true;
192  }
193 
194  void GpsPlugin::Draw(double x, double y, double scale)
195  {
196  if (DrawPoints(scale))
197  {
198  PrintInfo("OK");
199  }
200  }
201 
202  void GpsPlugin::LoadConfig(const YAML::Node& node, const std::string& path)
203  {
204  if (node["topic"])
205  {
206  std::string topic;
207  node["topic"] >> topic;
208  ui_.topic->setText(topic.c_str());
209  }
210 
211  if (node["color"])
212  {
213  std::string color;
214  node["color"] >> color;
215  SetColor(QColor(color.c_str()));
216  ui_.color->setColor(color_);
217  }
218 
219  if (node["draw_style"])
220  {
221  std::string draw_style;
222  node["draw_style"] >> draw_style;
223 
224  if (draw_style == "lines")
225  {
226  draw_style_ = LINES;
227  ui_.drawstyle->setCurrentIndex(0);
228  }
229  else if (draw_style == "points")
230  {
232  ui_.drawstyle->setCurrentIndex(1);
233  }
234  else if (draw_style == "arrows")
235  {
237  ui_.drawstyle->setCurrentIndex(2);
238  }
239  }
240 
241  if (node["position_tolerance"])
242  {
243  node["position_tolerance"] >> position_tolerance_;
244  ui_.positiontolerance->setValue(position_tolerance_);
245  }
246 
247  if (node["buffer_size"])
248  {
249  node["buffer_size"] >> buffer_size_;
250  ui_.buffersize->setValue(buffer_size_);
251  }
252 
253  if (node["show_laps"])
254  {
255  bool show_laps = false;
256  node["show_laps"] >> show_laps;
257  ui_.show_laps->setChecked(show_laps);
258  }
259 
260  if (node["static_arrow_sizes"])
261  {
262  bool static_arrow_sizes = node["static_arrow_sizes"].as<bool>();
263  ui_.static_arrow_sizes->setChecked(static_arrow_sizes);
264  SetStaticArrowSizes(static_arrow_sizes);
265  }
266 
267  if (node["arrow_size"])
268  {
269  ui_.arrow_size->setValue(node["arrow_size"].as<int>());
270  }
271 
272  TopicEdited();
273  }
274 
275  void GpsPlugin::SaveConfig(YAML::Emitter& emitter, const std::string& path)
276  {
277  std::string topic = ui_.topic->text().toStdString();
278  emitter << YAML::Key << "topic" << YAML::Value << topic;
279 
280  emitter << YAML::Key << "color" << YAML::Value
281  << ui_.color->color().name().toStdString();
282 
283  std::string draw_style = ui_.drawstyle->currentText().toStdString();
284  emitter << YAML::Key << "draw_style" << YAML::Value << draw_style;
285 
286  emitter << YAML::Key << "position_tolerance" <<
287  YAML::Value << position_tolerance_;
288 
289  if (!lap_checked_)
290  {
291  emitter << YAML::Key << "buffer_size" << YAML::Value << buffer_size_;
292  }
293  else
294  {
295  emitter << YAML::Key << "buffer_size" << YAML::Value << buffer_holder_;
296  }
297 
298  bool show_laps = ui_.show_laps->isChecked();
299  emitter << YAML::Key << "show_laps" << YAML::Value << show_laps;
300 
301  emitter << YAML::Key << "static_arrow_sizes" << YAML::Value << ui_.static_arrow_sizes->isChecked();
302 
303  emitter << YAML::Key << "arrow_size" << YAML::Value << ui_.arrow_size->value();
304  }
305 }
static ros::master::TopicInfo selectTopic(const std::string &datatype, QWidget *parent=0)
ros::NodeHandle node_
virtual void PositionToleranceChanged(double value)
virtual void SetArrowSize(int arrowSize)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
QWidget * GetConfigWidget(QWidget *parent)
Definition: gps_plugin.cpp:179
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)
void LoadConfig(const YAML::Node &node, const std::string &path)
Definition: gps_plugin.cpp:202
tf::Vector3 Point
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
TFSIMD_FORCE_INLINE const tfScalar & y() const
static void PrintInfoHelper(QLabel *status_label, const std::string &message, double throttle=0.0)
bool Initialize(QGLWidget *canvas)
Definition: gps_plugin.cpp:186
bool ToLocalXy(double latitude, double longitude, double &x, double &y) const
void GPSFixCallback(const gps_common::GPSFixConstPtr &gps)
Definition: gps_plugin.cpp:118
static Quaternion createQuaternionFromYaw(double yaw)
Ui::gps_config ui_
Definition: gps_plugin.h:80
#define ROS_INFO(...)
void Draw(double x, double y, double scale)
Definition: gps_plugin.cpp:194
void SaveConfig(YAML::Emitter &emitter, const std::string &path)
Definition: gps_plugin.cpp:275
TFSIMD_FORCE_INLINE const tfScalar & x() const
virtual void SetColor(const QColor &color)
virtual void SetStaticArrowSizes(bool isChecked)
void PrintWarning(const std::string &message)
Definition: gps_plugin.cpp:174
ros::Subscriber gps_sub_
Definition: gps_plugin.h:85
void PrintInfo(const std::string &message)
Definition: gps_plugin.cpp:169
void PrintError(const std::string &message)
Definition: gps_plugin.cpp:164
virtual void SetDrawStyle(QString style)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
swri_transform_util::LocalXyWgs84Util local_xy_util_
Definition: gps_plugin.h:88


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