項目(一)ES32獲取mpu9250數據網頁交互顯示

 

教程 https://www.hackster.io/donowak/esp32-mpu9250-3d-orientation-visualisation-467dc1html

項目地址 https://github.com/DominikN/ESP32-MPU9250-web-view/blob/master/html.hgit

硬件地址github

ESP32 <-> MPU9250 P22 <-> SCL P21 <-> SDA P19 <-> INT GND <-> GND

 版型1(中國深圳常買到)web

版型2(日本開發板)json

https://www.switch-science.com/catalog/3210ide

電路圖oop

https://s3-ap-northeast-1.amazonaws.com/switch-science.public/schematic/ESPr_Developer_32/ESPr_Developer_32.pdfui

 

軟件spa

配置Arduino IDE

要運行該項目,首先須要配置Arduino IDE:3d

1.爲ESP32安裝Husarnet軟件包:

  • 打開 File -> Preferences
  • 在字段中,其餘Board Manager URL 添加如下連接:https://files.husarion.com/arduino/package_esp32_index.json
  • 打開 Tools -> Board: ... -> Boards Manager ...
  • 搜索 esp32-husarnet by Husarion
  • 單擊安裝按鈕

2.選擇ESP32開發板:

  • 打開 Tools -> Board
  • 選擇「ESP32 Arduino(Husarnet)」部分下的ESP32開發模塊

3.安裝ArduinoJson庫:(可不安裝)

  • 打開 Tools -> Manage Libraries...
  • 搜索 ArduinoJson
  • 選擇版本 5.13.3
  • 單擊安裝按鈕

4.安裝arduinoWebSockets庫(Husarnet fork):(可不安裝)

  • 下載https://github.com/husarnet/arduinoWebSockets 做爲ZIP文件(這是由Links2004(Markus)提供的arduinoWebSockets的Husarnet兼容分支)
  • 打開Sketch -> Include Library -> Add .ZIP Library ...選擇剛下載的arduinoWebSockets-master.zip 文件,而後單擊打開按鈕

5.安裝SparkFun_MPU-9250-DMP_Arduino_Library:(必須安裝)

基本讀取示例

 

 

/************************************************************
MPU9250_Basic
 Basic example sketch for MPU-9250 DMP Arduino Library 
Jim Lindblom @ SparkFun Electronics
original creation date: November 23, 2016
https://github.com/sparkfun/SparkFun_MPU9250_DMP_Arduino_Library

This example sketch demonstrates how to initialize the 
MPU-9250, and stream its sensor outputs to a serial monitor.

Development environment specifics:
Arduino IDE 1.6.12
SparkFun 9DoF Razor IMU M0

Supported Platforms:
- ATSAMD21 (Arduino Zero, SparkFun SAMD21 Breakouts)
*************************************************************/
#include <SparkFunMPU9250-DMP.h>

#define SerialPort Serial

MPU9250_DMP imu;

void setup() 
{
  SerialPort.begin(115200);

  // Call imu.begin() to verify communication with and
  // initialize the MPU-9250 to it's default values.
  // Most functions return an error code - INV_SUCCESS (0)
  // indicates the IMU was present and successfully set up
  if (imu.begin() != INV_SUCCESS)
  {
    while (1)
    {
      SerialPort.println("Unable to communicate with MPU-9250");
      SerialPort.println("Check connections, and try again.");
      SerialPort.println();
      delay(5000);
    }
  }

  // Use setSensors to turn on or off MPU-9250 sensors.
  // Any of the following defines can be combined:
  // INV_XYZ_GYRO, INV_XYZ_ACCEL, INV_XYZ_COMPASS,
  // INV_X_GYRO, INV_Y_GYRO, or INV_Z_GYRO
  // Enable all sensors:
  imu.setSensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);

  // Use setGyroFSR() and setAccelFSR() to configure the
  // gyroscope and accelerometer full scale ranges.
  // Gyro options are +/- 250, 500, 1000, or 2000 dps
  imu.setGyroFSR(2000); // Set gyro to 2000 dps
  // Accel options are +/- 2, 4, 8, or 16 g
  imu.setAccelFSR(2); // Set accel to +/-2g
  // Note: the MPU-9250's magnetometer FSR is set at 
  // +/- 4912 uT (micro-tesla's)

  // setLPF() can be used to set the digital low-pass filter
  // of the accelerometer and gyroscope.
  // Can be any of the following: 188, 98, 42, 20, 10, 5
  // (values are in Hz).
  imu.setLPF(5); // Set LPF corner frequency to 5Hz

  // The sample rate of the accel/gyro can be set using
  // setSampleRate. Acceptable values range from 4Hz to 1kHz
  imu.setSampleRate(10); // Set sample rate to 10Hz

  // Likewise, the compass (magnetometer) sample rate can be
  // set using the setCompassSampleRate() function.
  // This value can range between: 1-100Hz
  imu.setCompassSampleRate(10); // Set mag rate to 10Hz
}

void loop() 
{
  // dataReady() checks to see if new accel/gyro data
  // is available. It will return a boolean true or false
  // (New magnetometer data cannot be checked, as the library
  //  runs that sensor in single-conversion mode.)
  if ( imu.dataReady() )
  {
    // Call update() to update the imu objects sensor data.
    // You can specify which sensors to update by combining
    // UPDATE_ACCEL, UPDATE_GYRO, UPDATE_COMPASS, and/or
    // UPDATE_TEMPERATURE.
    // (The update function defaults to accel, gyro, compass,
    //  so you don't have to specify these values.)
    imu.update(UPDATE_ACCEL | UPDATE_GYRO | UPDATE_COMPASS);
    printIMUData();
  }
}

void printIMUData(void)
{  
  // After calling update() the ax, ay, az, gx, gy, gz, mx,
  // my, mz, time, and/or temerature class variables are all
  // updated. Access them by placing the object. in front:

  // Use the calcAccel, calcGyro, and calcMag functions to
  // convert the raw sensor readings (signed 16-bit values)
  // to their respective units.
  float accelX = imu.calcAccel(imu.ax);
  float accelY = imu.calcAccel(imu.ay);
  float accelZ = imu.calcAccel(imu.az);
  float gyroX = imu.calcGyro(imu.gx);
  float gyroY = imu.calcGyro(imu.gy);
  float gyroZ = imu.calcGyro(imu.gz);
  float magX = imu.calcMag(imu.mx);
  float magY = imu.calcMag(imu.my);
  float magZ = imu.calcMag(imu.mz);
  
  SerialPort.println("Accel: " + String(accelX) + ", " +
              String(accelY) + ", " + String(accelZ) + " g");
  SerialPort.println("Gyro: " + String(gyroX) + ", " +
              String(gyroY) + ", " + String(gyroZ) + " dps");
  SerialPort.println("Mag: " + String(magX) + ", " +
              String(magY) + ", " + String(magZ) + " uT");
  SerialPort.println("Time: " + String(imu.time) + " ms");
  SerialPort.println();
}

  

更多程序

 http上傳程序

相關文章
相關標籤/搜索