test_action_interface.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import sys
19 import unittest
20 
21 import rospy
22 import actionlib
23 from simple_script_server import *
24 from cob_script_server.msg import ScriptAction, ScriptGoal
25 from std_srvs.srv import Trigger, TriggerResponse
26 from move_base_msgs.msg import MoveBaseAction, MoveBaseResult
27 
28 ## This test checks the correct call to commands from the cob_script_server. This does not cover the execution of the commands (this shoud be checked in the package where the calls refer to).
29 class TestActionInterface(unittest.TestCase):
30  def __init__(self, *args):
31  super(TestActionInterface, self).__init__(*args)
32  rospy.init_node('test_action_interface')
33  self.cb_executed = False
34  self.cb_move_executed = False
35  self.component_name = "arm" # testing for component arm
36 
37  # test trigger commands
38  def test_init(self):
39  goal = ScriptGoal()
40  goal.function_name = "init"
41  goal.component_name = "arm"
42  self.script_action_trigger(goal)
43 
44  def test_stop(self):
45  goal = ScriptGoal()
46  goal.function_name = "stop"
47  goal.component_name = "arm"
48  self.script_action_trigger(goal)
49 
50  def test_recover(self):
51  goal = ScriptGoal()
52  goal.function_name = "recover"
53  goal.component_name = "arm"
54  self.script_action_trigger(goal)
55 
56  def script_action_trigger(self,goal):
57  rospy.Service("/" + goal.component_name + "_controller/" + goal.function_name, Trigger, self.cb)
58  self.cb_executed = False
59 
60  client = actionlib.SimpleActionClient('/script_server', ScriptAction)
61 
62  if not client.wait_for_server(rospy.Duration(10)):
63  self.fail('Action server not ready')
64  client.send_goal(goal)
65  if not client.wait_for_result(rospy.Duration(10)):
66  self.fail('Action didnt give a result before timeout')
67 
68  if not self.cb_executed:
69  self.fail('Service Server not called. script server error_code: ' + str(client.get_result().error_code))
70 
71  def cb(self,req):
72  self.cb_executed = True
73  res = TriggerResponse()
74  res.success = True
75  return res
76 
77  # test move base commands
78 # def test_move_base(self):
79 # goal = ScriptGoal()
80 # goal.function_name = "move"
81 # goal.component_name = "base"
82 # goal.parameter_name = [0,0,0]
83 # self.script_action_move_base(goal)
84 
85 # def test_move_base_omni(self): #FIXME fails because client is already in DONE state (mode="" and mode="omni" is the same)
86 # goal = ScriptGoal()
87 # goal.function_name = "move"
88 # goal.component_name = "base"
89 # goal.parameter_name = "home"
90 # goal.mode = "omni"
91 # self.script_action_move_base(goal)
92 
93 # def test_move_base_diff(self):
94 # goal = ScriptGoal()
95 # goal.function_name = "move"
96 # goal.component_name = "base"
97 # goal.parameter_name = [0,0,0]
98 # goal.mode = "diff"
99 # self.script_action_move_base(goal)
100 
101 # def test_move_base_linear(self):
102 # goal = ScriptGoal()
103 # goal.function_name = "move"
104 # goal.component_name = "base"
105 # goal.parameter_name = [0,0,0]
106 # goal.mode = "linear"
107 # self.script_action_move_base(goal)
108 
109  def script_action_move_base(self,goal):
110  if goal.mode == None or goal.mode == "" or goal.mode == "omni":
111  as_name = "/move_base"
112  else:
113  as_name = "/move_base_" + goal.mode
114  self.as_server = actionlib.SimpleActionServer(as_name, MoveBaseAction, execute_cb=self.base_cb, auto_start=False)
115  self.as_server.start()
116 
117  client = actionlib.SimpleActionClient('/script_server', ScriptAction)
118 
119  self.cb_move_executed = False
120  if not client.wait_for_server(rospy.Duration(10)):
121  self.fail('Action server not ready')
122  client.send_goal(goal)
123  client.wait_for_result(rospy.Duration(10))
124  #if not client.wait_for_result(rospy.Duration(10)):
125  # self.fail('Action didnt give a result before timeout')
126 
127  #if not self.cb_executed:
128  # self.fail('Action Server not called. script server error_code: ' + str(client.get_result().error_code))
129 
130  def base_cb(self, goal):
131  self.cb_move_executed = True
132  result = MoveBaseResult()
133  self.as_server.set_succeeded(result)
134 
135  # test move trajectory commands
136  #TODO
137 
138 if __name__ == '__main__':
139  import rostest
140  rostest.rosrun('cob_script_server', 'action_interface', TestActionInterface)
This test checks the correct call to commands from the cob_script_server.


cob_script_server
Author(s): Florian Weisshardt
autogenerated on Wed Apr 7 2021 03:03:06