|
| 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 | +} |
0 commit comments