#define SV_Motor_x_PIN 32
#define SV_Motor_y_PIN 26
#define SV_Motor_z_PIN 14
#include<math.h>
#define Deg2rad 3.1415 /180
#include <MadgwickAHRS.h>
#include <Wire.h>
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ,MgX,MgY,MgZ;
float SF_Acc = 16384;
float SF_Gy = 131;
float SF_Mg = 500;
float SF_Tmp = 333.87;
float g2mpss = 9.80665;
float deg2rad = 3.14159265/180;
Madgwick MadgwickFilter;
float duty_min = 25.6;
float duty_max = 122.9;
float duty_active = duty_max - duty_min;
float duty_mid = (duty_min + duty_max)/2;
volatile int timeCounter1;
hw_timer_t *timer1 = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer1(){
portENTER_CRITICAL_ISR(&timerMux);
timeCounter1++;
portEXIT_CRITICAL_ISR(&timerMux);
}
void setup() {
pinMode(SV_Motor_x_PIN, OUTPUT);
pinMode(SV_Motor_y_PIN, OUTPUT);
pinMode(SV_Motor_z_PIN, OUTPUT);
ledcSetup(0, 50, 10);
ledcSetup(1, 50, 10);
ledcSetup(2, 50, 10);
ledcAttachPin(SV_Motor_x_PIN,0);
ledcAttachPin(SV_Motor_y_PIN,1);
ledcAttachPin(SV_Motor_z_PIN,2);
Serial.begin(115200);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
setupMPU9255();
MadgwickFilter.begin(10);
timer1 = timerBegin(0, 80, true);
timerAttachInterrupt(timer1, &onTimer1, true);
timerAlarmWrite(timer1, 100000, true);
timerAlarmEnable(timer1);
}
float AcX_SF;
float AcY_SF;
float AcZ_SF;
float GyX_SF;
float GyY_SF;
float GyZ_SF;
struct strc{
float AXIS_X;
float AXIS_Y;
float AXIS_Z;
};
float ROLL, PITCH, YAW;
void setupMPU9255() {
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x1A);
Wire.write(0x05);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x37);
Wire.write(0x02);
Wire.endTransmission();
Wire.beginTransmission(0x0C);
Wire.write(0x0A);
Wire.write(0x16);
Wire.endTransmission();
delay(500);
}
void readCompass() {
Wire.beginTransmission(0x0C);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(0x0C, 1);
uint8_t ST1 = Wire.read();
if (ST1 & 0x01) {
Wire.beginTransmission(0x0C);
Wire.write(0x03);
Wire.endTransmission();
Wire.requestFrom(0x0C, 7);
uint8_t i = 0;
uint8_t buf[7];
while (Wire.available()) {
buf[i++] = Wire.read();
}
if (!(buf[6] & 0x08)) {
MgX = ((int16_t)buf[1] << 8) | buf[0];
MgY = ((int16_t)buf[3] << 8) | buf[2];
MgZ = ((int16_t)buf[5] << 8) | buf[4];
}
}
}
void loop()
{
unsigned long time;
time = millis();
if (timeCounter1 > 0)
{
portENTER_CRITICAL(&timerMux);
timeCounter1--;
portEXIT_CRITICAL(&timerMux);
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,16,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
readCompass();
MadgwickFilter.update(GyX/SF_Gy, GyY/SF_Gy, GyZ/SF_Gy, AcX/SF_Acc, AcY/SF_Acc, AcZ/SF_Acc, MgX/SF_Mg, MgY/SF_Mg, MgZ/SF_Mg);
ROLL = MadgwickFilter.getRoll();
PITCH = MadgwickFilter.getPitch();
YAW = MadgwickFilter.getYaw();
Serial.print(ROLL); Serial.print(",");
Serial.print(PITCH); Serial.print(",");
Serial.print(YAW);
Serial.print("\n");
ROLL = duty_mid + ROLL/180 * duty_active;
ROLL = min(max(ROLL, duty_min), duty_max);
PITCH = duty_mid + PITCH/180 * duty_active;
PITCH = min(max(PITCH, duty_min), duty_max);
YAW = duty_mid + YAW/180 * duty_active;
YAW = min(max(YAW, duty_min), duty_max);
ledcWrite(0, ROLL);
ledcWrite(1, PITCH);
ledcWrite(2, YAW);
delay(10);
}