gyro.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2011, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of the Willow Garage nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 #Melonee Wise mwise@willowgarage.com
00034 
00035 import rospy
00036 import math
00037 import copy
00038 import sensor_msgs.msg
00039 import PyKDL
00040 
00041 class TurtlebotGyro():
00042     def __init__(self):
00043         self.cal_offset = 0.0
00044         self.orientation = 0.0
00045         self.cal_buffer =[]
00046         self.cal_buffer_length = 1000
00047         self.imu_data = sensor_msgs.msg.Imu(header=rospy.Header(frame_id="gyro_link"))
00048         self.imu_data.orientation_covariance = [1e6, 0, 0, 0, 1e6, 0, 0, 0, 1e-6]
00049         self.imu_data.angular_velocity_covariance = [1e6, 0, 0, 0, 1e6, 0, 0, 0, 1e-6]
00050         self.imu_data.linear_acceleration_covariance = [-1,0,0,0,0,0,0,0,0]
00051         self.gyro_measurement_range = rospy.get_param('~gyro_measurement_range', 150.0) 
00052         self.gyro_scale_correction = rospy.get_param('~gyro_scale_correction', 1.35)
00053         self.imu_pub = rospy.Publisher('imu/data', sensor_msgs.msg.Imu)
00054         self.imu_pub_raw = rospy.Publisher('imu/raw', sensor_msgs.msg.Imu)
00055 
00056     def reconfigure(self, config, level): 
00057         self.gyro_measurement_range = rospy.get_param('~gyro_measurement_range', 150.0) 
00058         self.gyro_scale_correction = rospy.get_param('~gyro_scale_correction', 1.35) 
00059         rospy.loginfo('self.gyro_measurement_range %f'%self.gyro_measurement_range) 
00060         rospy.loginfo('self.gyro_scale_correction %f'%self.gyro_scale_correction) 
00061 
00062     def update_calibration(self, sensor_state):
00063         #check if we're not moving and update the calibration offset
00064         #to account for any calibration drift due to temperature
00065         if sensor_state.requested_right_velocity == 0 and \
00066                sensor_state.requested_left_velocity == 0 and \
00067                sensor_state.distance == 0:
00068         
00069             self.cal_buffer.append(sensor_state.user_analog_input)
00070             if len(self.cal_buffer) > self.cal_buffer_length:
00071                 del self.cal_buffer[:-self.cal_buffer_length]
00072             self.cal_offset = sum(self.cal_buffer)/len(self.cal_buffer)
00073             
00074     def publish(self, sensor_state, last_time):
00075         if self.cal_offset == 0:
00076             return
00077 
00078         current_time = sensor_state.header.stamp
00079         dt = (current_time - last_time).to_sec()
00080         past_orientation = self.orientation
00081         self.imu_data.header.stamp =  sensor_state.header.stamp
00082         self.imu_data.angular_velocity.z  = (float(sensor_state.user_analog_input)-self.cal_offset)/self.cal_offset*self.gyro_measurement_range*(math.pi/180.0)*self.gyro_scale_correction
00083         #sign change
00084         self.imu_data.angular_velocity.z = -1.0*self.imu_data.angular_velocity.z
00085         self.orientation += self.imu_data.angular_velocity.z * dt
00086         #print orientation
00087         (self.imu_data.orientation.x, self.imu_data.orientation.y, self.imu_data.orientation.z, self.imu_data.orientation.w) = PyKDL.Rotation.RotZ(self.orientation).GetQuaternion()
00088         self.imu_pub.publish(self.imu_data)
00089 
00090         self.imu_data.header.stamp =  sensor_state.header.stamp
00091         self.imu_data.angular_velocity.z  = (float(sensor_state.user_analog_input)/self.gyro_measurement_range*(math.pi/180.0)*self.gyro_scale_correction)
00092         #sign change
00093         self.imu_data.angular_velocity.z = -1.0*self.imu_data.angular_velocity.z
00094         raw_orientation = past_orientation + self.imu_data.angular_velocity.z * dt
00095         #print orientation
00096         (self.imu_data.orientation.x, self.imu_data.orientation.y, self.imu_data.orientation.z, self.imu_data.orientation.w) = PyKDL.Rotation.RotZ(raw_orientation).GetQuaternion()
00097         self.imu_pub_raw.publish(self.imu_data)
00098 


wheeled_robin_node
Author(s): Johannes Mayr , Klemens Springer
autogenerated on Fri Aug 28 2015 13:39:00