函数未在范围内声明

Function was not declared in the scope

我不知道为什么我的程序抛出我没有定义函数的错误,即使我在程序中有它们。添加代码 INA219 monitor;

后发生这种情况

我的代码:

#include "BNO055_ESP32.h"
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <INA219.h>



#define SHUNT_MAX_V 0.02  /* Rated max for our shunt is 75mv for 50 A current: 
                             we will mesure only up to 20A so max is about 75mV*20/50 lets put some more*/
#define BUS_MAX_V   5  /* with 12v lead acid battery this should be enough*/
#define MAX_CURRENT 5    /* In our case this is enaugh even shunt is capable to 50 A*/
#define SHUNT_R   0.1   /* Shunt resistor in ohm */

#define SERVICE_UUID            "6E40180D-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID     "6E402A37-B5A3-F393-E0A9-E50E24DCCA9E"


// BLE variables
bool deviceConnected = false;

BLECharacteristic *pCharacteristic;

INA219 monitor;

void setup() {

  Wire.begin(21, 22, 400000);
  Serial.begin(115200); //opens serial terminal
  monitor.begin();
  monitor.configure(INA219::RANGE_16V, INA219::GAIN_1_40MV, INA219::ADC_16SAMP , INA219::ADC_16SAMP , INA219::CONT_SH_BUS);
  monitor.calibrate(SHUNT_R, SHUNT_MAX_V, BUS_MAX_V, MAX_CURRENT);

  BLEDevice::init("SMART HELMET");

  //----------BLE INITIALIZATION-------------

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
}

void loop() {

  readQuatData(BNO055_quatCount);
  // Calculate the quaternion values

  BNO055_quat[0] = (float)(BNO055_quatCount[0]) / 16384.;
  BNO055_quat[1] = (float)(BNO055_quatCount[1]) / 16384.;
  BNO055_quat[2] = (float)(BNO055_quatCount[2]) / 16384.;
  BNO055_quat[3] = (float)(BNO055_quatCount[3]) / 16384.;

  pCharacteristic->setValue((uint8_t*)BNO055_quat, 16);


  pCharacteristic->notify(); // Send the value to the app!

}

void readQuatData(int16_t * destination)
{
  uint8_t rawData[8];  // x/y/z gyro register data stored here
  readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]);  // Read the six raw data registers sequentially into data array
  destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ;       // Turn the MSB and LSB into a signed 16-bit value
  destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
  destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
  destination[3] = ((int16_t)rawData[7] << 8) | rawData[6] ;
}

我得到的错误:

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'void loop()':

BNO_jedna_charka:216:36: error: 'readQuatData' was not declared in this scope

       readQuatData(BNO055_quatCount);

                                    ^

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'void readQuatData(int16_t*)':

BNO_jedna_charka:282:66: error: 'readBytes' was not declared in this scope

   readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]);  // Read the six raw data registers sequentially into data array

                                                                  ^

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'bool initBNO055()':

BNO_jedna_charka:346:64: error: 'writeByte' was not declared in this scope

   writeByte(BNO055_ADDRESS, BNO055_OPR_MODE, BNO055_CONFIGMODE );

                                                                ^

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'void accelgyroCalBNO055(float*, float*)':

BNO_jedna_charka:390:49: error: 'writeByte' was not declared in this scope

   writeByte(BNO055_ADDRESS, BNO055_PAGE_ID, 0x00);

                                                 ^

BNO_jedna_charka:401:65: error: 'readBytes' was not declared in this scope

     readBytes(BNO055_ADDRESS, BNO055_ACC_DATA_X_LSB, 6, &data[0]);  // Read the six raw data registers into data array

                                                                 ^

BNO_jedna_charka:430:65: error: 'readBytes' was not declared in this scope

     readBytes(BNO055_ADDRESS, BNO055_GYR_DATA_X_LSB, 6, &data[0]);  // Read the six raw data registers into data array

                                                                 ^

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'void magCalBNO055(float*)':

BNO_jedna_charka:530:64: error: 'writeByte' was not declared in this scope

   writeByte(BNO055_ADDRESS, BNO055_OPR_MODE, BNO055_CONFIGMODE );

                                                                ^

exit status 1
'readQuatData' was not declared in this scope

C:\Users\Boris\Documents\Arduino\ina219_pokus_Whosebug\ina219_pokus_Whosebug.ino: In function 'void loop()':

ina219_pokus_Whosebug:45:32: error: 'readQuatData' was not declared in this scope

   readQuatData(BNO055_quatCount);

                                ^

C:\Users\Boris\Documents\Arduino\ina219_pokus_Whosebug\ina219_pokus_Whosebug.ino: In function 'void readQuatData(int16_t*)':

ina219_pokus_Whosebug:63:66: error: 'readBytes' was not declared in this scope

   readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]);  // Read the six raw data registers sequentially into data array

                                                                  ^

exit status 1
'readQuatData' was not declared in this scope

感谢您的帮助。

鲍里斯

在 C 和 C++ 中,您必须在调用函数之前声明它们。

您可以像在此处那样通过定义函数来做到这一点:

void readQuatData(int16_t * destination)
{
  uint8_t rawData[8];  // x/y/z gyro register data stored here
  readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]);  // Read the six raw data registers sequentially into data array
  destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ;       // Turn the MSB and LSB into a signed 16-bit value
  destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
  destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
  destination[3] = ((int16_t)rawData[7] << 8) | rawData[6] ;
}

或者您可以使用这样的函数声明来实现:

void readQuatData(int16_t * destination);

您在声明它之前调用 readQuatData(),所以您收到此错误。

您可以将函数的定义移动到使用它的地方之前,或者您可以在使用它之前声明它,如下所示:

INA219 monitor;

void readQuatData(int16_t * destination);

void setup() {

我把它放在对 setup() 的调用之前,因为在文件开头声明您需要声明的所有内容,在代码之前,通常比在整个代码中分散声明更清晰、更易于维护。

您需要为 writeByte()readBytes() 执行此操作。您共享的代码中似乎缺少这些,因此您只能自己处理它们,但您会做同样的事情 - 在使用它们之前定义它们,或者在使用它们之前为它们添加声明.