check_openni_node.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import sys,subprocess,traceback
00004 import rospy
00005 import cv
00006 import time
00007 import os
00008 from sensor_msgs.msg import Image
00009 from sensor_msgs.msg import CameraInfo
00010 from cv_bridge import CvBridge, CvBridgeError
00011 from sound_play.msg import SoundRequest
00012 
00013 class image_converter:
00014 
00015     def __init__(self):
00016         self.bridge = CvBridge()
00017         self.image_sub = None
00018         self.camera = rospy.get_param('~camera', 'kinect_head')
00019 
00020     def callback(self,data):
00021         if data.header.stamp < self.prev_time + rospy.Duration(60):
00022             return
00023         self.image_sub.unregister()
00024         self.prev_time = data.header.stamp
00025         try:
00026             cv_image = self.bridge.imgmsg_to_cv(data, "bgr8")
00027         except CvBridgeError, e:
00028             print e
00029 
00030         rospy.loginfo("sum of pixels is %d at %s", max(cv.Sum(cv_image)), data.header.stamp.secs)
00031         if max(cv.Sum(cv_image)) == 0 :
00032         #if True:
00033             rospy.logerr("Restart openni_node1")
00034             retcode = -1
00035             try:
00036                 #retcode = subprocess.call('rosnode kill /%s/driver' % self.camera, shell=True)
00037                 # 3. usbreset...
00038                 speak("resetting u s b")
00039                 p = subprocess.Popen("lsusb", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
00040                 stdout, stderr = p.communicate()
00041                 lines = stdout.split("\n")
00042                 ms_line = [l for l in lines if "Microsoft" in l][0]
00043                 # it should be like Bus 002 Device 013: ID 045e:02ad Microsoft Corp. Xbox NUI Audio
00044                 bus_id = ms_line.split(' ')[1]
00045                 bus_device_dir = "/dev/bus/usb/" + bus_id
00046                 files = os.listdir(bus_device_dir)
00047                 for f in files:
00048                     full_path = os.path.join(bus_device_dir, f)
00049                     if os.access(full_path, os.W_OK):
00050                         retcode = subprocess.call('rosrun openni2_camera usb_reset ' + full_path, shell=True)
00051                 time.sleep(10)
00052                 # 1. kill nodelet manager
00053                 speak("something wrong with kinect, I'll restart it, killing nodelet manager")
00054                 retcode = subprocess.call('rosnode kill /%s/%s_nodelet_manager' % (self.camera, self.camera), shell=True)
00055                 time.sleep(10)
00056                 # 2. pkill
00057                 speak("killing child processes")
00058                 retcode = subprocess.call('pkill -f %s_nodelet_manager' % self.camera, shell=True)
00059                 time.sleep(10)
00060                 # 3 restarting
00061                 speak("restarting processes")
00062                 retcode = subprocess.call('roslaunch openni_launch openni.launch camera:=%s publish_tf:=false depth_registration:=true rgb_processing:=false ir_processing:=false depth_processing:=false depth_registered_processing:=false disparity_processing:=false disparity_registered_processing:=false hw_registered_processing:=true sw_registered_processing:=false rgb_frame_id:=/head_mount_kinect_rgb_optical_frame depth_frame_id:=/head_mount_kinect_ir_optical_frame' % self.camera, shell=True)
00063             except Exception, e:
00064                 rospy.logerr('Unable to kill kinect node, caught exception:\n%s', traceback.format_exc())
00065 
00066     def process(self):
00067         if self.image_sub == None or self.image_sub.impl == None:
00068             self.image_sub = rospy.Subscriber("image",Image,self.callback,None,1)
00069 
00070 speak_pub = None
00071 def speak(string):
00072     global speak_pub
00073     rospy.logerr(string)
00074     msg = SoundRequest()
00075     msg.sound = SoundRequest.SAY
00076     msg.command = SoundRequest.PLAY_ONCE
00077     msg.arg = string
00078     speak_pub.publish(msg)
00079 
00080 
00081 def main(args):
00082     global speak_pub
00083     ic = image_converter()
00084     rospy.init_node('check_openni_node', anonymous=True)
00085     ic.prev_time = rospy.Time.now()
00086     speak_pub = rospy.Publisher("/robotsound", SoundRequest)
00087     while not rospy.is_shutdown():
00088         ic.process()
00089         rospy.sleep(3)
00090           
00091 
00092 if __name__ == '__main__':
00093     main(sys.argv)


jsk_pr2_startup
Author(s):
autogenerated on Wed Sep 16 2015 10:32:17