I2C extended MPU6886 to also support MPU9250 (found in Legacy M5Stack Fire)

This commit is contained in:
Stephan Hadinger 2021-06-11 13:27:34 +02:00
parent 8693f5b0a8
commit 1c5bcd6dad
9 changed files with 108 additions and 86 deletions

View File

@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
- Command ``Timers`` layout of JSON message changed to single line
- Command ``Gpio`` layout of JSON message changed to single line
- Command ``Modules`` layout of JSON message changed to single line
- I2C extended MPU6886 to also support MPU9250 (found in Legacy M5Stack Fire)
## [9.4.0.4]
### Added

View File

@ -91,5 +91,5 @@ Index | Define | Driver | Device | Address(es) | Description
55 | USE_EZOPMP | xsns_78 | EZOPMP | 0x61 - 0x70 | Peristaltic Pump
56 | USE_SEESAW_SOIL | xsns_81 | SEESOIL | 0x36 - 0x39 | Adafruit seesaw soil moisture sensor
57 | USE_TOF10120 | xsns_84 | TOF10120 | 0x52 | Time-of-flight (ToF) distance sensor
58 | USE_MPU6886 | xsns_85 | MPU6886 | 0x68 | MPU6886 M5Stack
58 | USE_MPU_ACCEL | xsns_85 | MPU_ACCEL| 0x68 | MPU6886/MPU9250 6-axis MotionTracking sensor from M5Stack
59 | USE_BM8563 | xdrv_56 | BM8563 | 0x51 | BM8563 RTC from M5Stack

View File

@ -1,9 +1,9 @@
name=MPU6886
name=MPU_accel
version=
author=M5StickC
maintainer=Stephan Hadinger
sentence=Support for MPU6886
paragraph=Support for MPU6886
sentence=Support for MPU6886, MPU9250
paragraph=Support for MPU6886, MPU9250
category=
url=https://github.com/m5stack/M5StickC/blob/master/src/utility/
architectures=esp32,esp8266
architectures=*

View File

