Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d84a70a

Browse files
committedSep 8, 2022
Fix IMU not work in some compiled environments
1 parent f77022f commit d84a70a

File tree

12 files changed

+207
-38
lines changed

12 files changed

+207
-38
lines changed
 

‎.github/workflows/clang-format-check.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
matrix:
99
path:
1010
- check: './' # path to include
11-
exclude: '(Fonts|utility|RFID)' # path to exclude
11+
exclude: '(Fonts|utility|RFID|THERMAL_MLX90640|HEART_MAX30100|Display|AC-SOCKET|BALA2)' # path to exclude
1212
# - check: 'src'
1313
# exclude: '(Fonts)' # Exclude file paths containing "Fonts"
1414
# - check: 'examples'

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ English | [中文](docs/getting_started_cn.md) | [日本語](docs/getting_starte
1010

1111
* **For the Detailed documentation of Basic, please [Click here](https://docs.m5stack.com/en/core/basic_v2.6)**
1212

13-
* **In order to buy Basic, please [Click here](https://shop.m5stack.com/collections/m5-controllers/products/esp32-basic-core-iot-development-kit-v2-6)**
13+
* **In order to buy Gray, please [Click here](https://shop.m5stack.com/products/grey-development-core)**
14+
15+
* **In order to buy Basic, please [Click here](https://shop.m5stack.com/products/esp32-basic-core-iot-development-kit-v2-6)**
1416

1517
*We have several master M5Cores with different configurations, this is the difference between them [Compared](https://docs.m5stack.com/en/products_selector).*
1618

‎examples/Basics/IMU/IMU.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
88
*
99
* Describe: MPU6886 example. 惯性传感器
10-
* Date: 2021/7/21
10+
* Date: 2022/9/8
1111
*******************************************************************************
1212
*/
13-
#define M5STACK_MPU6886
13+
1414
#include <M5Stack.h>
1515

1616
float accX = 0.0F; // Define variables for storing inertial sensor data

‎library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "https://github.com/m5stack/m5stack.git"
1212
},
13-
"version": "0.4.2",
13+
"version": "0.4.3",
1414
"frameworks": "arduino",
1515
"platforms": "espressif32",
1616
"headers": "M5Stack.h"

‎library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=M5Stack
2-
version=0.4.2
2+
version=0.4.3
33
author=M5Stack
44
maintainer=M5Stack
55
sentence=Library for M5Stack Core development kit

‎src/IMU.cpp

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include "IMU.h"
2+
3+
#include <Arduino.h>
4+
#include <math.h>
5+
6+
#include "M5Stack.h"
7+
#undef IMU
8+
9+
IMU::IMU() {
10+
}
11+
12+
int IMU::Init(void) {
13+
int imu_flag = M5.Sh200Q.Init();
14+
Serial.printf("imu_flag:%d", imu_flag);
15+
if (imu_flag != 0) {
16+
imu_flag = M5.Mpu6886.Init();
17+
if (imu_flag == 0) {
18+
imuType = IMU_MPU6886;
19+
Serial.printf("IMU_MPU6886");
20+
} else {
21+
imuType = IMU_UNKNOWN;
22+
Serial.printf("IMU_UNKNOWN");
23+
return -1;
24+
}
25+
} else {
26+
imuType = IMU_SH200Q;
27+
}
28+
return 0;
29+
}
30+
31+
void IMU::getGres() {
32+
if (imuType == IMU_SH200Q) {
33+
gRes = M5.Sh200Q.gRes;
34+
} else if (imuType == IMU_MPU6886) {
35+
gRes = M5.Mpu6886.gRes;
36+
}
37+
}
38+
39+
void IMU::getAres() {
40+
if (imuType == IMU_SH200Q) {
41+
aRes = M5.Sh200Q.aRes;
42+
} else if (imuType == IMU_MPU6886) {
43+
aRes = M5.Mpu6886.aRes;
44+
}
45+
}
46+
47+
void IMU::getAccelAdc(int16_t *ax, int16_t *ay, int16_t *az) {
48+
if (imuType == IMU_SH200Q) {
49+
M5.Sh200Q.getAccelAdc(ax, ay, az);
50+
} else if (imuType == IMU_MPU6886) {
51+
M5.Mpu6886.getAccelAdc(ax, ay, az);
52+
}
53+
}
54+
55+
void IMU::getAccelData(float *ax, float *ay, float *az) {
56+
if (imuType == IMU_SH200Q) {
57+
M5.Sh200Q.getAccelData(ax, ay, az);
58+
} else if (imuType == IMU_MPU6886) {
59+
M5.Mpu6886.getAccelData(ax, ay, az);
60+
}
61+
}
62+
63+
void IMU::getGyroAdc(int16_t *gx, int16_t *gy, int16_t *gz) {
64+
if (imuType == IMU_SH200Q) {
65+
M5.Sh200Q.getGyroAdc(gx, gy, gz);
66+
} else if (imuType == IMU_MPU6886) {
67+
M5.Mpu6886.getGyroAdc(gx, gy, gz);
68+
}
69+
}
70+
71+
void IMU::getGyroData(float *gx, float *gy, float *gz) {
72+
if (imuType == IMU_SH200Q) {
73+
M5.Sh200Q.getGyroData(gx, gy, gz);
74+
} else if (imuType == IMU_MPU6886) {
75+
M5.Mpu6886.getGyroData(gx, gy, gz);
76+
}
77+
}
78+
79+
void IMU::getTempAdc(int16_t *t) {
80+
if (imuType == IMU_SH200Q) {
81+
M5.Sh200Q.getTempAdc(t);
82+
} else if (imuType == IMU_MPU6886) {
83+
M5.Mpu6886.getTempAdc(t);
84+
}
85+
}
86+
87+
void IMU::getTempData(float *t) {
88+
if (imuType == IMU_SH200Q) {
89+
M5.Sh200Q.getTempData(t);
90+
} else if (imuType == IMU_MPU6886) {
91+
M5.Mpu6886.getTempData(t);
92+
}
93+
}
94+
95+
void IMU::getAhrsData(float *pitch, float *roll, float *yaw) {
96+
float accX = 0;
97+
float accY = 0;
98+
float accZ = 0;
99+
100+
float gyroX = 0;
101+
float gyroY = 0;
102+
float gyroZ = 0;
103+
104+
getGyroData(&gyroX, &gyroY, &gyroZ);
105+
getAccelData(&accX, &accY, &accZ);
106+
107+
MahonyAHRSupdateIMU(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD,
108+
gyroZ * DEG_TO_RAD, accX, accY, accZ, pitch, roll, yaw);
109+
}

‎src/IMU.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef __IMU_H__
2+
#define __IMU_H__
3+
4+
#include <Arduino.h>
5+
#include <Wire.h>
6+
7+
#include "utility/MahonyAHRS.h"
8+
9+
class IMU {
10+
public:
11+
enum ImuType { IMU_UNKNOWN = 0, IMU_SH200Q, IMU_MPU6886 };
12+
13+
IMU();
14+
15+
int Init(void);
16+
17+
void getGres();
18+
void getAres();
19+
20+
void getAccelAdc(int16_t *ax, int16_t *ay, int16_t *az);
21+
void getGyroAdc(int16_t *gx, int16_t *gy, int16_t *gz);
22+
void getTempAdc(int16_t *t);
23+
24+
void getAccelData(float *ax, float *ay, float *az);
25+
void getGyroData(float *gx, float *gy, float *gz);
26+
void getTempData(float *t);
27+
28+
void getAhrsData(float *pitch, float *roll, float *yaw);
29+
30+
ImuType imuType;
31+
float aRes, gRes;
32+
};
33+
34+
#endif

‎src/M5Stack.h

+19-20
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,14 @@
111111
#include "M5Display.h"
112112
#include "SD.h"
113113
#include "gitTagVersion.h"
114+
#include "IMU.h"
114115
#include "utility/Button.h"
115116
#include "utility/CommUtil.h"
116117
#include "utility/Config.h"
117118
#include "utility/Power.h"
118119
#include "utility/Speaker.h"
119-
120-
#if defined(M5STACK_MPU6886) || defined(M5STACK_MPU9250) || \
121-
defined(M5STACK_MPU6050)
122120
#include "utility/MPU6886.h"
123-
#elif defined M5STACK_200Q
124121
#include "utility/SH200Q.h"
125-
#endif
126122

127123
class M5Stack {
128124
public:
@@ -131,6 +127,12 @@ class M5Stack {
131127
bool SerialEnable = true, bool I2CEnable = false);
132128
void update();
133129

130+
// LCD
131+
M5Display Lcd = M5Display();
132+
133+
// Power
134+
POWER Power;
135+
134136
// Button API
135137
#define DEBOUNCE_MS 10
136138
Button BtnA = Button(BUTTON_A_PIN, true, DEBOUNCE_MS);
@@ -140,26 +142,17 @@ class M5Stack {
140142
// SPEAKER
141143
SPEAKER Speaker;
142144

143-
// LCD
144-
M5Display Lcd = M5Display();
145-
146-
// Power
147-
POWER Power;
148-
149145
// UART
150146
// HardwareSerial Serial0 = HardwareSerial(0);
151147
// HardwareSerial Serial2 = HardwareSerial(2);
152148

153-
#if defined(M5STACK_MPU6886) || defined(M5STACK_MPU9250) || \
154-
defined(M5STACK_MPU6050)
155-
MPU6886 IMU = MPU6886();
156-
#elif defined M5STACK_200Q
157-
SH200Q IMU = SH200Q();
158-
#endif
159-
160149
// I2C
150+
IMU Imu;
161151
CommUtil I2C = CommUtil();
162152

153+
MPU6886 Mpu6886;
154+
SH200Q Sh200Q;
155+
163156
/**
164157
* Function has been move to Power class.(for compatibility)
165158
* This name will be removed in a future release.
@@ -173,8 +166,14 @@ class M5Stack {
173166
};
174167

175168
extern M5Stack M5;
176-
#define m5 M5
177-
#define lcd Lcd
169+
#define m5 M5
170+
#define lcd Lcd
171+
#define imu Imu
172+
#define IMU Imu
173+
#define MPU6886 Mpu6886
174+
#define mpu6886 Mpu6886
175+
#define SH200Q Sh200Q
176+
#define sh200q Sh200Q
178177
#else
179178
#error "This library only supports boards with ESP32 processor."
180179
#endif

‎src/utility/MPU6886.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33
#include <Arduino.h>
44
#include <math.h>
55

6-
#include "../M5Stack.h"
7-
#include "MahonyAHRS.h"
8-
96
MPU6886::MPU6886() {
107
}
118

129
void MPU6886::I2C_Read_NBytes(uint8_t driver_Addr, uint8_t start_Addr,
1310
uint8_t number_Bytes, uint8_t* read_Buffer) {
14-
M5.I2C.readBytes(driver_Addr, start_Addr, number_Bytes, read_Buffer);
11+
Wire.beginTransmission(driver_Addr);
12+
Wire.write(start_Addr);
13+
Wire.endTransmission(false);
14+
uint8_t i = 0;
15+
Wire.requestFrom(driver_Addr, number_Bytes);
16+
17+
//! Put read results in the Rx buffer
18+
while (Wire.available()) {
19+
read_Buffer[i++] = Wire.read();
20+
}
1521
}
1622

1723
void MPU6886::I2C_Write_NBytes(uint8_t driver_Addr, uint8_t start_Addr,
1824
uint8_t number_Bytes, uint8_t* write_Buffer) {
19-
M5.I2C.writeBytes(driver_Addr, start_Addr, write_Buffer, number_Bytes);
25+
Wire.beginTransmission(driver_Addr);
26+
Wire.write(start_Addr);
27+
Wire.write(*write_Buffer);
28+
Wire.endTransmission();
2029
}
2130

2231
int MPU6886::Init(void) {
@@ -114,9 +123,9 @@ void MPU6886::getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz) {
114123

115124
void MPU6886::getTempAdc(int16_t* t) {
116125
uint8_t buf[2];
117-
I2C_Read_NBytes(MPU6886_ADDRESS, MPU6886_TEMP_OUT_H, 2, buf);
126+
I2C_Read_NBytes(MPU6886_ADDRESS, MPU6886_TEMP_OUT_H, 14, buf);
118127

119-
*t = ((uint16_t)buf[0] << 8) | buf[1];
128+
*t = ((uint16_t)buf[6] << 8) | buf[7];
120129
}
121130

122131
//!俯仰,航向,横滚: pitch,yaw,roll,指三维空间中飞行器的旋转状态。

‎src/utility/MPU6886.h

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <Arduino.h>
1212
#include <Wire.h>
1313

14+
#include "MahonyAHRS.h"
15+
1416
#define MPU6886_ADDRESS 0x68
1517
#define MPU6886_WHOAMI 0x75
1618
#define MPU6886_ACCEL_INTEL_CTRL 0x69

‎src/utility/SH200Q.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,32 @@
33
#include <Arduino.h>
44
#include <math.h>
55

6-
#include "../M5Stack.h"
7-
#include "MahonyAHRS.h"
8-
96
SH200Q::SH200Q() {
107
}
118

129
void SH200Q::I2C_Read_NBytes(uint8_t driver_Addr, uint8_t start_Addr,
1310
uint8_t number_Bytes, uint8_t* read_Buffer) {
14-
M5.I2C.readBytes(driver_Addr, start_Addr, number_Bytes, read_Buffer);
11+
Wire.beginTransmission(driver_Addr);
12+
Wire.write(start_Addr);
13+
Wire.endTransmission();
14+
uint8_t i = 0;
15+
Wire.requestFrom(driver_Addr, number_Bytes);
16+
// byte buf = Wire1.read();
17+
//*read_Buffer = buf;
18+
//! Put read results in the Rx buffer
19+
while (Wire.available()) {
20+
read_Buffer[i++] = Wire1.read();
21+
}
1522
}
1623

1724
void SH200Q::I2C_Write_NBytes(uint8_t driver_Addr, uint8_t start_Addr,
1825
uint8_t number_Bytes, uint8_t* write_Buffer) {
19-
M5.I2C.writeBytes(driver_Addr, start_Addr, write_Buffer, number_Bytes);
26+
Wire.beginTransmission(driver_Addr);
27+
Wire.write(start_Addr);
28+
Wire.write(*write_Buffer);
29+
Wire.endTransmission();
30+
// Serial.printf("I2C Write OP, ADDR: 0x%02x, ADS: 0x%02x, NumBytes: %u,
31+
// Data: 0x%02x\n\r", driver_Addr, start_Addr, number_Bytes, *write_Buffer);
2032
}
2133

2234
void SH200Q::sh200i_ADCReset(void) {

‎src/utility/SH200Q.h

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <Arduino.h>
55
#include <Wire.h>
66

7+
#include "MahonyAHRS.h"
8+
79
#define SH200I_ADDRESS 0x6C // 7bit i2c address
810
#define SH200I_WHOAMI 0x30
911
#define SH200I_ACC_CONFIG 0x0E

0 commit comments

Comments
 (0)
Please sign in to comment.