check_wge100_present.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2010, Willow Garage, Inc.
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 Willow Garage 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 ##\author Kevin Watts
00035 ##\brief Checks that wge100 camera is present using "discover"
00036 
00037 PKG = 'qualification'
00038 import roslib; roslib.load_manifest(PKG)
00039 
00040 import rospy
00041 
00042 from pr2_self_test_msgs.srv import ScriptDone, ScriptDoneRequest
00043 from pr2_self_test_msgs.srv import ConfirmConf, ConfirmConfResponse, ConfirmConfRequest
00044 
00045 import subprocess, sys
00046 
00047 
00048 SRV_NAME = 'prestartup_done'
00049 finish = rospy.ServiceProxy(SRV_NAME, ScriptDone)
00050 
00051 confirm_proxy = rospy.ServiceProxy('mcb_conf_results', ConfirmConf)
00052 
00053 ##\brief Returns True if user wants to try again
00054 def _report_no_cameras(interface):
00055     conf = ConfirmConfRequest()
00056     conf.message = "No cameras found on interface %s. Check camera lights. Click OK to retry." % interface
00057     conf.details = "No cameras found. This may be a problem with the cables to the camera.\nCamera light codes:\n\tGreen - Power\n\tOrange - Connection\n\nIf lights are on, unplug and plug in camera and retry."
00058 
00059     resp = confirm_proxy.call(conf)
00060     return resp.retry == ConfirmConfResponse.RETRY
00061 
00062 
00063 def check_camera(interface):
00064     while not rospy.is_shutdown():
00065         p = subprocess.Popen('rosrun wge100_camera discover %s' % interface, 
00066                              stdout=subprocess.PIPE,
00067                              stderr=subprocess.PIPE, shell=True)
00068 
00069         o,e = p.communicate()
00070         retcode = p.returncode
00071 
00072         if retcode != 0:
00073             if _report_no_cameras(interface):
00074                 continue
00075             print >> sys.stderr, "Unable to run discover. Camera may not be present"
00076             return False, "Unable to run discover. Camera may not be present"
00077         
00078         try_again = False
00079         for ln in e.split('\n'):
00080             if ln.find('No cam') > -1:
00081                 if not _report_no_cameras(interface):
00082                     print >> sys.stderr, "No cameras found"
00083                     return False, "No cameras found"
00084                 else:
00085                     try_again = True
00086                     break
00087         if try_again:
00088             continue
00089             
00090         found = 0
00091         for ln in o.split('\n'):
00092             if ln.find('No cam') > -1:
00093                 if _report_no_cameras(interface):
00094                     continue
00095                 print >> sys.stderr, "No cameras found"
00096                 return False, "No cameras found"
00097             elif ln.find('Found') == 0:
00098                 found += 1
00099         if found > 0:
00100             return True, ''
00101         if not _report_no_cameras(interface):
00102            return False, "No cameras"
00103 
00104     return False, 'Rospy shutdown'
00105 
00106 
00107 
00108 
00109 if __name__ == '__main__':
00110     rospy.init_node('check_wge100_present')
00111     args = rospy.myargv()
00112     if len(args) > 1:
00113         interface = args[1]
00114     else:
00115         interface = 'lan0'
00116     
00117 
00118     val, msg = check_camera(interface)
00119     print val, msg
00120 
00121     r = ScriptDoneRequest()
00122     r.result = 0
00123     r.failure_msg = 'Found wge100 camera on interface %s' % interface
00124     if not val:
00125         r.result = 1
00126         r.failure_msg = msg
00127 
00128     rospy.wait_for_service(SRV_NAME, 5)
00129     finish.call(r)
00130         
00131     rospy.spin()


qualification
Author(s): Kevin Watts (watts@willowgarage.com), Josh Faust (jfaust@willowgarage.com)
autogenerated on Sat Dec 28 2013 17:57:34