1
votes

I am trying to split up my arduino code base from a single 'ino' file into a proper-ish c++ program by creating classes which have both .h & .cpp files. I am running into a few errors that I just haven't been able to solve. I hope I am missing something simple.

When compiling in visual studio running the "Arduino IDE for Visual Studio" plugin, I get the following errors:

7:8: error: 'class MPU6050' has no member named 'timer

30:5: error: prototype for 'int MPU6050::MPU6050_read(int, uint8_t*, int)' does not match any in class 'MPU6050

What am I missing here?

.h

#ifndef MPU6050_H
#define MPU6050_H


class MPU6050
{

private:  // Vars
    uint32_t timer;  // Hold the value of the timer used for the complementary filter
    int error;

public:  // Methods
    MPU6050();
    ~MPU6050();
    
    void read_acc_gyr();
    float const * const getXa();
    float const * const getYa();
    float const * const getXvel();
    float const * const getYvel();
    float const * const getZvel();
    float const * const getZang();
    double const * const getDt();

private:
    void test_gyr_acc();
    void MPU6050_init();
    void printGyroValues();
    void calibrateGyro();
    int MPU6050_write(int start, const uint8_t *pData, int size);
    int MPU6050_read(int start, uint8_t *buffer, int size);
    int MPU6050_write_reg(int reg, uint8_t data);
};

#endif

.cpp

#include "MPU6050.h"
#include "Arduino.h"
#include "MPU6050Definitions.h"

MPU6050::MPU6050()
{
    timer = 0;  // Always start the timer at 0
    error = 1;

    Serial.println("Testing Gyro");
    test_gyr_acc();
}

MPU6050::~MPU6050()
{

}

int MPU6050::MPU6050_read(int start, uint8_t *buffer, int size)
{
    int i, n, error;

    Wire.beginTransmission(MPU6050_I2C_ADDRESS);
    n = Wire.write(start);
    if (n != 1)
        return (-10);

    n = Wire.endTransmission(false);    // hold the I2C-bus
    if (n != 0)
        return (n);

    // Third parameter is true: relase I2C-bus after data is read.
    Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
    i = 0;
    while (Wire.available() && i<size)
    {
        buffer[i++] = Wire.read();
    }
    if (i != size)
        return (-11);

    return (0);  // return : no error
}

void MPU6050::test_gyr_acc()
{
    uint8_t c = 0;

    error = MPU6050_read(MPU6050_WHO_AM_I, &c, 1);
    if (error != 0) {
        while (true) {
            digitalWrite(13, HIGH);
            delay(300);
            digitalWrite(13, LOW);
            delay(300);
        }
    }
}

MPUT6050Definitions.h includes all of my constants and looks like the following: (many more #defines which I don't think are relevant to include)

#pragma once

//All the values to setup the MPU6050 gyro/accelermeter

#define MPU6050_AUX_VDDIO          0x01   // R/W
#define MPU6050_SMPLRT_DIV         0x19   // R/W
#define MPU6050_CONFIG             0x1A   // R/W
#define MPU6050_GYRO_CONFIG        0x1B   // R/W
1
comment your timer = 0; // Always start the timer at 0 and see if any problem arises from the error - Vtik
I am pretty sure the compiler uses a different class definition as you are showing in the header file. Mixed some files up with older versions? Also, when you get this kind of errors, it it always a good idea to replace this #ifdef header barriers by #pragma once (although I do not see this is the issue here). Have you disabled precompiled headers? Also setup your compiler to give debug output for all header files included. Check that it is really that class definition it uses. Do a text search for MPU6050_H, it should only pop up in that single .h file. - philipp
@YassinNasri commenting out the timer variable caused the error go disappear which caused me to focus on another error which solved my problem. - Wesley
@philipp How do you disable precompiled headers? - Wesley

1 Answers

1
votes

It looks like I made a simple mistake.

A few more errors down the list was an unknown declaration of type uint_32 which propagated itself causing the errors I was seeing before. I included the Arduino.h header which resolved the uint_32 problem causing the rest of the code to compile as expected.

Moral of the story is don't try to fix your errors from the top down.