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
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 import time
00061 import inspect
00062
00063 import roslib
00064 roslib.load_manifest('cob_script_server')
00065 import rospy
00066 import actionlib
00067
00068 from cob_script_server.msg import *
00069 from simple_script_server import *
00070
00071 sss = simple_script_server()
00072
00073
00074
00075
00076
00077 class script_server():
00078
00079
00080 def __init__(self):
00081 self.ns_global_prefix = "/script_server"
00082 self.script_action_server = actionlib.SimpleActionServer(self.ns_global_prefix, ScriptAction, self.execute_cb, False)
00083 self.script_action_server.start()
00084
00085
00086
00087
00088
00089
00090 def execute_cb(self, server_goal):
00091 server_result = ScriptActionResult().result
00092 if server_goal.function_name == None or server_goal.function_name.strip() == "":
00093 rospy.logerr("function name cannot be blank")
00094 return
00095
00096 if server_goal.function_name in dir(sss):
00097 func = getattr(sss, server_goal.function_name)
00098 argspec = inspect.getargspec(func)
00099 args = {}
00100 for arg in argspec.args:
00101 if arg in dir(server_goal):
00102 serverArg = getattr(server_goal, arg)
00103 if type(serverArg) == str:
00104 try:
00105 serverArg = eval(serverArg)
00106 except:
00107 pass
00108 args[arg] = serverArg
00109
00110 handle01 = func(*(), **args)
00111 else:
00112 rospy.logerr("function <<%s>> not supported", server_goal.function_name)
00113 self.script_action_server.set_aborted(server_result)
00114 return
00115
00116 if 'get_error_code' in dir(handle01):
00117 server_result.error_code = handle01.get_error_code()
00118 else:
00119 rospy.logwarn("unexpected action result type <<%s>> for function %s", type(handle01), server_goal.function_name)
00120 if server_result.error_code == 0:
00121 rospy.logdebug("action result success")
00122 self.script_action_server.set_succeeded(server_result)
00123 else:
00124 rospy.logerr("action result error, error_code: " + str(server_result.error_code))
00125 self.script_action_server.set_aborted(server_result)
00126
00127
00128
00129 if __name__ == '__main__':
00130 rospy.init_node('script_server')
00131 script_server()
00132 rospy.loginfo("script_server is running")
00133 rospy.spin()