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

How to use msgs in ros

asked 2013-10-31 07:38:54 -0500

Hamid Didari gravatar image

updated 2013-11-18 18:48:17 -0500

tfoote gravatar image

Hi dear I want to write a program that is tell me how many msgs that one node has published How can I write this program I wrot this program but it's not worked

#include "ros/ros.h"
#include "std_msgs/String.h"
int i=0;
  void chatterCallback(const std_msgs::String::ConstPtr& msg)
 {
  //ROS_INFO("I heard: [%s]", msg->data.c_str());
      i++;
 }

  int main(int argc, char **argv)
  {
      ros::init(argc, argv, "lisiner");
      ros::NodeHandle n;
      ros::Subscriber sub = n.subscribe("chater", 1000, chatterCallback);
      ros::Rate timeloop(5);
      while(ros::ok())
      {
          ROS_INFO("%d",i);
          timeloop.sleep();

      }

      return 0;
  }
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-10-31 07:57:33 -0500

ZdenekM gravatar image

Try to add ros::spinOnce() into your while loop.

edit flag offensive delete link more

Comments

I test this but the node that published, publish 30 msgs but this node count 29 msgs how can I fix this?

Hamid Didari gravatar image Hamid Didari  ( 2013-10-31 08:17:22 -0500 )edit
1

Are you sure that you really send 30 messages, which one is left out? (hint: use the seq field in the header and rostopic to track what is going on, display the same data in your program and compare).

Thomas gravatar image Thomas  ( 2013-10-31 19:45:08 -0500 )edit

I have one node that published msgs and in this node in terminal count how many msgs has been published when 30 msgs published I press ctr+c to shut down the node but my Subscriber node count 29 and when after 50 msgs i press ctr+c the Subscriber node has count 47

Hamid Didari gravatar image Hamid Didari  ( 2013-11-02 04:31:54 -0500 )edit

Did you try to use suggested "seq" field? It's good idea to use it.

ZdenekM gravatar image ZdenekM  ( 2013-11-03 03:57:15 -0500 )edit

tnx man :)

Hamid Didari gravatar image Hamid Didari  ( 2013-11-05 00:20:36 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2013-10-31 07:38:54 -0500

Seen: 145 times

Last updated: Oct 31 '13