• 树莓派通过IIC读取MPU6050数据


    打开树莓派的IIC接口

     1 #!/usr/bin/python
     2 import smbus
     3 import math
     4 
     5 # Power management registers
     6 power_mgmt_1 = 0x6b
     7 power_mgmt_2 = 0x6c
     8 
     9 def read_byte(adr):
    10     return bus.read_byte_data(address, adr)
    11 
    12 def read_word(adr):
    13     high = bus.read_byte_data(address, adr)
    14     low = bus.read_byte_data(address, adr+1)
    15     val = (high << 8) + low
    16     return val
    17 
    18 def read_word_2c(adr):
    19     val = read_word(adr)
    20     if (val >= 0x8000):
    21         return -((65535 - val) + 1)
    22     else:
    23         return val
    24 
    25 def dist(a,b):
    26     return math.sqrt((a*a)+(b*b))
    27 
    28 def get_y_rotation(x,y,z):
    29     radians = math.atan2(x, dist(y,z))
    30     return -math.degrees(radians)
    31 
    32 def get_x_rotation(x,y,z):
    33     radians = math.atan2(y, dist(x,z))
    34     return math.degrees(radians)
    35 
    36 bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards
    37 address = 0x68       # This is the address value read via the i2cdetect command
    38 
    39 # Now wake the 6050 up as it starts in sleep mode
    40 bus.write_byte_data(address, power_mgmt_1, 0)
    41 
    42 print "gyro data"
    43 print "---------"
    44 
    45 gyro_xout = read_word_2c(0x43)
    46 gyro_yout = read_word_2c(0x45)
    47 gyro_zout = read_word_2c(0x47)
    48 
    49 print "gyro_xout: ", gyro_xout, " scaled: ", (gyro_xout / 131)
    50 print "gyro_yout: ", gyro_yout, " scaled: ", (gyro_yout / 131)
    51 print "gyro_zout: ", gyro_zout, " scaled: ", (gyro_zout / 131)
    52 
    53 print
    54 print "accelerometer data"
    55 print "------------------"
    56 
    57 accel_xout = read_word_2c(0x3b)
    58 accel_yout = read_word_2c(0x3d)
    59 accel_zout = read_word_2c(0x3f)
    60 
    61 accel_xout_scaled = accel_xout / 16384.0
    62 accel_yout_scaled = accel_yout / 16384.0
    63 accel_zout_scaled = accel_zout / 16384.0
    64 
    65 print "accel_xout: ", accel_xout, " scaled: ", accel_xout_scaled
    66 print "accel_yout: ", accel_yout, " scaled: ", accel_yout_scaled
    67 print "accel_zout: ", accel_zout, " scaled: ", accel_zout_scaled
    68 
    69 print "x rotation: " , get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
    70 print "y rotation: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
    View Code
  • 相关阅读:
    128. 最长连续序列( 数组背诵题吧)
    310. 最小高度树 (拓扑排序 bfs)
    797. 所有可能的路径(图的遍历)
    sql关于group by和max函数一起使用的坑(同理,求部门最高工资)
    计算正逆序
    886. 可能的二分法(dfs bfs二部图判断)
    java实现RSA加密
    读取logback日志
    logback进阶
    词法分析笔记2
  • 原文地址:https://www.cnblogs.com/duanjinjie/p/9121954.html
Copyright © 2020-2023  润新知