2024-09-11 12:19:10 +00:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "imu.h"
|
|
|
|
|
|
|
|
|
|
|
|
MPU6050 mpu6050(Wire,IMU_AC,IMU_GC);
|
|
|
|
|
2024-09-12 14:59:47 +00:00
|
|
|
float maxAccX;
|
|
|
|
float maxAccY;
|
|
|
|
float maxAccZ;
|
|
|
|
|
2024-09-11 12:19:10 +00:00
|
|
|
|
|
|
|
bool imu_bellyup=false;
|
|
|
|
|
|
|
|
void initIMU() {
|
|
|
|
initIMU(0,0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initIMU(float x,float y,float z) {
|
|
|
|
Wire.begin(25,24);
|
|
|
|
mpu6050.begin();
|
|
|
|
|
|
|
|
if (x==0 && y==0 && z==0) {
|
|
|
|
mpu6050.calcGyroOffsets(true);
|
|
|
|
}else{
|
|
|
|
mpu6050.setGyroOffsets(x, y, z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loopIMU(unsigned long millis) {
|
|
|
|
static unsigned long last_update_imu=0;
|
|
|
|
if (millis-last_update_imu>UPDATE_IMU_INTERVAL) {
|
|
|
|
last_update_imu=millis;
|
|
|
|
mpu6050.update();
|
|
|
|
/*Serial.print("angleX : ");
|
|
|
|
Serial.print(mpu6050.getAngleX());
|
|
|
|
Serial.print("\tangleY : ");
|
|
|
|
Serial.print(mpu6050.getAngleY());
|
|
|
|
Serial.print("\tangleZ : ");
|
|
|
|
Serial.println(mpu6050.getAngleZ());*/
|
|
|
|
|
2024-09-12 14:59:47 +00:00
|
|
|
|
|
|
|
maxAccX-=UPDATE_IMU_INTERVAL/1000.0*1.0; //slowly reduce values
|
|
|
|
maxAccY-=UPDATE_IMU_INTERVAL/1000.0*1.0;
|
|
|
|
maxAccZ-=UPDATE_IMU_INTERVAL/1000.0*1.0;
|
|
|
|
static float last_accX;
|
|
|
|
static float last_accY;
|
|
|
|
static float last_accZ;
|
|
|
|
float _accX=getAccX();
|
|
|
|
float _accY=getAccY();
|
|
|
|
float _accZ=getAccZ();
|
|
|
|
maxAccX=max(abs(last_accX-_accX), maxAccX);
|
|
|
|
maxAccY=max(abs(last_accY-_accY), maxAccY);
|
|
|
|
maxAccZ=max(abs(last_accZ-_accZ), maxAccZ);
|
|
|
|
last_accX=_accX;
|
|
|
|
last_accY=_accY;
|
|
|
|
last_accZ=_accZ;
|
|
|
|
|
2024-09-11 12:19:10 +00:00
|
|
|
if(mpu6050.getAccZ()<-0.5) { //is upside down
|
|
|
|
imu_bellyup=true;
|
|
|
|
}else{
|
|
|
|
if (mpu6050.getAccZ()>0.7) { //is on flat surface
|
|
|
|
imu_bellyup=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBellyUp() {
|
|
|
|
return imu_bellyup;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getAngleX() {
|
|
|
|
return mpu6050.getAngleX();
|
|
|
|
}
|
|
|
|
|
|
|
|
float getAngleY() {
|
|
|
|
return mpu6050.getAngleY();
|
|
|
|
}
|
|
|
|
|
|
|
|
float getAngleZ() {
|
|
|
|
return mpu6050.getAngleZ();
|
|
|
|
}
|
|
|
|
|
|
|
|
float getAccX() {
|
|
|
|
return mpu6050.getAccX();
|
|
|
|
}
|
|
|
|
float getAccY() {
|
|
|
|
return mpu6050.getAccY();
|
|
|
|
}
|
|
|
|
float getAccZ() {
|
|
|
|
return mpu6050.getAccZ();
|
|
|
|
}
|
2024-09-12 09:23:52 +00:00
|
|
|
|
2024-09-12 14:59:47 +00:00
|
|
|
|
|
|
|
float getMaxAccX() {
|
|
|
|
return maxAccX;
|
|
|
|
}
|
|
|
|
float getMaxAccY() {
|
|
|
|
return maxAccY;
|
|
|
|
}
|
|
|
|
float getMaxAccZ() {
|
|
|
|
return maxAccZ;
|
|
|
|
}
|
|
|
|
void resetMaxAcc(){
|
|
|
|
maxAccX=0;
|
|
|
|
maxAccY=0;
|
|
|
|
maxAccZ=0;
|
|
|
|
}
|
|
|
|
|
2024-09-12 09:23:52 +00:00
|
|
|
float getGyroAngleX() {
|
|
|
|
return mpu6050.getGyroAngleX();
|
|
|
|
}
|
|
|
|
float getGyroAngleY() {
|
|
|
|
return mpu6050.getGyroAngleY();
|
|
|
|
}
|
|
|
|
float getGyroAngleZ() {
|
|
|
|
return mpu6050.getGyroAngleZ();
|
|
|
|
}
|