manage_actionlib.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys
3 
4 import actionlib
5 import rospy
6 import time
7 """
8  @package manage_actionlib
9  This module provides utility methods for setting up actionlib calls through an overarching actionlib server.
10 """
11 
12 
13 def monitor_action_goal(action_server,
14  action_client,
15  action_goal,
16  action_name="current action",
17  time_limit=float("inf"),
18  abort_on_fail=False):
19  """Send and monitor an action goal to a given action client.
20 
21  The monitor will return in case of the client reporting success, preemption or
22  abortion, and will also pass through any incoming preemptions to the action server.
23 
24  @param action_server Action server that is calling the action client. If the action server is preempted, this is passed to the action client.
25  @param action_client The action client that is called by the action server. If the action server is preempted, the goal sent to this client is preempted as well.
26  @param action_goal The action goal to be sent to the action client.
27  @param action_name Optional name for the action. Will be displayed in the logs.
28  @param time_limit An optional time limit for the action. If exceeded, the action client is preempted
29  """
30 
31  success = False
32  rospy.loginfo("Sending goal to " + action_name)
33  action_client.send_goal(action_goal)
34  init_time = rospy.Time(0)
35 
36  while init_time == rospy.Time(0):
37  init_time = rospy.Time.now()
38  time.sleep(0.01)
39 
40  while action_server.is_active():
41  if (rospy.Time.now() - init_time).to_sec() > time_limit:
42  rospy.logwarn("Timeout of request")
43  action_client.cancel_goal()
44  success = False
45  break
46 
47  if action_server.is_preempt_requested():
48  rospy.logwarn("Preempting " + action_name)
49  action_client.cancel_goal()
50  finished = action_client.wait_for_result(
51  timeout=rospy.Duration(1.0))
52 
53  if not finished:
54  rospy.logerr(action_name +
55  " failed to preempt! This should never happen")
56  action_server.set_aborted(text="Aborted due to action " +
57  action_name + " failing to preempt")
58  else:
59  action_server.set_preempted(
60  text="Preempted while running " + action_name)
61  rospy.loginfo("Preempted while running")
62  break
63 
64  if action_client.get_state() == actionlib.GoalStatus.ABORTED:
65  rospy.logerr(action_name + " aborted!")
66  success = False
67  if abort_on_fail:
68  action_server.set_aborted(text=action_name + " aborted")
69  break
70 
71  if action_client.get_state() == actionlib.GoalStatus.PREEMPTED:
72  rospy.logerr(action_name + " preempted!")
73  success = False
74  if abort_on_fail:
75  action_server.set_aborted(text=action_name + " was preempted")
76  break
77 
78  if action_client.get_state() == actionlib.GoalStatus.SUCCEEDED:
79  rospy.loginfo(action_name + " succeeded!")
80  success = True
81  break
82 
83  time.sleep(0.1)
84 
85  return success
def monitor_action_goal(action_server, action_client, action_goal, action_name="current action", time_limit=float("inf"), abort_on_fail=False)


generic_control_toolbox
Author(s): diogo
autogenerated on Mon Feb 28 2022 22:24:37