rtmlaunch.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib
00004 roslib.load_manifest('openrtm')
00005 
00006 import sys
00007 import os
00008 import time
00009 import optparse
00010 
00011 from xml.dom.minidom import parse
00012 
00013 import rtctree
00014 from rtshell import rtcon
00015 from rtshell import path
00016 from rtshell import state_control_base
00017 from rtshell import rts_exceptions
00018 
00019 def alive_component(path):
00020     tree=rtctree.tree.RTCTree()
00021     if tree.has_path(path) and tree.is_component(path):
00022         node = tree.get_node(path)
00023         return node.plain_state_string
00024     else:
00025         return False
00026 
00027 def wait_component(cmd_path):
00028     count=0
00029     path = rtctree.path.parse_path(cmd_path)[0]
00030     node = alive_component(path)
00031     while not node and count < 30:
00032         node = alive_component(path)
00033         print >>sys.stderr, "\033[33m[rtmlaunch] Wait for ",cmd_path," ",count,"/30\033[0m"
00034         count += 1
00035         time.sleep(1)
00036     if not node:
00037         raise rts_exceptions.NoSuchObjectError(cmd_path)
00038     return node
00039 
00040 def check_connect(src_path, dest_path):
00041     tree=rtctree.tree.RTCTree()
00042     src_path, src_port = rtctree.path.parse_path(src_path)
00043     dest_path, dest_port = rtctree.path.parse_path(dest_path)
00044     src_node = tree.get_node(src_path)
00045     dest_node = tree.get_node(dest_path)
00046     port = src_node.get_port_by_name(src_port)
00047     for conn in port.connections:
00048         for name, p in conn.ports:
00049             tmp_dest_path, tmp_dest_port = rtctree.path.parse_path(name)
00050             if dest_path[-1] == tmp_dest_path[-1] and dest_port == tmp_dest_port:
00051                 return True
00052     return False
00053 
00054 def rtconnect(nameserver, tags):
00055     import re
00056     for tag in tags:
00057         source_path = nameserver+"/"+tag.attributes.get("from").value
00058         dest_path   = nameserver+"/"+tag.attributes.get("to").value
00059         source_path = re.sub("\$\(arg SIMULATOR_NAME\)",simulator,source_path);
00060         dest_path = re.sub("\$\(arg SIMULATOR_NAME\)",simulator,dest_path);
00061         # print >>sys.stderr, "[rtmlaunch] Connecting from %s to %s"%(source_path,dest_path)
00062         source_full_path = path.cmd_path_to_full_path(source_path)
00063         dest_full_path = path.cmd_path_to_full_path(dest_path)
00064         if tag.attributes.get("subscription_type") != None:
00065             sub_type = tag.attributes.get("subscription_type").value
00066             if not sub_type in ['flush','new','periodic']:
00067                 print >>sys.stderr, sub_type+' is not a subscription type'
00068                 continue
00069         else:
00070             sub_type = 'flush' # this is default value
00071         if sub_type == 'new':
00072             push_policy = 'all'
00073         # wait for proess
00074         try:
00075             wait_component(source_full_path)
00076             wait_component(dest_full_path)
00077             if check_connect(source_full_path,dest_full_path):
00078                 continue
00079         except Exception, e:
00080             print >>sys.stderr, '\033[31m[rtmlaunch] [ERROR] Could not Connect : ', e,'\033[0m'
00081             return 1
00082         #print source_path, source_full_path, dest_path, dest_full_path;
00083         try:
00084             sub_type = str(sub_type)
00085             props = {'dataport.subscription_type': sub_type}
00086             if sub_type == 'new':
00087                 props['dataport.publisher.push_policy'] = 'all'
00088             elif sub_type == 'periodic':
00089                 props['dataport.publisher.push_policy'] = 'all'
00090                 if tag.attributes.get("push_rate") != None:
00091                     props['dataport.push_rate'] = str(tag.attributes.get("push_rate").value)
00092                 else:
00093                     props['dataport.push_rate'] = str('50.0')
00094             options = optparse.Values({'verbose': False, 'id': '', 'name': None, 'properties': props})
00095             print >>sys.stderr, "[rtmlaunch] Connected from",source_path
00096             print >>sys.stderr, "[rtmlaunch]             to",dest_path
00097             print >>sys.stderr, "[rtmlaunch]           with",options
00098             try :
00099                 rtcon.connect_ports(source_path, source_full_path, dest_path, dest_full_path, options, tree=None)
00100             except Exception, e: # openrtm 1.1.0
00101                 rtcon.connect_ports([(source_path,source_full_path), (dest_path, dest_full_path)], options, tree=None)
00102         except Exception, e:
00103             print >>sys.stderr, '\033[31m[rtmlaunch] {0}: {1}'.format(os.path.basename(sys.argv[1]), e),'\033[0m'
00104     return 0
00105 
00106 def rtactivate(nameserver, tags):
00107     def activate_action(object, ec_index):
00108         object.activate_in_ec(ec_index)
00109     for tag in tags:
00110         cmd_path  = nameserver+"/"+tag.attributes.get("component").value
00111         full_path = path.cmd_path_to_full_path(cmd_path)
00112         # print >>sys.stderr, "[rtmlaunch] activate %s"%(full_path)
00113         try:
00114             state = wait_component(full_path)
00115             if state == 'Active':
00116                 continue
00117             else:
00118                 print >>sys.stderr, "[rtmlaunch] Activate <-",state,full_path
00119         except Exception, e:
00120             print >>sys.stderr, '\033[31m[rtmlaunch] Could not Activate : ', e,'\033[0m'
00121             return 1
00122         try:
00123             options = optparse.Values({"ec_index": 0, 'verbose': False})
00124             try :
00125                 state_control_base.alter_component_state(activate_action, cmd_path, full_path, options, None)
00126             except Exception, e: # openrtm 1.1.0
00127                 state_control_base.alter_component_states(activate_action, [(cmd_path, full_path)], options, None)
00128         except Exception, e:
00129             print >>sys.stderr, '[rtmlaunch] {0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00130             return 1
00131     return 0
00132 
00133 def main():
00134     global simulator
00135     usage = '''Usage: %prog [launchfile]'''
00136     if len(sys.argv) <= 1:
00137         print >>sys.stderr, usage
00138         return 1
00139     fullpathname = sys.argv[1]
00140     print >>sys.stderr, "[rtmlaunch] starting... ",fullpathname
00141     try:
00142         parser = get_true_tags(parse(fullpathname).getElementsByTagName("group"))
00143     except Exception,e:
00144         print e
00145         return 1
00146 
00147     nameserver = os.getenv("RTCTREE_NAMESERVERS","localhost")
00148     simulator = os.getenv("SIMULATOR_NAME","Simulator")
00149     print >>sys.stderr, "[rtmlaunch]", simulator
00150     while 1:
00151         print >>sys.stderr, "[rtmlaunch] check connection/activation"
00152         rtconnect(nameserver, parser.getElementsByTagName("rtconnect"))
00153         rtactivate(nameserver, parser.getElementsByTagName("rtactivate"))
00154         time.sleep(10)
00155 
00156 def get_true_tags(tags):
00157     import xml.dom.minidom
00158     parser_reduced = xml.dom.minidom.Document()
00159     for tag in tags:
00160         val = tag.attributes.get("if").value # For example, "$(arg USE_WALKING)"
00161         arg = val.split(" ")[1].strip(")") # To "USE_WALKING"
00162         if get_flag_from_argv(arg):
00163            parser_reduced.childNodes.append(tag)
00164     return parser_reduced
00165 
00166 def get_flag_from_argv(arg):
00167     for a in sys.argv:
00168         if arg in a: # If "USE_WALKING" is in argv
00169             return True if 'true' in a.split("=")[1] else False
00170 
00171 if __name__ == '__main__':
00172     main()
00173 
00174 
00175 
00176 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


openrtm
Author(s): AIST, ros package is maintained by Kei Okada
autogenerated on Tue Jul 23 2013 11:48:21