Ask Your Question
1

while(ros :: ok) Dynamic publish

asked 2018-06-22 00:52:57 -0600

kk2105 gravatar image

Hi All,

I am beginner in ROS, please excuse me if this question is silly.

I have the below snippet.

int main()
{
     //Initialize and start the node
     ros::Publisher pub;
     //Define and create some messages
     abc;
     while(ros : ok)
     {
           ros_time = ros::time::now();
           pub.publish(abc);
           ros::spinOnce();               
     }

 }

Now I need to update the abc every 5 ms and publish the updated one. Can anybody please tell me how this could be implemented.

Thank you. KK

edit retag flag offensive close merge delete

Comments

1

Hey! Could you maybe first do the tutorials on creating C++ Publishers? It quite exactly shows you what you need to do.

NEngelhard gravatar imageNEngelhard ( 2018-06-22 01:13:15 -0600 )edit

@NEngelhard thank you.. Going through the tutorials..

kk2105 gravatar imagekk2105 ( 2018-06-22 05:25:15 -0600 )edit

1 Answer

Sort by » oldest newest most voted
4

answered 2018-06-22 02:22:49 -0600

destogl gravatar image

updated 2018-06-26 06:39:49 -0600

You should use ros::Rate. See this Tutorials.

EDIT:

Exact code is:

 ros::Rate rate(200);
 while(ros::ok())
 {
       //calculate somehow new abc
       abc = new_abc;
       pub.publish(abc);
       loop_rate.sleep();               
 }

Thanks @NEngelhard!

edit flag offensive delete link more

Comments

Thank you @destogl ... Can we update that message every X ms and re-publish to the node ..?

kk2105 gravatar imagekk2105 ( 2018-06-22 05:28:00 -0600 )edit

Yes, you should just add new value into abc variable...

destogl gravatar imagedestogl ( 2018-06-22 05:38:49 -0600 )edit

while(ros :: ok) { ros_time = ros::time::now(); abc = new_abc; pub.publish(abc); ros::spinOnce;

}

@destol Is this the right way of doing it ?

kk2105 gravatar imagekk2105 ( 2018-06-22 05:50:36 -0600 )edit

Exactly. But if you want to do this every 5ms I would use exact code from my answer.

destogl gravatar imagedestogl ( 2018-06-22 06:03:32 -0600 )edit

@destogl Thank you, I just implemented that .. But one doubt, you missed sponOnce ?

kk2105 gravatar imagekk2105 ( 2018-06-23 08:39:49 -0600 )edit

@kishorkumar No I didn‘t missed it. If you read carefully tutorial explanation, in this case one does not need it.

destogl gravatar imagedestogl ( 2018-06-23 11:18:43 -0600 )edit

Thank you very much.. I will try implementing the same and update you on Tuesday..

kk2105 gravatar imagekk2105 ( 2018-06-23 11:32:35 -0600 )edit

@destogl Thanks for the input, I just implemented it and it worked perfectly fine.

kk2105 gravatar imagekk2105 ( 2018-06-26 06:35:06 -0600 )edit

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-06-22 00:52:57 -0600

Seen: 1,149 times

Last updated: Jun 26 '18