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 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
00064
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
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
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
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
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