向其写入值时,已定义数组的大小正在发生变化

The size of a defined array is changing when writing values to it

好的,我正在尝试从 arduino 的 gy521 传感器读取数据。我正处于从 FIFO 寄存器读取数据的阶段,但是当我尝试将数据分配给数组以便计算位置时,数组写入 20 个值,而不是我在开始时定义的 10 个值。这是我正在进行的工作代码:

#include <Wire.h>

  #define MPU_6050 0x68
  #define PWR_MGMT_1 0x6B 
  #define FIFO_EN 0x23
  #define FIFO_COUNTH 0x72
  #define FIFO_COUNTL 0x73
  #define FIFO_R_W 0x74
  #define SMPLRT_DIV 0x19
  #define USER_CTRL 0x6A
  #define GY_CONFIG 0x1B
  #define SELF_TEST_X 0x0D
  #define SELF_TEST_Y 0x0E
  #define SELF_TEST_Z 0x0F
  #define INT_EN 0x38
  #define INT_STAT 0x3A
  #define ARRAY_SIZE 10

  int FIFO_OVERFLOW_STAT;
  int FIFO_DATA_X;
  int FIFO_DATA_Y;
  int FIFO_DATA_Z;
  int x0;
  int y0;
  int z0;
  int FIFO_COUNT;
  int x_val[ARRAY_SIZE];
  int y_val[ARRAY_SIZE];
  int z_val[ARRAY_SIZE];

  void setup() {
   Serial.begin(115200); //Start a serial interface
   Wire.begin(); //set up i2c
   Wire.beginTransmission(MPU_6050); //start transmission to slave
   Wire.write(PWR_MGMT_1); 
   Wire.write(0); //Wake up
   Wire.endTransmission(true); // Send byte in buffer
   Wire.beginTransmission(MPU_6050);
   Wire.write(SMPLRT_DIV);
   Wire.write(5); //set the sample rate to 250hz (because 8000/1+31=250)
   Wire.endTransmission(true);
   disable_FIFO();
   Wire.beginTransmission(MPU_6050);
   Wire.write(USER_CTRL); 
   Wire.write(68); //enable the FIFO Line and Clear it
   Wire.endTransmission(true);
   check_FIFO_size();
   Wire.beginTransmission(MPU_6050);
   Wire.write(SELF_TEST_X); 
   Wire.write(7); //set up self tests
   Wire.endTransmission(true);
   Wire.beginTransmission(MPU_6050);
   Wire.write(SELF_TEST_Y); 
   Wire.write(7); //set up self tests
   Wire.endTransmission(true);
   Wire.beginTransmission(MPU_6050);
   Wire.write(SELF_TEST_Z); 
   Wire.write(7); //set up self tests
   Wire.endTransmission(true);
   Wire.beginTransmission(MPU_6050);
   Wire.write(GY_CONFIG); 
   Wire.write(224); //Self test gyro and set Full Scale range to 250*/s 
   Wire.endTransmission(true);
   Wire.beginTransmission(MPU_6050);
   Wire.write(INT_EN); 
   Wire.write(16); //set up interrupt overflow of FIFO to prevent loss of data
   Wire.endTransmission(true);
   enable_FIFO();
   get_0_vals();
  }

  void enable_FIFO(){
    Wire.beginTransmission(MPU_6050);
    Wire.write(FIFO_EN);
    Wire.write(112); //enable the FIFO line for GY x,y and z
    Wire.endTransmission(true);
  }

  void disable_FIFO(){
    Wire.beginTransmission(MPU_6050);
    Wire.write(FIFO_EN);
    Wire.write(0); //enable the FIFO line for GY x,y and z
    Wire.endTransmission(true);
  }

  void check_FIFO_overflow_stat(){
    Wire.beginTransmission(MPU_6050);
    Wire.write(INT_STAT);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_6050, 1, true);
    if(Wire.available() >= 1){
      FIFO_OVERFLOW_STAT = Wire.read() >> 4;
    }
    Serial.println(FIFO_OVERFLOW_STAT);
  }

  void check_FIFO_size(){
    Wire.beginTransmission(MPU_6050);
    Wire.write(FIFO_COUNTH);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_6050, 2, true);
    if(Wire.available() >=2){  
      FIFO_COUNT = Wire.read() << 8;
      FIFO_COUNT |= Wire.read();
    }
    Wire.endTransmission();
    Serial.print("FIFO count: ");
    Serial.println(FIFO_COUNT);
  }
  void get_0_vals(){
    Wire.beginTransmission(MPU_6050);
    Wire.write(FIFO_R_W);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_6050, 6, true);
    if(Wire.available() >= 6){  
      x0 = Wire.read() << 8;
      x0 |= Wire.read();
      y0 = Wire.read() << 8;
      y0 |= Wire.read();
      z0 = Wire.read() << 8;
      z0 |= Wire.read();
    }
    Wire.endTransmission();
  }
  void read_FIFO_xyz(){
    Wire.beginTransmission(MPU_6050);
    Wire.write(FIFO_R_W);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_6050, 6, true);
    if(Wire.available() >=6){  
       FIFO_DATA_X = Wire.read() << 8;
       FIFO_DATA_X |= Wire.read();
       FIFO_DATA_Y = Wire.read() << 8;
       FIFO_DATA_Y |= Wire.read();
       FIFO_DATA_Z = Wire.read() << 8;
       FIFO_DATA_Z |= Wire.read();
    }
    Wire.endTransmission(true);
  }

  void zero_FIFO_data(){
    FIFO_DATA_X = FIFO_DATA_X - x0;
    FIFO_DATA_Y = FIFO_DATA_Y - y0;
    FIFO_DATA_Z = FIFO_DATA_Z - z0;
  }

  void fill_arrays(){
    for(int i = 0; i<10; i++){   
      should_FIFO_be_on();
      if(FIFO_COUNT > 6){
        read_FIFO_xyz();
        zero_FIFO_data();
        x_val[i] = FIFO_DATA_X;
        y_val[i] = FIFO_DATA_Y;
        z_val[i] = FIFO_DATA_Z;
      }
    }
  }

  void should_FIFO_be_on(){
    check_FIFO_size();
    if(FIFO_COUNT > 900){
      disable_FIFO();
    }
    else{
      enable_FIFO();
     }
  }

  void loop() {
    Serial.print("Size of x_val: ");
    Serial.println(sizeof(x_val));
    Serial.println("Start of Main");
    check_FIFO_size();
    should_FIFO_be_on();
    if(FIFO_COUNT > 60){
      fill_arrays();
    }
    Serial.print("Size of x_val: ");
    Serial.println(sizeof(x_val));
    for(int i = 0; i<10; i++){
      Serial.print("Element ");
      Serial.print(i);
      Serial.print(" of x_val: ");
      Serial.println(x_val[i]);  
    }
  }

我已将 x_val 定义为大小 ARRAY_SIZE,定义为 10。

调用我的填充数组函数后,Serial.print(sizeof(x_val)); returns 20. 我觉得这与 FIFO_DATA_X 的值是 2 字节有关,但这对我来说没有意义,因为 int 可以是 2 或 4 字节。

提前抱歉,我的代码远非完美,但我无法使用 MPU_6050 库,所以我不得不从数据 sheet 和注册地图中尽力而为,加上我的对 C++ 和 arduino 的理解很差(可能很明显)。

您正在使用 sizeof operator,这不是 return 数组中元素的数量。

Returns size in bytes of the object representation of type.

这是一个解决方案:

sizeof(x_val) / sizeof(x_val[0])

returns 数组中的元素数。关于此的更多信息 here