{"id":1805,"date":"2018-03-11T16:46:28","date_gmt":"2018-03-11T07:46:28","guid":{"rendered":"http:\/\/research.itplants.com\/?p=1805"},"modified":"2018-03-14T15:17:26","modified_gmt":"2018-03-14T06:17:26","slug":"3%e8%bb%b8%e5%8a%a0%e9%80%9f%e5%ba%a6%e3%82%bb%e3%83%b3%e3%82%b5%e3%83%bc","status":"publish","type":"post","link":"https:\/\/research.itplants.com\/?p=1805","title":{"rendered":"3\u8ef8\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5\u30fc"},"content":{"rendered":"<p>\uff13\u8ef8\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5\u30fc\u3000 MPU6050\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002I2C\u306e\u30bb\u30f3\u30b5\u30fc\u3067\u3059\u304c\u3001\u3061\u3087\u3063\u3068\u7656\u304c\u3042\u308a\u305d\u3046\u3067\u3059\u3002\u7d50\u7dda\u306f\u3001\u666e\u901a\u306bI2C\u306b\u3064\u306a\u304e\u307e\u3057\u305f\u3002\u30a2\u30c9\u30ec\u30b9\u306f\u30010x68\u306b\u306a\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"https:\/\/research.itplants.com\/wp\/wp-content\/uploads\/2018\/03\/mpu-6050.jpg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><br \/>\n<img loading=\"lazy\" class=\"aligncenter wp-image-1806\" src=\"https:\/\/research.itplants.com\/wp\/wp-content\/uploads\/2018\/03\/mpu-6050.jpg\" alt=\"\" width=\"185\" height=\"242\" srcset=\"https:\/\/research.itplants.com\/wp\/wp-content\/uploads\/2018\/03\/mpu-6050.jpg 366w, https:\/\/research.itplants.com\/wp\/wp-content\/uploads\/2018\/03\/mpu-6050-229x300.jpg 229w, https:\/\/research.itplants.com\/wp\/wp-content\/uploads\/2018\/03\/mpu-6050-320x420.jpg 320w\" sizes=\"(max-width: 185px) 100vw, 185px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/github.com\/thisisG\/MPU6050-I2C-Python-Class\/blob\/master\/testMPU.py\">Github\u306b\u3042\u3063\u305fPython\u30b3\u30fc\u30c9<\/a>\u306f\u3069\u308c\u3082\u30a8\u30e9\u30fc\u304c\u3067\u307e\u3057\u305f\u3002<a href=\"http:\/\/www.widesnow.com\/entry\/2015\/09\/10\/061128\">\u3053\u3053\u3092\u53c2\u8003\u306b\u3057\u3066<\/a>\u3001python\u306e\u30b3\u30fc\u30c9\u3092\u5272\u308a\u8fbc\u307f\u3059\u308b\u3088\u3046\u306b\u66f8\u304d\u63db\u3048\u307e\u3057\u305f\u3002\u305d\u306e\u307e\u307e\u3067\u306f\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3082\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u3067\u6b62\u307e\u308a\u307e\u3059\u3002<\/p>\n<p><code>pi@rpi:~\/src\/MPU6050-I2C-Python-Class $\u00a0python mpu-6050_2.py<br \/>\nTraceback (most recent call last):<br \/>\nFile \"mpu-6050_2.py\", line 102, in &lt;module&gt;<br \/>\ntemp = get_temp()<br \/>\nFile \"mpu-6050_2.py\", line 52, in get_temp<br \/>\ntemp = read_word_sensor(TEMP_OUT)<br \/>\nFile \"mpu-6050_2.py\", line 43, in read_word_sensor<br \/>\nval = read_word(adr)<br \/>\nFile \"mpu-6050_2.py\", line 37, in read_word<br \/>\nhigh = bus.read_byte_data(DEV_ADDR, adr)<br \/>\nIOError: [Errno 121] Remote I\/O error<\/code><\/p>\n<div>\u3069\u3046\u3084\u3089\u3001\u3053\u306e\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5\u30fc\u306f\u3001\u52d5\u304d\u304c\u3042\u308b\u3068\u304d\u3060\u3051\u30c7\u30fc\u30bf\u304c\u8aad\u3081\u308b\u4ed5\u7d44\u307f\u306e\u3088\u3046\u3067\u3059\u3002INT\u306e\u4fe1\u53f7\u3067\u5272\u8fbc\u307f\u3092\u304b\u3051\u305f\u3089\u3001\u9023\u7d9a\u7684\u306b\u8aad\u3081\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u3057\u304b\u3057\u3001\u6025\u6fc0\u306a\u52d5\u304d\u306b\u8ffd\u5f93\u3057\u3066\u3044\u306a\u3044\u3088\u3046\u306a\u6c17\u304c\u3057\u307e\u3059\u3002<\/div>\n<p><code><br \/>\n#!\/usr\/bin\/python<br \/>\n# -*- coding: utf-8 -*<\/p>\n<p># import module<br \/>\nimport smbus # use I2C<br \/>\nimport math # mathmatics<br \/>\nfrom time import sleep # time module<br \/>\nimport RPi.GPIO as GPIO<br \/>\nGPIO.setmode(GPIO.BCM)<br \/>\n#<br \/>\n# define<br \/>\n#<br \/>\n# slave address<br \/>\nDEV_ADDR = 0x68 # device address<br \/>\n# int GPI<br \/>\npin=6<\/p>\n<p># register address<br \/>\nACCEL_XOUT = 0x3b<br \/>\nACCEL_YOUT = 0x3d<br \/>\nACCEL_ZOUT = 0x3f<br \/>\nTEMP_OUT = 0x41<br \/>\nGYRO_XOUT = 0x43<br \/>\nGYRO_YOUT = 0x45<br \/>\nGYRO_ZOUT = 0x47<br \/>\nPWR_MGMT_1 = 0x6b # PWR_MGMT_1<br \/>\nPWR_MGMT_2 = 0x6c # PWR_MGMT_2<\/p>\n<p>bus = smbus.SMBus(1)<\/p>\n<p># Sleep\u89e3\u9664. \u5148\u305a\u3001\u3053\u3053\u3067\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u3002wakeup\u306f\u5225\u306b\u3057\u306a\u304f\u3066\u3082\u52d5\u4f5c\u3059\u308b\u3002<\/p>\n<p>try:<br \/>\nbus.write_byte_data(DEV_ADDR, PWR_MGMT_1, 0)<br \/>\nexcept:<br \/>\npass<\/p>\n<p>#<br \/>\n# Sub function<br \/>\n#<br \/>\n# 1byte read<br \/>\ndef read_byte(adr):<br \/>\nreturn bus.read_byte_data(DEV_ADDR, adr)<br \/>\n# 2byte read<br \/>\ndef read_word(adr):<br \/>\nhigh = bus.read_byte_data(DEV_ADDR, adr)<br \/>\nlow = bus.read_byte_data(DEV_ADDR, adr+1)<br \/>\nval = (high &lt;&lt; 8) + low return val # Sensor data read def read_word_sensor(adr): val = read_word(adr) if (val &gt;= 0x8000): # minus<br \/>\nreturn -((65535 - val) + 1)<br \/>\nelse: # plus<br \/>\nreturn val<br \/>\n#<br \/>\n# \u6e29\u5ea6<br \/>\n#<br \/>\ndef get_temp():<br \/>\ntemp = read_word_sensor(TEMP_OUT)<br \/>\nx = temp \/ 340 + 36.53 # data sheet(register map)\u8a18\u8f09\u306e\u8a08\u7b97\u5f0f.<br \/>\nreturn x<\/p>\n<p>#<br \/>\n# \u89d2\u901f\u5ea6(full scale range \u00b1250 deg\/s<br \/>\n# LSB sensitivity 131 LSB\/deg\/s<br \/>\n# -&gt; \u00b1250 x 131 = \u00b132750 LSB[16bit\u3067\u8868\u73fe])<br \/>\n# Gyroscope Configuration GYRO_CONFIG (reg=0x1B)<br \/>\n# FS_SEL(Bit4-Bit3)\u3067full scale range\/LSB sensitivity\u306e\u5909\u66f4\u53ef.<br \/>\n#<br \/>\n# get gyro data<br \/>\ndef get_gyro_data_lsb():<br \/>\nx = read_word_sensor(GYRO_XOUT)<br \/>\ny = read_word_sensor(GYRO_YOUT)<br \/>\nz = read_word_sensor(GYRO_ZOUT)<br \/>\nreturn [x, y, z]<br \/>\ndef get_gyro_data_deg():<br \/>\nx,y,z = get_gyro_data_lsb()<br \/>\nx = x \/ 131.0<br \/>\ny = y \/ 131.0<br \/>\nz = z \/ 131.0<br \/>\nreturn [x, y, z]<\/p>\n<p>#<br \/>\n# \u52a0\u901f\u5ea6(full scale range \u00b12g<br \/>\n# LSB sensitivity 16384 LSB\/g)<br \/>\n# -&gt; \u00b12 x 16384 = \u00b132768 LSB[16bit\u3067\u8868\u73fe])<br \/>\n# Accelerometer Configuration ACCEL_CONFIG (reg=0x1C)<br \/>\n# AFS_SEL(Bit4-Bit3)\u3067full scale range\/LSB sensitivity\u306e\u5909\u66f4\u53ef.<br \/>\n#<br \/>\n# get accel data<br \/>\ndef get_accel_data_lsb():<br \/>\nx = read_word_sensor(ACCEL_XOUT)<br \/>\ny = read_word_sensor(ACCEL_YOUT)<br \/>\nz = read_word_sensor(ACCEL_ZOUT)<br \/>\nreturn [x, y, z]<br \/>\n# get accel data<br \/>\ndef get_accel_data_g():<br \/>\nx,y,z = get_accel_data_lsb()<br \/>\nx = x \/ 16384.0<br \/>\ny = y \/ 16384.0<br \/>\nz = z \/ 16384.0<br \/>\nreturn [x, y, z]<\/p>\n<p>def callBackTest(channel):<br \/>\ntry:<br \/>\n# \u6e29\u5ea6.<br \/>\ntemp = get_temp()<br \/>\n# \u89d2\u901f\u5ea6.<br \/>\ngyro_x,gyro_y,gyro_z = get_gyro_data_deg()<br \/>\n# \u52a0\u901f\u5ea6<br \/>\naccel_x,accel_y,accel_z = get_accel_data_g()<\/p>\n<p># \u5c0f\u6570\u70b9\u4ee5\u4e0b\u7b2c1\u4f4d\u307e\u3067\u8868\u793a.<br \/>\nprint 'temp:',<br \/>\nprint '\nprint '||',<br \/>\n# \u5c0f\u6570\u70b9\u4ee5\u4e0b\u7b2c3\u4f4d\u307e\u3067\u8868\u793a.<br \/>\nprint 'gyro[deg\/s]',<br \/>\nprint 'x: \nprint 'y: \nprint 'z: \nprint '||',<br \/>\n# \u5c0f\u6570\u70b9\u4ee5\u4e0b\u7b2c3\u4f4d\u307e\u3067\u8868\u793a.<br \/>\nprint 'accel[g]',<br \/>\nprint 'x: \nprint 'y: \nprint 'z: \nprint('') # \u6539\u884c.<\/p>\n<p>except:<br \/>\npass<\/p>\n<p>#<br \/>\n# Main function<br \/>\n#<br \/>\nGPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)<br \/>\nGPIO.add_event_detect(pin, GPIO.FALLING, callback=callBackTest, bouncetime=100)<\/p>\n<p>try:<br \/>\nwhile(True):<br \/>\nsleep(1)<\/p>\n<p>except KeyboardInterrupt:<br \/>\nprint \"break\"<br \/>\nGPIO.cleanup()<\/p>\n<p>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<br \/>\ntemp: 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<br \/>\ntemp: 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<br \/>\ntemp: 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<br \/>\ntemp: 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<br \/>\ntemp: 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<br \/>\ntemp: 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<br \/>\ntemp: 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<br \/>\ntemp: 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<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u306f\u3001\u52d5\u4f5c\u3057\u305f\u3068\u3044\u3046\u3053\u3068\u3067\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uff13\u8ef8\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5\u30fc\u3000 MPU6050\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002I2C\u306e\u30bb\u30f3\u30b5\u30fc\u3067\u3059\u304c\u3001\u3061\u3087\u3063\u3068\u7656\u304c\u3042\u308a\u305d\u3046\u3067\u3059\u3002\u7d50\u7dda\u306f\u3001\u666e\u901a\u306bI2C\u306b\u3064\u306a\u304e\u307e\u3057\u305f\u3002\u30a2\u30c9\u30ec\u30b9\u306f\u30010x68\u306b\u306a\u3063\u3066\u3044\u307e\u3057\u305f\u3002 Github\u306b\u3042\u3063\u305fPython\u30b3\u30fc\u30c9\u306f&#8230;<\/p>\n<p><a class=\"more\" href=\"https:\/\/research.itplants.com\/?p=1805\"> Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":1814,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts\/1805"}],"collection":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1805"}],"version-history":[{"count":10,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts\/1805\/revisions"}],"predecessor-version":[{"id":1829,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts\/1805\/revisions\/1829"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/media\/1814"}],"wp:attachment":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}