아래 블로그 참고

http://blog.bitify.co.uk/2013/11/interfacing-raspberry-pi-and-mpu-6050.html


1. sudo raspi-config 를 통해 i2c enable 설정



2. sudo vi /etc/modules 

아래 두 줄 추가 후 재부팅

i2c-bcm2708
i2c-dev



3. 하드웨어 선 연결

라즈베리파이의 pin1, 3, 5, 6을 센서의 VCC, SDA, SCL, GND에 아래와 같이 연결

  • Pin 1 - 3.3V connect to VCC
  • Pin 3 - SDA connect to SDA
  • Pin 5 - SCL connect to SCL
  • Pin 6 - Ground connect to GND

4. 테스트를 위한 파일 다운 및 테스트
sudo apt-get install i2c-tools

- 테스트

sudo i2cdetect -y 0 (라즈베리파이1버전) or
sudo i2cdetect -y 1 (라즈베리파이2)
- 아래와 같이 나오는 것 확인
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --


5. 파이썬으로 i2c접근을 위해 아래 파일 다운로드

sudo apt-get install python-smbus 


6. 소스 코드

mpu6050.py로 아래 파일 저장


#!/usr/bin/python

import smbus

import math


# 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)


bus = smbus.SMBus(0) # 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 data"

print "---------"

gyro_xout = read_word_2c(0x43)

gyro_yout = read_word_2c(0x45)

gyro_zout = read_word_2c(0x47)

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

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

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

 

print

print "accelerometer data"

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 "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)


7. 테스트
python mpu6050.py



Posted by 공놀이나하여보세
,