美文网首页
树莓派通过python读取陀螺仪MPU6050

树莓派通过python读取陀螺仪MPU6050

作者: 刘小白DOER | 来源:发表于2021-06-13 23:55 被阅读0次

    最近接触到陀螺仪(角速度传感器),感应任何方向的转角,可以用来检测姿势,在智能手机中大量应用。今天笔者使用树莓派来读取陀螺仪模块,陀螺仪MPU6050传感器模块与树莓派是基于I2C通信协议的。至于笔者这次测试的用途,暂时保密,敬请期待。

    笔者以前做过I2C 方式来读取传感器,可以参考。

    树莓派通过C语言和python操作LCD液晶屏

    树莓派通过C语言和python读取光照传感器

1、传感器接线图

GND --- GND      VCC --- 接树莓派 5V

SDA --- I2C 数据  SCL --- I2C 时钟

树莓派与陀螺仪接线图

        接完线后运行命令sudo i2cdetect -y 1 查看 地址为地址为0x68,说明成功连接到i2c传感器。

2、python程序

  python通过函数 read_word_2c 读取陀螺仪的寄存器 。程序包括温度测量、陀螺仪测量、加速度计测量。

import smbus

import math

import time

# 电源管理寄存器地址

power_mgmt_1 = 0x6b

power_mgmt_2 = 0x6c

def read_byte(adr):

    return bus.read_byte_data(address, adr)

def read_word(adr):

    high = bus.read_byte_data(address, adr)

    low = bus.read_byte_data(address, adr+1)

    val = (high << 8) + low

    return val

def read_word_2c(adr):

    val = read_word(adr)

    if (val >= 0x8000):

        return -((65535 - val) + 1)

    else:

        return val

def dist(a,b):

    return math.sqrt((a*a)+(b*b))

    #math.sqrt(x) 方法返回数字x的平方根

def get_y_rotation(x,y,z):

    radians = math.atan2(x, dist(y,z))

    #math.atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值

    return -math.degrees(radians)

    #math.degrees(x) 将弧度x转换为角度

def get_x_rotation(x,y,z):

    radians = math.atan2(y, dist(x,z))

    return math.degrees(radians)

bus = smbus.SMBus(1) # or bus = smbus.SMBus(1) for Revision 2 boards

address = 0x68      # This is the address value read via the i2cdetect command

# Now wake the 6050 up as it starts in sleep mode

bus.write_byte_data(address, power_mgmt_1, 0)

while True:

    time.sleep(0.1)

    print (' ')

    temp_out = read_word_2c(0x41)

    print ("温度传感器")

    print ("temperature: ", temp_out / 340 + 36.53)

    gyro_xout = read_word_2c(0x43)

    gyro_yout = read_word_2c(0x45)

    gyro_zout = read_word_2c(0x47)

    print ("XYZ轴陀螺仪计数值,每秒的旋转度数")

    print ("gyro_xout : ", gyro_xout, " scaled: ", (gyro_xout / 131)) #倍率250/s

    print ("gyro_yout : ", gyro_yout, " scaled: ", (gyro_yout / 131))

    print ("gyro_zout : ", gyro_zout, " scaled: ", (gyro_zout / 131))

    accel_xout = read_word_2c(0x3b)

    accel_yout = read_word_2c(0x3d)

    accel_zout = read_word_2c(0x3f)

    accel_xout_scaled = accel_xout / 16384.0 #倍率2g

    accel_yout_scaled = accel_yout / 16384.0

    accel_zout_scaled = accel_zout / 16384.0

    print ("XYZ轴加速度计数值,每秒的旋转度数")

    print ("accel_xout: ", accel_xout, " scaled: ", accel_xout_scaled)

    print ("accel_yout: ", accel_yout, " scaled: ", accel_yout_scaled)

    print ("accel_zout: ", accel_zout, " scaled: ", accel_zout_scaled)

    print ("XY轴旋转度数")

    print ("x rotation: " , get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))

    print ("y rotation: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))

    time.sleep(0.5)

3、测试运行结果

    python3 tuoluoyi.py  :

4、实际效果图

相关文章

网友评论

      本文标题:树莓派通过python读取陀螺仪MPU6050

      本文链接:https://www.haomeiwen.com/subject/kssieltx.html