#include "BluetoothSerial.h"
#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;
int i;
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
Madgwick MadgwickFilter;
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() {
Serial.begin(115200);
SerialBT.begin("ESP32test");
Serial.println("The device started, now you can pair it with bluetooth!");
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);
}
uint8_t calculate_checksum(uint8_t *data) {
uint8_t checksum = 0;
checksum |= 0b11000000 & data[1];
checksum |= 0b00110000 & data[2];
checksum |= 0b00001100 & data[3];
checksum |= 0b00000011 & data[4];
return checksum;
}
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");
delay(10);
uint8_t recv_data[6];
if (SerialBT.available()) {
SerialBT.readBytes(recv_data, 6);
if (recv_data[0] != 'T') {
Serial.print("Receive error!");
return;
}
if (recv_data[5] != calculate_checksum(recv_data)) {
Serial.print("Decode error!");
return;
}
Serial.printf("left_x: %d, left_y: %d, right_x: %d, right_y: %d\n", recv_data[1], recv_data[2], recv_data[3], recv_data[4]);
if((recv_data[2] < 50)&&(i < 200)){
ledcWrite(0, i++);
}
if((recv_data[2] > 200)&&(i >= 1)){
ledcWrite(0, i--);
}
Serial.printf("i:%d\n", i);
}
delay(20);
}
}