좋은 출처 : https://www.electronicwings.com/raspberry-pi/mpu6050-accelerometergyroscope-interfacing-with-raspberry-pi
MPU6050 Code for Raspberry Pi using Python
'''
Read Gyro and Accelerometer by Interfacing Raspberry Pi with MPU6050 using Python
http://www.electronicwings.com
'''
import smbus #import SMBus module of I2C
from time import sleep #import
#some MPU6050 Registers and their Address
PWR_MGMT_1 = 0x6B
SMPLRT_DIV = 0x19
CONFIG = 0x1A
GYRO_CONFIG = 0x1B
INT_ENABLE = 0x38
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
ACCEL_ZOUT_H = 0x3F
GYRO_XOUT_H = 0x43
GYRO_YOUT_H = 0x45
GYRO_ZOUT_H = 0x47
def MPU_Init():
#write to sample rate register
bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)
#Write to power management register
bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)
#Write to Configuration register
bus.write_byte_data(Device_Address, CONFIG, 0)
#Write to Gyro configuration register
bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)
#Write to interrupt enable register
bus.write_byte_data(Device_Address, INT_ENABLE, 1)
def read_raw_data(addr):
#Accelero and Gyro value are 16-bit
high = bus.read_byte_data(Device_Address, addr)
low = bus.read_byte_data(Device_Address, addr+1)
#concatenate higher and lower value
value = ((high << 8) | low)
#to get signed value from mpu6050
if(value > 32768):
value = value - 65536
return value
bus = smbus.SMBus(5) # or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x68 # MPU6050 device address
MPU_Init()
print (" Reading Data of Gyroscope and Accelerometer")
while True:
#Read Accelerometer raw value
acc_x = read_raw_data(ACCEL_XOUT_H)
acc_y = read_raw_data(ACCEL_YOUT_H)
acc_z = read_raw_data(ACCEL_ZOUT_H)
#Read Gyroscope raw value
gyro_x = read_raw_data(GYRO_XOUT_H)
gyro_y = read_raw_data(GYRO_YOUT_H)
gyro_z = read_raw_data(GYRO_ZOUT_H)
#Full scale range +/- 250 degree/C as per sensitivity scale factor
Ax = acc_x/16384.0
Ay = acc_y/16384.0
Az = acc_z/16384.0
Gx = gyro_x/131.0
Gy = gyro_y/131.0
Gz = gyro_z/131.0
print ("Gx=%.2f" %Gx, u'\u00b0'+ "/s", "\tGy=%.2f" %Gy, u'\u00b0'+ "/s", "\tGz=%.2f" %Gz, u'\u00b0'+ "/s", "\tAx=%.2f g" %Ax, "\tAy=%.2f g" %Ay, "\tAz=%.2f g" %Az)
sleep(1)
Code 실행 결과
Reading Data of Gyroscope and Accelerometer
Gx=0.00 °/s Gy=0.00 °/s Gz=0.00 °/s Ax=0.00 g Ay=0.00 g Az=0.00 g
Gx=-0.25 °/s Gy=-0.05 °/s Gz=-0.04 °/s Ax=-0.18 g Ay=0.56 g Az=0.72 g
Gx=-0.28 °/s Gy=-0.10 °/s Gz=-0.02 °/s Ax=-0.17 g Ay=0.56 g Az=0.74 g
Gx=21.01 °/s Gy=-46.73 °/s Gz=-66.13 °/s Ax=-0.60 g Ay=0.53 g Az=1.98 g
Gx=-1.12 °/s Gy=0.08 °/s Gz=-2.44 °/s Ax=0.91 g Ay=0.25 g Az=-0.42 g
Gx=-20.89 °/s Gy=22.92 °/s Gz=-12.68 °/s Ax=0.57 g Ay=0.91 g Az=-0.08 g
Gx=-8.25 °/s Gy=6.10 °/s Gz=-2.79 °/s Ax=-0.14 g Ay=-0.28 g Az=0.79 g
Gx=0.19 °/s Gy=-1.33 °/s Gz=0.41 °/s Ax=-0.27 g Ay=-0.19 g Az=0.89 g
Gx=-1.12 °/s Gy=0.74 °/s Gz=-1.82 °/s Ax=0.01 g Ay=-0.03 g Az=0.93 g
Gx=-16.98 °/s Gy=-8.69 °/s Gz=-0.35 °/s Ax=0.53 g Ay=0.11 g Az=0.41 g
Gx=-0.86 °/s Gy=-3.69 °/s Gz=-3.56 °/s Ax=0.41 g Ay=-0.85 g Az=1.86 g
Gx=8.50 °/s Gy=-13.91 °/s Gz=-2.17 °/s Ax=1.00 g Ay=0.22 g Az=-0.11 g
Gx=1.95 °/s Gy=-15.69 °/s Gz=-6.44 °/s Ax=0.88 g Ay=0.49 g Az=0.22 g
Gx=-0.90 °/s Gy=-19.22 °/s Gz=4.11 °/s Ax=0.79 g Ay=0.20 g Az=0.20 g
Gx=2.63 °/s Gy=-0.53 °/s Gz=3.50 °/s Ax=0.57 g Ay=0.22 g Az=0.47 g
Gx=7.98 °/s Gy=-5.44 °/s Gz=-4.53 °/s Ax=0.16 g Ay=-0.44 g Az=1.19 g
Gx=-1.49 °/s Gy=2.40 °/s Gz=-1.51 °/s Ax=0.50 g Ay=-0.40 g Az=0.69 g
Gx=9.06 °/s Gy=-0.34 °/s Gz=11.00 °/s Ax=-0.20 g Ay=-0.04 g Az=0.61 g
Gx=8.08 °/s Gy=-0.40 °/s Gz=-2.18 °/s Ax=-0.12 g Ay=0.08 g Az=0.93 g
Gx=-0.26 °/s Gy=-0.06 °/s Gz=-0.07 °/s Ax=-0.04 g Ay=0.12 g Az=0.94 g
Gx=-0.23 °/s Gy=-0.02 °/s Gz=-0.08 °/s Ax=-0.04 g Ay=0.12 g Az=0.94 g
Gx=-0.25 °/s Gy=-0.08 °/s Gz=-0.09 °/s Ax=-0.06 g Ay=0.15 g Az=0.93 g
Gx=-0.23 °/s Gy=-0.05 °/s Gz=-0.03 °/s Ax=-0.06 g Ay=0.17 g Az=0.93 g
Gx=-0.29 °/s Gy=-0.06 °/s Gz=-0.05 °/s Ax=-0.07 g Ay=0.15 g Az=0.92 g
Gx=-0.40 °/s Gy=0.06 °/s Gz=-0.11 °/s Ax=-0.07 g Ay=0.16 g Az=0.94 g
Gx=-0.28 °/s Gy=-0.04 °/s Gz=-0.08 °/s Ax=-0.08 g Ay=0.17 g Az=0.93 g
Gx=-0.25 °/s Gy=-0.07 °/s Gz=-0.04 °/s Ax=-0.06 g Ay=0.14 g Az=0.94 g
Gx=-0.27 °/s Gy=-0.05 °/s Gz=-0.02 °/s Ax=-0.06 g Ay=0.16 g Az=0.93 g
Gx=6.27 °/s Gy=8.89 °/s Gz=2.79 °/s Ax=0.06 g Ay=-0.07 g Az=0.90 g
Gx=4.08 °/s Gy=36.98 °/s Gz=-14.33 °/s Ax=0.28 g Ay=0.08 g Az=0.66 g
Gx=-2.27 °/s Gy=-2.36 °/s Gz=-1.52 °/s Ax=-0.13 g Ay=0.01 g Az=0.95 g
Gx=0.41 °/s Gy=-0.18 °/s Gz=-0.15 °/s Ax=-0.09 g Ay=0.30 g Az=0.89 g
^CTraceback (most recent call last):
File "/root/o/2.py", line 82, in <module>
sleep(1)
'Orange Pi > O-Pi Zero (H2)' 카테고리의 다른 글
Python Sensor Server (센서 서버) 만들기 - The Beginning (0) | 2021.03.03 |
---|