ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

The question about ros::publisher and ros::subscriber

asked 2017-06-27 06:20:10 -0600

hudaming gravatar image

updated 2017-06-27 11:59:30 -0600

allenh1 gravatar image

I want to achieve receiving and transform data using rostopic at the same time or in a low time. I know that ros::subscriber can subscribe rostopic and receive the data included in topic. So I hope to process the data in one topic and send the processing-data to another topic, and others can subscribe the new topic. Such as the code:

void receiveGPS(const sensor_msgs::NavSatFix::ConstPtr &msg)
{

  gps_common::GPSFix gps;

  gps.header.stamp=msg->header.stamp;

  gps.status.status=msg->status.status;

  gps.latitude=msg->latitude;

  gps.longitude=msg->longitude;

  gps.altitude=msg->altitude;

  gps.position_covariance[0]=msg->position_covariance[0];
  gps.position_covariance[4]=msg->position_covariance[4];
  gps.position_covariance[8]=msg->position_covariance[8];
}

int main(int argc, char **argv)
{

// Set up ROS.

  ros::init(argc, argv, "receiveGPS");

  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("gps", 2,&receiveGPS);

  ros::spin();

}

I can get gps.latitude from the topic "gps",and I hope to send gps.latitude to another topic and publish it. Could you tell me what should I do next? Thanks!

edit retag flag offensive close merge delete

Comments

Are you talking about a node that's simultaneously a subscriber and a publisher?

allenh1 gravatar image allenh1  ( 2017-06-27 12:00:44 -0600 )edit

I hope to subscribe the topic and deal with the data in it firstly, and the send the processed data to another topic that can be subscribed.

hudaming gravatar image hudaming  ( 2017-06-28 05:09:24 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-06-28 04:32:43 -0600

Haibo gravatar image

updated 2017-06-28 04:53:25 -0600

If my understanding is correct, what you want to do is subscribe to a message, process that message in the callback, then publish the processed message as another topic. You can just publish the message inside your callback. An example can be find from here.

A class is defined in the example I mentioned. You can also use global function as that in your sample code.

edit flag offensive delete link more

Comments

OK! You are right, I will try it soon. This is my first time using Q&A, this is a good start for me! Thank you!

hudaming gravatar image hudaming  ( 2017-06-28 05:12:27 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-06-27 06:20:10 -0600

Seen: 444 times

Last updated: Jun 28 '17