ref_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 
29 # Author: Alexander Sorokin.
30 # Based on code from ref_server.cpp by Vijay Pradeep
31 PKG = 'actionlib'
32 import rospy
33 
34 from actionlib.action_server import ActionServer
35 from actionlib.msg import TestAction, TestFeedback, TestResult
36 
37 
39 
40  def __init__(self, name):
41  action_spec = TestAction
42  ActionServer.__init__(
43  self, name, action_spec, self.goalCallback, self.cancelCallback, False)
44  self.start()
45  rospy.loginfo("Creating ActionServer [%s]\n", name)
46 
47  self.saved_goals = []
48 
49  def goalCallback(self, gh):
50  goal = gh.get_goal()
51 
52  rospy.loginfo("Got goal %d", int(goal.goal))
53  if goal.goal == 1:
54  gh.set_accepted()
55  gh.set_succeeded(None, "The ref server has succeeded")
56  elif goal.goal == 2:
57  gh.set_accepted()
58  gh.set_aborted(None, "The ref server has aborted")
59  elif goal.goal == 3:
60  gh.set_rejected(None, "The ref server has rejected")
61  elif goal.goal == 4:
62  self.saved_goals.append(gh)
63  gh.set_accepted()
64  elif goal.goal == 5:
65  gh.set_accepted()
66  for g in self.saved_goals:
67  g.set_succeeded()
68  self.saved_goals = []
69  gh.set_succeeded()
70  elif goal.goal == 6:
71  gh.set_accepted()
72  for g in self.saved_goals:
73  g.set_aborted()
74  self.saved_goals = []
75  gh.set_succeeded()
76  elif goal.goal == 7:
77  gh.set_accepted()
78  n = len(self.saved_goals)
79  for i, g in enumerate(self.saved_goals):
80  g.publish_feedback(TestFeedback(n-i))
81  gh.set_succeeded()
82  elif goal.goal == 8:
83  gh.set_accepted()
84  n = len(self.saved_goals)
85  for i, g in enumerate(self.saved_goals):
86  if i % 2 == 0:
87  g.set_succeeded(TestResult(n - i), "The ref server has succeeded")
88  else:
89  g.set_aborted(TestResult(n - i), "The ref server has aborted")
90  self.saved_goals = []
91  gh.set_succeeded()
92  else:
93  pass
94 
95  def cancelCallback(self, gh):
96  pass
97 
98 
99 if __name__ == "__main__":
100  rospy.init_node("ref_server")
101  ref_server = RefServer("reference_action")
102 
103  rospy.spin()
def start(self)
Start the action server.
def __init__(self, name)
Definition: ref_server.py:40
def goalCallback(self, gh)
Definition: ref_server.py:49
def cancelCallback(self, gh)
Definition: ref_server.py:95
The ActionServer is a helpful tool for managing goal requests to a node.


actionlib
Author(s): Eitan Marder-Eppstein, Vijay Pradeep, Mikael Arguedas
autogenerated on Mon Feb 28 2022 21:34:38