Arduino 和 cmy 库之间的通信中出现"未定义的引用"错误



我正在尝试开始开发一个arduino项目。我设想了一个名为"IMU"的库,用于保存对象Adafruit_BNO055.h和一个卡尔曼滤波器。 我按照Arduino教程创建库,但它不起作用。

编译时出现以下错误:

sketchmain.ino.cpp.o:(.literal._Z4loopv+0x18): undefined reference to `IMU::readAcc(double*, double*, double*)'
sketchmain.ino.cpp.o:(.literal.startup._GLOBAL__sub_I_x+0x4): undefined reference to `IMU::IMU()'
sketchmain.ino.cpp.o: In function `loop()':
C:UsersIagohDocumentsArduinolibrariesAdafruit_Unified_Sensor/Adafruit_Sensor.h:172: undefined reference to `IMU::readAcc(double*, double*, double*)'
sketchmain.ino.cpp.o: In function `_GLOBAL__sub_I_x':
C:UsersIagohDocumentsArduinolibrariesAdafruit_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    
}
}

我刚刚解决了这个问题。我已将文件移动到 imu 文件夹的外部。

现在 IMU.h 和 IMU.cpp 在同一个文件夹中,然后是 main.ino。

最新更新