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

Subscriber declaration in while loop

asked 2013-06-11 23:35:30 -0500

mateo_7_7 gravatar image

updated 2014-01-28 17:16:52 -0500

ngrennan gravatar image

If I declare a subscriber in the while (ros::ok()) loop is it is allowed to change the topic which I read from at each loop? Actually it doesn't work: the topics which i read from are always the same, the initial ones: "..Data1","...Data2" What am I doing wrong?

string targetI_id="1";
string targetII_id="2";

while (ros::ok())
{

 // here i change the string at each loop

  ros::Subscriber   vrepVideoSubscriber_trgII = n.subscribe("/vrep...
       /visionSensorData"+targetII_id,1,newImageTrigger_trgII);  
  ros::Subscriber   vrepVideoSubscriber_trgI = n.subscribe("/vrep...
       /visionSensorData"+targetI_id,1,newImageTrigger_trgI);  
   .....
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-06-12 00:54:54 -0500

dornhege gravatar image

In principle this is allowed. However, you are probably changing the subscriptions very fast. To successfully get a message through after the subscribe call, the nodes must arrange for a connection to be opened and a message must be sent. This usually takes some time.

As soon as one loop cycle ends your subscribers go out of scope and will be destroyed, so depended on the code after ..... this code won't do what you want it to do.

edit flag offensive delete link more

Comments

actually i receive the name of the topic (the string), which i have to read from, from another subscriber (and for sure the updating of this string is performed correctly)....but, as I said, i cannot read from this new topic"/vrep/visionSensorData"+targetII_id because the topic remains:

mateo_7_7 gravatar image mateo_7_7  ( 2013-06-12 02:06:08 -0500 )edit

"/vrep/visionSensorData"+NULL even if the string is updated. The updating of this string (apart from the first time that is performed after 1 loop) is performed very rarely ....how can i do???

mateo_7_7 gravatar image mateo_7_7  ( 2013-06-12 02:07:55 -0500 )edit

You have to give the subscribers some time to connect and receive messages in your code design. If you really just want to get something quickly, a service might be better suited.

dornhege gravatar image dornhege  ( 2013-06-12 06:07:33 -0500 )edit

Question Tools

Stats

Asked: 2013-06-11 23:35:30 -0500

Seen: 857 times

Last updated: Jun 12 '13