Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Find the product. 2b 3 (b 2 - 4b + 3)

Answered: 1 week ago