mock_simple_server.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # Copyright (c) 2009, Willow Garage, Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Willow Garage, Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 from __future__ import print_function
29 
30 import time
31 import rospy
32 
33 from actionlib.simple_action_server import SimpleActionServer
34 from actionlib.server_goal_handle import ServerGoalHandle
35 from actionlib.msg import TestRequestAction, TestRequestGoal, TestRequestResult
36 
37 
39  def __init__(self, name):
40  SimpleActionServer.__init__(self, name, TestRequestAction, self.execute_cb, False)
41  self.start()
42 
43  def execute_cb(self, goal):
44  rospy.logdebug("Goal:\n" + str(goal))
45  result = TestRequestResult(goal.the_result, True)
46 
47  if goal.pause_status > rospy.Duration(0.0):
48  rospy.loginfo("Locking the action server for %.3f seconds" % goal.pause_status.to_sec())
49  status_continue_time = rospy.get_rostime() + goal.pause_status
50  # Takes the action server lock to prevent status from
51  # being published (simulates a freeze).
52  with self.action_server.lock:
53  while rospy.get_rostime() < status_continue_time:
54  time.sleep(0.02)
55  rospy.loginfo("Unlocking the action server")
56 
57  terminate_time = rospy.get_rostime() + goal.delay_terminate
58  while rospy.get_rostime() < terminate_time:
59  time.sleep(0.02)
60  if not goal.ignore_cancel:
61  if self.is_preempt_requested():
62  self.set_preempted(result, goal.result_text)
63  return
64 
65  rospy.logdebug("Terminating goal as: %i" % goal.terminate_status)
66 
67  if goal.terminate_status == TestRequestGoal.TERMINATE_SUCCESS:
68  self.set_succeeded(result, goal.result_text)
69  elif goal.terminate_status == TestRequestGoal.TERMINATE_ABORTED:
70  self.set_aborted(result, goal.result_text)
71  elif goal.terminate_status == TestRequestGoal.TERMINATE_REJECTED:
72  rospy.logerr("Simple action server cannot reject goals")
73  self.set_aborted(None, "Simple action server cannot reject goals")
74  elif goal.terminate_status == TestRequestGoal.TERMINATE_DROP:
75  rospy.loginfo("About to drop the goal. This should produce an error message.")
76  return
77  elif goal.terminate_status == TestRequestGoal.TERMINATE_EXCEPTION:
78  rospy.loginfo("About to throw an exception. This should produce an error message.")
79  raise Exception("Terminating by throwing an exception")
80  elif goal.terminate_status == TestRequestGoal.TERMINATE_LOSE:
81  # Losing the goal requires messing about in the action server's innards
82  for i, s in enumerate(self.action_server.status_list):
83  if s.status.goal_id.id == self.current_goal.goal.goal_id.id:
84  del self.action_server.status_list[i]
85  break
87  else:
88  rospy.logerr("Don't know how to terminate as %d" % goal.terminate_status)
89  self.set_aborted(None, "Don't know how to terminate as %d" % goal.terminate_status)
90 
91 
92 if __name__ == '__main__':
93  rospy.init_node("ref_simple_server")
94  ref_server = RefSimpleServer("test_request_action")
95  print("Spinning")
96  rospy.spin()
def is_preempt_requested(self)
Allows polling implementations to query about preempt requests.
def set_aborted(self, result=None, text="")
Sets the status of the active goal to aborted.
def set_succeeded(self, result=None, text="")
Sets the status of the active goal to succeeded.
def set_preempted(self, result=None, text="")
Sets the status of the active goal to preempted.
def start(self)
Explicitly start the action server, used it auto_start is set to false.
The SimpleActionServer implements a singe goal policy on top of the ActionServer class.


actionlib
Author(s): Eitan Marder-Eppstein, Vijay Pradeep, Mikael Arguedas
autogenerated on Mon Feb 18 2019 03:59:59