跳至主要内容

树莓派控制陀螺仪

#!/usr/bin/python

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)
    gyro_xout = read_word_2c(0x43)
    gyro_yout = read_word_2c(0x45)
    gyro_zout = read_word_2c(0x47)
    
    print ' '
    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 "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 "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)

评论

此博客中的热门博文

css 屏幕横向滚动

  <div  class ="tech-slideshow" > <div class ="mover-1" ></div> <div class ="mover-2" ></div> </div> .tech-slideshow { height: 200px; max-width: 800px; margin: 0 auto; position: relative; overflow: hidden; transform: translate3d(0, 0, 0); } .tech-slideshow > div { height: 200px; width: 2526px; background: url(http://cdn.oushidai.com/static/upload/2018/11/12/20181112154712000000_1_84215_69.jpg); position: absolute; top: 0; left: 0; height: 100%; transform: translate3d(0, 0, 0); } .tech-slideshow .mover-1 { animation: moveSlideshow 60s linear infinite; } .tech-slideshow .mover-2 { opacity: 0; transition: opacity 0.5s ease-out; background-position: 0 -200px; animation: moveSlideshow 20s linear infinite; } .tech-slideshow:hover .mover-2 { opacity: 1; } @keyframes moveSlideshow { 100% { transform: translateX(-66.6666%); } }

树莓派头追舵机操作云台

  #!/usr/bin/python import smbus import math import time power_mgmt_1 = 0x6b power_mgmt_2 = 0x6c import RPi.GPIO as GPIO P_SERVO = [ 11 , 12 ] fPWM = 50 a = 10 b = 2 def setup (): global pwm global pwm1 GPIO.setmode(GPIO.BOARD) GPIO.setup(P_SERVO , GPIO.OUT) pwm = GPIO.PWM(P_SERVO[ 0 ] , fPWM) pwm1 = GPIO.PWM(P_SERVO[ 1 ] , fPWM) pwm.start( 0 ) pwm1.start( 0 ) def setDirection (x , y): pwm.ChangeDutyCycle( abs (y) / 10 ) pwm1.ChangeDutyCycle( abs (x)/ 10 ) time.sleep( 0.2 ) 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): rad...