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 import roslib
00033 roslib.load_manifest('pcl_to_scan')
00034 
00035 import rospy
00036 from sensor_msgs.msg import Imu
00037 from std_msgs.msg import Float64
00038 
00039 from math import *
00040 
00041 class TestAngle:
00042     def __init__(self):
00043         self.sub = rospy.Subscriber('imu', Imu, self.imu_cb)
00044         
00045         self.pub = rospy.Publisher('tilt_angle', Float64, None, False, True)
00046         
00047         self.sum_p = 0.0
00048         self.sum_r = 0.0
00049         self.cnt = 0
00050         self.measure = False
00051         self.duration = 10
00052         
00053 
00054     def setTilt(self):
00055         print "Set tilt..."
00056         self.pub.publish(0.0)
00057         rospy.sleep(5.0)
00058         if not rospy.is_shutdown():
00059             self.pub.publish(-20.0)
00060 
00061     def doMeasure(self):
00062         print "Do measure..."
00063         self.measure = True
00064         self.start_time = rospy.Time.now()
00065         rospy.sleep(self.duration)
00066         self.measure = False
00067         if not rospy.is_shutdown():
00068             rospy.loginfo("Current Kinect angle: pitch = %f, roll = %f" % (self.sum_p/self.cnt, self.sum_r/self.cnt))
00069             rospy.loginfo("[in degs: pitch = %f, roll = %f]" % (self.sum_p/self.cnt*180/pi, self.sum_r/self.cnt*180/pi))
00070 
00071     def imu_cb(self, msg):
00072         if (self.measure):
00073             x = msg.linear_acceleration.x
00074             y = msg.linear_acceleration.y
00075             z = msg.linear_acceleration.z
00076             pitch = asin(z/9.81)
00077             roll = asin(x/9.81)
00078             self.sum_p += pitch
00079             self.sum_r += roll
00080             self.cnt += 1
00081             
00082 
00083 
00084 def main():
00085     rospy.init_node('test_angle')
00086     tester = TestAngle()
00087     tester.setTilt()
00088     print "Wait..."
00089     rospy.sleep(5.0)
00090     tester.doMeasure()
00091     rospy.spin()
00092 
00093 
00094 if __name__ == '__main__':
00095     main()