0
$\begingroup$

Rosanswers logo

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.


Originally posted by rayane on ROS Answers with karma: 27 on 2017-04-26

Post score: 0


Original comments

Comment by billy on 2017-04-26:
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.

Comment by Geoff on 2017-04-26:
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.

Comment by rayane on 2017-04-27:
Yeah, Actually thats what i meant it never get back to the main function and i will post the whole code

Comment by rayane on 2017-04-27:
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!!

Comment by Geoff on 2017-04-27:
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.

Comment by rayane on 2017-04-28:
@Geoff here it is the link for my code,any suggestion is appreciated

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by angeltop with karma: 351 on 2017-04-27

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by rayane on 2017-04-27:
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.

Comment by angeltop on 2017-04-27:
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.

Comment by rayane on 2017-04-29:
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

Comment by angeltop on 2017-05-03:
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 ..

Comment by angeltop on 2017-05-03:
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(); } }

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.