how to create a subscriber node that also publishes message?
Hi everyone, I'm wondering if it's possible to create a node that serves both as a subscriber and a publisher at the same time? It will receive some messages and publish some messages to other topic.
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <nav_msgs/OccupancyGrid.h>
void mapConvert(const nav_msgs::OccupancyGrid::ConstPtr& msg)
{
ROS_INFO("I heard:1");
pub.publish(msg->info.width);//or just simply pub.publish(1);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "map_converter");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("map", 1000, mapConvert);
ros::Publisher pub = n.advertise<int>("mapconverted", 1000);
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
ros::spinOnce();
loop_rate.sleep();
}
ros::spin();
return 0;
}
I have tried this structure but it did not work.Please Help! Thank You in advance!