找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1563|回复: 0
打印 上一主题 下一主题
收起左侧

树莓派采集Mpu6050数据

[复制链接]
跳转到指定楼层
楼主
ID:417268 发表于 2018-10-29 14:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
树莓派采集Mpu6050数据,可算出翻滚角和俯仰角
  1. #!/usr/bin/python
  2. import smbus
  3. import math

  4. # Power management registers
  5. power_mgmt_1 = 0x6b
  6. power_mgmt_2 = 0x6c

  7. def read_byte(adr):
  8.     return bus.read_byte_data(address, adr)

  9. def read_word(adr):
  10.     high = bus.read_byte_data(address, adr)
  11.     low = bus.read_byte_data(address, adr+1)
  12.     val = (high << 8) + low
  13.     return val

  14. def read_word_2c(adr):
  15.     val = read_word(adr)
  16.     if (val >= 0x8000):
  17.         return -((65535 - val) + 1)
  18.     else:
  19.         return val

  20. def dist(a,b):
  21.     return math.sqrt((a*a)+(b*b))

  22. def get_y_rotation(x,y,z):
  23.     radians = math.atan2(x, dist(y,z))
  24.     return -math.degrees(radians)

  25. def get_x_rotation(x,y,z):
  26.     radians = math.atan2(y, dist(x,z))
  27.     return math.degrees(radians)

  28. bus = smbus.SMBus(0) # or bus = smbus.SMBus(1) for Revision 2 boards
  29. address = 0x68       # This is the address value read via the i2cdetect command

  30. # Now wake the 6050 up as it starts in sleep mode
  31. bus.write_byte_data(address, power_mgmt_1, 0)

  32. print "gyro data"
  33. print "---------"

  34. gyro_xout = read_word_2c(0x43)
  35. gyro_yout = read_word_2c(0x45)
  36. gyro_zout = read_word_2c(0x47)

  37. print "gyro_xout: ", gyro_xout, " scaled: ", (gyro_xout / 131)
  38. print "gyro_yout: ", gyro_yout, " scaled: ", (gyro_yout / 131)
  39. print "gyro_zout: ", gyro_zout, " scaled: ", (gyro_zout / 131)

  40. print()
  41. print "accelerometer data"
  42. print "------------------"

  43. accel_xout = read_word_2c(0x3b)
  44. accel_yout = read_word_2c(0x3d)
  45. accel_zout = read_word_2c(0x3f)

  46. accel_xout_scaled = accel_xout / 16384.0
  47. accel_yout_scaled = accel_yout / 16384.0
  48. accel_zout_scaled = accel_zout / 16384.0

  49. print "accel_xout: ", accel_xout, " scaled: ", accel_xout_scaled
  50. print "accel_yout: ", accel_yout, " scaled: ", accel_yout_scaled
  51. print "accel_zout: ", accel_zout, " scaled: ", accel_zout_scaled

  52. print "x rotation: " , get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
  53. print "y rotation: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)

复制代码


MPU6050.doc

1.98 KB, 下载次数: 4, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表