Question: I am having a problem with my Arduino IDE code concerning the filter of mpu 6 0 5 0 . The filter is not working

I am having a problem with my Arduino IDE code concerning the filter of mpu6050. The filter is not working properly. here is the code:
#include
#include
MPU6050 mpu;
const int buttonPin2=2;
const int buttonPin3=3;
const float dt =0.01; // Zaman adm(saniye cinsinden)
const float alpha =0.98; // Arlk faktr(ivmeler verileri iin)
const float beta =1- alpha; // Arlk faktr(jiroskop verileri iin)
float angleX =0.0;
float angleY =0.0;
float angleZ =0.0;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_16G)){
Serial.println("Could not find a valid MPU6050 sensor, check wiring !");
delay(500);
}
mpu.calibrateGyro();
mpu.setThreshold(3);
// Print calibration offsets (lk ayarlanma konumlar sonradan hatay gidermek iin.)
Serial.println("MPU6050 initialized and calibrated.");
Serial.print("Offsets: ");
Serial.print(mpu.getAccelOffsetX());
Serial.print("\t");
Serial.print(mpu.getAccelOffsetY());
Serial.print("\t");
Serial.print(mpu.getAccelOffsetZ());
Serial.print("\t");
Serial.print(mpu.getGyroOffsetX());
Serial.print("\t");
Serial.print(mpu.getGyroOffsetY());
Serial.print("\t");
Serial.println(mpu.getGyroOffsetZ());
}
void loop(){
Vector rawAccel = mpu.readRawAccel();
Vector rawGyro = mpu.readRawGyro();
// Convert raw values to meaningful units (acceleration in g's, angular velocity in degrees per second)
float accelX = rawAccel.XAxis /16384.0;
float accelY = rawAccel.YAxis /16384.0;
float accelZ = rawAccel.ZAxis /16384.0;
float gyroX = rawGyro.XAxis /131.0;
float gyroY = rawGyro.YAxis /131.0;
float gyroZ = rawGyro.ZAxis /131.0;
// Calculate angles using complementary filter
angleX = alpha *(angleX + gyroX * dt)+ beta *(atan2(accelY, accelZ)*180.0/ PI);
angleY = alpha *(angleY + gyroY * dt)+ beta *(atan(-accelX / sqrt(accelY * accelY + accelZ * accelZ))*180.0/ PI);
angleZ = alpha *(angleZ + gyroZ * dt)+ beta *(gyroZ *180.0/ PI);
// Read button states
int buttonState2= digitalRead(buttonPin2);
int buttonState3= digitalRead(buttonPin3);
// Print the data (optional)
Serial.print(angleX);
Serial.print(",");
Serial.print(angleY);
Serial.print(",");
Serial.println(angleZ);
if (buttonState2== LOW){
Serial.println("BUTTON2_PRESSED");
}
else if (buttonState2== HIGH){
Serial.println("BUTTON2_NOT_PRESSED");
}
if (buttonState3== LOW){
Serial.println("BUTTON3_PRESSED");
}
else if (buttonState3== HIGH){
Serial.println("BUTTON3_NOT_PRESSED");
}
delay(100); // Adjust the delay as needed
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!