Make string plugin also support string stamped (#666)

* Make string plugin also support string stamped

Co-authored-by: Matthew Bries <matthew.bries@swri.org>
This commit is contained in:
Matthew 2020-04-03 20:16:43 +00:00 committed by GitHub
parent f4d09d8dc3
commit 2dac5a7360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -46,7 +46,9 @@
#include <ros/ros.h>
#include <topic_tools/shape_shifter.h>
#include <std_msgs/String.h>
#include <marti_common_msgs/StringStamped.h>
// QT autogenerated files
#include "ui_string_config.h"
@ -130,7 +132,7 @@ namespace mapviz_plugins
QFont font_;
QStaticText message_;
void stringCallback(const std_msgs::StringConstPtr& str);
void stringCallback(const topic_tools::ShapeShifter::ConstPtr& msg);
std::string AnchorToString(Anchor anchor);
std::string UnitsToString(Units units);

View File

@ -295,7 +295,7 @@ namespace mapviz_plugins
void StringPlugin::SelectTopic()
{
ros::master::TopicInfo topic = mapviz::SelectTopicDialog::selectTopic(
"std_msgs/String");
"std_msgs/String", "marti_common_msgs/StringStamped");
if (!topic.name.empty())
{
@ -318,7 +318,7 @@ namespace mapviz_plugins
topic_ = topic;
if (!topic.empty())
{
string_sub_ = node_.subscribe(topic_, 1, &StringPlugin::stringCallback, this);
string_sub_ = node_.subscribe<topic_tools::ShapeShifter>(topic_, 1, &StringPlugin::stringCallback, this);
ROS_INFO("Subscribing to %s", topic_.c_str());
}
@ -387,9 +387,23 @@ namespace mapviz_plugins
offset_y_ = offset;
}
void StringPlugin::stringCallback(const std_msgs::StringConstPtr& str)
template <class T, class M>
bool is_instance(const M& msg)
{
message_.setText(QString(str->data.c_str()));
return msg->getDataType() == ros::message_traits::datatype<T>();
}
void StringPlugin::stringCallback(const topic_tools::ShapeShifter::ConstPtr& msg)
{
if (is_instance<std_msgs::String>(msg))
{
message_.setText(QString(msg->instantiate<std_msgs::String>()->data.c_str()));
}
else if (is_instance<marti_common_msgs::StringStamped>(msg))
{
message_.setText(QString(msg->instantiate<marti_common_msgs::StringStamped>()->value.c_str()));
}
message_.prepare(QTransform(), font_);
has_message_ = true;