gpt4 book ai didi

c++ - Arduino代码没有正确添加数字

转载 作者:行者123 更新时间:2023-11-28 05:06:31 25 4
gpt4 key购买 nike

我正在尝试创建一个函数来查找 Arduino 上传感器值的平均值以对其进行校准,但是求和无法正常工作,因此平均值不正确。下表显示了输出示例。左列应该是显示在右列中的输出的滚动总和(负数如何进入那里?)

-10782      17112        
6334 17116
23642 17308
-24802 17092
-7706 17096
9326 17032
26422 17096
-21986 17128

应该执行此操作的 calibrateSensors() 函数如下所示

void calibrateSensors(int16_t * accelOffsets){

int16_t rawAccel[3];
int sampleSize = 2000;


Serial.println("Accelerometer calibration in progress...");

for (int i=0; i<sampleSize; i ++){

readAccelData(rawAccel); // get raw accelerometer data
accelOffsets[0] += rawAccel[0]; // add x accelerometer values
accelOffsets[1] += rawAccel[1]; // add y accelerometer values
accelOffsets[2] += rawAccel[2]; // add z accelerometer values
Serial.print(accelOffsets[2]);
Serial.print("\t\t");
Serial.print(rawAccel[2]);
Serial.print(" \t FIRST I:");
Serial.println(i);
}

for (int i=0; i<3; i++)
{
accelOffsets[i] = accelOffsets[i] / sampleSize;
Serial.print("Second I:");
Serial.println(i);
}

Serial.println("Accelerometer calibration complete");
Serial.println("Accelerometer Offsets:");
Serial.print("Offsets (x,y,z): ");
Serial.print(accelOffsets[0]);
Serial.print(", ");
Serial.print(accelOffsets[1]);
Serial.print(", ");
Serial.println(accelOffsets[2]);
}

readAccelData()函数如下

void readAccelData(int16_t * destination){
// x/y/z accel register data stored here
uint8_t rawData[6];
// Read the six raw data registers into data array
I2Cdev::readBytes(MPU6050_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]);
// Turn the MSB and LSB into a signed 16-bit value
destination[0] = (int16_t)((rawData[0] << 8) | rawData[1]) ;
destination[1] = (int16_t)((rawData[2] << 8) | rawData[3]) ;
destination[2] = (int16_t)((rawData[4] << 8) | rawData[5]) ;

知道我哪里出错了吗?

最佳答案

你有两个问题:

  1. 你没有初始化你的数组。它们从其中的垃圾数据开始(分配空间,但未清除)。您可以通过执行以下操作将数组初始化为全零:

int array[5] = {};

这将产生一个最初看起来像 [0,0,0,0,0]

的数组

您的第二个问题是您正在创建一个 16 位有符号整数数组。一个 16 位整数可以存储 65536 个不同的值。问题是因为您使用的是有符号类型,所以只能使用 32767 个正整数值。当您尝试存储大于该值的值时,您会溢出并得到负数。

我相信 arduino 支持 32 位整数。如果您只需要正数,请使用无符号类型。

要使用明确的 32 位整数:

#include <stdint.h>
int32_t my_int = 0;

关于标准变量大小的一些信息(请注意,它们可以是不同的大小,具体取决于为代码构建的 arduino 模型):

https://www.arduino.cc/en/Reference/Int

On the Arduino Uno (and other ATMega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).

https://www.arduino.cc/en/Reference/UnsignedInt

On the Uno and other ATMEGA based boards, unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value. Instead of storing negative numbers however they only store positive values, yielding a useful range of 0 to 65,535 (2^16) - 1).

The Due stores a 4 byte (32-bit) value, ranging from 0 to 4,294,967,295 (2^32 - 1).

https://www.arduino.cc/en/Reference/UnsignedLong

Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).

关于c++ - Arduino代码没有正确添加数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44627894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com