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

Revision history [back]

There are a few things I can see that stand out. Firstly can you post more of your code, this snippet is incomplete and even has unbalanced parentheses.

The code you've shown appears to mostly be a callback function, inside this callback you're starting the asyncspinner as well as a normal spin at the end. Neither of these make any sense being inside a callback and could result in very strange recursive behaviours.

Your callback seems to create a motion plan and publish a plan message if it was successful. This doesn't require a spin at any point in this function.

As I've said, if you can post more of your code hopefully we can help you fix this, but as is it doesn't make much sense.

There are a few things I can see that stand out. Firstly can you post more of your code, this snippet is incomplete and even has unbalanced parentheses.

The code you've shown appears to mostly be a callback function, inside this callback you're starting the asyncspinner as well as a normal spin at the end. Neither of these make any sense being inside a callback and could result in very strange recursive behaviours.

Your callback seems to create a motion plan and publish a plan message if it was successful. This doesn't require a spin at any point in this function.

As I've said, if you can post more of your code hopefully we can help you fix this, but as is it doesn't make much sense.

sense.

Edit. You should only need a spin in your main function, if it's not working with than then there is a problem with your code you should not try and solve it by adding more spin calls. To be clear ros::spin() will not return until the node has been shutdown, either through a call to ros::shutdown() or a Ctrl-C. So calling it inside a callback will do strange recurrsive things!

There is a serious problem with the code snippet below:

  if(command == "automatic")
  {
    ros::NodeHandle n;

    ros::Subscriber sub2 = n.subscribe("point",1000,automaticCallback);

    //ros::spin();

    ROS_INFO("automatic control");
  }

The sub2 subscriber object will go out of scope and be destroyed when the end parenthesis of the if statement is reached. If you want it to remain subscribed you should make sub2 a global variable instead. Adding a spin here is causing the callbacks to be recursive which is why your code is trying to make multiple asyncspinner objects and crashing.

Remove all the spins and asyncspinners apart from main. You'll also need to remove the ros::waitForShutdown(); this will stop your node from doing anything.

I hope this helps, but I'd recommend learning some more about c++ and the thread of execution.