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 even though its not moving it keeps turning. here is the Arduino 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
}
and here is ther processing code :
import processing.serial.*;
Serial myPort; //Port Nesnesi
String portName = "COM3"; //Buraya Arduinonun PORT BALANTI ADI gelir.
int baudRate =9600; //Seri iletiim hz== Arduino IDE baudrate.
float geoX; //Jiroskop'tan gelen veriler iin.
float geoY;
float geoZ;
float boxSze =500;
color boxColor;
String[] userGuide =
{
"KULLANIM KILAVUZU:",
"3D KUMANDA UYGULAMAMIZA HOGELDNZ",
"Hareket iin jiroskopu hareket ettiriniz",
"--------------------",
"BUTTON2: Rastgele renk seimi",
"BUTTON3: Krenin boyutunu arttr",
"--------------------"
};
void setup()
{
size(1000,1000,P3D); //lk pencere boyutu ve 3D olmas ayar.
myPort = new Serial(this, portName, baudRate); //Seri letiim iin seri port balatma.
boxColor = color(0,5,255);
strokeWeight(6);
}
void draw()
{
background(0,0,255); // Arkaplan rengi
displayUserGuide(); // Kullanc klavuzunu gster
float dirX = map(geoX,-32768,32767,-1,1);
float dirY = map(geoY,-32768,32767,-1,1);
float dirZ = map(geoZ,-32768,32767,-1,1);
directionalLight(255,255,255, dirX, dirY, dirZ);
ambientLight(50,50,50);
translate(1000/2,1000/2,-300); // Orta noktay ekrann ortasna yerletirmek.
rotateX(geoX);//3 boyutlu cismin dndrlmesi iin veriler.
rotateY(geoY);
rotateZ(geoZ);
fill(boxColor);
box(boxSze);
delay(100);
}
void serialEvent(Serial myPort)
{
String message = myPort.readStringUntil('
');
if (message != null)
{
message = message.trim();//Mesaj temizler
println("Received: "+ message);
if (message.equals("BUTTON2_PRESSED"))
{
boxColor = color(random(255), random(255), random(255)); // Rastgele renk seimi
}
else if (message.equals("BUTTON3_PRESSED"))
{
if (boxSze <900)
{
boxSze

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

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

Are there professional development opportunities?

Answered: 1 week ago

Question

Why would unions target health care workers?

Answered: 1 week ago