00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <mapviz_plugins/tf_frame_plugin.h>
00031
00032
00033 #include <cstdio>
00034 #include <algorithm>
00035 #include <vector>
00036
00037
00038 #include <QGLWidget>
00039 #include <QPalette>
00040
00041
00042 #include <ros/master.h>
00043
00044 #include <mapviz/select_frame_dialog.h>
00045
00046
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
00062 QPalette p(config_widget_->palette());
00063 p.setColor(QPalette::Background, Qt::white);
00064 config_widget_->setPalette(p);
00065
00066
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 }