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

Cancel rospy action with Ctrl+C

asked 2019-10-10 12:22:37 -0500

muttidrhummer gravatar image

Hi to everyone, i'm using a class in which some methods call actions as SimpleActionClient. I create a node in the same executable i create the class instance and leave if the signal handling. When i press ctrl+c , the executable stops but the action is completed anyway. The code is:

self.__move_base_action = actionlib.SimpleActionClient('/sweepee/move_base', MoveBaseAction)
.....
try:
        while True:
            self.__move_base_action.wait_for_server()
            goal = MoveBaseGoal()
            goal.target_pose.header.frame_id = "map_sweepee"
            print("moving to : ",pos_in[0], pos_in[1], pos_in[2], pos_in[3])
            goal.target_pose.header.stamp = rospy.Time.now()
            goal.target_pose.pose.position.x = pos_in[0]
            goal.target_pose.pose.position.y = pos_in[1]
            goal.target_pose.pose.orientation.z = pos_in[2]
            goal.target_pose.pose.orientation.w = pos_in[3]

            self.__move_base_action.send_goal(goal)
            wait = self.__move_base_action.wait_for_result()
            if not wait:
                self.__move_base_action.cancel_all_goals()
                rospy.logerr("Action server interrupted!")
                return False
            else:
                return self.__move_base_action.get_result()
    except KeyboardInterrupt:
        self.__move_base_action.cancel_all_goals()
        return False

The code actually executes the line: self.__move_base_action.cancel_all_goals() but the action is not stopping. Any idea on why? Thanks

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-10-10 13:39:21 -0500

Hello,

Take a look at http://wiki.ros.org/rospy/Overview/In...

The part 2.2 :

Registering shutdown hooks

rospy.on_shutdown(h)

    Register handler to be called when rospy process begins shutdown. h is a function that takes no arguments. 

def myhook():
  print "shutdown time!"

rospy.on_shutdown(myhook)

You should be able to call your function self.__move_base_action.cancel_all_goals() in this shutdown hook, that's called before the node is closed.

edit flag offensive delete link more

Comments

Thanks, i already tried this but it does't work actually. If i call class.__move_base_action.cancel_all_goals(), python says " instance has no attribute '__move_base_action' Thanks"

muttidrhummer gravatar image muttidrhummer  ( 2019-10-11 02:54:52 -0500 )edit

OK, it works. I forgot that python adds a name in front of double underscored methods. Thanks

muttidrhummer gravatar image muttidrhummer  ( 2019-10-11 02:58:41 -0500 )edit
1

answered 2019-10-10 12:39:20 -0500

gvdhoorn gravatar image

The problem is most likely that at the point where cancel_all_goals() is called, ROS has already shut down. This prevents any actionlib messages from ever reaching your server.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-10-10 12:22:37 -0500

Seen: 943 times

Last updated: Oct 10 '19