Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import time
00019 import inspect
00020
00021 import rospy
00022 import actionlib
00023
00024 from cob_script_server.msg import *
00025 from cob_script_server.srv import *
00026 from simple_script_server import *
00027
00028 sss = simple_script_server()
00029
00030
00031
00032
00033
00034 class script_server():
00035
00036
00037 def __init__(self):
00038 self.ns_global_prefix = "/script_server"
00039 self.script_action_server = actionlib.SimpleActionServer(self.ns_global_prefix, ScriptAction, self.execute_cb, False)
00040 self.script_action_server.start()
00041 self.compose_trajectory_service = rospy.Service('~compose_trajectory', ComposeTrajectory, self.handle_compose_trajectory)
00042
00043
00044 def handle_compose_trajectory(self, req):
00045 print "compose trajectory", req.component_name, req.parameter_name
00046 traj_msg, error_code = sss.compose_trajectory(req.component_name, req.parameter_name)
00047 if error_code != 0:
00048 return None
00049 res = ComposeTrajectoryResponse()
00050 res.trajectory = traj_msg
00051 return res
00052
00053
00054
00055
00056
00057
00058 def execute_cb(self, server_goal):
00059 server_result = ScriptActionResult().result
00060 if server_goal.function_name == None or server_goal.function_name.strip() == "":
00061 rospy.logerr("function name cannot be blank")
00062 return
00063
00064 if server_goal.function_name in dir(sss):
00065 func = getattr(sss, server_goal.function_name)
00066 argspec = inspect.getargspec(func)
00067 args = {}
00068 for arg in argspec.args:
00069 if arg in dir(server_goal):
00070 serverArg = getattr(server_goal, arg)
00071 if type(serverArg) == str:
00072 try:
00073 serverArg = eval(serverArg)
00074 except:
00075 pass
00076 args[arg] = serverArg
00077
00078 handle01 = func(*(), **args)
00079 else:
00080 rospy.logerr("function <<%s>> not supported", server_goal.function_name)
00081 self.script_action_server.set_aborted(server_result)
00082 return
00083
00084 if 'get_error_code' in dir(handle01):
00085 server_result.error_code = handle01.get_error_code()
00086 else:
00087 rospy.logwarn("unexpected action result type <<%s>> for function %s", type(handle01), server_goal.function_name)
00088 if server_result.error_code == 0:
00089 rospy.logdebug("action result success")
00090 self.script_action_server.set_succeeded(server_result)
00091 else:
00092 rospy.logerr("action result error, error_code: " + str(server_result.error_code))
00093 self.script_action_server.set_aborted(server_result)
00094
00095
00096
00097 if __name__ == '__main__':
00098 rospy.init_node('script_server')
00099 script_server()
00100 rospy.loginfo("script_server is running")
00101 rospy.spin()