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

Passed array of integers are not displayed in the callback function

asked 2016-03-03 13:09:15 -0500

anonymous user

Anonymous

I have created a node which reads array of integers from a file and publishes over Topic_1 at once

...
 ros::Publisher publisher = nh.advertise<std_msgs::Int32MultiArray>("Topic_1",1000,true);


  std_msgs::Int32MultiArray vec;


  ROS_INFO("Array elements from file [array1.txt] are published to Topic_1:\n");
  ifstream infile("array1.txt");

  while(infile >> temp)
      vec.data.push_back(temp);

  publisher.publish(vec);
  ros::spin();
...

and here is the subscriber for Topic_1

void callback_1(const std_msgs::Int32MultiArray::ConstPtr& vec1)
{ 
  ROS_INFO("Vector recieved over Topic_1\n");
  vector<int>::const_iterator itr = vec1->data.begin();

  for(;itr != vec1->data.end();++itr)
    {
      ROS_INFO("%d ",*itr);
    }
}
...
ros::Subscriber sub_1 = nh.subscribe("Topic_1",1000,callback_1);
...

There are no errors when I compile these nodes. But when I launch these nodes using roslaunch, 'for' loop in callback_1 is not at all executed.

 ROS_INFO("Vector recieved over Topic_1\n"); is executed

What could probably be going wrong?

edit retag flag offensive close merge delete

Comments

Are you certain that infile contains data, and that the array is actually populated before it is published?

ahendrix gravatar image ahendrix  ( 2016-03-03 13:44:44 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-03-03 21:16:30 -0500

anonymous user

Anonymous

Yes, infile does contain data. I figured out the problem, When I start all the nodes using roslaunch, infile is not invoked but when I start the nodes using rosrun twice(for each node) everything is working perfectly fine (Even the entire vector is published in the subscriber). Why is this so?

edit flag offensive delete link more

Comments

When running nodes in roslaunch, roslaunch changes the working directory to ~/.ros before starting your node. Since your file is referenced by relative path, your node is no longer able to find it.

ahendrix gravatar image ahendrix  ( 2016-03-04 12:29:22 -0500 )edit

your node should either use a library like rospkg to locate files relative to the package that they're expected in, or you can use $(find) or similar in roslaunch to get the path to your file and pass it as a parameter or command-line argument.

ahendrix gravatar image ahendrix  ( 2016-03-04 12:30:53 -0500 )edit
0

answered 2016-03-03 15:54:15 -0500

jeremya gravatar image

Is it easier to try something like if you can use a C++11 or C++0x standard compiler:

for(auto const& dt: vec1->data)
{
  ROS_INFO("%d ",dt);
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-03 13:09:15 -0500

Seen: 612 times

Last updated: Mar 03 '16