This example may be run after building mrs_lib by executing rosrun mrs_lib subscribe_handler_simple_example
.
#include <mrs_lib/subscribe_handler.h>
#include <std_msgs/String.h>
int main(int argc, char **argv)
{
const std::string node_name = "subscribe_handler_simple_example";
ros::init(argc, argv, node_name);
ros::NodeHandle nh;
mrs_lib::SubscribeHandlerOptions shopts(nh);
shopts.node_name = node_name;
shopts.no_message_timeout = ros::Duration(3.0);
const std::string topic_name = "test_topic";
mrs_lib::SubscribeHandler<std_msgs::String> handler(
shopts,
topic_name
);
ros::Duration d(0.1);
while (ros::ok())
{
ROS_INFO_STREAM_THROTTLE(1.0, "[" << node_name << "]: Spinning");
ros::spinOnce();
if (handler.newMsg())
ROS_INFO_STREAM("[" << node_name << "]: Got a new message: " << handler.getMsg()->data);
d.sleep();
}
}