Files

80 lines
1.6 KiB
C++
Raw Permalink Normal View History

2020-01-06 18:03:24 +08:00
/*
* @Author: Sorzn
* @Date: 2020-01-06 11:00:49
* @LastEditTime : 2020-01-06 15:15:40
* @Description: M5Stack project
2022-07-06 14:21:26 +08:00
* @FilePath:
* /home/sakabin/Arduino/libraries/M5Stack/examples/Modules/Bala/imuAuto.cpp
2020-01-06 18:03:24 +08:00
*/
#include "imuCalibration.h"
static float gyroXOffset = 0;
static float gyroYOffset = 0;
static float gyroZOffset = 0;
static float accX = 0;
static float accY = 0;
static float accZ = 0;
static float gyroX = 0;
static float gyroY = 0;
static float gyroZ = 0;
2022-07-06 14:21:26 +08:00
static float angleAccX = 0;
2020-01-06 18:03:24 +08:00
static float angleGyroX = 0;
2022-07-06 14:21:26 +08:00
static float angleX = 0;
2020-01-06 18:03:24 +08:00
static uint32_t preInterval = 0;
void imu_CalcInit() {
2022-07-06 14:21:26 +08:00
M5.IMU.Init();
M5.IMU.setGyroFsr(M5.IMU.GFS_1000DPS);
2020-01-06 18:03:24 +08:00
}
2022-08-11 17:01:46 +08:00
void imu_setOffsetX(float x) {
gyroXOffset = x;
}
2020-01-06 18:03:24 +08:00
2022-08-11 17:01:46 +08:00
float imu_getOffsetX() {
return gyroXOffset;
}
2020-01-06 18:03:24 +08:00
void imu_calcGyroOffsets() {
2022-07-06 14:21:26 +08:00
float x = 0, y = 0, z = 0;
float x_total = 0, y_total = 0, z_total = 0;
for (int i = 0; i < 3000; i++) {
M5.IMU.getGyroData(&x, &y, &z);
x_total += x;
}
gyroXOffset = x_total / 3000;
2020-01-06 18:03:24 +08:00
}
void imu_update() {
2022-07-06 14:21:26 +08:00
float interval;
if (preInterval == 0) preInterval = millis();
2020-01-06 18:03:24 +08:00
2022-07-06 14:21:26 +08:00
M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ);
M5.IMU.getAccelData(&accX, &accY, &accZ);
2020-01-06 18:03:24 +08:00
2022-07-06 14:21:26 +08:00
angleAccX = atan2(accY, sqrt(accZ * accZ + accX * accX)) * 360 / 2.0 / PI;
2020-01-06 18:03:24 +08:00
2022-07-06 14:21:26 +08:00
gyroX -= gyroXOffset;
2020-01-06 18:03:24 +08:00
2022-07-06 14:21:26 +08:00
interval = (millis() - preInterval) * 0.001;
preInterval = millis();
angleX = (0.98 * (angleX + gyroX * interval)) + (0.02 * angleAccX);
2020-01-06 18:03:24 +08:00
}
float imu_getAngleX() {
2022-07-06 14:21:26 +08:00
#ifdef M5STACK_MPU6886 || M5STACK_200Q:
return 0 - angleX;
#else:
return angleX;
#endif
2020-01-06 18:03:24 +08:00
}