Package rosh :: Package impl :: Module ros_graph
[frames] | no frames]

Source Code for Module rosh.impl.ros_graph

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2010, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id: ros_graph.py 13210 2011-02-14 01:05:08Z kwc $ 
 34   
 35  import rosgraph.masterapi 
 36   
 37  import rosh.plugin 
 38  from rosh.impl.launch import launch_symbols 
 39  from rosh.impl.namespace import Namespace 
 40  from rosh.impl.node import Nodes 
 41  from rosh.impl.param import Parameters 
 42  from rosh.impl.service import Services 
 43  from rosh.impl.topic import Topics, LATEST, subscribe 
 44  from rosh.impl.top_tools import topic_tools_symbols 
 45   
 46  from rosh.impl.msg import Msgs 
 47  from rosh.impl.msg import Srvs 
 48   
 49  import rospy 
 50   
 51  # initialize globals 
52 -def init_master(plugin_context):
53 master = rosgraph.masterapi.Master('rosh') 54 plugin_context.ctx.master = master 55 return master.is_online()
56
57 -def serve_forever():
58 rospy.spin()
59
60 -def ok():
61 return not rospy.is_shutdown()
62
63 -def wait_for_service(service, timeout=None):
64 """ 65 Equivalent to rospy.wait_for_service(), but also handles unboxing of ROSH objects.. 66 67 @param service: Service 68 @type service: Namespace or str 69 @param timeout: (optional) wait_for_service timeout 70 @type timeout: float 71 """ 72 if isinstance(service, Namespace): 73 rospy.wait_for_service(service._name, timeout=timeout) 74 else: 75 rospy.wait_for_service(service, timeout=timeout)
76 77 # we use the general plugin loading framework to initialize to keep things tidy 78 _loaded_symbols = None
79 -def load_rosh_plugin(name, plugin_context, globals_=None):
80 global _loaded_symbols 81 if rosh.plugin.reentrant_load(_loaded_symbols, globals_): 82 return 83 84 ctx = plugin_context.ctx 85 rosh_lock = plugin_context.rosh_lock 86 87 if init_master(plugin_context): 88 rospy.init_node('rosh', anonymous=True, disable_signals=True) 89 90 _loaded_symbols = { 91 # useful aliases 92 'now': rospy.get_rostime, 93 'Header': rospy.Header, 94 'Time': rospy.Time, 95 'Duration': rospy.Duration, 96 'Rate': rospy.Rate, 97 'sleep': rospy.sleep, 98 'logfatal': rospy.logfatal, 99 'logerr': rospy.logerr, 100 'loginfo': rospy.loginfo, 101 'logdebug': rospy.logdebug, 102 103 # rospy by default (per API review) 104 'rospy': rospy, 105 106 #concepts 107 'nodes': Nodes(ctx, rosh_lock), 108 'topics': Topics(ctx, rosh_lock), 109 'services': Services(ctx, rosh_lock), 110 'parameters': Parameters(ctx, rosh_lock), 111 'srv': Srvs(ctx, rosh_lock), 112 'msg': Msgs(ctx, rosh_lock), 113 'serve_forever': serve_forever, 114 115 #constants 116 'LATEST': LATEST, 117 118 # api 119 'wait_for_service': wait_for_service, 120 'subscribe': subscribe, 121 } 122 # We use the loader style below for modules that export 123 # numerous symbols to let them manage which symbols they are 124 # exporting directly. 125 126 # Load topic tools 127 _loaded_symbols.update(topic_tools_symbols(ctx)) 128 # Load roslaunch 129 _loaded_symbols.update(launch_symbols(ctx)) 130 131 # save these symbols in the context 132 ctx.msg = _loaded_symbols['msg'] 133 ctx.srv = _loaded_symbols['srv'] 134 135 ctx.nodes = _loaded_symbols['nodes'] 136 ctx.topics = _loaded_symbols['topics'] 137 ctx.services = _loaded_symbols['services'] 138 ctx.parameters = _loaded_symbols['parameters'] 139 rosh.plugin.globals_load(plugin_context, globals_, _loaded_symbols)
140