Search code examples
c++arduinoesp32

“Undefined reference” error in communication between Arduino and cmy library


I'm trying to start the development of a arduino project. I implemmentted a library called "IMU", to hold the object Adafruit_BNO055.h and a kalman filter. I follow the Arduino tutorial to create libraries but it's not working.

when compilling i'm getting the following error:

sketch\main.ino.cpp.o:(.literal._Z4loopv+0x18): undefined reference to `IMU::readAcc(double*, double*, double*)'

sketch\main.ino.cpp.o:(.literal.startup._GLOBAL__sub_I_x+0x4): undefined reference to `IMU::IMU()'

sketch\main.ino.cpp.o: In function `loop()':

C:\Users\Iagoh\Documents\Arduino\libraries\Adafruit_Unified_Sensor/Adafruit_Sensor.h:172: undefined reference to `IMU::readAcc(double*, double*, double*)'

sketch\main.ino.cpp.o: In function `_GLOBAL__sub_I_x':

C:\Users\Iagoh\Documents\Arduino\libraries\Adafruit_Unified_Sensor/Adafruit_Sensor.h:172: undefined reference to `IMU::IMU()'

collect2.exe: error: ld returned 1 exit status

exit status 1
Erro compilando para a placa TTGO LoRa32-OLED V1

IMU.H :

#ifndef imu_h
#define imu_h

// Includes sectoin see readme.md to install the packs
#include "Arduino.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
/**
 * Class IMU to hold data from Adafruit bn055 sensor
 * 
*/



class IMU 
{
    public:
    IMU();
    void readAcc(double *x, double *y, double *z);

    private:
    int I2C_IMU_ADDR;
    int I2C_IMU_ID;
    Adafruit_BNO055 _bno;
};


#endif

IMU.CPP:

#include "Arduino.h"
#include "imu/imu.h"



IMU::IMU(){
    I2C_IMU_ADDR = 0x28;
    I2C_IMU_ID = 55;
    _bno = = Adafruit_BNO055(I2C_IMU_ID, I2C_IMU_ADDR); 
    if (Serial){
        if (!bno.begin())
        {
            if (Serial){
                /* There was a problem detecting the BNO055 ... check your connections */
                Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
                while (1);
            }else
            {
                /* There was a problem detecting the BNO055 ... check your connections */
                Serial.begin(9600);
                Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
                while (1);
            }
        }
    }  
}


void IMU::readAcc(double *x, double *y, double *z){
    sensors_event_t linearAccelData;
    _bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL);
    *x = linearAccelData.magnetic.x;
    *y = linearAccelData.magnetic.y;
    *z = linearAccelData.magnetic.z;
}

main.ino:

#include "imu/imu.h"
double x,y,z;
IMU myImu = IMU();
int main_state =0;


void setup() {
  // put your setup code here, to run once:

}

void loop() {
  if (main_state ==0){
    // Machine state 0
       main_state = 1;
    //Todo code here
    myImu.readAcc(&x,&y,&z);
    Serial.print(x);

  }else if (main_state == 1 ){
    // Machine state 1
       main_state = 2;
    //Todo code here

  }else if (main_state == 2 ){
    // Machine state 2
       main_state = 3;
    //Todo code here

  }else if (main_state == 3 ){
    // Machine state 3
       main_state = 4;
    //Todo code here

  }else if (main_state == 4 ){
    // Machine state 4
       main_state = 5;
    //Todo code here

  }else if (main_state ==5 ){
    // Machine state 5
       main_state = 0;
    //Todo code here    

  }
}

Solution

  • I've just solved the problem. I've mooved the files to outside of the imu folder.

    Now IMU.h and IMU.cpp is in the same folder then main.ino.