modes_to_standby_nonrosh.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 #  Copyright (c) 2012, UC Regents
00005 #  All rights reserved.
00006 #
00007 #  Redistribution and use in source and binary forms, with or without
00008 #  modification, are permitted provided that the following conditions
00009 #  are met:
00010 #
00011 #   * Redistributions of source code must retain the above copyright
00012 #     notice, this list of conditions and the following disclaimer.
00013 #   * Redistributions in binary form must reproduce the above
00014 #     copyright notice, this list of conditions and the following
00015 #     disclaimer in the documentation and/or other materials provided
00016 #     with the distribution.
00017 #   * Neither the name of the University of California nor the names of its
00018 #     contributors may be used to endorse or promote products derived
00019 #     from this software without specific prior written permission.
00020 #
00021 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 #  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 #  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 #  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 #  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 #  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 #  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 #  POSSIBILITY OF SUCH DAMAGE.
00033 """
00034 modes_to_standby_nonrosh.py
00035 
00036 This script is used to bring additional control modes to standby. It also necessarily brings the
00037 controller to operational first.
00038 
00039 This replaces the original modes_to_standby rosh nodelet and doesn't depend on rosh and its many
00040 dependencies.
00041 
00042 Usage:
00043 rosrun flyer_common/modes_to_standby_nonrosh.py_modes:='mode_1 mode_2 ...'
00044 """
00045 import roslib
00046 roslib.load_manifest('flyer_common')
00047 roslib.load_manifest('flyer_controller') # probably a no-no ...
00048 import rospy
00049 from rospy import loginfo
00050 import rosgraph
00051 from flyer_controller.msg import controller_cmd
00052 from flyer_controller.msg import control_mode_cmd
00053 
00054 
00055 def get_node_subscriptions(master, node_name):
00056     pubs, subs, svcs = master.getSystemState()
00057     node_subs = []
00058     for topic, subscribers in subs:
00059         if node_name in subscribers:
00060             node_subs.append(topic)
00061     return node_subs
00062 
00063 def resolve(name):
00064     return rosgraph.names.resolve_name(name, '')
00065     
00066 def is_node_subscribed_to_topic(master, node_name, topic_name):
00067     return resolve(topic_name) in get_node_subscriptions(master, node_name)
00068     
00069 rospy.init_node('modes_to_standby')
00070 
00071 ns = rospy.get_namespace()
00072 loginfo("namespace: " + ns)
00073 stby_modes = rospy.get_param('~modes').split() #parameters['~'].modes() #myargv(argv=sys.argv)
00074 loginfo('The following modes will be brought to STANDBY: %s' % stby_modes)
00075 rospy.sleep(5.0)
00076 rospy.loginfo('Bringing nodes to standby...')
00077 
00078 manager_node_name = resolve(ns + 'manager')
00079 
00080 master = rosgraph.Master('')
00081 
00082 print get_node_subscriptions(master, manager_node_name)
00083 
00084 while not (is_node_subscribed_to_topic(master, ns + 'manager', ns + 'controller/cmd') 
00085             or is_node_subscribed_to_topic(master, ns + 'controller', ns + 'controller/cmd')):
00086     loginfo('waiting for manager (or controller) to subscribe to controller_cmd...')
00087     rospy.sleep(1.0)
00088     
00089 loginfo('I see manager subscribed to controller/cmd, proceeding..')
00090 
00091 # Tell the controller to go to operational:
00092 ccmd_pub = rospy.Publisher(ns + 'controller/cmd', controller_cmd, latch=True)
00093 ccmd_pub.publish(controller_cmd('mode operational'))
00094 
00095 rospy.sleep(3.0) # would be better to check that the last command succeeded, oh well..
00096 
00097 # Now tell each mode listed in ~modes to go to STANDBY:
00098 for stby_mode in stby_modes:
00099     loginfo('Bringing mode %s to STANDBY...' % stby_mode)
00100     ccmd_pub.publish(control_mode_cmd('control_mode to_standby %s' % stby_mode))
00101     rospy.sleep(2.0)
00102 
00103 loginfo('modes_to_standby script finished')


flyer_common
Author(s): Patrick Bouffard
autogenerated on Sun Jan 5 2014 11:37:49