tf_frame_plugin.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #include <mapviz_plugins/tf_frame_plugin.h>
00031 
00032 // C++ standard libraries
00033 #include <cstdio>
00034 #include <algorithm>
00035 #include <vector>
00036 
00037 // QT libraries
00038 #include <QGLWidget>
00039 #include <QPalette>
00040 
00041 // ROS libraries
00042 #include <ros/master.h>
00043 
00044 #include <mapviz/select_frame_dialog.h>
00045 
00046 // Declare plugin
00047 #include <pluginlib/class_list_macros.h>
00048 PLUGINLIB_DECLARE_CLASS(mapviz_plugins,
00049                         tf_frame,
00050                         mapviz_plugins::TfFramePlugin,
00051                         mapviz::MapvizPlugin);
00052 
00053 namespace mapviz_plugins
00054 {
00055   TfFramePlugin::TfFramePlugin() : config_widget_(new QWidget())
00056   {
00057     ui_.setupUi(config_widget_);
00058 
00059     ui_.color->setColor(Qt::green);
00060 
00061     // Set background white
00062     QPalette p(config_widget_->palette());
00063     p.setColor(QPalette::Background, Qt::white);
00064     config_widget_->setPalette(p);
00065 
00066     // Set status text red
00067     QPalette p3(ui_.status->palette());
00068     p3.setColor(QPalette::Text, Qt::red);
00069     ui_.status->setPalette(p3);
00070 
00071     QObject::connect(ui_.selectframe, SIGNAL(clicked()), this,
00072                      SLOT(SelectFrame()));
00073     QObject::connect(ui_.frame, SIGNAL(editingFinished()), this,
00074                      SLOT(FrameEdited()));
00075     QObject::connect(ui_.positiontolerance, SIGNAL(valueChanged(double)), this,
00076                      SLOT(PositionToleranceChanged(double)));
00077     QObject::connect(ui_.buffersize, SIGNAL(valueChanged(int)), this,
00078                      SLOT(BufferSizeChanged(int)));
00079     QObject::connect(ui_.drawstyle, SIGNAL(activated(QString)), this,
00080                      SLOT(SetDrawStyle(QString)));
00081     QObject::connect(ui_.static_arrow_sizes, SIGNAL(clicked(bool)),
00082                      this, SLOT(SetStaticArrowSizes(bool)));
00083     QObject::connect(ui_.arrow_size, SIGNAL(valueChanged(int)),
00084                      this, SLOT(SetArrowSize(int)));
00085     connect(ui_.color, SIGNAL(colorEdited(const QColor&)), this,
00086             SLOT(SetColor(const QColor&)));
00087   }
00088 
00089   TfFramePlugin::~TfFramePlugin()
00090   {
00091   }
00092 
00093   void TfFramePlugin::SelectFrame()
00094   {
00095     std::string frame = mapviz::SelectFrameDialog::selectFrame(tf_);
00096     if (!frame.empty())
00097     {
00098       ui_.frame->setText(QString::fromStdString(frame));
00099       FrameEdited();
00100     }
00101   }
00102 
00103   void TfFramePlugin::FrameEdited()
00104   {
00105     source_frame_ = ui_.frame->text().toStdString();
00106     PrintWarning("Waiting for transform.");
00107 
00108     ROS_INFO("Setting target frame to to %s", source_frame_.c_str());
00109 
00110     initialized_ = true;
00111   }
00112 
00113   void TfFramePlugin::TimerCallback(const ros::TimerEvent& event)
00114   {
00115     swri_transform_util::Transform transform;
00116     if (GetTransform(ros::Time(), transform))
00117     {
00118       StampedPoint stamped_point;
00119       stamped_point.point = transform.GetOrigin();
00120       stamped_point.orientation = transform.GetOrientation();
00121       stamped_point.source_frame = target_frame_;
00122       stamped_point.stamp = transform.GetStamp();
00123       stamped_point.transformed = false;
00124 
00125       double distance = std::sqrt(
00126           std::pow(stamped_point.point.x() - points_.back().point.x(), 2) +
00127           std::pow(stamped_point.point.y() - points_.back().point.y(), 2));
00128 
00129       if (points_.empty() || distance >= position_tolerance_)
00130       {
00131         points_.push_back(stamped_point);
00132       }
00133 
00134       if (buffer_size_ > 0)
00135       {
00136         while (static_cast<int>(points_.size()) > buffer_size_)
00137         {
00138           points_.pop_front();
00139         }
00140       }
00141 
00142       cur_point_ = stamped_point;
00143     }
00144   }
00145 
00146   void TfFramePlugin::PrintError(const std::string& message)
00147   {
00148     if (message == ui_.status->text().toStdString())
00149     {
00150       return;
00151     }
00152 
00153     ROS_ERROR("Error: %s", message.c_str());
00154     QPalette p(ui_.status->palette());
00155     p.setColor(QPalette::Text, Qt::red);
00156     ui_.status->setPalette(p);
00157     ui_.status->setText(message.c_str());
00158   }
00159 
00160   void TfFramePlugin::PrintInfo(const std::string& message)
00161   {
00162     if (message == ui_.status->text().toStdString())
00163     {
00164       return;
00165     }
00166 
00167     ROS_INFO("%s", message.c_str());
00168     QPalette p(ui_.status->palette());
00169     p.setColor(QPalette::Text, Qt::green);
00170     ui_.status->setPalette(p);
00171     ui_.status->setText(message.c_str());
00172   }
00173 
00174   void TfFramePlugin::PrintWarning(const std::string& message)
00175   {
00176     if (message == ui_.status->text().toStdString())
00177     {
00178       return;
00179     }
00180 
00181     ROS_WARN("%s", message.c_str());
00182     QPalette p(ui_.status->palette());
00183     p.setColor(QPalette::Text, Qt::darkYellow);
00184     ui_.status->setPalette(p);
00185     ui_.status->setText(message.c_str());
00186   }
00187 
00188   QWidget* TfFramePlugin::GetConfigWidget(QWidget* parent)
00189   {
00190     config_widget_->setParent(parent);
00191 
00192     return config_widget_;
00193   }
00194 
00195   bool TfFramePlugin::Initialize(QGLWidget* canvas)
00196   {
00197     canvas_ = canvas;
00198 
00199     timer_ = node_.createTimer(ros::Duration(0.1),
00200                                &TfFramePlugin::TimerCallback, this);
00201 
00202     SetColor(ui_.color->color());
00203 
00204     return true;
00205   }
00206 
00207   void TfFramePlugin::Draw(double x, double y, double scale)
00208   {
00209     if (DrawPoints(scale))
00210     {
00211       PrintInfo("OK");
00212     }
00213   }
00214   void TfFramePlugin::LoadConfig(const YAML::Node& node,
00215                                  const std::string& path)
00216   {
00217     if (node["frame"])
00218     {
00219       node["frame"] >> source_frame_;
00220       ui_.frame->setText(source_frame_.c_str());
00221     }
00222 
00223     if (node["color"])
00224     {
00225       std::string color;
00226       node["color"] >> color;
00227       SetColor(QColor(color.c_str()));
00228       ui_.color->setColor(color_);
00229     }
00230 
00231     if (node["draw_style"])
00232     {
00233       std::string draw_style;
00234       node["draw_style"] >> draw_style;
00235 
00236       if (draw_style == "lines")
00237       {
00238         draw_style_ = LINES;
00239         ui_.drawstyle->setCurrentIndex(0);
00240       }
00241       else if (draw_style == "points")
00242       {
00243         draw_style_ = POINTS;
00244         ui_.drawstyle->setCurrentIndex(1);
00245       }
00246     }
00247 
00248     if (node["position_tolerance"])
00249     {
00250       node["position_tolerance"] >> position_tolerance_;
00251       ui_.positiontolerance->setValue(position_tolerance_);
00252     }
00253 
00254     if (node["buffer_size"])
00255     {
00256       node["buffer_size"] >> buffer_size_;
00257       ui_.buffersize->setValue(buffer_size_);
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     FrameEdited();
00273   }
00274 
00275   void TfFramePlugin::SaveConfig(YAML::Emitter& emitter,
00276                                  const std::string& path)
00277   {
00278     emitter << YAML::Key << "frame" << YAML::Value
00279             << ui_.frame->text().toStdString();
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     emitter << YAML::Key << "buffer_size" << YAML::Value << buffer_size_;
00290 
00291     emitter << YAML::Key << "static_arrow_sizes" << YAML::Value << ui_.static_arrow_sizes->isChecked();
00292 
00293     emitter << YAML::Key << "arrow_size" << YAML::Value << ui_.arrow_size->value();
00294   }
00295 }


mapviz_plugins
Author(s): Marc Alban
autogenerated on Thu Aug 24 2017 02:46:09