gpt4 book ai didi

c - 垃圾值(value)不知道从哪里来

转载 作者:行者123 更新时间:2023-11-30 18:58:41 26 4
gpt4 key购买 nike

我的代码在某个地方有问题,我已经研究了几个小时,现在试图发现问题,但无法确定原因。当我打印到屏幕上时,所有值都应为零,而是 angleY变量不断打印 34244当它应该为零时。我想看看是否有人可以告诉我这个值来自哪里以及为什么?我有这条线 printf("current rate: %d angle rate: %d angle rate: %d previous rate: %d \n",currentRateY, angleRateY, angleY, previousRateY);它会打印所有变量的值,以便我可以查明问题,并且所有变量仍保留 0直到angleY多变的。我的代码如下:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23


int fd;
short x = 0;
short y = 0;
short z = 0;
int main (){



fd = wiringPiI2CSetup(0x69); // I2C address of gyro
wiringPiI2CWriteReg8(fd, CTRL_REG1, 0x1F); //Turn on all axes, disable power down
wiringPiI2CWriteReg8(fd, CTRL_REG3, 0x08); //Enable control ready signal
wiringPiI2CWriteReg8(fd, CTRL_REG4, 0x80); // Set scale (500 deg/sec)
delay(200); // Wait to synchronize

void getGyroValues (){
int MSB, LSB;

LSB = wiringPiI2CReadReg8(fd, 0x28);
MSB = wiringPiI2CReadReg8(fd, 0x29);
x = ((MSB << 8) | LSB);

MSB = wiringPiI2CReadReg8(fd, 0x2B);
LSB = wiringPiI2CReadReg8(fd, 0x2A);
y = ((MSB << 8) | LSB);

MSB = wiringPiI2CReadReg8(fd, 0x2D);
LSB = wiringPiI2CReadReg8(fd, 0x2C);
z = ((MSB << 8) | LSB);
}
for (int i=0;i<1000;i++){

getGyroValues();

int previousRateZ = z /114;
int previousRateY = y /114;
int previousRateX = x /114;

delay(100);

getGyroValues();

int currentRateZ = z /114;
int currentRateY = y /114;
int currentRateX = x /114;

int angleRateZ = ((long)(previousRateZ + currentRateZ) * 105)/1000;
int angleRateY = ((long)(previousRateY + currentRateY) * 105)/1000;
int angleRateX = ((long)(previousRateX + currentRateX) * 105)/1000;

int angleZ = angleZ + angleRateZ;
int angleY = angleY + angleRateY;
int angleX = angleX + angleRateX;
printf("current rate: %d angle rate: %d angle rate: %d previous rate: %d \n",currentRateY, angleRateY, angleY, previousRateY);
delay(100);

if(i == 1){
printf("Z equals: %d\n", angleZ);
printf("Y equals: %d\n", angleY);
printf("X equals: %d\n", angleX);
i=0;
}
}


};

最佳答案

在此行中设置之前,您正在使用 angleY:

int angleY = angleY + angleRateY;

C 不保证值初始化为零。我猜你真的想要这样的东西:

int angleY = 0;
angleY = angleY + angleRateY;

我还假设第二行将在未来版本中以某种循环结束。

关于c - 垃圾值(value)不知道从哪里来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15216294/

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