Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
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
00060 self.info_ = CameraInfo()
00061 self.set_default_params_qvga(self.info_)
00062
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
00074 self.resolution = kQVGA
00075 self.colorSpace = kBGRColorSpace
00076 self.fps = 30
00077
00078 self.nameId = ''
00079 self.subscribe()
00080
00081 def subscribe(self):
00082
00083 self.camProxy.unsubscribeAllInstances("rospy_gvm")
00084
00085
00086 self.nameId = self.camProxy.subscribe("rospy_gvm", self.resolution, self.colorSpace, self.fps)
00087
00088
00089
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
00118 image = self.camProxy.getImageRemote(self.nameId)
00119
00120
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
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)
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')