This code was running perfectly fine on my other PC. However, on this PC it seems to be causing problems. I was not sure of which library of MPU6050 I was using hence I tried a few but yet no solution. Chat suggested electronic cats and Rowberg but non worked. Can you please help? Thanks in advance.
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Wire.begin();
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) {
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
Serial.println("MPU6050 connection successful");
}
void loop() {
// Read normalized accelerometer data
Vector normAccel = mpu.readNormalizeAccel();
// Calculate pitch and roll
float pitch_rad = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis))) +0.01745;
float roll_rad = (atan2(normAccel.YAxis, normAccel.ZAxis)) + 1.5708;
int pitch = pitch_rad * 180.0 / M_PI;
int roll = roll_rad * 180.0 / M_PI;
Serial.print(" Pitch: ");
Serial.print(pitch);
Serial.print(" Roll: ");
Serial.println(roll);
}
Error:
float pitch_rad = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis))) +0.01745;
^~~~~~~~~
exit status 1
Compilation error: 'MPU6050 {aka class MPU6050_Base}' has no member named 'begin'
This code was running perfectly fine on my other PC. However, on this PC it seems to be causing problems. I was not sure of which library of MPU6050 I was using hence I tried a few but yet no solution. Chat suggested electronic cats and Rowberg but non worked. Can you please help? Thanks in advance.
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Wire.begin();
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) {
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
Serial.println("MPU6050 connection successful");
}
void loop() {
// Read normalized accelerometer data
Vector normAccel = mpu.readNormalizeAccel();
// Calculate pitch and roll
float pitch_rad = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis))) +0.01745;
float roll_rad = (atan2(normAccel.YAxis, normAccel.ZAxis)) + 1.5708;
int pitch = pitch_rad * 180.0 / M_PI;
int roll = roll_rad * 180.0 / M_PI;
Serial.print(" Pitch: ");
Serial.print(pitch);
Serial.print(" Roll: ");
Serial.println(roll);
}
Error:
float pitch_rad = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis))) +0.01745;
^~~~~~~~~
exit status 1
Compilation error: 'MPU6050 {aka class MPU6050_Base}' has no member named 'begin'
I just ended up using Rowberg's library and avoid all the problems.
#include <Wire.h>
#include <MPU6050.h>
void setup() {
Serial.begin(115200);
Wire.begin();
mpu.initialize(); // Initialize the sensor
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed");
while (1); // Stop execution if the connection fails
}
Serial.println("MPU6050 connection successful");
}
void loop() {
// Read raw accelerometer data
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
// Normalize accelerometer data
float ax_norm = ax / 16384.0;
float ay_norm = ay / 16384.0;
float az_norm = az / 16384.0;
// Calculate pitch and roll in radians
float pitch_rad = -atan2(ax_norm, sqrt(ay_norm * ay_norm + az_norm * az_norm)) + 0.01745;
float roll_rad = atan2(ay_norm, az_norm) + 1.5708;
Serial.print("Pitch: ");
Serial.println(Pitch);
Serial.print("Roll: ");
Serial.println(roll);
}