{"id":305,"date":"2021-08-06T09:04:44","date_gmt":"2021-08-06T00:04:44","guid":{"rendered":"https:\/\/rfsec.ddns.net\/db\/?p=305"},"modified":"2021-08-06T11:37:14","modified_gmt":"2021-08-06T02:37:14","slug":"%e7%a3%81%e6%b0%97%e3%82%b3%e3%83%b3%e3%83%91%e3%82%b9%e8%a3%9c%e6%ad%a3%e3%83%87%e3%83%bc%e3%82%bf%e3%81%ae%e5%8f%af%e8%a6%96%e5%8c%96","status":"publish","type":"post","link":"https:\/\/rfsec.ddns.net\/db\/?p=305","title":{"rendered":"\u78c1\u6c17\u30b3\u30f3\u30d1\u30b9\u88dc\u6b63\u30c7\u30fc\u30bf\u306e\u53ef\u8996\u5316"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\uff19\u8ef8\u30bb\u30f3\u30b5\u30fcBMX055\u306e\u78c1\u6c17\u30b3\u30f3\u30d1\u30b9\u306e\u30c7\u30fc\u30bf(x,y,z)\u3092\u53d6\u5f97\u3057\u3066\u53ef\u8996\u5316\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u30bb\u30f3\u30b5\u30fc\u306fi2c\u3067Raspberry Pi\u306b\u63a5\u7d9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">jupyter notebook\u3067\uff13D\u30d7\u30ed\u30c3\u30c8\u3057\u305f\u69d8\u5b50<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8d64\u306e\u70b9\u306f\u3001x,y,z\u306e\u305d\u308c\u305e\u308c\u306e\u5e73\u5747\u5024\u3092\u793a\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"720\" src=\"https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/08\/compass-offset.png\" alt=\"\" class=\"wp-image-306\" srcset=\"https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/08\/compass-offset.png 960w, https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/08\/compass-offset-300x225.png 300w, https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/08\/compass-offset-768x576.png 768w, https:\/\/rfsec.ddns.net\/db\/wp-content\/uploads\/2021\/08\/compass-offset-624x468.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">BMX055\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code> -*- coding: utf-8 -*-\n#\n# https:\/\/taku-info.com\/bmx055howtouse-mag\/\n#\n\nfrom smbus import SMBus\nimport time\nimport math\nimport datetime\nimport csv\n\n# I2C\nACCL_ADDR = 0x19\nACCL_R_ADDR = 0x02\nGYRO_ADDR = 0x69\nGYRO_R_ADDR = 0x02\nMAG_ADDR = 0x13\nMAG_R_ADDR = 0x42\n\ni2c = SMBus(1)\n\ndef bmx_setup():\n    # acc_data_setup : \u52a0\u901f\u5ea6\u306e\u5024\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\n    i2c.write_byte_data(ACCL_ADDR, 0x0F, 0x03)\n    i2c.write_byte_data(ACCL_ADDR, 0x10, 0x08)\n    i2c.write_byte_data(ACCL_ADDR, 0x11, 0x00)\n    time.sleep(0.5)\n\n    # gyr_data_setup : \u30b8\u30e3\u30a4\u30ed\u5024\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\n    i2c.write_byte_data(GYRO_ADDR, 0x0F, 0x04)\n    i2c.write_byte_data(GYRO_ADDR, 0x10, 0x07)\n    i2c.write_byte_data(GYRO_ADDR, 0x11, 0x00)\n    time.sleep(0.5)\n\n    # mag_data_setup : \u5730\u78c1\u6c17\u5024\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\n    data = i2c.read_byte_data(MAG_ADDR, 0x4B)\n    if(data == 0):\n        i2c.write_byte_data(MAG_ADDR, 0x4B, 0x83)\n        time.sleep(0.5)\n    i2c.write_byte_data(MAG_ADDR, 0x4B, 0x01)\n    i2c.write_byte_data(MAG_ADDR, 0x4C, 0x00)\n    i2c.write_byte_data(MAG_ADDR, 0x4E, 0x84)\n    i2c.write_byte_data(MAG_ADDR, 0x51, 0x04)\n    i2c.write_byte_data(MAG_ADDR, 0x52, 0x16)\n    time.sleep(0.5)\n\ndef acc_value():\n    data = [0, 0, 0, 0, 0, 0]\n    acc_data = [0.0, 0.0, 0.0]\n\n    try:\n        for i in range(6):\n            data[i] = i2c.read_byte_data(ACCL_ADDR, ACCL_R_ADDR + i)\n\n        for i in range(3):\n            acc_data[i] = ((data[2*i + 1] * 256) + int(data[2*i] & 0xF0)) \/ 16\n            if acc_data[i] &gt; 2047:\n                acc_data[i] -= 4096\n            acc_data[i] *= 0.0098\n\n    except IOError as e:\n        print(&quot;I\/O error({0}): {1}&quot;.format(e.errno, e.strerror))\n\n    return acc_data\n\ndef gyro_value():\n    data = [0, 0, 0, 0, 0, 0]\n    gyro_data = [0.0, 0.0, 0.0]\n\n    try:\n        for i in range(6):\n            data[i] = i2c.read_byte_data(GYRO_ADDR, GYRO_R_ADDR + i)\n\n        for i in range(3):\n            gyro_data[i] = (data[2*i + 1] * 256) + data[2*i]\n            if gyro_data[i] &gt; 32767:\n                gyro_data[i] -= 65536\n            gyro_data[i] *= 0.0038\n\n    except IOError as e:\n        print(&quot;I\/O error({0}): {1}&quot;.format(e.errno, e.strerror))\n\n    return gyro_data\n\ndef mag_value():\n    data = [0, 0, 0, 0, 0, 0, 0, 0]\n    mag_data = [0.0, 0.0, 0.0]\n\n    try:\n        for i in range(8):\n            data[i] = i2c.read_byte_data(MAG_ADDR, MAG_R_ADDR + i)\n\n        for i in range(3):\n            if i != 2:\n                mag_data[i] = ((data[2*i + 1] * 256) + (data[2*i] & 0xF8)) \/ 8\n                if mag_data[i] &gt; 4095:\n                    mag_data[i] -= 8192\n            else:\n                mag_data[i] = ((data[2*i + 1] * 256) + (data[2*i] & 0xFE)) \/ 2\n                if mag_data[i] &gt; 16383:\n                    mag_data[i] -= 32768\n\n    except IOError as e:\n        print(&quot;I\/O error({0}): {1}&quot;.format(e.errno, e.strerror))\n\n    return mag_data\n\nif __name__ == &quot;__main__&quot;:\n\n    bmx_setup()\n    time.sleep(0.1)\n    now_time = datetime.datetime.now()\n    filename = &#39;test_&#39; + now_time.strftime(&#39;%Y%m%d_%H%M%S&#39;) + &#39;.csv&#39;\n    # \u30d5\u30a1\u30a4\u30eb\uff0c1\u884c\u76ee(\u30ab\u30e9\u30e0)\u306e\u4f5c\u6210\n    with open(filename, &#39;a&#39;) as f:\n        writer = csv.writer(f)\n        writer.writerow([&#39;Mag_x&#39;, &#39;Mag_y&#39;, &#39;Mag_z&#39;])\n    while True:\n        #acc = acc_value()\n        #gyro= gyro_value()\n        mag = mag_value()\n        theta = math.atan2(mag[1],mag[0]) * 180.0 \/ 3.141592\n        if ( theta &lt; 0 ):\n            theta = theta + 360.0\n        &#39;&#39;&#39;\n        theta = 360.0 - theta\n        print(&quot;Accl -&gt; x:{}, y:{}, z: {}&quot;.format(acc[0], acc[1], acc[2]))\n        print(&quot;Gyro -&gt; x:{}, y:{}, z: {}&quot;.format(gyro[0], gyro[1], gyro[2]))\n        print(&quot;Mag -&gt; x:{}, y:{}, z: {}&quot;.format(mag[0], mag[1], mag[2]))\n        &#39;&#39;&#39;\n        print(theta)\n        time.sleep(0.02)\n        with open(filename, &#39;a&#39;, newline=&quot;&quot;) as f:\n            writer = csv.writer(f)\n            writer.writerow([mag[0], mag[1], mag[2]])<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092jupyter notebook\u3067\u53ef\u8996\u5316<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>%matplotlib nbagg\n\nimport os\nimport pandas as pd\nfrom mpl_toolkits.mplot3d import Axes3D\nimport matplotlib.pyplot as plt\nimport math\nimport numpy as np\n\n### \u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\ndf = pd.read_csv(&#39;BMX055\/data4.csv&#39;)\nprint(df)\na_x=np.average(df[&#39;Mag_x&#39;])\na_y=np.average(df[&#39;Mag_y&#39;])\na_z=np.average(df[&#39;Mag_z&#39;])\nprint(round(a_x,2),round(a_y,2),round(a_z,2))\n# \u3053\u3053\u304b\u3089\u30b0\u30e9\u30d5\u63cf\u753b\n \n# \u30b0\u30e9\u30d5\u306e\u5165\u308c\u7269\u3092\u7528\u610f\u3059\u308b\u3002\nfig = plt.figure()\n#ax = Axes3D(fig)\u3000\u3000\u3000\u3000&lt;--- warning\u5bfe\u7b56\nax = fig.add_subplot(111, projection=&#39;3d&#39;) \n# \u8ef8\u306e\u30e9\u30d9\u30eb\u3092\u8a2d\u5b9a\u3059\u308b\u3002\nax.set_xlabel(&#39;x&#39;)\nax.set_ylabel(&#39;y&#39;)\nax.set_zlabel(&#39;z&#39;)\n\n# \u30b0\u30e9\u30d5\u3092\u8868\u793a\u3059\u308b\u3002\nax.scatter3D(df[&#39;Mag_x&#39;],df[&#39;Mag_y&#39;],df[&#39;Mag_z&#39;],color=&quot;blue&quot;)\nax.scatter3D(a_x,a_y,a_z,color=&quot;red&quot;)\nplt.show()<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uff19\u8ef8\u30bb\u30f3\u30b5\u30fcBMX055\u306e\u78c1\u6c17\u30b3\u30f3\u30d1\u30b9\u306e\u30c7\u30fc\u30bf(x,y,z)\u3092\u53d6\u5f97\u3057\u3066\u53ef\u8996\u5316\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u30bb\u30f3\u30b5\u30fc\u306fi2c\u3067Raspberry Pi\u306b\u63a5\u7d9a\u3057\u3066\u3044\u307e\u3059\u3002 jupyter notebook\u3067\uff13D\u30d7\u30ed\u30c3\u30c8\u3057\u305f\u69d8\u5b50 \u8d64\u306e\u70b9\u306f\u3001x [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"featured_image_src":null,"author_info":{"display_name":"mars","author_link":"https:\/\/rfsec.ddns.net\/db\/?author=1"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=305"}],"version-history":[{"count":3,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rfsec.ddns.net\/db\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}