0
$\begingroup$

Rosanswers logo

Hello, When I followed the tutorials of MoveIt!(C++), I found this line:

  ros::AsyncSpinner spinner(1); 
  spinner.start();

I searched for it and know its definition, but my question is why moveit needs asyncspinner to run the program? If delete it, the error is occurred.

The whole code is below:

  ros::init(argc, argv, "move_to_position");
  ros::NodeHandle nh;
  
  ros::AsyncSpinner spinner(1); 
  spinner.start();

  moveit::planning_interface::MoveGroupInterface group_r1("r1");
  
  group_r1.setGoalJointTolerance(0.001);
  group_r1.setMaxAccelerationScalingFactor(0.5);
  group_r1.setMaxVelocityScalingFactor(0.03);

  geometry_msgs::PoseStamped object_pose;

    group_r1.setStartState(*group_r1.getCurrentState());
    group_r1.setPoseTarget(pose);

    moveit::planning_interface::MoveGroupInterface::Plan group_Planer;

    bool success = (group_r1.plan(group_Planer) == moveit::planning_interface::MoveItErrorCode::SUCCESS);

    if (success)
    {
      ros::Duration(1).sleep();
      group_r1.execute(group_Planer);
    }

Thanks for your answer!


Originally posted by A_YIng on ROS Answers with karma: 37 on 2019-10-02

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

While ros::spin() blocks the current thread, AsyncSpinner creates a thread with a spinner, so you can continue using the current thread. On the example code provided above, it creates one spinner thread and starts it in parallel with your main thread. If you want to know more about ros::spin and AsyncSpinner, I recommend reading this blog post: https://yuzhangbit.github.io/tools/several-ways-of-writing-a-ros-node/


Originally posted by lucascoelho with karma: 497 on 2019-10-02

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by A_YIng on 2019-10-02:
I still confuse about it. Why can't it just use the current thread(main thread) to run the moveit program?

Comment by lucascoelho on 2019-10-02:
I think it's just a design choice, there are other ways to make the program above to work with only the current thread. I'm not very sure, but I think if you add a ros::spinOnce() inside the if (success) bracket, it would work as well. But you definitely need some kind of spinner either ros::spin(), ros::spinOnce, AsyncSpinner or MultiThreadedSpinner to process the ROS messages and callbacks

Comment by A_YIng on 2019-10-02:
Thanks, I will give a try. Now, it's more clearer to me.

$\endgroup$

Your Answer

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