nao_camera.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #
00004 # ROS node to provide access to the camera by wrapping NaoQI access (may not
00005 # be the most efficient way...)
00006 #
00007 # Copyright 2012 Daniel Maier, University of Freiburg
00008 # http://www.ros.org/wiki/nao
00009 #
00010 # Redistribution and use in source and binary forms, with or without
00011 # modification, are permitted provided that the following conditions are met:
00012 #
00013 #    # Redistributions of source code must retain the above copyright
00014 #       notice, this list of conditions and the following disclaimer.
00015 #    # Redistributions in binary form must reproduce the above copyright
00016 #       notice, this list of conditions and the following disclaimer in the
00017 #       documentation and/or other materials provided with the distribution.
00018 #    # Neither the name of the University of Freiburg nor the names of its
00019 #       contributors may be used to endorse or promote products derived from
00020 #       this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00026 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00028 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00029 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00030 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 #
00034 
00035 import roslib
00036 roslib.load_manifest('nao_driver')
00037 import rospy
00038 from sensor_msgs.msg import Image, CameraInfo
00039 from nao_driver import NaoNode
00040 from sensor_msgs.srv import SetCameraInfo
00041 
00042 
00043 from naoqi import ALProxy
00044 from vision_definitions import *
00045 
00046 
00047 class NaoCam (NaoNode):
00048     def __init__(self):
00049         NaoNode.__init__(self)
00050         rospy.init_node('nao_camera')
00051         self.camProxy = self.getProxy("ALVideoDevice")
00052         if self.camProxy is None:
00053             exit(1)
00054 
00055         # ROS pub/sub
00056         self.pub_img_ = rospy.Publisher('image_raw', Image)
00057         self.pub_info_ = rospy.Publisher('camera_info', CameraInfo)
00058         self.set_camera_info_service_ = rospy.Service('set_camera_info', SetCameraInfo, self.set_camera_info)
00059         # Messages
00060         self.info_ = CameraInfo()
00061         self.set_default_params_qvga(self.info_) #default params should be overwritten by service call
00062         # parameters
00063         self.camera_switch = rospy.get_param('~camera_switch', 0)
00064         if self.camera_switch == 0:
00065             self.frame_id = "/CameraTop_frame"
00066         elif self.camera_switch == 1:
00067             self.frame_id = "/CameraBottom_frame"
00068         else:
00069             rospy.logerr('Invalid camera_switch. Must be 0 or 1')
00070             exit(1)
00071         print "Using namespace ", rospy.get_namespace()
00072         print "using camera: ", self.camera_switch
00073         #TODO: parameters
00074         self.resolution = kQVGA
00075         self.colorSpace = kBGRColorSpace
00076         self.fps = 30
00077         # init
00078         self.nameId = ''
00079         self.subscribe()
00080 
00081     def subscribe(self):
00082         # unsubscribe for all zombie subscribers
00083         self.camProxy.unsubscribeAllInstances("rospy_gvm")
00084 
00085         # subscribe
00086         self.nameId = self.camProxy.subscribe("rospy_gvm", self.resolution, self.colorSpace, self.fps)
00087         #print "subscriber name is ", self.nameId
00088 
00089         # set params
00090         rospy.sleep(1)
00091         self.camProxy.setParam(kCameraSelectID, self.camera_switch)
00092         self.camProxy.setResolution(self.nameId, self.resolution)
00093 
00094     def set_camera_info(self, cameraInfoMsg):
00095         print "Received new camera info"
00096         self.info_ = cameraInfoMsg.camera_info
00097 
00098 
00099 
00100     def set_default_params_qvga(self, cam_info):
00101         cam_info.P[0] = 382.92
00102         cam_info.P[1] = 0.0
00103         cam_info.P[2] = 160.0
00104         cam_info.P[3] = 0
00105         cam_info.P[4] = 0.0
00106         cam_info.P[5] = 373.31
00107         cam_info.P[6] = 120.0
00108         cam_info.P[7] = 0.0
00109         cam_info.P[8] = 0.0
00110         cam_info.P[9] = 0.0
00111         cam_info.P[10] = 1.0
00112         cam_info.P[11] = 0.0
00113 
00114     def main_loop(self):
00115         img = Image()
00116         while not rospy.is_shutdown():
00117             #print "getting image..",
00118             image = self.camProxy.getImageRemote(self.nameId)
00119             #print "ok"
00120             # TODO: better time
00121             img.header.stamp = rospy.Time.now()
00122             img.header.frame_id = self.frame_id
00123             img.height = image[1]
00124             img.width = image[0]
00125             nbLayers = image[2]
00126             #colorspace = image[3]
00127             if image[3] == kYUVColorSpace:
00128                 encoding = "mono8"
00129             elif image[3] == kRGBColorSpace:
00130                 encoding = "rgb8"
00131             elif image[3] == kBGRColorSpace:
00132                 encoding = "bgr8"
00133             else:
00134                 rospy.logerror("Received unknown encoding: {0}".format(image[3]))
00135 
00136             img.encoding = encoding
00137             img.step = img.width * nbLayers
00138             img.data = image[6]
00139             self.info_.width = img.width
00140             self.info_.height = img.height
00141             self.info_.header = img.header
00142             self.pub_img_.publish(img)
00143             self.pub_info_.publish(self.info_)
00144             rospy.sleep(0.0001)# TODO: is this necessary?
00145 
00146 
00147         self.camProxy.unsubscribe(self.nameId)
00148 
00149 if __name__ == "__main__":
00150     try:
00151         naocam = NaoCam()
00152         naocam.main_loop()
00153     except RuntimeError, e:
00154         rospy.logerr('Something went wrong: %s' % (e) )
00155     rospy.loginfo('Camera stopped')
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


nao_driver
Author(s): Armin Hornung, Stefan Osswald, Daniel Maier
autogenerated on Tue Oct 15 2013 10:06:23