/cob_script_server/src/script_server.py

https://github.com/renxi-cu/cob_command_tools · Python · 133 lines · 52 code · 10 blank · 71 comment · 13 complexity · a157ae2fcfb2514e259b89849fa24d6a MD5 · raw file

  1. #!/usr/bin/python
  2. #################################################################
  3. ##\file
  4. #
  5. # \note
  6. # Copyright (c) 2010 \n
  7. # Fraunhofer Institute for Manufacturing Engineering
  8. # and Automation (IPA) \n\n
  9. #
  10. #################################################################
  11. #
  12. # \note
  13. # Project name: care-o-bot
  14. # \note
  15. # ROS stack name: cob_apps
  16. # \note
  17. # ROS package name: cob_script_server
  18. #
  19. # \author
  20. # Author: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de
  21. # \author
  22. # Supervised by: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de
  23. #
  24. # \date Date of creation: Aug 2010
  25. #
  26. # \brief
  27. # Implementation of ROS node for script_server.
  28. #
  29. #################################################################
  30. #
  31. # Redistribution and use in source and binary forms, with or without
  32. # modification, are permitted provided that the following conditions are met:
  33. #
  34. # - Redistributions of source code must retain the above copyright
  35. # notice, this list of conditions and the following disclaimer. \n
  36. # - Redistributions in binary form must reproduce the above copyright
  37. # notice, this list of conditions and the following disclaimer in the
  38. # documentation and/or other materials provided with the distribution. \n
  39. # - Neither the name of the Fraunhofer Institute for Manufacturing
  40. # Engineering and Automation (IPA) nor the names of its
  41. # contributors may be used to endorse or promote products derived from
  42. # this software without specific prior written permission. \n
  43. #
  44. # This program is free software: you can redistribute it and/or modify
  45. # it under the terms of the GNU Lesser General Public License LGPL as
  46. # published by the Free Software Foundation, either version 3 of the
  47. # License, or (at your option) any later version.
  48. #
  49. # This program is distributed in the hope that it will be useful,
  50. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. # GNU Lesser General Public License LGPL for more details.
  53. #
  54. # You should have received a copy of the GNU Lesser General Public
  55. # License LGPL along with this program.
  56. # If not, see <http://www.gnu.org/licenses/>.
  57. #
  58. #################################################################
  59. import time
  60. import inspect
  61. import roslib
  62. roslib.load_manifest('cob_script_server')
  63. import rospy
  64. import actionlib
  65. from cob_script_server.msg import *
  66. from simple_script_server import *
  67. sss = simple_script_server()
  68. ## Script server class which inherits from script class.
  69. #
  70. # Implements actionlib interface for the script server.
  71. #
  72. class script_server():
  73. ## Initializes the actionlib interface of the script server.
  74. #
  75. def __init__(self):
  76. self.ns_global_prefix = "/script_server"
  77. self.script_action_server = actionlib.SimpleActionServer(self.ns_global_prefix, ScriptAction, self.execute_cb, False)
  78. self.script_action_server.start()
  79. #------------------- Actionlib section -------------------#
  80. ## Executes actionlib callbacks.
  81. #
  82. # \param server_goal ScriptActionGoal
  83. #
  84. def execute_cb(self, server_goal):
  85. server_result = ScriptActionResult().result
  86. if server_goal.function_name == None or server_goal.function_name.strip() == "":
  87. rospy.logerr("function name cannot be blank")
  88. return
  89. if server_goal.function_name in dir(sss):
  90. func = getattr(sss, server_goal.function_name)
  91. argspec = inspect.getargspec(func)
  92. args = {}
  93. for arg in argspec.args:
  94. if arg in dir(server_goal):
  95. serverArg = getattr(server_goal, arg)
  96. if type(serverArg) == str:
  97. try:
  98. serverArg = eval(serverArg)
  99. except:
  100. pass
  101. args[arg] = serverArg
  102. handle01 = func(*(), **args)
  103. else:
  104. rospy.logerr("function <<%s>> not supported", server_goal.function_name)
  105. self.script_action_server.set_aborted(server_result)
  106. return
  107. if 'get_error_code' in dir(handle01):
  108. server_result.error_code = handle01.get_error_code()
  109. else:
  110. rospy.logwarn("unexpected action result type <<%s>> for function %s", type(handle01), server_goal.function_name)
  111. if server_result.error_code == 0:
  112. rospy.logdebug("action result success")
  113. self.script_action_server.set_succeeded(server_result)
  114. else:
  115. rospy.logerr("action result error, error_code: " + str(server_result.error_code))
  116. self.script_action_server.set_aborted(server_result)
  117. ## Main routine for running the script server
  118. #
  119. if __name__ == '__main__':
  120. rospy.init_node('script_server')
  121. script_server()
  122. rospy.loginfo("script_server is running")
  123. rospy.spin()