Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Looks like you need to implement a service.

But if you really need to use publisher, you need to make publisher accessable from function by making global pointer.

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <nav_msgs/OccupancyGrid.h>

ros::Publisher * pub;

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);
  pub = new ros::Publisher(n.advertise<int>("mapconverted", 1000));
  ros::Rate loop_rate(10);

 int count = 0;
  while (ros::ok())
  {
     ros::spinOnce();
     loop_rate.sleep();
  }
  ros::spin();
  delete pub;
  return 0;
}

Looks like you need to implement a service.

But if you really need to use publisher, you need to make publisher accessable from function by making global pointer.

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <nav_msgs/OccupancyGrid.h>

ros::Publisher * pub;

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);
  pub = new ros::Publisher(n.advertise<int>("mapconverted", 1000));
  ros::Rate loop_rate(10);

 int count = 0;
  while (ros::ok())
  {
     ros::spinOnce();
     loop_rate.sleep();
  }
  ros::spin();
  delete pub;
  return 0;
}

Looks like you need to implement a service.

But if you really need to use publisher, you need to make publisher accessable from function by making global pointer.

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <nav_msgs/OccupancyGrid.h>

ros::Publisher * pub;

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);
  pub = new ros::Publisher(n.advertise<int>("mapconverted", 1000));
  ros::Rate loop_rate(10);

 int count = 0;
  while (ros::ok())
  {
     ros::spinOnce();
     loop_rate.sleep();
  }
  ros::spin();
  delete pub;
  return 0;
}

Looks like you need to implement a service.

But if you really need to use publisher, you need to make publisher accessable from function by making global pointer.

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <nav_msgs/OccupancyGrid.h>

ros::Publisher * pub;

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);
  pub = new ros::Publisher(n.advertise<int>("mapconverted", 1000));
  ros::Rate loop_rate(10);

 int count = 0;
  while (ros::ok())
  {
     ros::spinOnce();
     loop_rate.sleep();
  }
  ros::spin();
  delete pub;
  return 0;
}

Looks like you need to implement a service.

But if you really need to use publisher, you need to should make publisher accessable from function by making global pointer.

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <nav_msgs/OccupancyGrid.h>

ros::Publisher * pub;

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);
  pub = new ros::Publisher(n.advertise<int>("mapconverted", 1000));
  ros::Rate loop_rate(10);

 int count = 0;
  while (ros::ok())
  {
     ros::spinOnce();
     loop_rate.sleep();
  }
  ros::spin();
  delete pub;
  return 0;
}