Ask Your Question
0

For loop contaning a subscriber does just the first iteration

asked 2017-04-26 13:11:26 -0500

rayane gravatar image

updated 2019-09-04 02:14:18 -0500

hi there, i want to do spiral trajectory using a PID controller, for that i used a for loop so that at each iteration it gives a new target. I used this code for the PID controller. Inside the for loop in my main code a callback function which is a methode in a class called when subscribing, the problem is that the code manages just the first iteration once the callback function is running it never stops. please any idea how to loop throw all the values Here you can find my code.

edit retag flag offensive close merge delete

Comments

I'm not 100% sure I understand your question. But when you says the callback never stops, do you mean it never comes back to the 'main'? If that is your concern, remember that spin() never returns, spinonce() will return.

billy gravatar image billy  ( 2017-04-26 21:13:14 -0500 )edit

Please post your complete code when asking a question. If there is a lot of code, then putting it on Github and giving us a link to the repository, or creating a Gist (but never delete it or the question will become useless!) would be easier.

Geoff gravatar image Geoff  ( 2017-04-26 21:53:42 -0500 )edit

Yeah, Actually thats what i meant it never get back to the main function and i will post the whole code

rayane gravatar image rayane  ( 2017-04-27 03:10:33 -0500 )edit

sorry dont know how to creat a Gist but i posted all the codes i used ( two classes and the main code). And for spinOnce it didn't work, it didn't even run the callback function. please any other ideas!!

rayane gravatar image rayane  ( 2017-04-27 04:20:14 -0500 )edit

You haven't posted all your code. Where are the header files?

The link that I posted above explains how to create a gist.

You should try and reduce your code down a minimal program that has the same problem, fix the problem, then add features.

Geoff gravatar image Geoff  ( 2017-04-27 20:24:57 -0500 )edit

@Geoffhere it is the link for my code,any suggestion is appreciated

rayane gravatar image rayane  ( 2017-04-28 03:11:29 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-04-27 08:52:48 -0500

angeltop gravatar image

ros::spinOnce() gets all messages(executes callbacks) and actions if there is a server once and returns. ros::spin() means do that all the time until a shutdown of the node is requested and then return. I suggest to define the publisher and subscriber outside the for loop, define a rate(ros::Rate loopRate(100) runs at 100Hz) for the execution of the thread and use ros::SpinOnce() and then loopRate.sleep() inside the for loop.

edit flag offensive delete link more

Comments

I did what you said but still i never changes( always 1) .I put the pub and sub before the for loop but it didn't work and for nbr defined in the pid_node class it is always 0 even that i is 1 it supposed to take the value of i .i did it just to see if it is there any iteration.

rayane gravatar image rayane  ( 2017-04-27 14:01:38 -0500 )edit

10 * i does not mean i * =10. And your problem was that there was only one iteration of the for loop. How many iterations does it do now? I didn't try to debug your code, I just tried to explain how spin and spinonce works.

angeltop gravatar image angeltop  ( 2017-04-27 14:31:25 -0500 )edit

i changed it i did i++ and still not working. what i m asking is that if we call a callback function that is defined as a methode in a class how to get back to the main code and change the arguments for the class

rayane gravatar image rayane  ( 2017-04-29 07:46:08 -0500 )edit

callback functions are called every time their node spins and there are new data available. If you want to use the received data you can write your algorithm in the callback function, otherwise you should asign the received value to a class member variable and use that variable in a another class ..

angeltop gravatar image angeltop  ( 2017-05-03 01:46:12 -0500 )edit

function. Make sure that you use new data on every run. This means that your function or main code should be called after a spin of the node. i.e. main(){ ...

while(ros::ok()){ do_something(); spinOnce(); ros::Duration(1.0).sleep(); } }

angeltop gravatar image angeltop  ( 2017-05-03 01:49:55 -0500 )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

1 follower

Stats

Asked: 2017-04-26 13:11:26 -0500

Seen: 887 times

Last updated: Sep 04 '19