|
@@ -10,6 +10,8 @@
|
|
# 20191031: Initial this file.
|
|
# 20191031: Initial this file.
|
|
# 20191209:Add get imu chip temperature function-get_temp().
|
|
# 20191209:Add get imu chip temperature function-get_temp().
|
|
# 20200702: 读取各数据后需要使用np.short转换才能进行后续数据处理.
|
|
# 20200702: 读取各数据后需要使用np.short转换才能进行后续数据处理.
|
|
|
|
+# 20210319:从寄存器地址读取数据时,直接一次性读取多个字节,不是依次
|
|
|
|
+# 读取多个寄存器地址,每个地址读取2字节.
|
|
import smbus
|
|
import smbus
|
|
import rospy
|
|
import rospy
|
|
import numpy as np
|
|
import numpy as np
|
|
@@ -21,60 +23,34 @@ class MyIMU(object):
|
|
|
|
|
|
def get_YPRAG(self):
|
|
def get_YPRAG(self):
|
|
try:
|
|
try:
|
|
- #roll_tmp = self.i2c.read_i2c_block_data(self.addr, 0x3d, 6)
|
|
|
|
- #pitch_tmp = self.i2c.read_i2c_block_data(self.addr, 0x3e, 2)
|
|
|
|
- #yaw_tmp = self.i2c.read_i2c_block_data(self.addr, 0x3f, 2)
|
|
|
|
- roll_tmp = self.i2c.read_i2c_block_data(self.addr, 0x3d, 2)
|
|
|
|
- pitch_tmp = self.i2c.read_i2c_block_data(self.addr, 0x3e, 2)
|
|
|
|
- yaw_tmp = self.i2c.read_i2c_block_data(self.addr, 0x3f, 2)
|
|
|
|
- #rospy.loginfo("Read roll_tmp:%#x %#x", roll_tmp[0], roll_tmp[1])
|
|
|
|
- #rospy.loginfo("Read pitch_tmp:%#x %#x", roll_tmp[2], roll_tmp[3])
|
|
|
|
- #rospy.loginfo("Read yaw_tmp:%#x %#x", roll_tmp[4], roll_tmp[5])
|
|
|
|
-
|
|
|
|
- ax_tmp = self.i2c.read_i2c_block_data(self.addr, 0x34, 2)
|
|
|
|
- ay_tmp = self.i2c.read_i2c_block_data(self.addr, 0x35, 2)
|
|
|
|
- az_tmp = self.i2c.read_i2c_block_data(self.addr, 0x36, 2)
|
|
|
|
- #rospy.loginfo("Read i2c accX:" + str(ax_tmp))
|
|
|
|
- #rospy.loginfo("Read i2c accY:" + str(ay_tmp))
|
|
|
|
- #rospy.loginfo("Read i2c accZ:" + str(az_tmp))
|
|
|
|
-
|
|
|
|
- gx_tmp = self.i2c.read_i2c_block_data(self.addr, 0x37, 2)
|
|
|
|
- gy_tmp = self.i2c.read_i2c_block_data(self.addr, 0x38, 2)
|
|
|
|
- gz_tmp = self.i2c.read_i2c_block_data(self.addr, 0x39, 2)
|
|
|
|
|
|
+ rpy_data = self.i2c.read_i2c_block_data(self.addr, 0x3d, 6)
|
|
|
|
+ axyz_data = self.i2c.read_i2c_block_data(self.addr, 0x34, 6)
|
|
|
|
+ gxyz_data = self.i2c.read_i2c_block_data(self.addr, 0x37, 6)
|
|
except IOError:
|
|
except IOError:
|
|
- rospy.logerr("Read IMU YPRAG date error !")
|
|
|
|
|
|
+ rospy.logerr("Read IMU RPYAG date error !")
|
|
else:
|
|
else:
|
|
- self.raw_roll = float(np.short(np.short(roll_tmp[1]<<8)|roll_tmp[0])/32768.0*180.0)
|
|
|
|
- self.raw_pitch = float(np.short(np.short(pitch_tmp[1]<<8)|pitch_tmp[0])/32768.0*180.0)
|
|
|
|
- self.raw_yaw = float(np.short(np.short(yaw_tmp[1]<<8)|yaw_tmp[0])/32768.0*180.0)
|
|
|
|
- #self.raw_roll = float(np.short(np.short(roll_tmp[1]<<8)|roll_tmp[0])/32768.0*180.0)
|
|
|
|
- #self.raw_pitch = float(np.short(np.short(roll_tmp[3]<<8)|roll_tmp[2])/32768.0*180.0)
|
|
|
|
- #self.raw_yaw = float(np.short(np.short(roll_tmp[5]<<8)|roll_tmp[4])/32768.0*180.0)
|
|
|
|
|
|
+ self.raw_roll = float(np.short(np.short(rpy_data[1]<<8)|rpy_data[0])/32768.0*180.0)
|
|
|
|
+ self.raw_pitch = float(np.short(np.short(rpy_data[3]<<8)|rpy_data[2])/32768.0*180.0)
|
|
|
|
+ self.raw_yaw = float(np.short(np.short(rpy_data[5]<<8)|rpy_data[4])/32768.0*180.0)
|
|
|
|
|
|
- self.raw_ax = float(np.short(np.short(ax_tmp[1]<<8)|ax_tmp[0])/32768.0*16.0)
|
|
|
|
- self.raw_ay = float(np.short(np.short(ay_tmp[1]<<8)|ay_tmp[0])/32768.0*16.0)
|
|
|
|
- self.raw_az = float(np.short(np.short(az_tmp[1]<<8)|az_tmp[0])/32768.0*16.0)
|
|
|
|
- #rospy.loginfo("Read raw accX:" + str(self.raw_ax))
|
|
|
|
- #rospy.loginfo("Read raw accY:" + str(self.raw_ay))
|
|
|
|
- #rospy.loginfo("Read raw accZ:" + str(self.raw_az))
|
|
|
|
|
|
+ self.raw_ax = float(np.short(np.short(axyz_data[1]<<8)|axyz_data[0])/32768.0*16.0)
|
|
|
|
+ self.raw_ay = float(np.short(np.short(axyz_data[3]<<8)|axyz_data[2])/32768.0*16.0)
|
|
|
|
+ self.raw_az = float(np.short(np.short(axyz_data[5]<<8)|axyz_data[4])/32768.0*16.0)
|
|
|
|
|
|
- self.raw_gx = float(np.short(np.short(gx_tmp[1]<<8)|gx_tmp[0])/32768.0*2000.0)
|
|
|
|
- self.raw_gy = float(np.short(np.short(gy_tmp[1]<<8)|gy_tmp[0])/32768.0*2000.0)
|
|
|
|
- self.raw_gz = float(np.short(np.short(gz_tmp[1]<<8)|gz_tmp[0])/32768.0*2000.0)
|
|
|
|
|
|
+ self.raw_gx = float(np.short(np.short(gxyz_data[1]<<8)|gxyz_data[0])/32768.0*2000.0)
|
|
|
|
+ self.raw_gy = float(np.short(np.short(gxyz_data[3]<<8)|gxyz_data[2])/32768.0*2000.0)
|
|
|
|
+ self.raw_gz = float(np.short(np.short(gxyz_data[5]<<8)|gxyz_data[4])/32768.0*2000.0)
|
|
|
|
|
|
def get_quatern(self):
|
|
def get_quatern(self):
|
|
try:
|
|
try:
|
|
- q0 = self.i2c.read_i2c_block_data(self.addr, 0x51, 2)
|
|
|
|
- q1 = self.i2c.read_i2c_block_data(self.addr, 0x52, 2)
|
|
|
|
- q2 = self.i2c.read_i2c_block_data(self.addr, 0x53, 2)
|
|
|
|
- q3 = self.i2c.read_i2c_block_data(self.addr, 0x54, 2)
|
|
|
|
|
|
+ quat_data = self.i2c.read_i2c_block_data(self.addr, 0x51, 8)
|
|
except IOError:
|
|
except IOError:
|
|
rospy.logerr("Read IMU quaternion date error !")
|
|
rospy.logerr("Read IMU quaternion date error !")
|
|
else:
|
|
else:
|
|
- self.raw_q0 = float((np.short((q0[1]<<8)|q0[0]))/32768.0)
|
|
|
|
- self.raw_q1 = float((np.short((q1[1]<<8)|q1[0]))/32768.0)
|
|
|
|
- self.raw_q2 = float((np.short((q2[1]<<8)|q2[0]))/32768.0)
|
|
|
|
- self.raw_q3 = float((np.short((q3[1]<<8)|q3[0]))/32768.0)
|
|
|
|
|
|
+ self.raw_q0 = float((np.short(np.short(quat_data[1]<<8)|quat_data[0]))/32768.0)
|
|
|
|
+ self.raw_q1 = float((np.short(np.short(quat_data[3]<<8)|quat_data[2]))/32768.0)
|
|
|
|
+ self.raw_q2 = float((np.short(np.short(quat_data[5]<<8)|quat_data[4]))/32768.0)
|
|
|
|
+ self.raw_q3 = float((np.short(np.short(quat_data[7]<<8)|quat_data[6]))/32768.0)
|
|
|
|
|
|
def get_two_float(self, data, n):
|
|
def get_two_float(self, data, n):
|
|
data = str(data)
|
|
data = str(data)
|