@ -1,8 +1,8 @@
#include "MPU6886.h"
#include "MPU_accel.h"
#include <math.h>
#include <Arduino.h>
void MPU6886::I2C_Read_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t number_Bytes, uint8_t *read_Buffer){
void MPU_accel::I2C_Read_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t number_Bytes, uint8_t *read_Buffer){
myWire->beginTransmission(driver_Addr);
myWire->write(start_Addr);
@ -16,7 +16,7 @@ void MPU6886::I2C_Read_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t n
}
}
void MPU6886::I2C_Write_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t number_Bytes, uint8_t *write_Buffer){
void MPU_accel::I2C_Write_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t number_Bytes, uint8_t *write_Buffer){
myWire->beginTransmission(driver_Addr);
myWire->write(start_Addr);
@ -25,13 +25,19 @@ void MPU6886::I2C_Write_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t
}
int MPU6886::Init(void){
int MPU_accel::Init(void){
unsigned char tempdata[1];
unsigned char regdata;
I2C_Read_NBytes(MPU6886_ADDRESS, MPU6886_WHOAMI, 1, tempdata);
if(tempdata[0] != 0x19)
const uint8_t whoami = tempdata[0];
if (whoami == 0x19) {
model = 6886; // MPU6886
} else if (whoami == 0x71) {
model = 9250; // MPU9250
} else {
return -1;
}
delay(1);
regdata = 0x00;
@ -91,7 +97,7 @@ int MPU6886::Init(void){
return 0;
}
void MPU6886::getAccelAdc(int16_t* ax, int16_t* ay, int16_t* az){
void MPU_accel::getAccelAdc(int16_t* ax, int16_t* ay, int16_t* az){
uint8_t buf[6];
I2C_Read_NBytes(MPU6886_ADDRESS,MPU6886_ACCEL_XOUT_H,6,buf);
@ -101,7 +107,7 @@ void MPU6886::getAccelAdc(int16_t* ax, int16_t* ay, int16_t* az){
*az=((int16_t)buf[4]<<8)|buf[5];
}
void MPU6886::getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz){
void MPU_accel::getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz){
uint8_t buf[6];
I2C_Read_NBytes(MPU6886_ADDRESS,MPU6886_GYRO_XOUT_H,6,buf);
@ -112,7 +118,7 @@ void MPU6886::getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz){
}
void MPU6886::getTempAdc(int16_t *t){
void MPU_accel::getTempAdc(int16_t *t){
uint8_t buf[2];
I2C_Read_NBytes(MPU6886_ADDRESS,MPU6886_TEMP_OUT_H,2,buf);
@ -123,7 +129,7 @@ void MPU6886::getTempAdc(int16_t *t){
// //!俯仰航向横滚pitchyawroll指三维空间中飞行器的旋转状态。
// void MPU6886::getAhrsData(float *pitch,float *roll,float *yaw){
// void MPU_accel::getAhrsData(float *pitch,float *roll,float *yaw){
// float accX = 0;
// float accY = 0;
@ -141,7 +147,7 @@ void MPU6886::getTempAdc(int16_t *t){
// }
void MPU6886::getGres(){
void MPU_accel::getGres(){
switch (Gyscale)
{
@ -167,7 +173,7 @@ void MPU6886::getGres(){
}
void MPU6886::getAres(){
void MPU_accel::getAres(){
switch (Acscale)
{
// Possible accelerometer scales (and their register bit settings) are:
@ -193,7 +199,7 @@ void MPU6886::getAres(){
}
void MPU6886::SetGyroFsr(Gscale scale)
void MPU_accel::SetGyroFsr(Gscale scale)
{
//return IIC_Write_Byte(MPU_GYRO_CFG_REG,scale<<3);//设置陀螺仪满量程范围
unsigned char regdata;
@ -205,7 +211,7 @@ void MPU6886::SetGyroFsr(Gscale scale)
getGres();
}
void MPU6886::SetAccelFsr(Ascale scale)
void MPU_accel::SetAccelFsr(Ascale scale)
{
unsigned char regdata;
regdata = (scale<<3);
@ -219,7 +225,7 @@ void MPU6886::SetAccelFsr(Ascale scale)
// x/y/z are in 1/1000 if g
// avoiding costly float calculations
void MPU6886::getAccelDataInt(int16_t* ax, int16_t* ay, int16_t* az) {
void MPU_accel::getAccelDataInt(int16_t* ax, int16_t* ay, int16_t* az) {
int16_t accX = 0;
int16_t accY = 0;
int16_t accZ = 0;
@ -231,7 +237,7 @@ void MPU6886::getAccelDataInt(int16_t* ax, int16_t* ay, int16_t* az) {
}
void MPU6886::getAccelData(float* ax, float* ay, float* az){
void MPU_accel::getAccelData(float* ax, float* ay, float* az){
int16_t accX = 0;
@ -248,7 +254,7 @@ void MPU6886::getAccelData(float* ax, float* ay, float* az){
// x/y/z are in dps - degrees per second
// avoiding costly float calculations
void MPU6886::getGyroDataInt(int16_t* ax, int16_t* ay, int16_t* az) {
void MPU_accel::getGyroDataInt(int16_t* ax, int16_t* ay, int16_t* az) {
int16_t gyX = 0;
int16_t gyY = 0;
int16_t gyZ = 0;
@ -260,7 +266,7 @@ void MPU6886::getGyroDataInt(int16_t* ax, int16_t* ay, int16_t* az) {
}
void MPU6886::getGyroData(float* gx, float* gy, float* gz){
void MPU_accel::getGyroData(float* gx, float* gy, float* gz){
int16_t gyroX = 0;
int16_t gyroY = 0;
int16_t gyroZ = 0;
@ -271,7 +277,7 @@ void MPU6886::getGyroData(float* gx, float* gy, float* gz){
*gz = (float)gyroZ * gRes;
}
void MPU6886::getTempData(float *t){
void MPU_accel::getTempData(float *t){
int16_t temp = 0;
getTempAdc(&temp);

View File

@ -5,6 +5,9 @@
library in the Wire.h/twi.c utility file. We are also using the 400 kHz fast
I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file.
*/
// Extended to support MPU9250 and other variants
#ifndef _MPU6886_H_
#define _MPU6886_H_
@ -48,7 +51,7 @@
#define AtR 0.0174533
#define Gyro_Gr 0.0010653
class MPU6886 {
class MPU_accel {
public:
enum Ascale {
AFS_2G = 0,
@ -68,14 +71,16 @@ class MPU6886 {
Ascale Acscale = AFS_8G;
int16_t acRange = 8000; // 1/1000 of g
int16_t gyRange = 2000; // dps - degree per second
uint32_t model = 6886; // MPU model number
public:
MPU6886(void) {};
MPU_accel(void) {};
#ifdef ESP32
void setBus(uint32_t _bus) { myWire = _bus ? &Wire1 : &Wire; };
#else
void setBus(uint32_t _bus) { myWire = &Wire; };
#endif
int Init(void);
uint32_t getModel(void) const { return model; }
void getAccelAdc(int16_t* ax, int16_t* ay, int16_t* az);
void getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz);
void getTempAdc(int16_t *t);

View File

@ -7,6 +7,7 @@
class MPU6886 : Driver
var wire #- if wire == nil then the module is not initialized -#
var device
var gres, ares
var accel, gyro
@ -15,7 +16,10 @@ class MPU6886 : Driver
if self.wire
var v = self.wire.read(0x68,0x75,1)
if v != 0x19 return end #- wrong device -#
if v == 0x19 self.device = 6886
elif v == 0x71 self.device = 9250
else self.wire = nil return #- wrong device -#
end
self.wire.write(0x68, 0x6B, 0, 1)
tasmota.delay(10)
@ -46,7 +50,7 @@ class MPU6886 : Driver
self.gres = 2000.0/32768.0
self.ares = 8.0/32678.0
print("I2C: MPU6886 detected on bus "+str(self.wire.bus))
print("I2C:","MPU"+str(self.device),"detected on bus",self.wire.bus)
end
end
@ -90,13 +94,14 @@ class MPU6886 : Driver
if !self.wire return nil end #- exit if not initialized -#
import string
var msg = string.format(
"{s}MPU6886 acc_x{m}%.3f G{e}"..
"{s}MPU6886 acc_y{m}%.3f G{e}"..
"{s}MPU6886 acc_z{m}%.3f G{e}"..
"{s}MPU6886 gyr_x{m}%i dps{e}"..
"{s}MPU6886 gyr_y{m}%i dps{e}"..
"{s}MPU6886 gyr_z{m}%i dps{e}",
self.accel[0], self.accel[1], self.accel[2], self.gyro[0], self.gyro[1], self.gyro[2])
"{s}MPU%d acc_x{m}%.3f G{e}"..
"{s}MPU%d acc_y{m}%.3f G{e}"..
"{s}MPU%d acc_z{m}%.3f G{e}"..
"{s}MPU%d gyr_x{m}%i dps{e}"..
"{s}MPU%d gyr_y{m}%i dps{e}"..
"{s}MPU%d gyr_z{m}%i dps{e}",
self.device, self.accel[0], self.device, self.accel[1], self.device, self.accel[2],
self.device, self.gyro[0], self.device, self.gyro[1], self.device, self.gyro[2])
tasmota.web_send_decimal(msg)
end
@ -107,11 +112,11 @@ class MPU6886 : Driver
var ax = int(self.accel[0] * 1000)
var ay = int(self.accel[1] * 1000)
var az = int(self.accel[2] * 1000)
var msg = string.format(",\"MPU6886\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}",
ax, ay, az, self.gyro[0], self.gyro[1], self.gyro[2])
var msg = string.format(",\"MPU%d\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}",
self.device, ax, ay, az, self.gyro[0], self.gyro[1], self.gyro[2])
tasmota.response_append(msg)
end
end
mpu6886 = MPU6886()
tasmota.add_driver(mpu6886)
mpu_accel = MPU6886()
tasmota.add_driver(mpu_accel)

View File

@ -628,8 +628,8 @@
// #define USE_EZORGB // [I2cDriver55] Enable support for EZO's RGB sensor (+0k5 code) - Shared EZO code required for any EZO device (+1k2 code)
// #define USE_EZOPMP // [I2cDriver55] Enable support for EZO's PMP sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code)
// #define USE_SEESAW_SOIL // [I2cDriver56] Enable Capacitice Soil Moisture & Temperature Sensor (I2C addresses 0x36 - 0x39) (+1k3 code)
// #define USE_MPU6886 // [I2cDriver58] Enable MPU6886 - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x68) (+2k code)
// #define USE_BM8563 // [I2cDriver58] Enable BM8563 RTC - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x51) (+2.5k code)
// #define USE_MPU_ACCEL // [I2cDriver58] Enable MPU6886/MPU9250 - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x68) (+2k code)
// #define USE_BM8563 // [I2cDriver59] Enable BM8563 RTC - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x51) (+2.5k code)
// #define USE_DISPLAY // Add I2C Display Support (+2k code)
#define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0

View File

@ -106,7 +106,7 @@
#define USE_I2C
#define USE_BMA423
#define USE_MPU6886
#define USE_MPU_ACCEL
#define USE_SPI
#define USE_DISPLAY
#ifdef USE_UNIVERSAL_DISPLAY
@ -233,7 +233,7 @@
//#define USE_MPR121 // [I2cDriver23] Enable MPR121 controller (I2C addresses 0x5A, 0x5B, 0x5C and 0x5D) in input mode for touch buttons (+1k3 code)
//#define USE_CCS811 // [I2cDriver24] Enable CCS811 sensor (I2C address 0x5A) (+2k2 code)
#define USE_CCS811_V2 // [I2cDriver24] Enable CCS811 sensor (I2C addresses 0x5A and 0x5B) (+2k8 code)
#define USE_MPU6886 // [I2cDriver??] Enable MPU6886 6-axis MotionTracking sensor (I2C address 0x68)
#define USE_MPU_ACCEL // [I2cDriver58] Enable MPU6886, MPU9250 6-axis MotionTracking sensor (I2C address 0x68)
//#define USE_MPU6050 // [I2cDriver25] Enable MPU6050 sensor (I2C address 0x68 AD0 low or 0x69 AD0 high) (+3K3 of code and 188 Bytes of RAM)
//#define USE_DS3231 // [I2cDriver26] Enable DS3231 external RTC in case no Wifi is avaliable. See docs in the source file (+1k2 code)
//#define USE_MGC3130 // [I2cDriver27] Enable MGC3130 Electric Field Effect Sensor (I2C address 0x42) (+2k7 code, 0k3 mem)

View File

@ -18,11 +18,11 @@
*/
#ifdef USE_I2C
#ifdef USE_MPU6886
#if defined(USE_MPU6886) || defined(USE_MPU_ACCEL) // USE_MPU6886 deprecated, prefer USE_MPU_ACCEL
#include <MPU6886.h>
#include <MPU_accel.h>
/*********************************************************************************************\
* MPU6886
* MPU6886/MPU9250
* Internal chip found in M5Stack devices, using `Wire1` internal I2C bus
*
* I2C Address: 0x68
@ -32,65 +32,70 @@
#define XSNS_85 85
#define XI2C_58 58 // See I2CDEVICES.md
#define MPU6886_ADDRESS 0x68
#define MPU_ACCEL_ADDRESS 0x68
struct {
MPU6886 Mpu;
bool ready = false;
MPU_accel Mpu;
uint32_t mpu_model;
int16_t ax=0, ay=0, az=0; // accelerator data
int16_t gyx=0, gyy=0, gyz=0; // accelerator data
} mpu6886_sensor;
uint8_t bus = 0; // I2C bus
bool ready = false;
} mpu_sensor;
/********************************************************************************************/
const char HTTP_MPU6686[] PROGMEM =
"{s}MPU6886 acc_x" "{m}%3_f G" "{e}"
"{s}MPU6886 acc_y" "{m}%3_f G" "{e}"
"{s}MPU6886 acc_z" "{m}%3_f G" "{e}"
"{s}MPU6886 gyr_x" "{m}%i dps" "{e}"
"{s}MPU6886 gyr_y" "{m}%i dps" "{e}"
"{s}MPU6886 gyr_z" "{m}%i dps" "{e}"
"{s}MPU%d acc_x" "{m}%3_f G" "{e}"
"{s}MPU%d acc_y" "{m}%3_f G" "{e}"
"{s}MPU%d acc_z" "{m}%3_f G" "{e}"
"{s}MPU%d gyr_x" "{m}%i dps" "{e}"
"{s}MPU%d gyr_y" "{m}%i dps" "{e}"
"{s}MPU%d gyr_z" "{m}%i dps" "{e}"
;
void MPU6686_Show(uint32_t json) {
void MPU_Show(uint32_t json) {
if (json) {
ResponseAppend_P(PSTR(",\"MPU6886\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}"),
mpu6886_sensor.ax, mpu6886_sensor.ay, mpu6886_sensor.az,
mpu6886_sensor.gyx, mpu6886_sensor.gyy, mpu6886_sensor.gyz);
ResponseAppend_P(PSTR(",\"MPU%d\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}"),
mpu_sensor.mpu_model,
mpu_sensor.ax, mpu_sensor.ay, mpu_sensor.az,
mpu_sensor.gyx, mpu_sensor.gyy, mpu_sensor.gyz);
} else {
float ax = mpu6886_sensor.ax / 1000.0f;
float ay = mpu6886_sensor.ay / 1000.0f;
float az = mpu6886_sensor.az / 1000.0f;
WSContentSend_PD(HTTP_MPU6686, &ax, &ay, &az,
mpu6886_sensor.gyx, mpu6886_sensor.gyy, mpu6886_sensor.gyz);
float ax = mpu_sensor.ax / 1000.0f;
float ay = mpu_sensor.ay / 1000.0f;
float az = mpu_sensor.az / 1000.0f;
WSContentSend_PD(HTTP_MPU6686, mpu_sensor.mpu_model, &ax, mpu_sensor.mpu_model, &ay, mpu_sensor.mpu_model, &az,
mpu_sensor.mpu_model, mpu_sensor.gyx, mpu_sensor.mpu_model, mpu_sensor.gyy, mpu_sensor.mpu_model, mpu_sensor.gyz);
}
}
void MPU6686Detect(void) {
void MPU_Detect(void) {
#ifdef ESP32
if (!I2cSetDevice(MPU6886_ADDRESS, 0)) {
if (!I2cSetDevice(MPU6886_ADDRESS, 1)) { return; } // check on bus 1
mpu6886_sensor.Mpu.setBus(1); // switch to bus 1
I2cSetActiveFound(MPU6886_ADDRESS, "MPU6886", 1);
} else {
I2cSetActiveFound(MPU6886_ADDRESS, "MPU6886", 0);
if (!I2cSetDevice(MPU_ACCEL_ADDRESS, 0)) {
if (!I2cSetDevice(MPU_ACCEL_ADDRESS, 1)) { return; } // check on bus 1
mpu_sensor.bus = 1;
mpu_sensor.Mpu.setBus(mpu_sensor.bus); // switch to bus 1
}
#else
if (!I2cSetDevice(MPU6886_ADDRESS)) { return; }
I2cSetActiveFound(MPU6886_ADDRESS, "MPU6886");
if (!I2cSetDevice(MPU_ACCEL_ADDRESS)) { return; }
#endif
mpu6886_sensor.Mpu.Init();
mpu6886_sensor.ready = true;
if (mpu_sensor.Mpu.Init() == 0) {
char model_name[16];
mpu_sensor.mpu_model = mpu_sensor.Mpu.getModel();
snprintf_P(model_name, sizeof(model_name), PSTR("MPU%04d"), mpu_sensor.mpu_model);
I2cSetActiveFound(MPU_ACCEL_ADDRESS, model_name, mpu_sensor.bus);
mpu_sensor.ready = true;
}
}
void MPU6886Every_Second(void) {
mpu6886_sensor.Mpu.getAccelDataInt(&mpu6886_sensor.ax, &mpu6886_sensor.ay, &mpu6886_sensor.az);
mpu6886_sensor.Mpu.getGyroDataInt(&mpu6886_sensor.gyx, &mpu6886_sensor.gyy, &mpu6886_sensor.gyz);
void MPU_Every_Second(void) {
mpu_sensor.Mpu.getAccelDataInt(&mpu_sensor.ax, &mpu_sensor.ay, &mpu_sensor.az);
mpu_sensor.Mpu.getGyroDataInt(&mpu_sensor.gyx, &mpu_sensor.gyy, &mpu_sensor.gyz);
// AddLog(LOG_LEVEL_DEBUG, PSTR(">> Acc x=%i y=%i z=%i gx=%i gy=%i gz=%i"), mpu6886_sensor.ax, mpu6886_sensor.ay, mpu6886_sensor.az,
// mpu6886_sensor.gyx, mpu6886_sensor.gyy, mpu6886_sensor.gyz);
// AddLog(LOG_LEVEL_DEBUG, PSTR(">> Acc x=%i y=%i z=%i gx=%i gy=%i gz=%i"), mpu_sensor.ax, mpu_sensor.ay, mpu_sensor.az,
// mpu_sensor.gyx, mpu_sensor.gyy, mpu_sensor.gyz);
}
@ -104,19 +109,19 @@ bool Xsns85(uint8_t function) {
bool result = false;
if (FUNC_INIT == function) {
MPU6686Detect();
MPU_Detect();
}
else if (mpu6886_sensor.ready) {
else if (mpu_sensor.ready) {
switch (function) {
case FUNC_EVERY_SECOND:
MPU6886Every_Second();
MPU_Every_Second();
break;
case FUNC_JSON_APPEND:
MPU6686_Show(1);
MPU_Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
MPU6686_Show(0);
MPU_Show(0);
break;
#endif // USE_WEBSERVER
}
@ -124,5 +129,5 @@ bool Xsns85(uint8_t function) {
return result;
}
#endif // USE_MPU6886
#endif // USE_MPU6886 USE_MPU_ACCEL
#endif // USE_I2C