Ask Your Question
0

print the number of received messages

asked 2019-09-17 09:34:43 -0500

kaank_1993 gravatar image

Hi all!

Every time a node receives a message, i want to increment and print the total number of messages that ha been received so far, starting with 0 every time the node starts! Any tips?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-09-17 21:20:42 -0500

updated 2019-09-17 21:21:23 -0500

Something like will do the job.

void callback(msg::type & msg)
{
  static int counter = 0;
  counter++;
  std::cout << counter << std::endl;
}


int main()
{
  ros::init(argc, argv, "node_name");

  ros::Subscriber sub = n.subscribe("your_sub_topic", 1, callback);

  ros::spin();

}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-09-17 09:34:43 -0500

Seen: 59 times

Last updated: Sep 17