こんにちは。岩本です。今週もマウスやっていきます。
今回はジャイロを使ってみたいと思います。
今回の追加購入部品
今回購入したジャイロはMPU9250/6500です。

参考サイト
今回参考にしたのは以下のサイトです。
SPIとI2Cは何が違うのか
どちらも、デバイス同士を接続するのによく利用される同期式シリアル通信の一つです。
SPI通信はI2Cと比較して通信速度は速いが4本の信号線が必要で、I2C通信は速度は劣るが2本の信号線ですべてのデータの送受信を行える。
ESP32に接続できるピンに限りがあるので、基本的には、I2Cを使ってみようかと思っています。通信速度がダメそうなら、別途検討します。
ジャイロの動作確認
SPI通信でのチェック方法は、shotaのマイクロマウス研修11[ESP32でMPU9250とSPI通信する]で記載されているので割愛します。
今回は、GitHub_I2C参考のサイトを参考にしてジャイロの値をターミナルに表示させたいと思います。
まずは、GitHub_I2C参考のmain.cとcomponentsフォルダをコピーします。
次にsdkconfigの中に”CONFIG_SAMPLE_RATE_Hz=200”を追記します。
ついでに、mpu9250.cの中にピン配置の設定箇所があったので、ピン設定を変えてみました。
/***************************************************************************** * * * Copyright 2018 Simon M. Werner * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * * * *****************************************************************************/ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "i2c-easy.h" #include "mpu9250.h" #include "ak8963.h" #define I2C_MASTER_SCL_IO 14 //22 /*!< gpio number for I2C master clock */ #define I2C_MASTER_SDA_IO 13 //21 /*!< gpio number for I2C master data */ #define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */
配線
注意事項(monitor_speed)
Platformioでmonitorを使うときの注意事項ですがplatformio.iniに”monitor_speed = 115200”を追記しないと、表示される文字化けします。
; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html [env:esp32dev] platform = espressif32 board = esp32dev framework = espidf monitor_speed = 115200
結果
出来たプログラムをアップロードしてモニターすると、以下のように角度っぽい数字が出てくるので、これを使っていきたいと思います。
下の参考はジャイロをぐるぐる回しているので数字が結構変わっているのが分かると思います。
?[0;32mI (28127) main: heading: 116.050?, pitch: 10.924?, roll: -92.052?, Temp 29.434?C?[0m ?[0;32mI (28147) main: heading: 116.458?, pitch: 7.414?, roll: -87.190?, Temp 29.291?C?[0m ?[0;32mI (28177) main: heading: 116.482?, pitch: 3.250?, roll: -80.802?, Temp 29.291?C?[0m ?[0;32mI (28197) main: heading: 115.935?, pitch: -0.613?, roll: -74.481?, Temp 29.530?C?[0m ?[0;32mI (28217) main: heading: 114.540?, pitch: -5.753?, roll: -67.776?, Temp 29.195?C?[0m ?[0;32mI (28247) main: heading: 112.625?, pitch: -10.387?, roll: -60.422?, Temp 29.195?C?[0m ?[0;32mI (28267) main: heading: 110.305?, pitch: -13.156?, roll: -53.357?, Temp 29.339?C?[0m ?[0;32mI (28287) main: heading: 107.687?, pitch: -13.524?, roll: -49.375?, Temp 29.434?C?[0m ?[0;32mI (28317) main: heading: 105.218?, pitch: -12.556?, roll: -49.070?, Temp 29.195?C?[0m
今回は以上!

