00001
00002
00003 import time
00004 from random import *
00005
00006 import roslib
00007 roslib.load_manifest('cob_script_server')
00008 import rospy
00009
00010 import tf
00011 from tf.transformations import euler_from_quaternion
00012
00013 import cv
00014 from sensor_msgs.msg import Image
00015 from cv_bridge import CvBridge, CvBridgeError
00016 from math import *
00017
00018
00019 from simple_script_server import script
00020
00021 class CalibCam(script):
00022
00023 def Initialize(self):
00024 self.bridge = CvBridge()
00025 self.image_sub = rospy.Subscriber("/stereo/right/image_color",Image,self.callback)
00026 self.cv_image = cv.CreateImage((1,1), 1 , 3)
00027
00028 self.sss.init("torso")
00029
00030
00031 def Run(self):
00032 print "start"
00033 seed()
00034 maxVal = 0.05
00035 file_path = "./"
00036 listener = tf.TransformListener()
00037 nr_images = 14
00038
00039
00040
00041 self.sss.move("arm","home")
00042 self.sss.move("torso","home")
00043
00044
00045
00046
00047 self.sss.wait_for_input()
00048
00049
00050 for i in range(1,nr_images):
00051 if i==1:
00052 r1 = maxVal
00053 r2 = maxVal
00054 elif i==2:
00055 r1 = -maxVal
00056 r2 = maxVal
00057 elif i==3:
00058 r1 = maxVal
00059 r2 = -maxVal
00060 elif i==4:
00061 r1 = -maxVal
00062 r2 = -maxVal
00063 else:
00064 r1 = (random()-0.5)*1.5*maxVal;
00065 r2 = (random()-0.5)*1.5*maxVal;
00066 r3 = (random()-0.5)*2*maxVal;
00067 r4 = (random()-0.5)*2*maxVal;
00068 if i<5:
00069 self.sss.move("torso",[[0.75*r1,0.75*r2,r1,r2]])
00070 else:
00071 self.sss.move("torso",[[r1,r2,r3,r4]])
00072 self.sss.sleep(1)
00073 try:
00074 (trans,rot) = listener.lookupTransform('/base_link', '/head_color_camera_r_link', rospy.Time(0))
00075 rpy = euler_from_quaternion(rot)
00076 cyaw = cos(rpy[2])
00077 syaw = sin(rpy[2])
00078 cpitch = cos(rpy[1])
00079 spitch = sin(rpy[1])
00080 croll = cos(rpy[0])
00081 sroll = sin(rpy[0])
00082 R11 = cyaw*cpitch
00083 R12 = cyaw*spitch*sroll-syaw*croll
00084 R13 = cyaw*spitch*croll+syaw*sroll
00085 R21 = syaw*cpitch
00086 R22 = syaw*spitch*sroll+cyaw*croll
00087 R23 = syaw*spitch*croll-cyaw*sroll
00088 R31 = -spitch
00089 R32 = cpitch*sroll
00090 R33 = cpitch*croll
00091 fout = open(file_path+'calpic'+str(i)+'.coords','w')
00092 fout.write(str(R11)+' '+str(R12)+' '+str(R13)+' '+str(trans[0]*1000)+'\n'+str(R21)+' '+str(R22)+' '+str(R23)+' '+str(trans[1]*1000)+'\n'+str(R31)+' '+str(R32)+' '+str(R33)+' '+str(trans[2]*1000))
00093 fout.close()
00094 except (tf.LookupException, tf.ConnectivityException):
00095 print "tf exception"
00096
00097 self.sss.sleep(1)
00098 cv.SaveImage(file_path+'calpic'+str(i)+'.png',self.cv_image)
00099 self.sss.sleep(1)
00100 self.sss.move("torso","home")
00101 print "finished"
00102
00103 def callback(self,data):
00104 try:
00105 self.cv_image = self.bridge.imgmsg_to_cv(data, "bgr8")
00106 except CvBridgeError, e:
00107 print e
00108
00109 if __name__ == "__main__":
00110 SCRIPT = CalibCam()
00111 SCRIPT.Start()