树梅派MPU6050陀螺仪传感器

接线图如下:我只接了SDA和SLA  ,vcc和gnd自己接

Python代码

#!/usr/bin/python
import smbus
import math
import time
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BOARD)  
GPIO.setup(16, GPIO.OUT)
 
# Power management registers
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))
 
def get_y_rotation(x,y,z):
    radians = math.atan2(x, dist(y,z))
    return -math.degrees(radians)
 
def get_x_rotation(x,y,z):
    radians = math.atan2(y, dist(x,z))
    return math.degrees(radians)
ii=1;
while True:
 print(ii)
 ii=ii+1
 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)
 
 
 # print ("-----------------------陀螺仪数据--------------------")
 
 
 gyro_xout = read_word_2c(0x43)
 gyro_yout = read_word_2c(0x45)
 gyro_zout = read_word_2c(0x47)
 
# print ("X轴陀螺仪计数值: ", gyro_xout, " X每秒的旋转度数: ", (gyro_xout / 131))
# print ("Y轴陀螺仪计数值: ", gyro_yout, " Y每秒的旋转度数: ", (gyro_yout / 131))
# print ("Z轴陀螺仪计数值: ", gyro_zout, " Z每秒的旋转度数: ", (gyro_zout / 131))
 
 
# print ("------加速度数据--------")
 
 
 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
 accel_yout_scaled = accel_yout / 16384.0
 accel_zout_scaled = accel_zout / 16384.0
  
# print ("X轴加速度计数值: ", accel_xout, " X每秒的旋转度数: ", accel_xout_scaled)
# print ("Y轴加速度计数值: ", accel_yout, " Y每秒的旋转度数: ", accel_yout_scaled)
# print ("Z轴加速度计数值: ", accel_zout, " Z每秒的旋转度数: ", accel_zout_scaled)
 
# print ("X轴旋转度数: " , get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))
 
# print ("Y轴旋转度数: " , get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled))
 xx=get_x_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
 yy=get_y_rotation(accel_xout_scaled, accel_yout_scaled, accel_zout_scaled)
 print(xx," ",yy)

如果觉得本文对您有所帮助,可以支持下博主,一分也是缘

评论

  1. 博主
    Windows Chrome
    3周前
    2022-11-18 0:49:22

  2. kanerty
    Android Chrome
    2周前
    2022-11-25 14:44:37

    树莓派

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