script_server.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 inspect
19 
20 import rospy
21 import actionlib
22 
23 from cob_script_server.msg import ScriptAction, ScriptActionResult
24 from cob_script_server.srv import ComposeTrajectory, ComposeTrajectoryResponse
25 from simple_script_server import *
27 
28 ## Script server class which inherits from script class.
29 #
30 # Implements actionlib interface for the script server.
31 #
32 class script_server():
33  ## Initializes the actionlib interface of the script server.
34  #
35  def __init__(self):
36  self.ns_global_prefix = "/script_server"
38  self.script_action_server.start()
39  self.compose_trajectory_service = rospy.Service('~compose_trajectory', ComposeTrajectory, self.handle_compose_trajectory)
40 
41 #------------------- Service section -------------------#
42  def handle_compose_trajectory(self, req):
43  print("compose trajectory", req.component_name, req.parameter_name)
44  traj_msg, error_code = sss.compose_trajectory(req.component_name, req.parameter_name)
45  if error_code != 0:
46  return None
47  res = ComposeTrajectoryResponse()
48  res.trajectory = traj_msg
49  return res
50 
51 #------------------- Actionlib section -------------------#
52  ## Executes actionlib callbacks.
53  #
54  # \param server_goal ScriptActionGoal
55  #
56  def execute_cb(self, server_goal):
57  server_result = ScriptActionResult().result
58  if server_goal.function_name == None or server_goal.function_name.strip() == "":
59  rospy.logerr("function name cannot be blank")
60  return
61 
62  if server_goal.function_name in dir(sss):
63  func = getattr(sss, server_goal.function_name)
64  argspec = inspect.getargspec(func)
65  args = {}
66  for arg in argspec.args:
67  if arg in dir(server_goal):
68  serverArg = getattr(server_goal, arg)
69  if type(serverArg) == str:
70  try:
71  serverArg = eval(serverArg)
72  except:
73  pass
74  args[arg] = serverArg
75 
76  handle01 = func(*(), **args)
77  else:
78  rospy.logerr("function <<%s>> not supported", server_goal.function_name)
79  self.script_action_server.set_aborted(server_result)
80  return
81 
82  if 'get_error_code' in dir(handle01):
83  server_result.error_code = handle01.get_error_code()
84  else:
85  rospy.logwarn("unexpected action result type <<%s>> for function %s", type(handle01), server_goal.function_name)
86  if server_result.error_code == 0:
87  rospy.logdebug("action result success")
88  self.script_action_server.set_succeeded(server_result)
89  else:
90  rospy.logerr("action result error, error_code: " + str(server_result.error_code))
91  self.script_action_server.set_aborted(server_result)
92 
93 ## Main routine for running the script server
94 #
95 if __name__ == '__main__':
96  rospy.init_node('script_server')
98  rospy.loginfo("script_server is running")
99  rospy.spin()
def execute_cb(self, server_goal)
Executes actionlib callbacks.
def handle_compose_trajectory(self, req)
def __init__(self)
Initializes the actionlib interface of the script server.
Script server class which inherits from script class.


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