当前位置:   article > 正文

Micropython——九轴传感器(MPU6050)的使用及算法(二)_micropython陀螺仪积分获取角度

micropython陀螺仪积分获取角度

前言:

在上篇文章中,简单地实现了九轴传感器(MPU6050)的获取加速度、角速度以及温度的数值。但是,我们知道,对于MPU6050来说,其提供的数据会夹杂有严重的噪音,在芯片处理静止状态时数据摆动都可能超过2%。除了噪音以外,其数据还含有偏移现象。这对于我们来说是无法忍受的。所以,我们要先对其生成的数据偏移进行校准,然后我们在处理其噪音现象。


1、对数据偏移进行校准

如何校准是我们所要关注的重点。对于数据来说,比较准确的偏移量要对大量的数据进行统计才能获知,数据量越大越准,但统计的时间也就越慢。一般校准可以在每次启动系统时进行,那么你应当在准确度和启动时间之间做一个权衡。

更改后的驱动代码实现

  1. import machine
  2. class accel():
  3. def __init__(self, i2c, addr=0x68):
  4. self.iic = i2c
  5. self.addr = addr
  6. self.iic.start()
  7. self.iic.writeto(self.addr, bytearray([107, 0]))
  8. self.iic.stop()
  9. def get_raw_values(self):
  10. self.iic.start()
  11. a = self.iic.readfrom_mem(self.addr, 0x3B, 14)
  12. self.iic.stop()
  13. return a
  14. def get_ints(self):
  15. b = self.get_raw_values()
  16. c = []
  17. for i in b:
  18. c.append(i)
  19. return c
  20. def bytes_toint(self, firstbyte, secondbyte):
  21. if not firstbyte & 0x80:
  22. return firstbyte << 8 | secondbyte
  23. return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1)
  24. def get_values(self):
  25. self.AcX_high_bite = self.get_raw_values[0]
  26. self.AcX_low_bite = self.get_raw_values[1]
  27. self.AcX = self.bytes_toint(self.AcX_high_bite,self.AcX_low_bite)
  28. self.AcX_calibre = self.AcX - self.AcX_calibration
  29. self.AcY_high_bite = self.get_raw_values[2]
  30. self.AcY_low_bite = self.get_raw_values[3]
  31. self.AcY = self.bytes_toint(self.AcY_high_bite,self.AcY_low_bite)
  32. self.AcY_calibre = self.AcY - self.AcY_calibration
  33. self.AcZ_high_bite = self.get_raw_values[4]
  34. self.AcZ_low_bite = self.get_raw_values[5]
  35. self.AcZ = self.bytes_toint(self.AcZ_high_bite,self.AcZ_low_bite)
  36. self.AcZ_calibre = self.AcZ - self.AcZ_calibration
  37. self.temp_high_byte = self.get_raw_values[6]
  38. self.temp_low_byte = self.get_raw_values[7]
  39. self.temp = self.bytes_toint( self.temp_high_byte,self.temp_low_byte)
  40. self.temperature = self.temp / 340.00 + 36.53
  41. self.GyX_high_byte = self.captures[8]
  42. self.GyX_low_byte = self.captures[9]
  43. self.GyX = self.bytes_to_int(self.GyX_high_byte, self.GyX_low_byte)#100ms陀螺仪输出一次值
  44. self.GyX_calibre = self.GyX - self.GyX_calibration
  45. self.GyY_high_byte = self.captures[10]
  46. self.GyY_low_byte = self.captures[11]
  47. self.GyY = self.bytes_to_int(self.GyY_high_byte, self.GyY_low_byte)#100ms陀螺仪输出一次值
  48. self.GyY_calibre = self.GyY - self.GyY_calibration
  49. self.GyZ_high_byte = self.captures[12]
  50. self.GyZ_low_byte = self.captures[13]
  51. self.GyZ = self.bytes_to_int(self.GyZ_high_byte, self.GyZ_low_byte)#100ms陀螺仪输出一次值
  52. self.GyZ_calibre = self.GyZ - self.GyZ_calibration
  53. # -32768 to 32767
  54. #移位合并
  55. def bytes_toint(self, firstbyte, secondbyte):
  56. if not firstbyte & 0x80:
  57. return firstbyte << 8 | secondbyte
  58. return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1)
  59. #读取100次求出平均值,第一次初始化时求出陀螺仪6轴初始值,不动的情况下,理论初始值等于0
  60. def calibration(self):
  61. i = 0
  62. self.GyX_calibration = 0
  63. self.GyY_calibration = 0
  64. self.GyZ_calibration = 0
  65. self.AcX_calibration = 0
  66. self.AcY_calibration = 0
  67. self.AcZ_calibration = 0
  68. while i < 100:
  69. self.i2c.readform_mem_into(self.address,0x3B,14)
  70. self.get_values()
  71. self.AcX_calibration += self.AcX
  72. self.AcY_calibration += self.AcY
  73. self.AcZ_calibration += self.AcZ
  74. self.GyX_calibration += self.GyX
  75. self.GyY_calibration += self.GyY
  76. self.GyZ_calibration += self.GyZ
  77. i+=1
  78. time.sleep(0.1)
  79. self.GyX_calibration /= 100
  80. self.GyY_calibration /= 100
  81. self.GyZ_calibration /= 100
  82. self.AcX_calibration /= 100
  83. self.AcY_calibration /= 100
  84. self.AcZ_calibration /= 100
  85. def val_test(self): # ONLY FOR TESTING! Also, fast reading sometimes crashes IIC
  86. from time import sleep
  87. while 1:
  88. print(self.get_values())
  89. sleep(0.05)

这里我们对于每次对100份数据进行处理,以达到处理其数据偏移的问题。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/795458
推荐阅读
  

闽ICP备14008679号