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 std_srvs.srv import Empty, EmptyResponse
00009 from sensor_msgs.msg import Image
00010 from sensor_msgs.msg import CameraInfo
00011 from cv_bridge import CvBridge, CvBridgeError
00012 from sound_play.msg import SoundRequest
00013 
00014 class CheckOpenNINode:
00015     def __init__(self):
00016         self.bridge = CvBridge()
00017         self.image_sub = None
00018         self.camera = rospy.get_param('~camera', 'kinect_head')
00019         self.sleep_cycle = rospy.get_param('~sleep_cycle', 60)
00020         self.speak_enabled = rospy.get_param("~speak", True)
00021         self.speak_pub = rospy.Publisher("/robotsound", SoundRequest)
00022         self.restart_srv = rospy.Service('~restart', Empty, self.restart_service_callback)
00023 
00024     def speak(self, speak_str):
00025         rospy.logerr("[%s] %s", self.__class__.__name__, speak_str)
00026         if self.speak_enabled:
00027             msg = SoundRequest()
00028             msg.sound = SoundRequest.SAY
00029             msg.command = SoundRequest.PLAY_ONCE
00030             msg.arg = speak_str
00031             self.speak_pub.publish(msg)
00032 
00033     def restart_service_callback(self, req):
00034         self.restart_openni_node()
00035         return EmptyResponse()
00036 
00037     def image_callback(self, msg):
00038         self.image_sub.unregister()
00039         try:
00040             cv_image = self.bridge.imgmsg_to_cv(msg, "bgr8")
00041         except CvBridgeError, e:
00042             rospy.logerr("[%s] failed to convert image to cv", self.__class__.__name__)
00043             return
00044 
00045         sum_of_pixels = max(cv.Sum(cv_image))
00046         rospy.loginfo("[%s] sum of pixels is %d at %s", self.__class__.__name__, sum_of_pixels, msg.header.stamp.secs)
00047         if sum_of_pixels == 0:
00048             self.restart_openni_node()
00049 
00050     def restart_openni_node(self):
00051         rospy.logerr("Restart openni_node1")
00052         retcode = -1
00053         try:
00054             # 3. usbreset...
00055             self.speak("resetting u s b")
00056             p = subprocess.Popen("lsusb", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
00057             stdout, stderr = p.communicate()
00058             lines = stdout.split("\n")
00059             ms_line = [l for l in lines if "Microsoft" in l][0]
00060             # it should be like Bus 002 Device 013: ID 045e:02ad Microsoft Corp. Xbox NUI Audio
00061             bus_id = ms_line.split(' ')[1]
00062             bus_device_dir = "/dev/bus/usb/" + bus_id
00063             files = os.listdir(bus_device_dir)
00064             for f in files:
00065                 full_path = os.path.join(bus_device_dir, f)
00066                 if os.access(full_path, os.W_OK):
00067                     retcode = subprocess.call('rosrun openni2_camera usb_reset ' + full_path, shell=True)
00068             time.sleep(10)
00069             # 1. kill nodelet manager
00070             self.speak("something wrong with kinect, I'll restart it, killing nodelet manager")
00071             retcode = subprocess.call('rosnode kill /%s/%s_nodelet_manager' % (self.camera, self.camera), shell=True)
00072             time.sleep(10)
00073             # 2. pkill
00074             self.speak("killing child processes")
00075             retcode = subprocess.call('pkill -f %s_nodelet_manager' % self.camera, shell=True)
00076             time.sleep(10)
00077             # 3 restarting
00078             self.speak("restarting processes")
00079             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)
00080         except Exception, e:
00081             rospy.logerr('[%s] Unable to kill kinect node, caught exception:\n%s', self.__class__.__name__, traceback.format_exc())
00082 
00083     def run(self):
00084         while not rospy.is_shutdown():
00085             if self.image_sub == None or self.image_sub.impl == None:
00086                 self.image_sub = rospy.Subscriber("image", Image, self.image_callback, None, 1)
00087             rospy.sleep(self.sleep_cycle)
00088 
00089 
00090 if __name__ == '__main__':
00091     rospy.init_node('check_openni_node', anonymous=True)
00092     ic = CheckOpenNINode()
00093     ic.run()


jsk_pr2_startup
Author(s):
autogenerated on Sat Jul 1 2017 02:43:24