gps_plugin.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (C) 2013 All Right Reserved, Southwest Research Institute® (SwRI®)
00004 //
00005 // Contract No.  10-58058A
00006 // Contractor    Southwest Research Institute® (SwRI®)
00007 // Address       6220 Culebra Road, San Antonio, Texas 78228-0510
00008 // Contact       Steve Dellenback <sdellenback@swri.org> (210) 522-3914
00009 //
00010 // This code was developed as part of an internal research project fully funded
00011 // by Southwest Research Institute®.
00012 //
00013 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
00014 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
00015 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
00016 // PARTICULAR PURPOSE.
00017 //
00018 // *****************************************************************************
00019 
00020 #include <mapviz_plugins/gps_plugin.h>
00021 
00022 // C++ standard libraries
00023 #include <cstdio>
00024 #include <vector>
00025 
00026 // QT libraries
00027 #include <QDialog>
00028 #include <QGLWidget>
00029 #include <QPalette>
00030 
00031 #include <opencv2/core/core.hpp>
00032 
00033 // ROS libraries
00034 #include <ros/master.h>
00035 
00036 #include <swri_image_util/geometry_util.h>
00037 #include <swri_transform_util/transform_util.h>
00038 #include <mapviz/select_topic_dialog.h>
00039 
00040 // Declare plugin
00041 #include <pluginlib/class_list_macros.h>
00042 PLUGINLIB_EXPORT_CLASS(mapviz_plugins::GpsPlugin, mapviz::MapvizPlugin)
00043 
00044 namespace mapviz_plugins
00045 {
00046   GpsPlugin::GpsPlugin() : config_widget_(new QWidget())
00047   {
00048     ui_.setupUi(config_widget_);
00049 
00050     ui_.color->setColor(Qt::green);
00051 
00052     // Set background white
00053     QPalette p(config_widget_->palette());
00054     p.setColor(QPalette::Background, Qt::white);
00055     config_widget_->setPalette(p);
00056 
00057     // Set status text red
00058     QPalette p3(ui_.status->palette());
00059     p3.setColor(QPalette::Text, Qt::red);
00060     ui_.status->setPalette(p3);
00061 
00062     QObject::connect(ui_.selecttopic, SIGNAL(clicked()), this,
00063                      SLOT(SelectTopic()));
00064     QObject::connect(ui_.topic, SIGNAL(editingFinished()), this,
00065                      SLOT(TopicEdited()));
00066     QObject::connect(ui_.positiontolerance, SIGNAL(valueChanged(double)), this,
00067                      SLOT(PositionToleranceChanged(double)));
00068     QObject::connect(ui_.buffersize, SIGNAL(valueChanged(int)), this,
00069                      SLOT(BufferSizeChanged(int)));
00070     QObject::connect(ui_.drawstyle, SIGNAL(activated(QString)), this,
00071                      SLOT(SetDrawStyle(QString)));
00072     QObject::connect(ui_.static_arrow_sizes, SIGNAL(clicked(bool)),
00073                      this, SLOT(SetStaticArrowSizes(bool)));
00074     QObject::connect(ui_.arrow_size, SIGNAL(valueChanged(int)),
00075                      this, SLOT(SetArrowSize(int)));
00076     connect(ui_.color, SIGNAL(colorEdited(const QColor&)), this,
00077             SLOT(SetColor(const QColor&)));
00078   }
00079 
00080   GpsPlugin::~GpsPlugin()
00081   {
00082   }
00083 
00084   void GpsPlugin::SelectTopic()
00085   {
00086     ros::master::TopicInfo topic =
00087         mapviz::SelectTopicDialog::selectTopic("gps_common/GPSFix");
00088 
00089     if (!topic.name.empty())
00090     {
00091       ui_.topic->setText(QString::fromStdString(topic.name));
00092       TopicEdited();
00093     }
00094   }
00095 
00096   void GpsPlugin::TopicEdited()
00097   {
00098     std::string topic = ui_.topic->text().trimmed().toStdString();
00099     if (topic != topic_)
00100     {
00101       initialized_ = false;
00102       points_.clear();
00103       has_message_ = false;
00104       PrintWarning("No messages received.");
00105 
00106       gps_sub_.shutdown();
00107 
00108       topic_ = topic;
00109       if (!topic.empty())
00110       {
00111         gps_sub_ = node_.subscribe(topic_, 1, &GpsPlugin::GPSFixCallback, this);
00112 
00113         ROS_INFO("Subscribing to %s", topic_.c_str());
00114       }
00115     }
00116   }
00117 
00118   void GpsPlugin::GPSFixCallback(const gps_common::GPSFixConstPtr& gps)
00119   {
00120     if (!local_xy_util_.Initialized())
00121     {
00122       return;
00123     }
00124     if (!has_message_)
00125     {
00126       initialized_ = true;
00127       has_message_ = true;
00128     }
00129 
00130     StampedPoint stamped_point;
00131     stamped_point.stamp = gps->header.stamp;
00132     stamped_point.source_frame = local_xy_util_.Frame();
00133     double x;
00134     double y;
00135     local_xy_util_.ToLocalXy(gps->latitude, gps->longitude, x, y);
00136 
00137     stamped_point.point = tf::Point(x, y, gps->altitude);
00138     lap_checked_ = ui_.show_laps->isChecked();
00139     // The GPS "track" is in degrees, but createQuaternionFromYaw expects
00140     // radians.
00141     // Furthermore, the track rotates in the opposite direction and is also
00142     // offset by 90 degrees, so all of that has to be compensated for.
00143     stamped_point.orientation =
00144         tf::createQuaternionFromYaw((-gps->track * (M_PI / 180.0)) + M_PI_2);
00145 
00146     if (points_.empty() ||
00147         (stamped_point.point.distance(points_.back().point)) >=
00148             (position_tolerance_))
00149     {
00150       points_.push_back(stamped_point);
00151     }
00152 
00153     if (buffer_size_ > 0)
00154     {
00155       while (static_cast<int>(points_.size()) > buffer_size_)
00156       {
00157         points_.pop_front();
00158       }
00159     }
00160 
00161     cur_point_ = stamped_point;
00162   }
00163 
00164   void GpsPlugin::PrintError(const std::string& message)
00165   {
00166     PrintErrorHelper(ui_.status, message);
00167   }
00168 
00169   void GpsPlugin::PrintInfo(const std::string& message)
00170   {
00171     PrintInfoHelper(ui_.status, message);
00172   }
00173 
00174   void GpsPlugin::PrintWarning(const std::string& message)
00175   {
00176     PrintWarningHelper(ui_.status, message);
00177   }
00178 
00179   QWidget* GpsPlugin::GetConfigWidget(QWidget* parent)
00180   {
00181     config_widget_->setParent(parent);
00182 
00183     return config_widget_;
00184   }
00185 
00186   bool GpsPlugin::Initialize(QGLWidget* canvas)
00187   {
00188     canvas_ = canvas;
00189     SetColor(ui_.color->color());
00190 
00191     return true;
00192   }
00193 
00194   void GpsPlugin::Draw(double x, double y, double scale)
00195   {
00196     if (DrawPoints(scale))
00197     {
00198       PrintInfo("OK");
00199     }
00200   }
00201 
00202   void GpsPlugin::LoadConfig(const YAML::Node& node, const std::string& path)
00203   {
00204     if (node["topic"])
00205     {
00206       std::string topic;
00207       node["topic"] >> topic;
00208       ui_.topic->setText(topic.c_str());
00209     }
00210 
00211     if (node["color"])
00212     {
00213       std::string color;
00214       node["color"] >> color;
00215       SetColor(QColor(color.c_str()));
00216       ui_.color->setColor(color_);
00217     }
00218 
00219     if (node["draw_style"])
00220     {
00221       std::string draw_style;
00222       node["draw_style"] >> draw_style;
00223 
00224       if (draw_style == "lines")
00225       {
00226         draw_style_ = LINES;
00227         ui_.drawstyle->setCurrentIndex(0);
00228       }
00229       else if (draw_style == "points")
00230       {
00231         draw_style_ = POINTS;
00232         ui_.drawstyle->setCurrentIndex(1);
00233       }
00234       else if (draw_style == "arrows")
00235       {
00236         draw_style_ = ARROWS;
00237         ui_.drawstyle->setCurrentIndex(2);
00238       }
00239     }
00240 
00241     if (node["position_tolerance"])
00242     {
00243       node["position_tolerance"] >> position_tolerance_;
00244       ui_.positiontolerance->setValue(position_tolerance_);
00245     }
00246 
00247     if (node["buffer_size"])
00248     {
00249       node["buffer_size"] >> buffer_size_;
00250       ui_.buffersize->setValue(buffer_size_);
00251     }
00252 
00253     if (node["show_laps"])
00254     {
00255       bool show_laps = false;
00256       node["show_laps"] >> show_laps;
00257       ui_.show_laps->setChecked(show_laps);
00258     }
00259 
00260     if (node["static_arrow_sizes"])
00261     {
00262       bool static_arrow_sizes = node["static_arrow_sizes"].as<bool>();
00263       ui_.static_arrow_sizes->setChecked(static_arrow_sizes);
00264       SetStaticArrowSizes(static_arrow_sizes);
00265     }
00266 
00267     if (node["arrow_size"])
00268     {
00269       ui_.arrow_size->setValue(node["arrow_size"].as<int>());
00270     }
00271 
00272     TopicEdited();
00273   }
00274 
00275   void GpsPlugin::SaveConfig(YAML::Emitter& emitter, const std::string& path)
00276   {
00277     std::string topic = ui_.topic->text().toStdString();
00278     emitter << YAML::Key << "topic" << YAML::Value << topic;
00279 
00280     emitter << YAML::Key << "color" << YAML::Value
00281             << ui_.color->color().name().toStdString();
00282 
00283     std::string draw_style = ui_.drawstyle->currentText().toStdString();
00284     emitter << YAML::Key << "draw_style" << YAML::Value << draw_style;
00285 
00286     emitter << YAML::Key << "position_tolerance" <<
00287                YAML::Value << position_tolerance_;
00288 
00289     if (!lap_checked_)
00290     {
00291       emitter << YAML::Key << "buffer_size" << YAML::Value << buffer_size_;
00292     }
00293     else
00294     {
00295       emitter << YAML::Key << "buffer_size" << YAML::Value << buffer_holder_;
00296     }
00297 
00298     bool show_laps = ui_.show_laps->isChecked();
00299     emitter << YAML::Key << "show_laps" << YAML::Value << show_laps;
00300 
00301     emitter << YAML::Key << "static_arrow_sizes" << YAML::Value << ui_.static_arrow_sizes->isChecked();
00302 
00303     emitter << YAML::Key << "arrow_size" << YAML::Value << ui_.arrow_size->value();
00304   }
00305 }


mapviz_plugins
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 18:51:07