3軸加速度センサー

2018年3月11日 - 未分類

3軸加速度センサー  MPU6050を試してみました。I2Cのセンサーですが、ちょっと癖がありそうです。結線は、普通にI2Cにつなぎました。アドレスは、0x68になっていました。


GithubにあったPythonコードはどれもエラーがでました。ここを参考にして、pythonのコードを割り込みするように書き換えました。そのままでは、このコードも以下のようなエラーで止まります。

pi@rpi:~/src/MPU6050-I2C-Python-Class $ python mpu-6050_2.py
Traceback (most recent call last):
File "mpu-6050_2.py", line 102, in <module>
temp = get_temp()
File "mpu-6050_2.py", line 52, in get_temp
temp = read_word_sensor(TEMP_OUT)
File "mpu-6050_2.py", line 43, in read_word_sensor
val = read_word(adr)
File "mpu-6050_2.py", line 37, in read_word
high = bus.read_byte_data(DEV_ADDR, adr)
IOError: [Errno 121] Remote I/O error

どうやら、この加速度センサーは、動きがあるときだけデータが読める仕組みのようです。INTの信号で割込みをかけたら、連続的に読めるようになりました。しかし、急激な動きに追従していないような気がします。


#!/usr/bin/python
# -*- coding: utf-8 -*

# import module
import smbus # use I2C
import math # mathmatics
from time import sleep # time module
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
#
# define
#
# slave address
DEV_ADDR = 0x68 # device address
# int GPI
pin=6

# register address
ACCEL_XOUT = 0x3b
ACCEL_YOUT = 0x3d
ACCEL_ZOUT = 0x3f
TEMP_OUT = 0x41
GYRO_XOUT = 0x43
GYRO_YOUT = 0x45
GYRO_ZOUT = 0x47
PWR_MGMT_1 = 0x6b # PWR_MGMT_1
PWR_MGMT_2 = 0x6c # PWR_MGMT_2

bus = smbus.SMBus(1)

# Sleep解除. 先ず、ここでエラーが出る。wakeupは別にしなくても動作する。

try:
bus.write_byte_data(DEV_ADDR, PWR_MGMT_1, 0)
except:
pass

#
# Sub function
#
# 1byte read
def read_byte(adr):
return bus.read_byte_data(DEV_ADDR, adr)
# 2byte read
def read_word(adr):
high = bus.read_byte_data(DEV_ADDR, adr)
low = bus.read_byte_data(DEV_ADDR, adr+1)
val = (high << 8) + low return val # Sensor data read def read_word_sensor(adr): val = read_word(adr) if (val >= 0x8000): # minus
return -((65535 - val) + 1)
else: # plus
return val
#
# 温度
#
def get_temp():
temp = read_word_sensor(TEMP_OUT)
x = temp / 340 + 36.53 # data sheet(register map)記載の計算式.
return x

#
# 角速度(full scale range ±250 deg/s
# LSB sensitivity 131 LSB/deg/s
# -> ±250 x 131 = ±32750 LSB[16bitで表現])
# Gyroscope Configuration GYRO_CONFIG (reg=0x1B)
# FS_SEL(Bit4-Bit3)でfull scale range/LSB sensitivityの変更可.
#
# get gyro data
def get_gyro_data_lsb():
x = read_word_sensor(GYRO_XOUT)
y = read_word_sensor(GYRO_YOUT)
z = read_word_sensor(GYRO_ZOUT)
return [x, y, z]
def get_gyro_data_deg():
x,y,z = get_gyro_data_lsb()
x = x / 131.0
y = y / 131.0
z = z / 131.0
return [x, y, z]

#
# 加速度(full scale range ±2g
# LSB sensitivity 16384 LSB/g)
# -> ±2 x 16384 = ±32768 LSB[16bitで表現])
# Accelerometer Configuration ACCEL_CONFIG (reg=0x1C)
# AFS_SEL(Bit4-Bit3)でfull scale range/LSB sensitivityの変更可.
#
# get accel data
def get_accel_data_lsb():
x = read_word_sensor(ACCEL_XOUT)
y = read_word_sensor(ACCEL_YOUT)
z = read_word_sensor(ACCEL_ZOUT)
return [x, y, z]
# get accel data
def get_accel_data_g():
x,y,z = get_accel_data_lsb()
x = x / 16384.0
y = y / 16384.0
z = z / 16384.0
return [x, y, z]

def callBackTest(channel):
try:
# 温度.
temp = get_temp()
# 角速度.
gyro_x,gyro_y,gyro_z = get_gyro_data_deg()
# 加速度
accel_x,accel_y,accel_z = get_accel_data_g()

# 小数点以下第1位まで表示.
print 'temp:',
print print '||',
# 小数点以下第3位まで表示.
print 'gyro[deg/s]',
print 'x: print 'y: print 'z: print '||',
# 小数点以下第3位まで表示.
print 'accel[g]',
print 'x: print 'y: print 'z: print('') # 改行.

except:
pass

#
# Main function
#
GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.FALLING, callback=callBackTest, bouncetime=100)

try:
while(True):
sleep(1)

except KeyboardInterrupt:
print "break"
GPIO.cleanup()

emp: 25.5 || gyro[deg/s] x: 0071.031 y: 0059.901 z: 0007.313 || accel[g] x: 00.487 y: 00.729 z: 00.837
temp: 25.5 || gyro[deg/s] x: 0071.008 y: 0059.969 z: 0007.237 || accel[g] x: 00.472 y: 00.728 z: 00.818
temp: 25.5 || gyro[deg/s] x: 0070.977 y: 0060.450 z: 0007.130 || accel[g] x: 00.477 y: 00.709 z: 00.825
temp: 25.5 || gyro[deg/s] x: 0070.947 y: 0060.061 z: 0007.817 || accel[g] x: 00.463 y: 00.712 z: 00.807
temp: 25.5 || gyro[deg/s] x: 0071.038 y: 0060.076 z: 0007.634 || accel[g] x: 00.468 y: 00.705 z: 00.876
temp: 25.5 || gyro[deg/s] x: 0071.053 y: 0059.763 z: 0007.756 || accel[g] x: 00.442 y: 00.681 z: 00.841
temp: 25.5 || gyro[deg/s] x: 0070.954 y: 0059.611 z: 0007.374 || accel[g] x: 00.465 y: 00.708 z: 00.833
temp: 25.5 || gyro[deg/s] x: 0070.985 y: 0059.962 z: 0007.557 || accel[g] x: 00.957 y: 00.718 z: 00.841
temp: 25.5 || gyro[deg/s] x: 0070.985 y: 0059.809 z: 0007.328 || accel[g] x: 00.477 y: 00.723 z: 00.833

とりあえずは、動作したということで。

Translate »