初始化提交
This commit is contained in:
57
arduino-cli/libraries/MPU6050_ESP8266/MPU6050_ESP8266.cpp
Normal file
57
arduino-cli/libraries/MPU6050_ESP8266/MPU6050_ESP8266.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "MPU6050_ESP8266.h"
|
||||
|
||||
MPU6050_ESP8266::MPU6050_ESP8266(){
|
||||
Wire.begin();
|
||||
}
|
||||
|
||||
void MPU6050_ESP8266::I2C_Write(uint8_t regAddress, uint8_t data){
|
||||
Wire.beginTransmission(MPU6050SlaveAddress);
|
||||
Wire.write(regAddress);
|
||||
Wire.write(data);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
//初始化 MPU6050
|
||||
void MPU6050_ESP8266::begin(){
|
||||
I2C_Write(MPU6050_REGISTER_SMPLRT_DIV, 0x07);
|
||||
I2C_Write(MPU6050_REGISTER_PWR_MGMT_1, 0x01);
|
||||
I2C_Write(MPU6050_REGISTER_PWR_MGMT_2, 0x00);
|
||||
I2C_Write(MPU6050_REGISTER_CONFIG, 0x00);
|
||||
I2C_Write(MPU6050_REGISTER_GYRO_CONFIG, 0x00);//set +/-250 degree/second full scale
|
||||
I2C_Write(MPU6050_REGISTER_ACCEL_CONFIG, 0x00);// set +/- 2g full scale
|
||||
I2C_Write(MPU6050_REGISTER_FIFO_EN, 0x00);
|
||||
I2C_Write(MPU6050_REGISTER_INT_ENABLE, 0x01);
|
||||
I2C_Write(MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);
|
||||
I2C_Write(MPU6050_REGISTER_USER_CTRL, 0x00);
|
||||
}
|
||||
|
||||
// 更新数据
|
||||
void MPU6050_ESP8266::update(){
|
||||
Wire.beginTransmission(MPU6050SlaveAddress);
|
||||
Wire.write(MPU6050_REGISTER_ACCEL_XOUT_H);
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(MPU6050SlaveAddress, (uint8_t)14);
|
||||
|
||||
int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;
|
||||
|
||||
AccelX = (((int16_t)Wire.read()<<8) | Wire.read());
|
||||
AccelY = (((int16_t)Wire.read()<<8) | Wire.read());
|
||||
AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());
|
||||
Temperature = (((int16_t)Wire.read()<<8) | Wire.read());
|
||||
GyroX = (((int16_t)Wire.read()<<8) | Wire.read());
|
||||
GyroY = (((int16_t)Wire.read()<<8) | Wire.read());
|
||||
GyroZ = (((int16_t)Wire.read()<<8) | Wire.read());
|
||||
|
||||
angleAccX = (double)AccelX/AccelScaleFactor*100;
|
||||
angleAccY = (double)AccelY/AccelScaleFactor*100;
|
||||
angleZ = (double)AccelZ/AccelScaleFactor*100;
|
||||
temp = (double)Temperature/340+36.53; //温度公式
|
||||
accX = (double)GyroX/GyroScaleFactor;
|
||||
accY = (double)GyroY/GyroScaleFactor;
|
||||
accZ = (double)GyroZ/GyroScaleFactor;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
46
arduino-cli/libraries/MPU6050_ESP8266/MPU6050_ESP8266.h
Normal file
46
arduino-cli/libraries/MPU6050_ESP8266/MPU6050_ESP8266.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef MPU6050_ESP8266_H
|
||||
#define MPU6050_ESP8266_H
|
||||
|
||||
#include "Arduino.h"
|
||||
#include <Wire.h>
|
||||
|
||||
|
||||
// MPU6050 配置寄存器地址
|
||||
#define MPU6050_REGISTER_SMPLRT_DIV 0x19
|
||||
#define MPU6050_REGISTER_USER_CTRL 0x6A
|
||||
#define MPU6050_REGISTER_PWR_MGMT_1 0x6B
|
||||
#define MPU6050_REGISTER_PWR_MGMT_2 0x6C
|
||||
#define MPU6050_REGISTER_CONFIG 0x1A
|
||||
#define MPU6050_REGISTER_GYRO_CONFIG 0x1B
|
||||
#define MPU6050_REGISTER_ACCEL_CONFIG 0x1C
|
||||
#define MPU6050_REGISTER_FIFO_EN 0x23
|
||||
#define MPU6050_REGISTER_INT_ENABLE 0x38
|
||||
#define MPU6050_REGISTER_ACCEL_XOUT_H 0x3B
|
||||
#define MPU6050_REGISTER_SIGNAL_PATH_RESET 0x68
|
||||
|
||||
// 数据表中提供的灵敏度比例系数分别为满刻度设置
|
||||
#define AccelScaleFactor 16384
|
||||
#define GyroScaleFactor 131
|
||||
#define MPU6050SlaveAddress 0x68
|
||||
|
||||
class MPU6050_ESP8266{
|
||||
public:
|
||||
MPU6050_ESP8266();
|
||||
float getTemp(){ return temp; };
|
||||
float getAccX(){ return accX; };
|
||||
float getAccY(){ return accY; };
|
||||
float getAccZ(){ return accZ; };
|
||||
float getAccAngleX(){ return angleAccX; };
|
||||
float getAccAngleY(){ return angleAccY; };
|
||||
float getAngleZ(){ return angleZ; };
|
||||
|
||||
void begin();
|
||||
void I2C_Write(uint8_t regAddress, uint8_t data);
|
||||
void update();
|
||||
|
||||
private:
|
||||
float temp, accX, accY, accZ, angleAccX, angleAccY, angleZ;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user