00001 #!/usr/bin/python 00002 ################################################################# 00003 ##\file 00004 # 00005 # \note 00006 # Copyright (c) 2010 \n 00007 # Fraunhofer Institute for Manufacturing Engineering 00008 # and Automation (IPA) \n\n 00009 # 00010 ################################################################# 00011 # 00012 # \note 00013 # Project name: care-o-bot 00014 # \note 00015 # ROS stack name: cob_apps 00016 # \note 00017 # ROS package name: cob_script_server 00018 # 00019 # \author 00020 # Author: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de 00021 # \author 00022 # Supervised by: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de 00023 # 00024 # \date Date of creation: Aug 2010 00025 # 00026 # \brief 00027 # Implements general script functionalities. 00028 # 00029 ################################################################# 00030 # 00031 # Redistribution and use in source and binary forms, with or without 00032 # modification, are permitted provided that the following conditions are met: 00033 # 00034 # - Redistributions of source code must retain the above copyright 00035 # notice, this list of conditions and the following disclaimer. \n 00036 # - Redistributions in binary form must reproduce the above copyright 00037 # notice, this list of conditions and the following disclaimer in the 00038 # documentation and/or other materials provided with the distribution. \n 00039 # - Neither the name of the Fraunhofer Institute for Manufacturing 00040 # Engineering and Automation (IPA) nor the names of its 00041 # contributors may be used to endorse or promote products derived from 00042 # this software without specific prior written permission. \n 00043 # 00044 # This program is free software: you can redistribute it and/or modify 00045 # it under the terms of the GNU Lesser General Public License LGPL as 00046 # published by the Free Software Foundation, either version 3 of the 00047 # License, or (at your option) any later version. 00048 # 00049 # This program is distributed in the hope that it will be useful, 00050 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00051 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00052 # GNU Lesser General Public License LGPL for more details. 00053 # 00054 # You should have received a copy of the GNU Lesser General Public 00055 # License LGPL along with this program. 00056 # If not, see <http://www.gnu.org/licenses/>. 00057 # 00058 ################################################################# 00059 00060 # script server imports 00061 import simple_script_server 00062 00063 ## Script class from which all script inherit. 00064 # 00065 # Implements basic functionalities for all scripts. 00066 class script(): 00067 def __init__(self): 00068 self.graph="" 00069 self.graph_wait_list=[] 00070 self.function_counter = 0 00071 self.ah_counter = 0 00072 self.graph = pgv.AGraph() 00073 self.graph.node_attr['shape']='box' 00074 self.last_node = "Start" 00075 00076 # use filename as nodename 00077 filename = os.path.basename(sys.argv[0]) 00078 self.basename, extension = os.path.splitext(filename) 00079 rospy.init_node(self.basename) 00080 self.graph_pub = rospy.Publisher("/script_server/graph", String) 00081 00082 # Sets the graph. 00083 def set_graph(self,graph): 00084 self.graph = graph 00085 00086 # Gets the graph. 00087 def get_graph(self): 00088 return self.graph 00089 00090 ## Dummy function for initialization 00091 def Initialize(self): 00092 pass 00093 00094 ## Dummy function for main run function 00095 def Run(self): 00096 pass 00097 00098 ## Function to start the script 00099 # 00100 # First does a simulated turn and then calls Initialize() and Run(). 00101 def Start(self): 00102 self.Parse() 00103 global ah_counter 00104 ah_counter = 0 00105 self.sss = simple_script_server.simple_script_server() 00106 self.graph = self.sss.action_handle.get_graph() 00107 rospy.loginfo("Starting <<%s>> script...",self.basename) 00108 self.Initialize() 00109 self.Run() 00110 # wait until last threaded action finishes 00111 rospy.loginfo("Wait for script to finish...") 00112 while ah_counter != 0: 00113 rospy.sleep(1) 00114 rospy.loginfo("...script finished.") 00115 00116 ## Function to generate graph view of script. 00117 # 00118 # Starts the script in simulation mode and calls Initialize() and Run(). 00119 def Parse(self): 00120 rospy.loginfo("Start parsing...") 00121 global graph 00122 global function_counter 00123 function_counter = 0 00124 # run script in simulation mode 00125 self.sss = simple_script_server.simple_script_server(parse=True) 00126 self.Initialize() 00127 self.Run() 00128 00129 # save graph on parameter server for further processing 00130 # self.graph = graph 00131 rospy.set_param("/script_server/graph", self.graph.string()) 00132 self.graph_pub.publish(graph.string()) 00133 rospy.loginfo("...parsing finished") 00134 function_counter = 0 00135 return graph.string()