c++ - 'MPU6050 {aka class MPU6050_Base}' has no member named 'begin' - Stack Overflow

admin2025-04-25  2

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'
Share Improve this question asked Jan 16 at 11:41 DailyReaderDailyReader 451 silver badge8 bronze badges 6
  • isnt this actually the same problem as in your last question? If you solve one issue first chances are high that the other will be gone too – 463035818_is_not_an_ai Commented Jan 16 at 11:43
  • @463035818_is_not_an_ai this is actually my original problem before I tried to solve it.. I will start from here, its safer. – DailyReader Commented Jan 16 at 11:45
  • 1 if the old question is obsolete you should delete it. Though anybody who contributed already wont be happy about that. As the old quesiton has no answers yet you could edit it if you think that helps the question. – 463035818_is_not_an_ai Commented Jan 16 at 11:46
  • I solved this by using the implementation using rowberg's library. Ask GPT. – DailyReader Commented Jan 16 at 12:29
  • 2 if you found the answer to a question yourself you can write an answer to that question. If you are no longer interested in getting an answer to a question you can delete the question – 463035818_is_not_an_ai Commented Jan 16 at 12:31
 |  Show 1 more comment

1 Answer 1

Reset to default 0

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);
}
转载请注明原文地址:http://anycun.com/QandA/1745534201a90878.html