我正在从陀螺仪读取数据,我得到的数据很好,但值不符合预期。我认为这是由于我在四处寻找解决方案后编码错误所致,我在此处发表了一篇文章,其中指出:
确保正确读取输出寄存器,数据是 2 的补码中的 16 位值(即 MSB 是符号位,然后是 15 位的值)
这让我很困惑,我不确定我的编码是否按预期读取值。我正在使用wiringPi
I2C 库将 Arduino 的现有代码转换为在 Raspberry Pi 上运行。我在下面有我的代码我希望有人能告诉我他们是否看到正确尝试读取 2 的恭维中的 16 位值。在getGyroValues
函数内部是我唯一能看到这种情况发生的地方。我的代码是否正确读取了这些值?
#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;
int x = 0;
int y = 0;
int 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(100); // Wait to synchronize
void getGyroValues (){
int MSB, LSB;
LSB = wiringPiI2CReadReg16(fd, 0x28);
MSB = wiringPiI2CReadReg16(fd, 0x29);
x = ((MSB << 8) | LSB);
MSB = wiringPiI2CReadReg16(fd, 0x2B);
LSB = wiringPiI2CReadReg16(fd, 0x2A);
y = ((MSB << 8) | LSB);
MSB = wiringPiI2CReadReg16(fd, 0x2D);
LSB = wiringPiI2CReadReg16(fd, 0x2C);
z = ((MSB << 8) | LSB);
}
for (int i=0;i<10;i++){
getGyroValues();
// In following Divinding by 114 reduces noise
printf("Value of X is: %d\n", x / 114);
printf("Value of Y is: %d\n", y / 114);
printf("Value of Z is: %d\n", z / 114);
int t = wiringPiI2CReadReg8(fd, 0x26);
t = (t*1.8)+32;//convert Celcius to Fareinheit
int a = wiringPiI2CReadReg16(fd,0x2B);
int b = wiringPiI2CReadReg16(fd,0x2A);
printf("Y_L equals: %d\n", a);
printf("Y_H equals: %d\n", b);
int c = wiringPiI2CReadReg16(fd,0x28);
int d = wiringPiI2CReadReg16(fd,0x29);
printf("X_L equals: %d\n", c);
printf("X_H equals: %d\n", d);
int e = wiringPiI2CReadReg16(fd,0x2C);
int f = wiringPiI2CReadReg16(fd,0x2D);
printf("Z_L equals: %d\n", e);
printf("Z_H equals: %d\n", f);
printf("The temperature is: %d\n\n\n", t);
delay(500);
}
};