script_viewer.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 
00018 import gtk
00019 import gtk.gdk
00020 import xdot
00021 
00022 import pygraphviz as pgv
00023 
00024 import rospy
00025 from std_msgs.msg import String
00026 from cob_script_server.msg import *
00027 
00028 gtk.gdk.threads_init()
00029 
00030 
00031 # check, if graph is available on parameter server
00032 if rospy.has_param('script_server/graph'):
00033         dotcode = rospy.get_param("script_server/graph")
00034         G=pgv.AGraph(dotcode)
00035 else:
00036         G=pgv.AGraph()
00037         G.add_node('no graph available')
00038         dotcode = G.string()
00039 
00040 ## Graph callback.
00041 def graph_cb(msg):
00042         print "new graph received"
00043         global dotcode
00044         global G
00045         dotcode = msg.data
00046         print dotcode
00047         G=pgv.AGraph(dotcode)
00048 
00049         # update vizualisation
00050         gtk.gdk.threads_enter()
00051         widget.set_dotcode(dotcode)
00052         widget.zoom_to_fit()
00053         gtk.gdk.threads_leave()
00054 
00055 
00056 ## State callback.
00057 def state_cb(msg):
00058         global widget
00059 
00060         # modify active node
00061         active_node = msg.full_graph_name
00062         rospy.loginfo("Received state <<%s>> from node <<%s>>",str(msg.state),active_node)
00063         try:
00064                 n=G.get_node(active_node)
00065         except:
00066                 rospy.logwarn("Node <<%s>> not found in graph",active_node)
00067                 return
00068         n.attr['style']='filled'
00069         if msg.state == ScriptState.UNKNOWN:
00070                 n.attr['fillcolor']='white'
00071         elif msg.state == ScriptState.ACTIVE:
00072                 n.attr['fillcolor']='yellow'
00073         elif msg.state == ScriptState.SUCCEEDED:
00074                 n.attr['fillcolor']='green'
00075         elif msg.state == ScriptState.FAILED:
00076                 n.attr['fillcolor']='red'
00077         elif msg.state == ScriptState.PAUSED:
00078                 n.attr['fillcolor']='orange'
00079         else:
00080                 n.attr['fillcolor']='blue'
00081         dotcode = G.string()
00082 
00083         # update vizualisation
00084         gtk.gdk.threads_enter()
00085         widget.set_dotcode(dotcode)
00086         widget.zoom_to_fit()
00087         gtk.gdk.threads_leave()
00088 
00089 
00090 # create gtk window
00091 window = gtk.Window()
00092 window.set_title('script viewer')
00093 window.set_default_size(600, 800)
00094 vbox = gtk.VBox()
00095 window.add(vbox)
00096 
00097 widget = xdot.DotWidget()
00098 widget.set_dotcode(dotcode)
00099 widget.zoom_to_fit()
00100 
00101 
00102 vbox.pack_start(widget)
00103 
00104 window.show_all()
00105 
00106 window.connect('destroy', gtk.main_quit)
00107 
00108 rospy.init_node('script_viewer', anonymous=True)
00109 rospy.Subscriber("/script_server/graph", String, graph_cb)
00110 rospy.Subscriber("/script_server/state", ScriptState, state_cb)
00111 gtk.main()


cob_script_server
Author(s): Florian Weisshardt
autogenerated on Sun Jun 9 2019 20:20:12