.. note:: こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。 **参加する理由は?** - **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。 - **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。 - **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。 - **特別割引**:最新製品の独占割引をお楽しみください。 - **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。 👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう! .. _2.2.9_py: 2.2.9 MPU6050モジュール ======================= はじめに --------- MPU-6050は、世界初かつ唯一の6軸モーショントラッキングデバイス(3軸ジャイロスコープおよび3軸加速度計)です。スマートフォン、タブレット、ウェアラブルセンサーに必要な低消費電力、低コスト、高性能な特性を備えています。 この実験では、I2Cを使用してMPU6050の三軸加速度センサーと三軸ジャイロスコープの値を取得し、それらを画面に表示します。 必要な部品 ----------------------- このプロジェクトには、以下のコンポーネントが必要です。 .. image:: ../img/list_2.2.6.png 一式を購入するのが非常に便利です、リンクはこちら: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - 名前 - このキットのアイテム - リンク * - Raphael Kit - 337 - |link_Raphael_kit| 以下のリンクから個別にも購入できます。 .. list-table:: :widths: 30 20 :header-rows: 1 * - コンポーネントの紹介 - 購入リンク * - :ref:`cpn_gpio_board` - |link_gpio_board_buy| * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_mpu6050` - |link_mpu6050_buy| 回路図 ------------- MPU6050はI2Cバスインターフェースを介してマイクロコントローラと通信します。SDA1とSCL1は対応するピンに接続する必要があります。 .. image:: ../img/image330.png 実験手順 --------------- **ステップ1:** 回路を組む。 .. image:: ../img/image227.png **ステップ2:** I2Cの設定を行う(詳細は付録 :ref:`i2c_config` を参照。既に設定している場合はこのステップをスキップします。) **ステップ3:** コードのフォルダに移動する。 .. raw:: html .. code-block:: cd ~/raphael-kit/python **ステップ4:** 実行可能なファイルを実行する。 .. raw:: html .. code-block:: sudo python3 2.2.9_mpu6050.py コードを実行すると、MPU6050で読み取った各軸の加速度、角速度、およびx軸とy軸の偏角が計算された後に画面に表示されます。 .. note:: * エラー ``FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-1'`` が出た場合は、 :ref:`i2c_config` を参照してI2Cを有効にしてください。 * エラー ``ModuleNotFoundError: No module named 'smbus2'`` が出た場合は、 ``sudo apt install python3-smbus2`` を実行してください。 * エラー ``OSError: [Errno 121] Remote I/O error`` が出た場合、モジュールの配線が間違っているか、モジュール自体が壊れています。 **コード** .. note:: コードは以下で **編集/リセット/コピー/実行/停止** できます。ただし、それを行う前にソースコードのパスに移動する必要があります。例: ``raphael-kit/python`` 。コードを変更した後、直接実行して効果を確認できます。 .. raw:: html .. code-block:: python import smbus import math import time # 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(1) # 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) while True: time.sleep(0.1) 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)) 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)) time.sleep(1) **コード説明** .. code-block:: python 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 MPU6050から送られてくるセンサデータを読み込む。 .. code-block:: python def get_y_rotation(x,y,z): radians = math.atan2(x, dist(y,z)) return -math.degrees(radians) Y軸のたわみ角を計算する。 .. code-block:: python def get_x_rotation(x,y,z): radians = math.atan2(y, dist(x,z)) return math.degrees(radians) X軸のたわみ角を計算する。 .. code-block:: python 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)) 加速度センサー上のx軸、y軸、z軸の値を読み取り、それらの要素を加速度値(重力単位)に変換して印刷します。 .. code-block:: python 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) 加速度センサー上のx軸、y軸、z軸の値を読み取り、それらの要素を加速度値(重力単位)に変換して印刷します。 .. code-block:: python 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)) x軸およびy軸の偏角を印刷します。 現象の画像 ------------- .. image:: ../img/image228.jpeg