我的代码有问题,我一直在它几个小时,现在试图发现问题,但无法确定原因。当我在屏幕上打印时,所有的值都应该等于零,但是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;
}
}
};
angleX,angleZ的值如何?它们是零还是有意想不到的值? – 2013-03-05 05:17:04
罪魁祸首:int angleZ = angleZ + angleRateZ; int angleY = angleY + angleRateY; int angleX = angleX + angleRateX; – 2013-03-05 05:18:03
我不知何故知道这是一个四转子! – 2013-03-05 05:20:54