imu_6dof_iic.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. # Copyright: 2016-2022 https://www.corvin.cn ROS小课堂
  4. # Description: 从IIC接口中读取6DOF IMU模块的三轴加速度、角度、四元数,
  5. # 然后将其打印输出.这里需要注意IMU模块使用的坐标系为ENU东北天坐标系.
  6. # Author: corvin
  7. # History:
  8. # 20220523:Initial this file.
  9. import math,time
  10. from imu_data_proc import MyIMUClass
  11. # 默认IMU模块的iic地址为0x50
  12. imu_iic_addr = 0x50
  13. # 将度数转换为弧度
  14. deg2rad = math.pi/180.0
  15. acc_factor = 9.806 #sensor acc g convert to m/s^2.
  16. if __name__=='__main__':
  17. myIMU = MyIMUClass(imu_iic_addr)
  18. print("Now 6DOF IMU Module is working ...\n")
  19. while True:
  20. myIMU.get_YPRAG()
  21. yaw_deg = myIMU.raw_yaw
  22. if yaw_deg >= 180.0:
  23. yaw_deg -= 360.0
  24. yaw_data = yaw_deg*deg2rad
  25. pitch_data = myIMU.raw_pitch*deg2rad
  26. roll_data = myIMU.raw_roll*deg2rad
  27. print("roll:",roll_data, " pitch:",pitch_data, " yaw:",yaw_data)
  28. # 读取四元数,将其打印输出
  29. myIMU.get_quatern()
  30. print("quat_x:",myIMU.raw_q1, " quat_y:",myIMU.raw_q2, " quat_z:",myIMU.raw_q3, " quat_w:",myIMU.raw_q0)
  31. # 打印输出三轴角速度,弧度值
  32. print("angular_x:",myIMU.raw_gx*deg2rad, " angular_y:",myIMU.raw_gy*deg2rad, " angular_z:",myIMU.raw_gz*deg2rad)
  33. # 打印输出三轴线加速度
  34. print("line_acc_x:",myIMU.raw_ax*acc_factor, " line_acc_y:",myIMU.raw_ay*acc_factor, " line_acc_z:",myIMU.raw_az*acc_factor)
  35. print("------------------------------------------------------------------------------------\n")
  36. time.sleep(0.1)