nao_vision.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # setup ROS
00004 import roslib;
00005 roslib.load_manifest('nao_vision')
00006 import rospy
00007 from sensor_msgs.msg import Image
00008 from std_msgs.msg import Header
00009 from std_msgs.msg import Time
00010 import sys
00011 
00012  # the path parameter is optional
00013 path = rospy.get_param("/naoqi/path", "")
00014 sys.path.append(path)
00015 
00016 # attempt to load NAOqi
00017 try:
00018     from naoqi import ALProxy
00019     from vision_definitions import*
00020 except ImportError:
00021     rospy.logerr("NAOqi not found - Please check to see if it is in your PYTHONPATH variable.")
00022     exit(1)
00023     
00024 class NaoVision():
00025     def __init__(self):
00026         rospy.init_node("nao_vision")
00027         
00028         # attempt to get the host and port
00029         try:
00030             host = rospy.get_param("/naoqi/host")
00031         except KeyError:
00032             rospy.logerr("Unable to find parameter /naoqi/host")
00033             exit(1)
00034         try:
00035             port = rospy.get_param("/naoqi/port")
00036         except KeyError:
00037             rospy.logerr("Unable to find parameter /naoqi/port")
00038             exit(1)
00039         
00040         #connect to the Nao
00041         try:
00042             self.nao_cam = ALProxy("ALVideoDevice", host, port)
00043         except Exception:
00044             rospy.logerr("Unable to create vision proxy.")
00045             exit(1)
00046             
00047         # get any optional parameters
00048         resolution = rospy.get_param("~resolution", 1)
00049         cam = rospy.get_param("~camera", 0)
00050         
00051         #setup the camera stream
00052         self.proxy_name = self.nao_cam.subscribe("nao_image", resolution, kRGBColorSpace, 30)
00053         self.nao_cam_pub = rospy.Publisher("nao_camera", Image)
00054         self.nao_cam.setParam(kCameraSelectID, cam)
00055 
00056         rospy.loginfo("NAO Vision Node Initialized")
00057     
00058     def publish_image(self):
00059         # get the image from the Nao
00060         img = self.nao_cam.getImageRemote(self.proxy_name)
00061         # copy the data into the ROS Image
00062         ros_img = Image()
00063         ros_img.width = img[0]
00064         ros_img.height = img[1]
00065         ros_img.step = img[2] * img[0]
00066         ros_img.header.stamp.secs = img[5]
00067         ros_img.data = img[6]
00068         ros_img.is_bigendian = False
00069         ros_img.encoding = "rgb8"
00070         ros_img.data = img[6]
00071         # publish the image
00072         self.nao_cam_pub.publish(ros_img)
00073         
00074     def disconnect(self):
00075         self.nao_cam.unsubscribe(self.proxy_name)
00076 
00077 if __name__ == '__main__':
00078     vision = NaoVision()
00079     #operate at 30 Hz
00080     rate = rospy.Rate(30)
00081     while not rospy.is_shutdown():
00082         vision.publish_image()
00083         rate.sleep()
00084     vision.disconnect()
00085 


nao_vision
Author(s): Russell Toris
autogenerated on Mon Jan 6 2014 11:27:48