未在范围中声明功能

时间:2020-03-20 09:50:18

标签: class arduino-ide esp32

我不知道为什么我的程序会向我抛出错误,即使我在程序中有未定义的函数。添加代码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_stackoverflow\ina219_pokus_stackoverflow.ino: In function 'void loop()':

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

   readQuatData(BNO055_quatCount);

                                ^

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

ina219_pokus_stackoverflow: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

感谢您的帮助。

鲍里斯

1 个答案:

答案 0 :(得分:0)

在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()执行此操作。您共享的代码中似乎缺少这些内容,因此您只能自己使用它们,但是您会做同样的事情-在使用它们之前先定义它们,或者在使用它们之前添加声明